You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2003/12/01 08:46:13 UTC
cvs commit: jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two TestBinaryChop.java
rwaldhoff 2003/11/30 23:46:13
Modified: functor/src/test/org/apache/commons/functor/example/kata/two
TestBinaryChop.java
Log:
don't assume compareTo returns -1 or 1
Revision Changes Path
1.2 +17 -22 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/TestBinaryChop.java
Index: TestBinaryChop.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/TestBinaryChop.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestBinaryChop.java 1 Dec 2003 07:30:24 -0000 1.1
+++ TestBinaryChop.java 1 Dec 2003 07:46:13 -0000 1.2
@@ -121,16 +121,14 @@
int cur = 0;
while(low < high) {
cur = (high+low)/2;
- switch(((Comparable)(list.get(cur))).compareTo(seeking)) {
- case 0: // equal
- return cur;
- case 1:
- high = cur;
- break;
- case -1:
- if(low == cur) { cur++; }
- low = cur;
- break;
+ int comp = ((Comparable)(list.get(cur))).compareTo(seeking);
+ if(comp == 0) {
+ return cur;
+ } else if(comp > 0) {
+ high = cur;
+ } else {
+ if(low == cur) { cur++; }
+ low = cur;
}
}
return -1;
@@ -147,16 +145,13 @@
} else {
int pivot = list.size()/2;
int offset = 0;
- int comp = ((Comparable)(list.get(pivot))).compareTo(seeking);
- switch(comp) {
- case 0:
- return pivot;
- case -1:
- offset = find(seeking,list.subList(Math.max(pivot,1),list.size()));
- break;
- case 1:
- offset = find(seeking,list.subList(0,pivot));
- break;
+ int comp = ((Comparable)(list.get(pivot))).compareTo(seeking);
+ if(comp == 0) {
+ return pivot;
+ } else if(comp < 0) {
+ offset = find(seeking,list.subList(Math.max(pivot,1),list.size()));
+ } else {
+ offset = find(seeking,list.subList(0,pivot));
}
return -1 == offset ? -1 : (comp == 1) ? offset : pivot+offset;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org