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