/usr/include/vigra/bucket_queue.hxx is in libvigraimpex-dev 1.9.0+dfsg-10+b2.
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | /************************************************************************/
/* */
/* Copyright 2010-2011 by Ullrich Koethe */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_BUCKET_QUEUE_HXX
#define VIGRA_BUCKET_QUEUE_HXX
#include "config.hxx"
#include "error.hxx"
#include "array_vector.hxx"
#include <queue>
namespace vigra {
/** \brief Priority queue implemented using bucket sort.
This template implements functionality similar to <tt><a href="http://www.sgi.com/tech/stl/priority_queue.html">std::priority_queue</a></tt>,
but uses a more efficient algorithm based on bucket sort. It can be used
when all priorities are positive integers in a given range (typically, 0...255).
By default, <tt>BucketQueue\<ValueType\></tt> sorts the elements in descending order,
i.e. like in <tt>std::priority_queue</tt> the largest element has highest priority.
An ascending queue can be specified as <tt>BucketQueue\<ValueType, true\></tt>.
Elements with equal priorities are returned in a first-in first-out fashion.
The main difference to <tt>std::priority_queue</tt> is the function <tt>push</tt>
which explicitly takes the priority of the element to be added as a second argument.
This allows optimization of <tt>ValueType</tt>: since the bucket uniquely
determines an element's priority, there is no need for <tt>ValueType</tt> to
store redundant priority information. If compatibility to <tt>std::priority_queue</tt>
is more important, use \ref vigra::MappedBucketQueue.
<b>\#include</b> \<vigra/bucket_queue.hxx\><br>
Namespace: vigra
*/
template <class ValueType,
bool Ascending = false> // std::priority_queue is descending
class BucketQueue
{
ArrayVector<std::queue<ValueType> > buckets_;
std::size_t size_;
std::ptrdiff_t top_;
public:
typedef ValueType value_type;
typedef ValueType & reference;
typedef ValueType const & const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t priority_type;
/** \brief Create bucket queue with \arg bucket_count entries.
Priorities must be integers in the range <tt>[0, ..., bucket_count-1]</tt>.
*/
BucketQueue(size_type bucket_count = 256)
: buckets_(bucket_count),
size_(0), top_(0)
{}
/** \brief Number of elements in this queue.
*/
size_type size() const
{
return size_;
}
/** \brief Queue contains no elements.
Equivalent to <tt>size() == 0</tt>.
*/
bool empty() const
{
return size() == 0;
}
/** \brief Maximum index (i.e. priority) allowed in this queue.
Equivalent to <tt>bucket_count - 1</tt>.
*/
priority_type maxIndex() const
{
return (priority_type)buckets_.size() - 1;
}
/** \brief Priority of the current top element.
*/
priority_type topPriority() const
{
return top_;
}
/** \brief The current top element.
*/
const_reference top() const
{
return buckets_[top_].front();
}
/** \brief Remove the current top element.
*/
void pop()
{
--size_;
buckets_[top_].pop();
while(top_ > 0 && buckets_[top_].size() == 0)
--top_;
}
/** \brief Insert new element \arg v with given \arg priority.
*/
void push(value_type const & v, priority_type priority)
{
++size_;
buckets_[priority].push(v);
if(priority > top_)
top_ = priority;
}
};
template <class ValueType>
class BucketQueue<ValueType, true> // ascending queue
{
ArrayVector<std::queue<ValueType> > buckets_;
std::size_t size_;
std::ptrdiff_t top_;
public:
typedef ValueType value_type;
typedef ValueType & reference;
typedef ValueType const & const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t priority_type;
BucketQueue(size_type bucket_count = 256)
: buckets_(bucket_count),
size_(0), top_((priority_type)bucket_count)
{}
size_type size() const
{
return size_;
}
bool empty() const
{
return size() == 0;
}
priority_type maxIndex() const
{
return (priority_type)buckets_.size() - 1;
}
priority_type topPriority() const
{
return top_;
}
const_reference top() const
{
return buckets_[top_].front();
}
void pop()
{
--size_;
buckets_[top_].pop();
while(top_ < (priority_type)buckets_.size() && buckets_[top_].size() == 0)
++top_;
}
void push(value_type const & v, priority_type priority)
{
++size_;
buckets_[priority].push(v);
if(priority < top_)
top_ = priority;
}
};
/** \brief Priority queue implemented using bucket sort (STL compatible).
This template is compatible to <tt><a href="http://www.sgi.com/tech/stl/priority_queue.html">std::priority_queue</a></tt>,
but uses a more efficient algorithm based on bucket sort. It us used
like \ref vigra::BucketQueue, but has an additional <tt>PriorityFunctor</tt>
which extracts the priority value of an element of type <tt>ValueType</tt>.
Thus functor is called within <tt>push</tt> so that it does not need an
extra argument.
<b>\#include</b> \<vigra/bucket_queue.hxx\><br>
Namespace: vigra
*/
template <class ValueType,
class PriorityFunctor,
bool Ascending = false>
class MappedBucketQueue
: public BucketQueue<ValueType, Ascending>
{
PriorityFunctor get_priority_;
public:
typedef BucketQueue<ValueType, Ascending> BaseType;
typedef typename BaseType::value_type value_type;
typedef typename BaseType::reference reference;
typedef typename BaseType::const_reference const_reference;
typedef typename BaseType::size_type size_type;
typedef typename BaseType::priority_type priority_type;
/** \brief Create a queue with \arg bucket_count entries.
Priorities will be computed by the <tt>PriorityFunctor</tt>
given in \arg priority (i.e. <tt>priority(v)</tt> must result in an integer,
where <tt>v</tt> is an instance of <tt>ValueType</tt>).
*/
MappedBucketQueue(unsigned int bucket_count = 256,
PriorityFunctor const & priority = PriorityFunctor())
: BaseType(bucket_count),
get_priority_(priority)
{}
/** \brief Insert new element \arg v.
Its priority is calculated by <tt>priority(v)</tt>,
where <tt>priority</tt> is an instance of the
<tt>PriorityFunctor</tt> passed in the constructor.
If the priority is outside the range <tt>[0, ..., bucket_count-1]</tt>,
it is clamped to the range borders.
*/
void push(value_type const & v)
{
priority_type index = get_priority_(v);
// clamp index to the allowed range
if(index > BaseType::maxIndex())
index = BaseType::maxIndex();
else if (index < 0)
index = 0;
BaseType::push(v, index);
}
};
/** \brief Heap-based priority queue compatible to BucketQueue.
This template is compatible to \ref vigra::BucketQueue, but except arbitrary priority
types. Internally, it uses a <tt>std::priority_queue</tt>, but implements an
API where priorities and payload data are separate, like in \ref vigra::BucketQueue.
<b>\#include</b> \<vigra/bucket_queue.hxx\><br>
Namespace: vigra
*/
template <class ValueType,
class PriorityType,
bool Ascending = false> // std::priority_queue is descending
class PriorityQueue
{
typedef std::pair<ValueType, PriorityType> ElementType;
struct Compare
{
typename IfBool<Ascending, std::greater<PriorityType>,
std::less<PriorityType> >::type cmp;
bool operator()(ElementType const & l, ElementType const & r) const
{
return cmp(l.second, r.second);
}
};
typedef std::priority_queue<ElementType, std::vector<ElementType>, Compare> Heap;
Heap heap_;
public:
typedef ValueType value_type;
typedef ValueType & reference;
typedef ValueType const & const_reference;
typedef typename Heap::size_type size_type;
typedef PriorityType priority_type;
/** \brief Create empty priority queue.
*/
PriorityQueue()
: heap_()
{}
/** \brief Number of elements in this queue.
*/
size_type size() const
{
return heap_.size();
}
/** \brief Queue contains no elements.
Equivalent to <tt>size() == 0</tt>.
*/
bool empty() const
{
return size() == 0;
}
/** \brief Maximum index (i.e. priority) allowed in this queue.
Equivalent to <tt>bucket_count - 1</tt>.
*/
priority_type maxIndex() const
{
return NumericTraits<priority_type>::max();
}
/** \brief Priority of the current top element.
*/
priority_type topPriority() const
{
return heap_.top().second;
}
/** \brief The current top element.
*/
const_reference top() const
{
return heap_.top().first;
}
/** \brief Remove the current top element.
*/
void pop()
{
heap_.pop();
}
/** \brief Insert new element \arg v with given \arg priority.
*/
void push(value_type const & v, priority_type priority)
{
heap_.push(ElementType(v, priority));
}
};
} // namespace vigra
#endif // VIGRA_BUCKET_QUEUE_HXX
|