/usr/include/CLAM/stl_optbinsearch.h is in libclam-dev 1.4.0-5build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | /*
* Copyright (c) 2001-2002 MUSIC TECHNOLOGY GROUP (MTG)
* UNIVERSITAT POMPEU FABRA
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef __stl_optbinsearch__
#define __stl_optbinsearch__
#include <iterator>
namespace std
{
namespace detail
{
template < typename RandomIterator, typename value_type >
unsigned bisect( RandomIterator begin, RandomIterator end,
const value_type& val,
unsigned lowerBound, unsigned upperBound )
{
lowerBound = ( lowerBound > 0) ? lowerBound-- : lowerBound;
int dist = std::distance( begin, end );
bool ascending = *(begin+dist-1) >= *(begin);
while( upperBound - lowerBound > 1 )
{
unsigned mid = ( upperBound + lowerBound ) >> 1;
if ( val >= *(begin+mid) == ascending )
lowerBound = mid;
else
upperBound = mid;
}
return lowerBound;
}
template < typename RandomIterator, typename value_type, typename GreatEqCmp >
unsigned bisect( RandomIterator begin, RandomIterator end,
const value_type& val,
unsigned lowerBound, unsigned upperBound, GreatEqCmp cmpOp )
{
lowerBound = ( lowerBound > 0) ? lowerBound-- : lowerBound;
int dist = std::distance( begin, end );
bool ascending = cmpOp( *(begin+dist-1), *(begin) );
while( upperBound - lowerBound > 1 )
{
unsigned mid = ( upperBound + lowerBound ) >> 1;
if ( cmpOp( val, *(begin+mid) ) == ascending )
lowerBound = mid;
else
upperBound = mid;
}
return lowerBound;
}
}
template < typename RandomIterator, typename value_type, typename GreatEqCmp >
RandomIterator hunt( RandomIterator begin, RandomIterator end,
const value_type& val, RandomIterator last, GreatEqCmp cmpOp )
{
unsigned guess = std::distance( begin, last );
unsigned dist = std::distance( begin, end );
if ( last < begin || last >= end )
return begin + detail::bisect( begin, end, val, 0, dist, cmpOp );
bool ascending = cmpOp( *(begin+dist-1), *(begin) );
unsigned inc = 1;
unsigned upperBound;
if ( cmpOp( val, *(begin+guess) ) == ascending )
{
if ( guess == dist - 1 )
return end;
upperBound = guess+1;
while( cmpOp( val, *(begin+upperBound) ) == ascending )
{
guess = upperBound;
inc <<= 1;
upperBound += inc;
if ( upperBound >= dist )
{
RandomIterator result = begin + detail::bisect( begin, end, val, guess, dist, cmpOp );
if ( result == end-1 )
return end;
else
return result;
}
}
}
else
{
if ( guess == 0 ) return end;
upperBound = guess--;
while ( !(cmpOp( val, *(begin+guess))) == ascending )
{
upperBound = guess;
inc <<= 1;
if ( inc >= upperBound )
{
RandomIterator result = begin + detail::bisect( begin, end, val, 0, upperBound, cmpOp );
if ( result == begin )
return end;
else
return result;
}
guess = upperBound - inc;
}
}
return begin + detail::bisect( begin, end, val, guess, upperBound, cmpOp );
}
template < typename RandomIterator, typename value_type, typename GreatEqCmp >
RandomIterator hunt( RandomIterator begin, RandomIterator end,
const value_type& val, GreatEqCmp cmpOp )
{
return hunt( begin, end, val, begin, cmpOp );
}
template < typename RandomIterator, typename value_type >
RandomIterator hunt( RandomIterator begin, RandomIterator end,
const value_type& val, RandomIterator last )
{
unsigned guess = std::distance( begin, last );
unsigned dist = std::distance( begin, end );
if ( last < begin || last >= end )
return begin + detail::bisect( begin, end, val, 0, dist );
bool ascending = *(begin+dist-1) >= *(begin);
unsigned inc = 1;
unsigned upperBound;
if ( val >= *(begin+guess) == ascending )
{
if ( guess == dist - 1 )
return end;
upperBound = guess+1;
while( val >= *(begin+upperBound) == ascending )
{
guess = upperBound;
inc <<= 1;
upperBound += inc;
if ( upperBound >= dist )
{
RandomIterator result = begin + detail::bisect( begin, end, val, guess, dist );
if ( result == end-1 )
return end;
else
return result;
}
}
}
else
{
if ( guess == 0 ) return end;
upperBound = guess--;
while ( !(val >= *(begin+guess)) == ascending )
{
upperBound = guess;
inc <<= 1;
if ( inc >= upperBound )
{
RandomIterator result = begin + detail::bisect( begin, end, val, 0, upperBound );
if ( result == begin )
return end;
else
return result;
}
guess = upperBound - inc;
}
}
return begin + detail::bisect( begin, end, val, guess, upperBound );
}
// TODO: Commented out as g++ 4.1 complaints, it seems not to be used
template < typename RandomIterator, typename value_type >
RandomIterator hunt( RandomIterator begin, RandomIterator end,
const value_type& val )
{
return hunt( begin, end, val, begin);
}
}
#endif // stl_optbinsearch.h
|