TPIE

v1.1rc1-6-g0c97303
priority_queue.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2008, 2012, 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 
24 
25 #ifndef _TPIE_PRIORITY_QUEUE_H_
26 #define _TPIE_PRIORITY_QUEUE_H_
27 
28 #include <tpie/config.h>
29 #include "portability.h"
30 #include "tpie_log.h"
31 #include <cassert>
32 #include "pq_overflow_heap.h"
33 #include <iostream>
34 #include <fstream>
35 #include <stdexcept>
36 #include <cmath>
37 #include <string>
38 #include <cstring> // for memcpy
39 #include <sstream>
40 #include "pq_merge_heap.h"
41 #include <tpie/err.h>
42 #include <tpie/stream.h>
43 #include <tpie/array.h>
44 #include <boost/filesystem.hpp>
45 
46 namespace tpie {
47 
48  struct priority_queue_error : public std::logic_error {
49  priority_queue_error(const std::string& what) : std::logic_error(what)
50  { }
51  };
52 
75 
76 template<typename T, typename Comparator = std::less<T>, typename OPQType = pq_overflow_heap<T, Comparator> >
78  typedef memory_size_type group_type;
79  typedef memory_size_type slot_type;
80 public:
87  priority_queue(double f=1.0, float b=0.0625);
88 
89 #ifndef DOXYGEN
90  // \param mmavail Number of bytes the priority queue is allowed to use.
91  // \param b Block factor
92  priority_queue(memory_size_type mm_avail, float b=0.0625);
93 #endif
94 
95 
101  ~priority_queue();
102 
110  void push(const T& x);
111 
117  void pop();
118 
126  const T& top();
127 
135  stream_size_type size() const;
136 
144  bool empty() const;
145 
157  template <typename F> F pop_equals(F f);
158 
159 private:
160  Comparator comp_;
161  T dummy;
162 
163  T min;
164  bool min_in_buffer;
165 
168 
170  tpie::array<T> buffer;
171 
176  tpie::array<T> gbuffer0;
177 
179  tpie::array<T> mergebuffer;
180 
185 
188  tpie::array<memory_size_type> group_state;
189 
191  memory_size_type setting_k;
193  memory_size_type current_r;
195  memory_size_type setting_m;
197  memory_size_type setting_mmark;
198 
199  memory_size_type slot_data_id;
200 
201  stream_size_type m_size;
202  memory_size_type buffer_size;
203  memory_size_type buffer_start;
204 
205  float block_factor;
206 
207  void init(memory_size_type mm_avail);
208 
209  void slot_start_set(slot_type slot, memory_size_type n);
210  memory_size_type slot_start(slot_type slot) const;
211  void slot_size_set(slot_type slot, memory_size_type n);
212  memory_size_type slot_size(slot_type slot) const;
213  void group_start_set(group_type group, memory_size_type n);
214  memory_size_type group_start(group_type group) const;
215  void group_size_set(group_type group, memory_size_type n);
216  memory_size_type group_size(group_type group) const;
218  array<temp_file> datafiles;
219  array<temp_file> groupdatafiles;
220 
221  temp_file & slot_data(slot_type slotid);
222  void slot_data_set(slot_type slotid, memory_size_type n);
223  temp_file & group_data(group_type groupid);
224  memory_size_type slot_max_size(slot_type slotid);
225  void write_slot(slot_type slotid, T* arr, memory_size_type len);
226  slot_type free_slot(group_type group);
227  void empty_group(group_type group);
228  void fill_buffer();
229  void fill_group_buffer(group_type group);
230  void compact(slot_type slot);
231  void validate();
232  void remove_group_buffer(group_type group);
233  void dump();
234 };
235 
236 #include "priority_queue.inl"
237 
238  namespace ami {
239  using tpie::priority_queue;
240  } // ami namespace
241 
242 } // tpie namespace
243 
244 #endif
External memory priority queue implementation.
Priority queue overflow heap.
Priority queue merge heap.
This file contains a few deprecated definitions for legacy code.
Logging functionality and log_level codes for different priorities of log messages.
AMI streams.
Generic internal array with known memory requirements.
priority_queue(double f=1.0, float b=0.0625)
Constructor.
bool empty() const
Return true if queue is empty otherwise false.
Class representing the existence of a temporary file.
Definition: tempname.h:152
Legacy AMI error types.
F pop_equals(F f)
Pop all elements with priority equal to that of the top element, and process each by invoking f's cal...
stream_size_type size() const
Returns the size of the queue.
~priority_queue()
Destructor.
const T & top()
See what's on the top of the priority queue.
void pop()
Remove the top element from the priority queue.
void push(const T &x)
Insert an element into the priority queue.