TPIE

11a2c2d
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 constexpr double memory_coefficient() noexcept {
160  }
161 
166  static constexpr double memory_overhead() noexcept {
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::internal_priority_queue::size
size_type size() const
Returns the size of the queue.
Definition: internal_priority_queue.h:102
tpie::internal_priority_queue::unsafe_push
void unsafe_push(const T &v)
Insert an element into the priority queue, possibly destroying ordering information.
Definition: internal_priority_queue.h:76
tpie::internal_priority_queue::pop
void pop()
Remove the minimum element from heap.
Definition: internal_priority_queue.h:124
tpie::linear_memory_base
Base class of data structures with linear memory usage.
Definition: util.h:98
tpie::internal_priority_queue
Standard binary internal heap.
Definition: internal_priority_queue.h:37
tpie::internal_priority_queue::get_array
tpie::array< T > & get_array()
Return the underlying array.
Definition: internal_priority_queue.h:175
tpie::internal_priority_queue::clear
void clear()
Clear the structure of all elements.
Definition: internal_priority_queue.h:182
tpie::array< T >
tpie::internal_priority_queue::internal_priority_queue
internal_priority_queue(size_type max_size, comp_t c=comp_t(), memory_bucket_ref bucket=memory_bucket_ref())
Construct a priority queue.
Definition: internal_priority_queue.h:45
tpie::array::begin
iterator begin()
Return an iterator to the beginning of the array.
Definition: array.h:312
tpie::internal_priority_queue::push
void push(const T &v)
Insert an element into the priority queue.
Definition: internal_priority_queue.h:109
tpie::array::memory_overhead
static constexpr double memory_overhead() noexcept
Return the memory overhead of the structure.
Definition: array.h:405
array.h
tpie::internal_priority_queue::make_safe
void make_safe()
Make the priority queue safe after a sequence of calls to unsafe_push.
Definition: internal_priority_queue.h:88
tpie::internal_priority_queue::resize
void resize(size_t s)
Resize priority queue to given size.
Definition: internal_priority_queue.h:188
tpie::array::find
iterator find(size_t idx)
Return an iterator to the i'th element of the array.
Definition: array.h:172
tpie::memory_bucket_ref
Class storring a reference to a memory bucket.
Definition: memory.h:334
tpie::internal_priority_queue::internal_priority_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.
Definition: internal_priority_queue.h:53
tpie::internal_priority_queue::memory_coefficient
static constexpr double memory_coefficient() noexcept
Return the memory coefficient of the structure.
Definition: internal_priority_queue.h:158
tpie::internal_priority_queue::insert
void insert(const IT &start, const IT &end)
Insert some elements and run make_heap.
Definition: internal_priority_queue.h:64
tpie::binary_argument_swap
A binary functor with the arguments swapped.
Definition: util.h:187
tpie::internal_priority_queue::pop_and_push
void pop_and_push(const T &v)
Remove the minimum element and insert a new element.
Definition: internal_priority_queue.h:133
tpie::array::memory_coefficient
static constexpr double memory_coefficient() noexcept
Return the memory coefficient of the structure.
Definition: array.h:398
tpie::array::resize
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:490
tpie::internal_priority_queue::memory_overhead
static constexpr double memory_overhead() noexcept
Return the memory overhead of the structure.
Definition: internal_priority_queue.h:166
util.h
tpie::pop_and_push_heap
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:149
tpie
Definition: access_type.h:26
tpie::internal_priority_queue::top
const T & top() const
Return the minimum element.
Definition: internal_priority_queue.h:150
tpie::array::size
size_type size() const
Return the size of the array.
Definition: array.h:531
tpie::internal_priority_queue::empty
bool empty() const
Is the queue empty?
Definition: internal_priority_queue.h:96