casacore
Loading...
Searching...
No Matches
LinearSearch.h
Go to the documentation of this file.
1// # LinearSearch.h: Linear search through linear data structures
2// # Copyright (C) 1997,1999
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_LINEARSEARCH_H
27#define CASA_LINEARSEARCH_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31
32namespace casacore { // # NAMESPACE CASACORE - BEGIN
33
34// <summary>
35// Linear search a linear data structure.
36// </summary>
37
38// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tLinearSearch" demos="">
39// </reviewed>
40
41// <synopsis>
42// These linear search functions work on linear data structures
43// which have operator() or operator[] defined on them (<i>e.g.</i>
44// C-array, Vector, IPosition, Block, ScalarColumn, <i>etc.</i>)
45// Two versions of the functions are provided, one which uses
46// parentheses () for indexing, one which uses square brackets [] (obviously
47// the latter one can also be used for ordinary C-style pointers and arrays).
48// It is assumed that the container uses zero-based indexing.
49//
50// The returned index is in the range [0..n-1]. When the value is
51// not found, -1 is returned.
52// <note role=tip>
53// While normally you want to search a container with indices in the range
54// <src>[0 ... n-1]</src>, any desired lower bound may be used instead.
55// </note>
56// <note role=caution>
57// Linear searching should only be used for small arrays.
58// For larger arrays sort and
59// <linkto group=BinarySearch.h#binarysearch>binarySearch</linkto>
60// should be used.
61// </note>
62// </synopsis>
63//
64// <example>
65// <srcblock>
66// Vector<Int> vi;
67// ... // Sets vi somehow
68// Int val;
69// Bool found;
70// while (cin >> val && val != -999) {
71// Int where = linearSearch(found, vi, val, vi.nelements());
72// if (found) {
73// cout << "Found " << val << " at position " << where << endl;
74// } else {
75// cout << val << " is not in the vector, but it belongs at " <<
76// where << endl;
77// }
78// }
79// </srcblock>
80// </example>
81//
82// <motivation>
83// Neil Killeen needed a linear search on a Vector.
84// Modelling it after BinarySearch was the logical step to take.
85// </motivation>
86//
87// <templating arg=Container>
88// <li> operator(Int) or operator[Int] needs to be defined.
89// <li> The index must be zero based.
90// <li> The result of that indexing must be an expression that can be
91// compared with an object of class ElType. Normally in fact it would
92// be a temporary of class ElType.
93// <li> Member function nelements() is needed when the shorthand is taken.
94// </templating>
95// <templating arg=ElType>
96// <li> The equal operator (==) need to be defined.
97// </templating>
98//
99// <todo asof="yyyy/mm/dd">
100// <li> I suspect that an implementation is possible that only calls
101// operator() or [] once during each evaluation of the while loop.
102// <li> MACROize implementation so that code isn't repeated twice. Or,
103// possibly implement one using the other (e.g. by introducing an adapter
104// class that turns (i) into [i].
105// </todo>
106
107// <group name=linearsearch>
109// Search <i>container</i> for <i>value</i>. There are assumed to be at least
110// <i>n</i> elements in the container. The container will be searched for
111// indices in the range <src>[lower ... lower + n - 1]</src> Return the index
112// of the first element which is greater than or equal to (ascending order) or
113// less than or equal to (descending order) the value.
114// When not found, -1 is returned and found is set to False.
115// # GvD 19971008: The functions need different names, because g++ gives errors
116// # when instantiating.
117// <group>
118// This version of the function is for containers that use () for indexing.
119template <class Container, class ElType>
120Int linearSearch1(const Container& container, const ElType& value, uInt lower = 0);
121template <class Container, class ElType>
122Int linearSearch(Bool& found, const Container& container, const ElType& value, uInt n,
123 uInt lower = 0);
124// This version of the function is for containers that use [] for indexing.
125template <class Container, class ElType>
126Int linearSearchBrackets1(const Container& container, const ElType& value, uInt lower = 0);
127template <class Container, class ElType>
128Int linearSearchBrackets(Bool& found, const Container& container, const ElType& value, uInt n,
129 uInt lower = 0);
130// </group>
131// </group>
132
133} // namespace casacore
134
135#ifndef CASACORE_NO_AUTO_TEMPLATES
136#include <casacore/casa/Utilities/LinearSearch.tcc>
137#endif // # CASACORE_NO_AUTO_TEMPLATES
138#endif
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360
Int linearSearch1(const Container &container, const ElType &value, uInt lower=0)
Search container for value.
Int linearSearchBrackets(Bool &found, const Container &container, const ElType &value, uInt n, uInt lower=0)
Int linearSearch(Bool &found, const Container &container, const ElType &value, uInt n, uInt lower=0)
Int linearSearchBrackets1(const Container &container, const ElType &value, uInt lower=0)
This version of the function is for containers that use [] for indexing.