1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef min_max_heap_ordered_by_predicate_h
26 #define min_max_heap_ordered_by_predicate_h
28 //////////////////////////////////////////////////////////////////////////////
29 // A min-max heap is a variant of binary heap that allows O(1) minimum
30 // and maximum extraction. The other operations(enqueue, delete_min)
32 //////////////////////////////////////////////////////////////////////////////
34 #include <A/generic/ordering.h>
36 template <class T
, class Array
>
37 class MinMaxHeap
: public Array
{
38 int queued_elements
; // number of elements on queue
41 //////////////////////////////////////////////////////////////////////
42 // Constructors and destructor
43 //////////////////////////////////////////////////////////////////////
44 MinMaxHeap(size
) : queued_elements(0), Array(size
) {}
45 MinMaxHeap(const MinMaxHeap
& H
) : Array(H
.size()) { *this = H
; }
48 //////////////////////////////////////////////////////////////////////
50 //////////////////////////////////////////////////////////////////////
51 MinMaxHeap
& operator = (const MinMaxHeap
&);
53 //////////////////////////////////////////////////////////////////////
55 //////////////////////////////////////////////////////////////////////
56 inline int size() const { return queued_elements
; }
57 // int capacity() const; // inherited from Array
58 inline Bool
is_empty() const { return queued_elements
== 0; }
59 inline Bool
is_full() const { return queued_elements
== capacity(); }
65 //////////////////////////////////////////////////////////////////////
67 //////////////////////////////////////////////////////////////////////
68 Ix
insert(const T
& e
) { return enqueue(e
); }
73 //////////////////////////////////////////////////////////////////////
75 //////////////////////////////////////////////////////////////////////
76 // Inherited from class Array
79 ////////////////////////////////////////////////////////////////////////////
80 // Implementation of the template methods
81 ////////////////////////////////////////////////////////////////////////////
83 MinMaxHeap
<T
>& MinMaxHeap
<T
>::operator = (const MinMaxHeap
<T
>& H
)
90 Ix MinMaxHeap
<T
>::enqueue(const T
& e
)
95 Bool MinMaxHeap
<T
>::delete_min()
100 Bool MinMaxHeap
<T
>::delete_max()