LYNX’sDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/yeremyacuna/LYNX/llms.txt
Use this file to discover all available pages before exploring further.
SearchAlgorithms<T>, defined in include/SearchAlgorithms.h, provides four static binary search overloads that cover every search scenario in the system. The two default overloads rely on the type’s operator== and operator<; the two custom-comparator overloads accept a less lambda so that any field of any type can be used as a search key without the type needing to define comparison operators. Both iterative and recursive forms are available — choose iterative to avoid stack overhead on large arrays, or recursive when clarity is preferred. All four overloads return the index of the matching element, or -1 if the target is not found.
Method Reference
Default overloads (uses operator== and operator<)
== and < operators. The iterative version maintains left and right index pointers in a while loop — O(log n) time, O(1) space. The recursive version delegates to an internal helper that shrinks the search window on each call — O(log n) time, O(log n) stack space due to recursive call frames.
Custom-comparator overloads (lambda less)
less(a, b) must return true if a should come before b in the sorted array — exactly the same convention used by the sorting comparators in AdvancedSorts. Equality is derived from the comparator: two elements are considered equal when neither is strictly less than the other.
With the lambda overloads, two elements
a and b are considered equal when !less(a, b) && !less(b, a) — that is, neither element is strictly less than the other. This is the standard strict weak ordering contract. Your comparator must satisfy this contract; if less(a, b) is true, then less(b, a) must be false, and equality must be transitive.| Overload | Time | Space | Equality test |
|---|---|---|---|
binarySearchIterative (default) | O(log n) | O(1) | arr[mid] == target |
binarySearchRecursive (default) | O(log n) | O(log n) | arr[mid] == target |
binarySearchIterative (comparator) | O(log n) | O(1) | !less(a,b) && !less(b,a) |
binarySearchRecursive (comparator) | O(log n) | O(log n) | !less(a,b) && !less(b,a) |
Usage in LYNX
TripManager::searchTripByIdBinary(string code) is the primary consumer of SearchAlgorithms in the codebase. It retrieves all trips, sorts them by ID using heapSort with a tripIdLess comparator, then calls binarySearchRecursive with the same comparator to locate the target trip.
Prerequisites and Pitfalls
- Sort before searching. Call the appropriate sort from
AdvancedSorts<T>orAdvancedOrders<T>on the same array with the same comparator immediately before calling binary search. Inserting new elements after sorting invalidates the sorted order. - Consistent comparator. The
lesslambda passed to the search must define the same ordering as the one used during sorting. Swapping comparators (e.g., ascending sort then descending search) will silently return wrong results or -1. - Unique keys for predictable results. Binary search returns the index of one matching element, not necessarily the first or last. If multiple elements share the same key, the returned index is implementation-defined. For unique keys (such as trip IDs), this is not a concern.
- Mid-point overflow. The implementation uses
mid = left + (right - left) / 2rather than(left + right) / 2, which avoids signed integer overflow for large arrays.