MCPcopy Create free account
hub / github.com/OpenXcom/OpenXcom / searchCodeIndex

Function searchCodeIndex

src/lodepng.cpp:1295–1312  ·  view source on GitHub ↗

search the index in the array, that has the largest value smaller than or equal to the given value, given array must be sorted (if no value is smaller, it returns the size of the given array)*/

Source from the content-addressed store, hash-verified

1293/*search the index in the array, that has the largest value smaller than or equal to the given value,
1294given array must be sorted (if no value is smaller, it returns the size of the given array)*/
1295static size_t searchCodeIndex(const unsigned* array, size_t array_size, size_t value)
1296{
1297 /*linear search implementation*/
1298 /*for(size_t i = 1; i < array_size; i++) if(array[i] > value) return i - 1;
1299 return array_size - 1;*/
1300
1301 /*binary search implementation (not that much faster) (precondition: array_size > 0)*/
1302 size_t left = 1;
1303 size_t right = array_size - 1;
1304 while(left <= right)
1305 {
1306 size_t mid = (left + right) / 2;
1307 if(array[mid] <= value) left = mid + 1; /*the value to find is more to the right*/
1308 else if(array[mid - 1] > value) right = mid - 1; /*the value to find is more to the left*/
1309 else return mid - 1;
1310 }
1311 return array_size - 1;
1312}
1313
1314static void addLengthDistance(uivector* values, size_t length, size_t distance)
1315{

Callers 1

addLengthDistanceFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected