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
27#ifndef CASA_LINEARSEARCH_H
28#define CASA_LINEARSEARCH_H
29
30//# Includes
31#include <casacore/casa/aips.h>
32
33namespace casacore { //# NAMESPACE CASACORE - BEGIN
34
35// <summary>
36// Linear search a linear data structure.
37// </summary>
38
39// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tLinearSearch" demos="">
40// </reviewed>
41
42// <synopsis>
43// These linear search functions work on linear data structures
44// which have operator() or operator[] defined on them (<i>e.g.</i>
45// C-array, Vector, IPosition, Block, ScalarColumn, <i>etc.</i>)
46// Two versions of the functions are provided, one which uses
47// parentheses () for indexing, one which uses square brackets [] (obviously
48// the latter one can also be used for ordinary C-style pointers and arrays).
49// It is assumed that the container uses zero-based indexing.
50//
51// The returned index is in the range [0..n-1]. When the value is
52// not found, -1 is returned.
53// <note role=tip>
54// While normally you want to search a container with indices in the range
55// <src>[0 ... n-1]</src>, any desired lower bound may be used instead.
56// </note>
57// <note role=caution>
58// Linear searching should only be used for small arrays.
59// For larger arrays sort and
60// <linkto group=BinarySearch.h#binarysearch>binarySearch</linkto>
61// should be used.
62// </note>
63// </synopsis>
64//
65// <example>
66// <srcblock>
67// Vector<Int> vi;
68// ... // Sets vi somehow
69// Int val;
70// Bool found;
71// while (cin >> val && val != -999) {
72// Int where = linearSearch(found, vi, val, vi.nelements());
73// if (found) {
74// cout << "Found " << val << " at position " << where << endl;
75// } else {
76// cout << val << " is not in the vector, but it belongs at " <<
77// where << endl;
78// }
79// }
80// </srcblock>
81// </example>
82//
83// <motivation>
84// Neil Killeen needed a linear search on a Vector.
85// Modelling it after BinarySearch was the logical step to take.
86// </motivation>
87//
88// <templating arg=Container>
89// <li> operator(Int) or operator[Int] needs to be defined.
90// <li> The index must be zero based.
91// <li> The result of that indexing must be an expression that can be
92// compared with an object of class ElType. Normally in fact it would
93// be a temporary of class ElType.
94// <li> Member function nelements() is needed when the shorthand is taken.
95// </templating>
96// <templating arg=ElType>
97// <li> The equal operator (==) need to be defined.
98// </templating>
99//
100// <todo asof="yyyy/mm/dd">
101// <li> I suspect that an implementation is possible that only calls
102// operator() or [] once during each evaluation of the while loop.
103// <li> MACROize implementation so that code isn't repeated twice. Or,
104// possibly implement one using the other (e.g. by introducing an adapter
105// class that turns (i) into [i].
106// </todo>
107
108
109// <group name=linearsearch>
111// Search <i>container</i> for <i>value</i>. There are assumed to be at least
112// <i>n</i> elements in the container. The container will be searched for
113// indices in the range <src>[lower ... lower + n - 1]</src> Return the index
114// of the first element which is greater than or equal to (ascending order) or
115// less than or equal to (descending order) the value.
116// When not found, -1 is returned and found is set to False.
117//# GvD 19971008: The functions need different names, because g++ gives errors
118//# when instantiating.
119// <group>
120// This version of the function is for containers that use () for indexing.
121template<class Container, class ElType>
122Int linearSearch1 (const Container& container, const ElType& value,
123 uInt lower = 0);
124template<class Container, class ElType>
125Int linearSearch (Bool& found, const Container& container,
126 const ElType& value, uInt n, uInt lower=0);
127// This version of the function is for containers that use [] for indexing.
128template<class Container, class ElType>
129Int linearSearchBrackets1 (const Container& container, const ElType& value,
130 uInt lower = 0);
131template<class Container, class ElType>
132Int linearSearchBrackets (Bool& found, const Container& container,
133 const ElType& value, uInt n, uInt lower=0);
134// </group>
135// </group>
136
137
138
139} //# NAMESPACE CASACORE - END
140
141#ifndef CASACORE_NO_AUTO_TEMPLATES
142#include <casacore/casa/Utilities/LinearSearch.tcc>
143#endif //# CASACORE_NO_AUTO_TEMPLATES
144#endif
this file contains all the compiler specific defines
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
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
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.