TPIE

2362a60
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 
76 
77 template<typename T, typename Comparator = std::less<T>, typename OPQType = pq_overflow_heap<T, Comparator> >
79  typedef memory_size_type group_type;
80  typedef memory_size_type slot_type;
81 public:
82  static constexpr float default_blocksize = 0.0625;
83 
90  priority_queue(double f=1.0, float b=default_blocksize, stream_size_type n = std::numeric_limits<stream_size_type>::max());
91 
92 #ifndef DOXYGEN
93  // \param mmavail Number of bytes the priority queue is allowed to use.
94  // \param b Block factor
95  priority_queue(memory_size_type mm_avail, float b=default_blocksize, stream_size_type n = std::numeric_limits<stream_size_type>::max());
96 #endif
97 
104  static memory_size_type memory_usage(stream_size_type n, float b=default_blocksize);
105 
111  ~priority_queue();
112 
120  void push(const T& x);
121 
127  void pop();
128 
136  const T& top();
137 
145  stream_size_type size() const;
146 
154  bool empty() const;
155 
167  template <typename F> F pop_equals(F f);
168 
169 private:
170  Comparator comp_;
171  T dummy;
172 
173  T min;
174  bool min_in_buffer;
175 
178 
180  tpie::array<T> buffer;
181 
186  tpie::array<T> gbuffer0;
187 
189  tpie::array<T> mergebuffer;
190 
195 
198  tpie::array<memory_size_type> group_state;
199 
201  memory_size_type setting_k;
203  memory_size_type current_r;
205  memory_size_type setting_m;
207  memory_size_type setting_mmark;
208 
209  memory_size_type slot_data_id;
210 
211  stream_size_type m_size;
212  memory_size_type buffer_size;
213  memory_size_type buffer_start;
214 
215  float block_factor;
216 
217  void init(memory_size_type mm_avail, stream_size_type n = std::numeric_limits<stream_size_type>::max() );
218 
219  void slot_start_set(slot_type slot, memory_size_type n);
220  memory_size_type slot_start(slot_type slot) const;
221  void slot_size_set(slot_type slot, memory_size_type n);
222  memory_size_type slot_size(slot_type slot) const;
223  void group_start_set(group_type group, memory_size_type n);
224  memory_size_type group_start(group_type group) const;
225  void group_size_set(group_type group, memory_size_type n);
226  memory_size_type group_size(group_type group) const;
227 
228  array<temp_file> datafiles;
229  array<temp_file> groupdatafiles;
230 
231  temp_file & slot_data(slot_type slotid);
232  void slot_data_set(slot_type slotid, memory_size_type n);
233  temp_file & group_data(group_type groupid);
234  memory_size_type slot_max_size(slot_type slotid);
235  void write_slot(slot_type slotid, T* arr, memory_size_type len);
236  slot_type free_slot(group_type group);
237  void empty_group(group_type group);
238  void fill_buffer();
239  void fill_group_buffer(group_type group);
240  void compact(slot_type slot);
241  void validate();
242  void remove_group_buffer(group_type group);
243  void dump();
244 };
245 
246 #include "priority_queue.inl"
247 
248  namespace ami {
249  using tpie::priority_queue;
250  } // ami namespace
251 
252 } // tpie namespace
253 
254 #endif
static memory_size_type memory_usage(stream_size_type n, float b=default_blocksize)
Compute the maximal amount of memory it makes sence to give a queue that will contain atmount n eleme...
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.
Generic internal array with known memory requirements.
bool empty() const
Return true if queue is empty otherwise false.
Class representing a reference to a temporary file.
Definition: tempname.h:202
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.
std::unique_ptr< T, tpie_deleter > unique_ptr
like std::unique_ptr, but delete the object with tpie_delete.
Definition: memory.h:338
void pop()
Remove the top element from the priority queue.
priority_queue(double f=1.0, float b=default_blocksize, stream_size_type n=std::numeric_limits< stream_size_type >::max())
Constructor.
void push(const T &x)
Insert an element into the priority queue.