You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@uima.apache.org by Adam Lally <al...@alum.rpi.edu> on 2007/01/10 19:53:10 UTC

Questionable code in FSIndexRepository.incrementIllegalIndexUpdateDetector

I stumbled across this:

  private void incrementIllegalIndexUpdateDetector(int typeCode) {
    this.detectIllegalIndexUpdates[typeCode] =
(this.detectIllegalIndexUpdates[typeCode] == Integer.MAX_VALUE) ?
Integer.MIN_VALUE
            : this.detectIllegalIndexUpdates[typeCode] + 1;
  }


I think this is just a really long way of writing:

this.detectIllegalIndexUpdates[typeCode]++;


Because Integer.MAX_VALUE + 1 == Integer.MIN_VALUE

Any objects to my shortening this while I'm in this code?

-Adam

Re: Questionable code in FSIndexRepository.incrementIllegalIndexUpdateDetector

Posted by Adam Lally <al...@alum.rpi.edu>.
On 1/10/07, Marshall Schor <ms...@schor.com> wrote:
> Adam Lally wrote:
> > I stumbled across this:
> >
> >  private void incrementIllegalIndexUpdateDetector(int typeCode) {
> >    this.detectIllegalIndexUpdates[typeCode] =
> > (this.detectIllegalIndexUpdates[typeCode] == Integer.MAX_VALUE) ?
> > Integer.MIN_VALUE
> >            : this.detectIllegalIndexUpdates[typeCode] + 1;
> >  }
> >
> >
> > I think this is just a really long way of writing:
> >
> > this.detectIllegalIndexUpdates[typeCode]++;
> >
> >
> > Because Integer.MAX_VALUE + 1 == Integer.MIN_VALUE
> >
> > Any objects to my shortening this while I'm in this code?
>
> I wrote that piece, because I guessed (maybe incorrectly) that adding 1
> to a max-value would cause an integer-overflow-exception.   I looked
> this up, and according to one source,
> MAX_VALUE + 1 = -2147483648. (What happens is that any extra bits beyond
> the 32-nd bit in the correct answer are discarded. Values greater than
> 2147483647 will "wrap around" to negative values. Mathematically
> speaking, the result is always "correct modulo 2^32".)
>

Yup, this is old-fashioned computer arithmetic, none of that fancy
exception stuff. :)


> If there is no assumption dependency that this value is non-negative,
> then I think the change is OK.
>

If there was such an assumption, this code was broken already.  I'm
not doing anything that will change the behavior of this code in any
way, except that it might possibly run a little faster.

-Adam

Re: Questionable code in FSIndexRepository.incrementIllegalIndexUpdateDetector

Posted by Marshall Schor <ms...@schor.com>.
Adam Lally wrote:
> I stumbled across this:
>
>  private void incrementIllegalIndexUpdateDetector(int typeCode) {
>    this.detectIllegalIndexUpdates[typeCode] =
> (this.detectIllegalIndexUpdates[typeCode] == Integer.MAX_VALUE) ?
> Integer.MIN_VALUE
>            : this.detectIllegalIndexUpdates[typeCode] + 1;
>  }
>
>
> I think this is just a really long way of writing:
>
> this.detectIllegalIndexUpdates[typeCode]++;
>
>
> Because Integer.MAX_VALUE + 1 == Integer.MIN_VALUE
>
> Any objects to my shortening this while I'm in this code?

I wrote that piece, because I guessed (maybe incorrectly) that adding 1 
to a max-value would cause an integer-overflow-exception.   I looked 
this up, and according to one source,
MAX_VALUE + 1 = -2147483648. (What happens is that any extra bits beyond 
the 32-nd bit in the correct answer are discarded. Values greater than 
2147483647 will "wrap around" to negative values. Mathematically 
speaking, the result is always "correct modulo 2^32".)

If there is no assumption dependency that this value is non-negative, 
then I think the change is OK.

-Marshall