3 ms·
I just took a look at the rangeCheck: https://majadhondt.wordpress.com/2012/05/16/googles-9-lines/ https://majadhondt.wordpress.com/2012/05/16/googles-9-lines/
by distribot 6y ago
I just took a look at the rangeCheck:
https://majadhondt.wordpress.com/2012/05/16/googles-9-lines/ https://majadhondt.wordpress.com/2012/05/16/googles-9-lines/
Is this example correct? Can someone explain to me how this:
if (toIndex > arrayLen)
is correct?
If the array length is say 5, and toIndex is 5, that should still throw an index out of bounds exception, right? But it would be acceptable here.
- mikevm 6y agotoIndex is defined to be the index after the last element of the range (a half-closed interval).
- Jabbles 6y agoThe example is correct. toIndex is exclusive. Here is Dijkstra's opinion on why: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- tomsmeding 6y agoAside from the questionable inequalities, there's a missing closing parenthesis after the arguments on the first line, too. EDIT: as has been pointed out, the inequalities are correct. The original code can e.g. be found here, including doc comments showing the intended meaning of the arguments: http://www.groklaw.net/pdf3/OraGoogletrialexhibits/2012-04-20/0045.001.pdf http://www.groklaw.net/pdf3/OraGoogletrialexhibits/2012-04-2... (all the way at the end)
- distribot 6y agoThanks this makes sense. So the toIndex is literally defined as "the index after the last element in the range".
- jameshart 6y agoThe upper bound of the range is intended to be exclusive, while the lower bound is inclusive - basically, the same s array indexing convention. It allows the case where fromIndex == toIndex to represent the empty range (note that this check does permit fromIndex to equal toIndex, but never to exceed it), and the number of entries in a given range is always defined by toIndex - fromIndex.
- asgeir 6y agoThis is correct for a half-closed interval https://mathworld.wolfram.com/Half-ClosedInterval.html https://mathworld.wolfram.com/Half-ClosedInterval.html