initial
[prop.git] / include / AD / contain / mmh.h
blob6f061d532bf3a879d3eea0b692f530e2ccaea116
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
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
9 // your programs.
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
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
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)
31 // stays at O(log n).
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
39 public:
41 //////////////////////////////////////////////////////////////////////
42 // Constructors and destructor
43 //////////////////////////////////////////////////////////////////////
44 MinMaxHeap(size) : queued_elements(0), Array(size) {}
45 MinMaxHeap(const MinMaxHeap& H) : Array(H.size()) { *this = H; }
46 ~MinMaxHeap() {}
48 //////////////////////////////////////////////////////////////////////
49 // Assignment
50 //////////////////////////////////////////////////////////////////////
51 MinMaxHeap& operator = (const MinMaxHeap&);
53 //////////////////////////////////////////////////////////////////////
54 // Selectors
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(); }
60 const T& min() const;
61 T& min();
62 const T& max() const;
63 T& max();
65 //////////////////////////////////////////////////////////////////////
66 // Mutators
67 //////////////////////////////////////////////////////////////////////
68 Ix insert(const T& e) { return enqueue(e); }
69 Ix enqueue(const T&);
70 Bool delete_min();
71 Bool delete_max();
73 //////////////////////////////////////////////////////////////////////
74 // Iterators
75 //////////////////////////////////////////////////////////////////////
76 // Inherited from class Array
79 ////////////////////////////////////////////////////////////////////////////
80 // Implementation of the template methods
81 ////////////////////////////////////////////////////////////////////////////
82 template <class T>
83 MinMaxHeap<T>& MinMaxHeap<T>::operator = (const MinMaxHeap<T>& H)
84 { if (this != &H) {
86 return *this;
89 template <class T>
90 Ix MinMaxHeap<T>::enqueue(const T& e)
94 template <class T>
95 Bool MinMaxHeap<T>::delete_min()
99 template <class T>
100 Bool MinMaxHeap<T>::delete_max()
104 #endif