Condition outside loop or separate loop for different condition?

Discussion in 'Java' started by -, Jun 13, 2005.

  1. -

    - Guest

    Should I adopt the first or the second way?
    The first makes it faster. The second makes the code shorter.
    The size to compare is not in the millions so which should i go with?

    First Way:

    private Comparator createXComparator() {
    return new Comparator() {
    public int compare(Object object1, Object object2) {
    int value = 0;

    A a1 = (A) object1;
    A a2 = (A) object2;

    if (a1.getX() < a2.getX()) {
    value = -1;
    } else {
    value = 1;
    }

    return value;
    }
    };
    }

    private Comparator createYComparator() {
    return new Comparator() {
    public int compare(Object object1, Object object2) {
    int value = 0;

    A a1 = (A) object1;
    A a2 = (A) object2;

    if (a1.getY() < a2.getY()) {
    value = -1;
    } else {
    value = 1;
    }

    return value;
    }
    };
    }

    Second Way:

    private Comparator createComparator() {
    return new Comparator() {
    public int compare(Object object1, Object object2) {
    int value = 0;

    A a1 = (A) object1;
    A a2 = (A) object2;

    if (...) {
    if (a1.getX() < a2.getX()) {
    value = -1;
    } else {
    value = 1;
    }
    } else {
    if (a1.getY() < a2.getY()) {
    value = -1;
    } else {
    value = 1;
    }
    }

    return value;
    }
    };
    }
     
    -, Jun 13, 2005
    #1
    1. Advertising

  2. - <> wrote:

    > private Comparator createXComparator() {
    > return new Comparator() {
    > public int compare(Object object1, Object object2) {
    > int value = 0;
    >
    > A a1 = (A) object1;
    > A a2 = (A) object2;
    >
    > if (a1.getX() < a2.getX()) {
    > value = -1;
    > } else {
    > value = 1;
    > }
    >
    > return value;


    Note that this implementation is wrong (inconsistent results for
    a1.getX() == a2.getX()). Also (in this case) there is no need to
    create more than one XComparator as it has no state.



    Christian
     
    Christian Kaufhold, Jun 13, 2005
    #2
    1. Advertising

  3. Hi -,

    - wrote:
    > Should I adopt the first or the second way?
    > The first makes it faster. The second makes the code shorter.
    > The size to compare is not in the millions so which should i go with?


    Why are you so sure that there are not millions of comparisons? Because
    the Comparator is only used inside your package as an internal?

    Anyway, I would always use the faster way. So, if you will reuse the
    code in the future (which you probably are not planing now), it is safe
    for using it in "millions-comparison-environments".

    If you want so, you can add a convenience method:

    Comparartor createComparator() {
    if(...) {
    return createXComparator();
    } else {
    return createYComparator();
    }
    }


    Ciao,
    Ingo
     
    Ingo R. Homann, Jun 13, 2005
    #3
  4. -

    - Guest

    Christian Kaufhold wrote:

    > Note that this implementation is wrong (inconsistent results for
    > a1.getX() == a2.getX()).


    To make it right is to synchronize it?

    > Also (in this case) there is no need to
    > create more than one XComparator as it has no state.


    I don't get what you mean. Do rephrase.

    Thanks.
     
    -, Jun 13, 2005
    #4
  5. - <> wrote:
    > Christian Kaufhold wrote:
    >
    >> Note that this implementation is wrong (inconsistent results for
    >> a1.getX() == a2.getX()).

    >
    > To make it right is to synchronize it?


    No, to return 0 then, as the state in question is equal.


    >> Also (in this case) there is no need to
    >> create more than one XComparator as it has no state.

    >
    > I don't get what you mean. Do rephrase.


    How does one XComparator differ from another?


    Christian
     
    Christian Kaufhold, Jun 13, 2005
    #5
  6. -

    - Guest

    Christian Kaufhold wrote:
    >
    > No, to return 0 then, as the state in question is equal.


    return 0 when a.getX() == a.getY() ?

    > How does one XComparator differ from another?


    No difference. So I should make it static or something?

    Perhaps you can correct the example earlier so that i can visually see
    what's wrong
     
    -, Jun 13, 2005
    #6
  7. -

    - Guest

    Christian Kaufhold wrote:

    > No, to return 0 then, as the state in question is equal.


    Oh i get it

    int value = 0;

    if (a1.getX() < a2.getX()) {
    value = -1;
    } else if (a1.getX() > a2.getX()) {
    value = 1;
    }

    return 0;

    >
    > How does one XComparator differ from another?


    I don't understand this though.
     
    -, Jun 13, 2005
    #7
  8. -

    Dale King Guest

    - wrote:
    > Christian Kaufhold wrote:
    >
    >> No, to return 0 then, as the state in question is equal.

    >
    >
    > Oh i get it
    >
    > int value = 0;
    >
    > if (a1.getX() < a2.getX()) {
    > value = -1;
    > } else if (a1.getX() > a2.getX()) {
    > value = 1;
    > }
    >
    > return 0;


    Note that Comparator does not require that the values be restricted to
    -1, 0, and 1. You can use any negative number instead of -1 and any
    positive number instead of 1. Therefore if getX returns an integer the
    above can be replaced with:

    return a1.getX() - a2.getX();

    >> How does one XComparator differ from another?

    >
    > I don't understand this though.


    I think he meant that there is no actual state information in the
    comparator. So you only need one instance of the comparator to exist in
    your entire program. You could create one and each call returns the same
    instance.

    --
    Dale King
     
    Dale King, Jun 14, 2005
    #8
  9. -

    Chris Uppal Guest

    Dale King wrote:

    > Therefore if getX returns an integer the
    > above can be replaced with:
    >
    > return a1.getX() - a2.getX();


    Only if the range of getX() is small enough that integer wrap-around can be
    guaranteed not to occur.

    -- chris
     
    Chris Uppal, Jun 14, 2005
    #9
  10. -

    Eric Sosman Guest

    Dale King wrote:
    > - wrote:
    >
    >>Christian Kaufhold wrote:
    >>
    >>
    >>>No, to return 0 then, as the state in question is equal.

    >>
    >>
    >>Oh i get it
    >>
    >>int value = 0;
    >>
    >>if (a1.getX() < a2.getX()) {
    >> value = -1;
    >>} else if (a1.getX() > a2.getX()) {
    >> value = 1;
    >>}
    >>
    >>return 0;

    >
    >
    > Note that Comparator does not require that the values be restricted to
    > -1, 0, and 1. You can use any negative number instead of -1 and any
    > positive number instead of 1. Therefore if getX returns an integer the
    > above can be replaced with:
    >
    > return a1.getX() - a2.getX();


    Not always. For example, consider what happens if
    a1.getX() returns zero and a2.getX() returns INT_MIN.
    This substitution is appropriate only if you're 100%
    sure the subtraction cannot produce "wraparound."

    --
     
    Eric Sosman, Jun 14, 2005
    #10
  11. "Eric Sosman" <> wrote in message
    news:d8mqou$jjl$...
    >
    >
    > Dale King wrote:
    > > - wrote:
    > >
    > >>Christian Kaufhold wrote:
    > >>
    > >>
    > >>>No, to return 0 then, as the state in question is equal.
    > >>
    > >>
    > >>Oh i get it
    > >>
    > >>int value = 0;
    > >>
    > >>if (a1.getX() < a2.getX()) {
    > >> value = -1;
    > >>} else if (a1.getX() > a2.getX()) {
    > >> value = 1;
    > >>}
    > >>
    > >>return 0;

    > >
    > >
    > > Note that Comparator does not require that the values be restricted to
    > > -1, 0, and 1. You can use any negative number instead of -1 and any
    > > positive number instead of 1. Therefore if getX returns an integer the
    > > above can be replaced with:
    > >
    > > return a1.getX() - a2.getX();

    >
    > Not always. For example, consider what happens if
    > a1.getX() returns zero and a2.getX() returns INT_MIN.
    > This substitution is appropriate only if you're 100%
    > sure the subtraction cannot produce "wraparound."
    >
    > --
    >
    >


    This is not correct, 0 - INT_MIN = INT_MIN, and as such, his test would
    still work. Once a1.getX() becomes negative then it will wraparound though,
    so the point is valid, the example given isnt.

    That said, as of 5.0 i would've preffered an enum for the result type of
    compare(). CompareResult.LESS, CompareResult.EQUAL, CompareResult.GREATER or
    something. Bit more elegant and i feel new features of the language should
    be used consistently in the APIs of said language.

    Remon
     
    Remon van Vliet, Jun 14, 2005
    #11
  12. -

    Eric Sosman Guest

    Remon van Vliet wrote:
    > "Eric Sosman" <> wrote in message
    > news:d8mqou$jjl$...
    >
    >>
    >>Dale King wrote:
    >>>
    >>>Note that Comparator does not require that the values be restricted to
    >>>-1, 0, and 1. You can use any negative number instead of -1 and any
    >>>positive number instead of 1. Therefore if getX returns an integer the
    >>>above can be replaced with:
    >>>
    >>> return a1.getX() - a2.getX();

    >>
    >> Not always. For example, consider what happens if
    >>a1.getX() returns zero and a2.getX() returns INT_MIN.
    >>This substitution is appropriate only if you're 100%
    >>sure the subtraction cannot produce "wraparound."
    >>

    >
    > This is not correct, 0 - INT_MIN = INT_MIN, and as such, his test would
    > still work. Once a1.getX() becomes negative then it will wraparound though,
    > so the point is valid, the example given isnt.


    0 - INT_MIN is INT_MIN, as you say, but that result has
    already suffered wraparound. Consider: INT_MIN is negative,
    and subtracting a negative number from zero "mathematically"
    should yield a positive result. But the "delivered" answer
    is negative, showing that programming and mathematics have
    parted company somewhere along the way ...

    But don't take my word for it; try it for yourself:

    int x = 0;
    int y = Integer.MIN_VALUE;
    System.out.println("x < y = " + (x < y));
    System.out.println("x - y < 0 = " + (x - y < 0));

    --
     
    Eric Sosman, Jun 14, 2005
    #12
  13. "Eric Sosman" <> wrote in message
    news:d8n8me$q9q$...
    >
    >
    > Remon van Vliet wrote:
    > > "Eric Sosman" <> wrote in message
    > > news:d8mqou$jjl$...
    > >
    > >>
    > >>Dale King wrote:
    > >>>
    > >>>Note that Comparator does not require that the values be restricted to
    > >>>-1, 0, and 1. You can use any negative number instead of -1 and any
    > >>>positive number instead of 1. Therefore if getX returns an integer the
    > >>>above can be replaced with:
    > >>>
    > >>> return a1.getX() - a2.getX();
    > >>
    > >> Not always. For example, consider what happens if
    > >>a1.getX() returns zero and a2.getX() returns INT_MIN.
    > >>This substitution is appropriate only if you're 100%
    > >>sure the subtraction cannot produce "wraparound."
    > >>

    > >
    > > This is not correct, 0 - INT_MIN = INT_MIN, and as such, his test would
    > > still work. Once a1.getX() becomes negative then it will wraparound

    though,
    > > so the point is valid, the example given isnt.

    >
    > 0 - INT_MIN is INT_MIN, as you say, but that result has
    > already suffered wraparound. Consider: INT_MIN is negative,
    > and subtracting a negative number from zero "mathematically"
    > should yield a positive result. But the "delivered" answer
    > is negative, showing that programming and mathematics have
    > parted company somewhere along the way ...
    >
    > But don't take my word for it; try it for yourself:
    >
    > int x = 0;
    > int y = Integer.MIN_VALUE;
    > System.out.println("x < y = " + (x < y));
    > System.out.println("x - y < 0 = " + (x - y < 0));
    >
    > --
    >
    >


    Oh i did not know that, i missed the point you were making (my apologies). I
    was consider value wraparound (i.e -1 minus MAX_INT is positive), not it's
    sign partner, which i wasnt aware exists for the case where one of the
    operands is 0.

    Remon
     
    Remon van Vliet, Jun 15, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EvgueniB
    Replies:
    1
    Views:
    663
    Anthony Borla
    Dec 15, 2003
  2. Frank Fredstone
    Replies:
    1
    Views:
    463
    Jean-Francois Briere
    Jun 27, 2006
  3. Bill W.
    Replies:
    13
    Views:
    308
    Phillip Gawlowski
    May 9, 2011
  4. Krzysztof Poc

    outside type, outside function

    Krzysztof Poc, Feb 3, 2012, in forum: C++
    Replies:
    1
    Views:
    304
    Victor Bazarov
    Feb 7, 2012
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page