casacore
Loading...
Searching...
No Matches
BinarySearch.h
Go to the documentation of this file.
1// # BinarySearch.h: Binary search through linear, sorted, data structures
2// # Copyright (C) 1995,1996,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_BINARYSEARCH_H
27#define CASA_BINARYSEARCH_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31
32namespace casacore { // # NAMESPACE CASACORE - BEGIN
33
34// <summary>
35// Binary search a sorted, linear, data structure.
36// </summary>
37
38// <reviewed reviewer="Ger van Diepen" date="1995/03/31" tests="tBinarySearch" demos="">
39// </reviewed>
40
41// <synopsis>
42// These binary search functions work on sorted, 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 container must be sorted (sorting is available through the
51// <linkto class="Sort">Sort</linkto> and
52// <linkto class="GenSort">GenSort</linkto>
53// classes, and from various
54// <linkto class="Table">Table</linkto> sort functions). The returned index
55// is in the range [0..n] inclusive. That is, from the first element of the
56// container to one past the last element of the container (zero-based indices).
57// If the container is sorted in ascending order, the returned index is the
58// first one whose element is greater than or equal to the searched for value.
59// If it is sorted in descending order, the returned index is the first which
60// is less than or equal to the searched for value. That is, the returned
61// index gives the position at which the value would be inserted (possibly
62// either at the end, or requiring the existing values to be "pushed" to the
63// right) maintaining the sort order. Obviously index n can only be
64// returned if the value searched for is past the end of the array, thus
65// has to be inserted at the end.
66//
67// The functions determine for themselves whether the container is sorted in
68// ascending or descending order by comparing the first and last element.
69// <note role=tip>
70// While normally you want to search a container with indices in the range
71// <src>[0 ... n-1]</src>, any desired lower bound may be used instead.
72// </note>
73// <note role=warning>
74// The functions do not check if the container is valid, <i>i.e.</i> if
75// the container is sorted and if the container does not contain duplicate
76// values.
77// </note>
78//
79// These functions loosely follow some written by Ger van Diepen in a more
80// specialized context.
81// </synopsis>
82//
83// <example>
84// <srcblock>
85// Vector<Int> vi;
86// ... // Sets vi somehow
87// genSort(vi);
88// Int val;
89// Bool found;
90// while (cin >> val && val != -999) {
91// Int where = binarySearch(found, vi, val, vi.nelements());
92// if (found) {
93// cout << "Found " << val << " at position " << where << endl;
94// } else {
95// cout << val << " is not in the vector, but it belongs at " <<
96// where << endl;
97// }
98// }
99// </srcblock>
100// </example>
101//
102// <motivation>
103// I found that I (BEG) was writing binary search functions several times,
104// for example when checking whether the cached off and gain scans in time
105// sorted data needed to be refilled. It generally seems like a useful little
106// utility function.
107// </motivation>
108//
109// <templating arg=Container>
110// <li> operator(Int) or operator[Int] needs to be defined.
111// <li> The index must be zero based.
112// <li> The result of that indexing must be an expression that can be
113// compared with an object of class ElType. Normally in fact it would
114// be a temporary of class ElType.
115// </templating>
116// <templating arg=ElType>
117// <li> The less than operator (<) and greater than (>) operators need to
118// be defined, and have their usual ordering relations.
119// </templating>
120//
121// <todo asof="yyyy/mm/dd">
122// <li> I suspect that an implementation is possible that only calls
123// operator() or [] once during each evaluation of the while loop.
124// <li> MACROize implementation so that code isn't repeated twice. Or,
125// possibly implement one using the other (e.g. by introducing an adapter
126// class that turns (i) into [i].
127// </todo>
128
129// <group name=binarysearch>
131// Search <i>container</i> for <i>value</i>. There are assumed to be at least
132// <i>n</i> elements in the container. The container will be searched for
133// indices in the range <src>[lower ... lower + n - 1]</src> Return the index
134// of the first element which is greater than or equal to (ascending order) or
135// less than or equal to (descending order) the value.
136// <group>
137// This version of the function is for containers that use () for indexing.
138template <class Container, class ElType>
139Int binarySearch(Bool &found, const Container &container, const ElType &value, uInt n,
140 Int lower = 0);
141// This version of the function is for containers that use [] for indexing.
142template <class Container, class ElType>
143Int binarySearchBrackets(Bool &found, const Container &container, const ElType &value, uInt n,
144 Int lower = 0);
145// </group>
146// </group>
147
148} // namespace casacore
149
150#ifndef CASACORE_NO_AUTO_TEMPLATES
151#include <casacore/casa/Utilities/BinarySearch.tcc>
152#endif // # CASACORE_NO_AUTO_TEMPLATES
153#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 binarySearch(Bool &found, const Container &container, const ElType &value, uInt n, Int lower=0)
Search container for value.
Int binarySearchBrackets(Bool &found, const Container &container, const ElType &value, uInt n, Int lower=0)
This version of the function is for containers that use [] for indexing.