TPIE

2362a60
mergeheap.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, 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 
28 
29 #ifndef _MERGE_HEAP_H
30 #define _MERGE_HEAP_H
31 
32 // Get definitions for working with Unix and Windows
33 #include <tpie/portability.h>
34 #include <tpie/memory.h>
36 
37 namespace tpie {
38  namespace ami {
39 
45  template<class REC>
46  class heap_ptr {
47  public:
48  heap_ptr() : recptr(NULL), run_id(0) {};
49  heap_ptr(const REC * a, size_t b) : recptr(a), run_id(b) {};
50  const REC *recptr;
51  size_t run_id;
52  };
53 
58  template<class REC, class comp_t=std::less<REC> >
60  private:
61  struct comp: public std::binary_function<heap_ptr<REC>, heap_ptr<REC>, bool> {
62  comp_t c;
63  comp(comp_t & _): c(_) {}
64  inline bool operator()(const heap_ptr<REC> & a, const heap_ptr<REC> & b) const {
65  return c(*a.recptr, *b.recptr);
66  }
67  };
68 
70 
71  public:
72  merge_heap_ptr_op(comp_t c=comp_t()): pq(0, comp(c)) {}
73 
77  size_t sizeofheap() {return pq.size();}
78 
82  inline size_t get_min_run_id() {return pq.top().run_id;}
83 
87  void allocate(size_t size) {pq.resize(size);}
88 
92  void insert(const REC *ptr, size_t run_id) {pq.unsafe_push(heap_ptr<REC>(ptr, run_id) );}
93 
99  void extract_min(REC& el, size_t& run_id) {
100  el=*pq.top().ptr;
101  run_id=*pq.top().run_id();
102  pq.pop();
103  }
104 
108  void deallocate() {pq.resize(0);}
109 
113  void initialize() {pq.make_safe(); }
114 
116  // Deletes the current minimum and inserts the new item from the same
117  // source / run.
119  inline void delete_min_and_insert(const REC *nextelement_same_run) {
120  if (nextelement_same_run)
121  pq.pop_and_push(heap_ptr<REC>(nextelement_same_run, pq.top().run_id));
122  else
123  pq.pop();
124  }
125 
129  inline size_t space_per_item() { return static_cast<size_t>(pq.memory_coefficient()); }
130 
134  inline size_t space_overhead() { return static_cast<size_t>(pq.memory_overhead()); }
135  };
136 
140  template<class REC, class CMPR>
141  class merge_heap_ptr_obj: public merge_heap_ptr_op<REC, CMPR>{
142  public:
143  merge_heap_ptr_obj(CMPR *cmptr):
145  };
146 
151  template<class KEY>
152  class heap_element {
153  public:
154  heap_element() : key(), run_id(0) {}
155  heap_element(KEY k, size_t r): key(k), run_id(r) {}
156 
157  KEY key;
158  size_t run_id;
159  };
160 
165  template<class REC, class comp_t=std::less<REC> >
167  private:
168  struct comp: public std::binary_function<heap_element<REC>, heap_element<REC>, bool> {
169  comp_t c;
170  comp(comp_t & _): c(_) {}
171  inline bool operator()(const heap_element<REC> & a, const heap_element<REC> & b) {
172  return c(a.key, b.key);
173  }
174  };
175 
177 
178  public:
179  merge_heap_op(comp_t c=comp_t()): pq(0, comp(c)) {}
180 
184  size_t sizeofheap() {return pq.size();}
185 
189  inline size_t get_min_run_id() {return pq.top().run_id;};
190 
194  void allocate(size_t size) {pq.resize(size);}
195 
199  void insert(const REC *ptr, size_t run_id) {pq.unsafe_push(heap_element<REC>(*ptr, run_id));}
200 
206  void extract_min(REC& el, size_t& run_id) {
207  el=pq.top().key;
208  run_id=pq.top().run_id;
209  pq.pop();
210  }
211 
215  void deallocate() {pq.resize(0);}
216 
220  void initialize(void) {pq.make_safe();}
221 
223  // Deletes the current minimum and inserts the new item from the same
224  // source / run.
226  inline void delete_min_and_insert(const REC *nextelement_same_run) {
227  if (nextelement_same_run)
228  pq.pop_and_push(heap_element<REC>(*nextelement_same_run, pq.top().run_id));
229  else
230  pq.pop();
231  }
232 
236  inline size_t space_per_item(void) {return static_cast<size_t>(pq.memory_coefficient());}
237 
241  inline size_t space_overhead(void) {return static_cast<size_t>(pq.memory_overhead());}
242  };
243 
244 
245  // ********************************************************************
246  // * A merge heap that uses a comparison object *
247  // ********************************************************************
248  template<class REC, class Compare>
249  class merge_heap_obj: public merge_heap_op<REC, Compare> {
250  public:
251  merge_heap_obj(Compare cmp):
253  };
254 
255  } // ami namespace
256 } // tpie namespace
257 
258 #endif // _MERGE_HEAP_H
size_t sizeofheap()
Reports the size of Heap (number of elements).
Definition: mergeheap.h:77
Memory management subsystem.
void deallocate()
Deallocates the space used by the heap.
Definition: mergeheap.h:215
size_t space_per_item()
Returns the main memory space usage per item.
Definition: mergeheap.h:129
size_t space_per_item(void)
Returns the main memory space usage per item.
Definition: mergeheap.h:236
void initialize(void)
Heapifies an initial array of elements.
Definition: mergeheap.h:220
void initialize()
Heapifies an initial array of elements;.
Definition: mergeheap.h:113
This file contains a few deprecated definitions for legacy code.
size_t space_overhead(void)
Returns the fixed main memory space overhead, regardless of item count.
Definition: mergeheap.h:241
Standard binary internal heap.
Simple heap based priority queue implementation.
size_t space_overhead()
Returns the fixed main memory space overhead, regardless of item count.
Definition: mergeheap.h:134
A record pointer heap that uses a comparison object.
Definition: mergeheap.h:141
A merge heap object base class - also serves as the full implementation for objects with a < comparis...
Definition: mergeheap.h:166
A record pointer heap base class - also serves as the full implementation for objects with a < compar...
Definition: mergeheap.h:59
void insert(const REC *ptr, size_t run_id)
Copies an (initial) element into the heap array.
Definition: mergeheap.h:92
size_t get_min_run_id()
Returns the run with the minimum key.
Definition: mergeheap.h:189
This is a record pointer element.
Definition: mergeheap.h:46
size_t get_min_run_id()
Returns the run with the minimum key.
Definition: mergeheap.h:82
void allocate(size_t size)
Allocates space for the heap.
Definition: mergeheap.h:87
size_t sizeofheap()
Reports the size of Heap (number of elements).
Definition: mergeheap.h:184
void insert(const REC *ptr, size_t run_id)
Copies an (initial) element into the heap array/.
Definition: mergeheap.h:199
void deallocate()
Deallocates the space used by the heap.
Definition: mergeheap.h:108
void allocate(size_t size)
Allocates space for the heap.
Definition: mergeheap.h:194
void extract_min(REC &el, size_t &run_id)
Extracts minimum element from heap array.
Definition: mergeheap.h:206
void extract_min(REC &el, size_t &run_id)
Extracts minimum element from heap array.
Definition: mergeheap.h:99
This is a heap element.
Definition: mergeheap.h:152