casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
LinearSearch.h
Go to the documentation of this file.
00001 //# LinearSearch.h: Linear search through linear data structures
00002 //# Copyright (C) 1997,1999
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: LinearSearch.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00028 
00029 
00030 #ifndef CASA_LINEARSEARCH_H
00031 #define CASA_LINEARSEARCH_H
00032 
00033 //# Includes
00034 #include <casa/aips.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 // <summary>
00039 // Linear search a linear data structure.
00040 // </summary>
00041 
00042 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tLinearSearch" demos="">
00043 // </reviewed>
00044 
00045 // <synopsis>
00046 // These linear search functions work on linear data structures
00047 // which have operator() or operator[] defined on them (<i>e.g.</i>
00048 // C-array, Vector, IPosition, Block, ScalarColumn, <i>etc.</i>)
00049 // Two versions of the functions are provided, one which uses
00050 // parentheses () for indexing, one which uses square brackets [] (obviously
00051 // the latter one can also be used for ordinary C-style pointers and arrays).
00052 // It is assumed that the container uses zero-based indexing.
00053 //
00054 // The returned index is in the range [0..n-1]. When the value is
00055 // not found, -1 is returned.
00056 // <note role=tip>
00057 // While normally you want to search a container with indices in the range
00058 // <src>[0 ... n-1]</src>, any desired lower bound may be used instead.
00059 // </note>
00060 // <note role=caution>
00061 // Linear searching should only be used for small arrays.
00062 // For larger arrays sort and
00063 // <linkto group=BinarySearch.h#binarysearch>binarySearch</linkto>
00064 // should be used.
00065 // </note>
00066 // </synopsis>
00067 //
00068 // <example>
00069 // <srcblock>
00070 // Vector<Int> vi;
00071 // ...  // Sets vi somehow
00072 // Int val;
00073 // Bool found;
00074 // while (cin >> val  &&  val != -999) {
00075 //     Int where = linearSearch(found, vi, val, vi.nelements());
00076 //     if (found) {
00077 //       cout << "Found " << val << " at position " << where << endl;
00078 //     } else {
00079 //       cout << val << " is not in the vector, but it belongs at " <<
00080 //            where << endl;
00081 //     }
00082 // } 
00083 // </srcblock>
00084 // </example>
00085 //
00086 // <motivation>
00087 // Neil Killeen needed a linear search on a Vector.
00088 // Modelling it after BinarySearch was the logical step to take.
00089 // </motivation>
00090 //
00091 // <templating arg=Container>
00092 //    <li> operator(Int) or operator[Int] needs to be defined.
00093 //    <li> The index must be zero based.
00094 //    <li> The result of that indexing must be an expression that can be 
00095 //         compared with an object of class ElType. Normally in fact it would
00096 //         be a temporary of class ElType.
00097 //    <li> Member function nelements() is needed when the shorthand is taken.
00098 // </templating>
00099 // <templating arg=ElType>
00100 //    <li> The equal operator (==) need to be defined.
00101 // </templating>
00102 //
00103 // <todo asof="yyyy/mm/dd">
00104 //   <li> I suspect that an implementation is possible that only calls
00105 //        operator() or [] once during each evaluation of the while loop.
00106 //   <li> MACROize implementation so that code isn't repeated twice. Or, 
00107 //        possibly implement one using the other (e.g. by introducing an adapter
00108 //        class that turns (i) into [i].
00109 // </todo>
00110 
00111 
00112 // <group name=linearsearch>  
00113 
00114 // Search <i>container</i> for <i>value</i>. There are assumed to be at least
00115 // <i>n</i> elements in the container. The container will be searched for
00116 // indices in the range <src>[lower ... lower + n - 1]</src> Return the index
00117 // of the first element which is greater than or equal to (ascending order) or
00118 // less than or equal to (descending order) the value.
00119 // When not found, -1 is returned and found is set to False.
00120 //# GvD 19971008: The functions need different names, because g++ gives errors
00121 //# when instantiating.
00122 // <group>
00123 // This version of the function is for containers that use () for indexing.
00124 template<class Container, class ElType>
00125 Int linearSearch1 (const Container& container, const ElType& value,
00126                    uInt lower = 0);
00127 template<class Container, class ElType>
00128 Int linearSearch (Bool& found, const Container& container, 
00129                   const ElType& value, uInt n, uInt lower=0);
00130 // This version of the function is for containers that use [] for indexing.
00131 template<class Container, class ElType>
00132 Int linearSearchBrackets1 (const Container& container, const ElType& value,
00133                            uInt lower = 0);
00134 template<class Container, class ElType>
00135 Int linearSearchBrackets (Bool& found, const Container& container, 
00136                           const ElType& value, uInt n, uInt lower=0);
00137 // </group>
00138 // </group>
00139 
00140 
00141 
00142 } //# NAMESPACE CASA - END
00143 
00144 #ifndef CASACORE_NO_AUTO_TEMPLATES
00145 #include <casa/Utilities/LinearSearch.tcc>
00146 #endif //# CASACORE_NO_AUTO_TEMPLATES
00147 #endif