TPIE

v1.1rc1-6-g0c97303
internal_priority_queue.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2008, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef __TPIE_INTERNAL_PRIORITY_QUEUE_H__
21 #define __TPIE_INTERNAL_PRIORITY_QUEUE_H__
22 #include <tpie/array.h>
23 #include <algorithm>
24 #include <tpie/util.h>
25 namespace tpie {
30 
36 template <typename T, typename comp_t = std::less<T> >
37 class internal_priority_queue: public linear_memory_base< internal_priority_queue<T, comp_t> > {
38 public:
39  typedef memory_size_type size_type;
40 
45  internal_priority_queue(size_type max_size, comp_t c=comp_t()): pq(max_size), sz(0), comp(c) {}
46 
50  template <typename IT>
51  internal_priority_queue(size_type max_size, const IT & start, const IT & end,
52  comp_t c=comp_t()): pq(max_size), sz(0), comp(c) {
53  insert(start, end);
54  }
55 
59  template <typename IT>
60  void insert(const IT & start, const IT & end) {
61  std::copy(start, end, pq.find(sz));
62  sz += (end - start);
63  make_safe();
64  }
65 
72  void unsafe_push(const T & v) {
73  pq[sz++] = v;
74  }
75 
80  void make_safe() {
81  std::make_heap(pq.begin(), pq.find(sz), comp);
82  }
83 
88  bool empty() const {return sz == 0;}
89 
94  inline size_type size() const {return sz;}
95 
101  inline void push(const T & v) {
102  assert(size() < pq.size());
103  pq[sz++] = v;
104  std::push_heap(pq.begin(), pq.find(sz), comp);
105  }
106 
110  inline void pop() {
111  assert(!empty());
112  std::pop_heap(pq.begin(), pq.find(sz), comp);
113  --sz;
114  }
115 
119  inline void pop_and_push(const T & v) {
120  assert(!empty());
121  pq[0] = v;
122  pop_and_push_heap(pq.begin(), pq.find(sz), comp);
123  }
124 
130  inline const T & top() const {return pq[0];}
131 
136  inline static double memory_coefficient() {
138  }
139 
144  inline static double memory_overhead() {
146  }
147 
154  return pq;
155  }
156 
160  inline void clear() {sz=0;}
161 
166  inline void resize(size_t s) {sz=0; pq.resize(s);}
167 private:
168  tpie::array<T> pq;
169  size_type sz;
171 };
172 
173 } // tpie namespace
174 #endif //__TPIE_INTERNAL_PRIORITY_QUEUE_H__
tpie::array< T > & get_array()
Return the underlying array.
void pop_and_push_heap(T a, T b, C lt)
Restore heap invariants after the first element has been replaced by some other element.
Definition: util.h:119
iterator find(size_t idx)
Return an iterator to the i'th element of the array.
Definition: array.h:166
void push(const T &v)
Insert an element into the priority queue.
Base class of data structures with linear memory usage.
Definition: util.h:73
size_type size() const
Returns the size of the queue.
internal_priority_queue(size_type max_size, const IT &start, const IT &end, comp_t c=comp_t())
Construct a priority queue with given elements.
static double memory_overhead()
Return the memory overhead of the structure.
Standard binary internal heap.
bool empty() const
Is the queue empty?
Generic internal array with known memory requirements.
void pop()
Remove the minimum element from heap.
void pop_and_push(const T &v)
Remove the minimum element and insert a new element.
void resize(size_t s)
Resize priority queue to given size.
Miscellaneous utility functions.
static double memory_coefficient()
Return the memory coefficient of the structure.
Definition: array.h:360
void clear()
Clear the structure of all elements.
const T & top() const
Return the minimum element.
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:431
static double memory_overhead()
Return the memory overhead of the structure.
Definition: array.h:367
void insert(const IT &start, const IT &end)
Insert some elements and run make_heap.
static double memory_coefficient()
Return the memory coefficient of the structure.
iterator begin()
Return an iterator to the beginning of the array.
Definition: array.h:274
size_type size() const
Return the size of the array.
Definition: array.h:472
void unsafe_push(const T &v)
Insert an element into the priority queue, possibly destroying ordering information.
void make_safe()
Make the priority queue safe after a sequence of calls to unsafe_push.
internal_priority_queue(size_type max_size, comp_t c=comp_t())
Construct a priority queue.