|
Authored by: Anonymous on Wednesday, May 16 2012 @ 12:27 PM EDT |
...then what is?
[ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Wednesday, May 16 2012 @ 01:05 PM EDT |
They should have rewitten it:
void range_check(int array_len, int from_index, int to_index) {
if (array_len < to_index) throw out_of_bounds_error(to_index);
if (0 > from_index) throw out_of_bounds_error(from_index);
if (to_index < from_index) throw illegal_argument_error(description);
}
See how much more original that is? ;-)
[ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Wednesday, May 16 2012 @ 01:37 PM EDT |
There is a lot of research into how to count lines of
code. Certainly one of
the fastest, but least infomative, is
to count the number of new line
characters. In this case,
the number of lines of code is 4. Let me
reformat
it a bit:
void range_check( int array_len, int
from_index,
int
to_index )
{
if ( from_index > to_index )
throw
illegal_argument_error( description );
if ( from_index
if (
to_index > array_len ) throw out_of_bounds_error(
to_index );
} [ Reply to This | Parent | # ]
|
|
Authored by: Chromatix on Wednesday, May 16 2012 @ 02:54 PM EDT |
I'm not sure whether there s a strict requirement in the spec to throw an
exception for these errors. If there isn't, then there's a completely
different
way to handle erroneous parameters, which is to adjust them or
re-interpret
them safely. To do this, we need to delete rangeCheck() entirely,
and go to
the single place in the TimSort code where it is called. By
writing
code inline, we get to adjust the variables rather than just being able
to check
them.
If we look there, we can see that rangeCheck() is simply
called as a validation
step, and there is a second validation step which
recognises that a zero-
element or one-element list is, by definition, already
sorted. We can use that
existing test to eliminate the need for one of the
tests in rangeCheck -
because if 'hi' is less than 'lo', the length will be
negative and therefore is less
than 2. (Note also that the same values have
different names in this code.)
The remaining conditions can be satisfied by
two simple statements, inserted
instead of the call to
rangeCheck():
if(hi > a.length) hi = a.length;
if(lo < 0)
lo = 0;
This provides reasonable behaviour by causing TimSort to
operate on the
intersection of the full array and the specified
range.
Of course, if the exception behaviour is specified, then there
is little
alternative to writing very similar code to what is in rangeCheck.
However, it
could still be made different by choosing different variable names,
placing it
inline to the sort() method instead of out-of-line, rewriting the
exception
message, or changing the order of the tests. Or, for that matter,
adding a
comment explaining why the exception behaviour is necessary.
It
seems likely however that Bloch simply forgot that rangeCheck() was not an
integral part of TimSort when he copied TimSort in. Probably he should have
found an independent source for TimSort that was less likely to contain any
Sun code.
[ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Wednesday, May 16 2012 @ 06:04 PM EDT |
The valid indexes to an array of length are either 1..length
or 0..length-1. C uses the latter. The example code allows both
0 and length, which is an error.
There is
absolutely no way how three trivial comparisons should be copyrightable. Why?
Because there are only 36 possible comparisons AT ALL you can do with three
variables (from_index, to_index, length) and
zero!
36 is the absolute maximum; it is calculated considering
"smaller", "smaller or equal", "equal", "greater", and "greater or equal" as
different comparisons. For integers, there really are only 25 unique comparisons
between three variables and zero you can do. (For floating point values, there
are more, because the rules say that a = a is not always
true.)
It is literally like somebody just copyrighted three letters of
the alphabet. [ Reply to This | Parent | # ]
|
|
|
|
|