TPIE

2362a60
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(),
47  : pq(max_size, bucket), sz(0), comp(c) {}
48 
52  template <typename IT>
53  internal_priority_queue(size_type max_size, const IT & start, const IT & end,
54  comp_t c=comp_t(),
56  : pq(max_size, bucket), sz(0), comp(c) {
57  insert(start, end);
58  }
59 
63  template <typename IT>
64  void insert(const IT & start, const IT & end) {
65  std::copy(start, end, pq.find(sz));
66  sz += (end - start);
67  make_safe();
68  }
69 
76  void unsafe_push(const T & v) {
77  pq[sz++] = v;
78  }
79 
80  void unsafe_push(T && v) {
81  pq[sz++] = std::move(v);
82  }
83 
88  void make_safe() {
89  std::make_heap(pq.begin(), pq.find(sz), comp);
90  }
91 
96  bool empty() const {return sz == 0;}
97 
102  size_type size() const {return sz;}
103 
109  void push(const T & v) {
110  assert(size() < pq.size());
111  pq[sz++] = v;
112  std::push_heap(pq.begin(), pq.find(sz), comp);
113  }
114 
115  void push(T && v) {
116  assert(size() < pq.size());
117  pq[sz++] = std::move(v);
118  std::push_heap(pq.begin(), pq.find(sz), comp);
119  }
120 
124  void pop() {
125  assert(!empty());
126  std::pop_heap(pq.begin(), pq.find(sz), comp);
127  --sz;
128  }
129 
133  void pop_and_push(const T & v) {
134  assert(!empty());
135  pq[0] = v;
136  pop_and_push_heap(pq.begin(), pq.find(sz), comp);
137  }
138 
139  void pop_and_push(T && v) {
140  assert(!empty());
141  pq[0] = std::move(v);
142  pop_and_push_heap(pq.begin(), pq.find(sz), comp);
143  }
144 
150  const T & top() const {return pq[0];}
151 
152  T & top() {return pq[0];}
153 
158  static double memory_coefficient() {
160  }
161 
166  static double memory_overhead() {
168  }
169 
176  return pq;
177  }
178 
182  void clear() {sz=0;}
183 
188  void resize(size_t s) {sz=0; pq.resize(s);}
189 private:
190  tpie::array<T> pq;
191  size_type sz;
193 };
194 
195 } // tpie namespace
196 #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:167
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(), memory_bucket_ref bucket=memory_bucket_ref())
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.
internal_priority_queue(size_type max_size, comp_t c=comp_t(), memory_bucket_ref bucket=memory_bucket_ref())
Construct a priority queue.
Class storring a reference to a memory bucket.
Definition: memory.h:366
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:393
void clear()
Clear the structure of all elements.
const T & top() const
Return the minimum element.
static double memory_overhead()
Return the memory overhead of the structure.
Definition: array.h:400
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:307
size_type size() const
Return the size of the array.
Definition: array.h:526
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.