#include <LinearSearch.h>
These linear search functions work on linear data structures which have operator() or operator[] defined on them (e.g. C-array, Vector, IPosition, Block, ScalarColumn, etc.) Two versions of the functions are provided, one which uses parentheses () for indexing, one which uses square brackets [] (obviously the latter one can also be used for ordinary C-style pointers and arrays). It is assumed that the container uses zero-based indexing.
The returned index is in the range [0.\.n-1]. When the value is not found, -1 is returned. Tip: While normally you want to search a container with indices in the range [0 .\.. n-1], any desired lower bound may be used instead. Caution: Linear searching should only be used for small arrays. For larger arrays sort and binarySearch should be used.
Vector<Int> vi;
.\.. // Sets vi somehow
Int val;
Bool found;
while (cin >> val && val != -999) {
Int where = linearSearch(found, vi, val, vi.nelements());
if (found) {
cout << "Found " << val << " at position " << where << endl;
} else {
cout << val << " is not in the vector, but it belongs at " <<
where << endl;
}
}
Neil Killeen needed a linear search on a Vector. Modelling it after BinarySearch was the logical step to take.
Definition at line 113 of file LinearSearch.h.
Public Member Functions | |
| template<class Container, class ElType> | |
| Int | linearSearch1 (const Container &container, const ElType &value, uInt lower=0) |
| This version of the function is for containers that use () for indexing. | |
| template<class Container, class ElType> | |
| Int | linearSearch (Bool &found, const Container &container, const ElType &value, uInt n, uInt lower=0) |
| template<class Container, class ElType> | |
| Int | linearSearchBrackets1 (const Container &container, const ElType &value, uInt lower=0) |
| This version of the function is for containers that use [] for indexing. | |
| template<class Container, class ElType> | |
| Int | linearSearchBrackets (Bool &found, const Container &container, const ElType &value, uInt n, uInt lower=0) |
| Int casa::LinearSearch_global_functions_linearsearch::linearSearch1 | ( | const Container & | container, | |
| const ElType & | value, | |||
| uInt | lower = 0 | |||
| ) |
This version of the function is for containers that use () for indexing.
| Int casa::LinearSearch_global_functions_linearsearch::linearSearch | ( | Bool & | found, | |
| const Container & | container, | |||
| const ElType & | value, | |||
| uInt | n, | |||
| uInt | lower = 0 | |||
| ) |
| Int casa::LinearSearch_global_functions_linearsearch::linearSearchBrackets1 | ( | const Container & | container, | |
| const ElType & | value, | |||
| uInt | lower = 0 | |||
| ) |
This version of the function is for containers that use [] for indexing.
| Int casa::LinearSearch_global_functions_linearsearch::linearSearchBrackets | ( | Bool & | found, | |
| const Container & | container, | |||
| const ElType & | value, | |||
| uInt | n, | |||
| uInt | lower = 0 | |||
| ) |
1.5.1