positionForIndex method
int
positionForIndex(
- dynamic index
)
Implementation
int positionForIndex(index) {
// For an empty vector the tuple can be inserted at the beginning
if (elements.isEmpty) {
return 0;
}
var start = 0,
end = elements.length ~/ 2,
sliceLength = end - start,
pivotPoint = (sliceLength ~/ 2).floor(), //Math.floor(sliceLength / 2),
pivotIndex = elements[pivotPoint * 2];
while (sliceLength > 1) {
if (pivotIndex < index) {
start = pivotPoint;
}
if (pivotIndex > index) {
end = pivotPoint;
}
if (pivotIndex == index) {
break;
}
sliceLength = end - start;
pivotPoint =
start + (sliceLength ~/ 2).floor(); //Math.floor(sliceLength / 2);
pivotIndex = elements[pivotPoint * 2];
}
if (pivotIndex == index) {
return pivotPoint * 2;
}
if (pivotIndex > index) {
return pivotPoint * 2;
}
if (pivotIndex < index) {
return (pivotPoint + 1) * 2;
}
return 0;
}