Check equality first, then less-than, default to greater-than. Use DIV.
var contains_eq_lt_gt_div = function (collection, target) {
var low = 0;
var high = collection.length-1;
while (low <= high) {
var mid = (low+high)/2;
var rc = collection[mid] - target;
if (rc == 0) {
return true;
} else if (rc < 0) {
low = mid+1;
} else {
high = mid-1;
}
}
return false;
}
Check less-than first, then greater-than, default to greater equality. Use DIV.
var contains_lt_gt_eq_div = function (collection, target) {
var low = 0;
var high = collection.length-1;
while (low <= high) {
var mid = (low+high)/2;
var rc = collection[mid] - target;
if (rc < 0) {
low = mid+1;
} else if (rc > 0) {
high = mid-1;
} else {
return true;
}
}
return false;
}
Check greater-than first, then equality, default to less-than. Use SHIFT.
var contains_gt_eq_lt_shift = function (collection, target) {
var low = 0;
var high = collection.length-1;
while (low <= high) {
var mid = (low+high) >> 1;
var rc = collection[mid] - target;
if (rc > 0) {
high = mid-1;
} else if (rc == 0) {
return true;
} else {
low = mid+1;
}
}
return false;
}
Check less-than first, then greater-than, default to equality. Use SHIFT.
var contains_lt_gt_eq_shift = function(collection, target) {
var low = 0;
var high = collection.length-1;
while (low <= high) {
var mid = (low+high) >> 1;
var rc = collection[mid] - target;
if (rc < 0) {
low = mid+1;
} else if (rc > 0) {
high = mid-1;
} else {
return true;
}
}
return false;
}