00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef SGI_container_with_membership_h_INCLUDED
00042 #define SGI_container_with_membership_h_INCLUDED
00043
00044 #include <function.h>
00045 #include <vector>
00046 #include <iterator.h>
00047 #include "misc_extension.h"
00048
00049 namespace SGI {
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 template <class Value,
00065 class IndexFunction = converter<Value, int>,
00066 class Compare = less<Value> >
00067 class heap_with_membership {
00068 public:
00069 typedef heap_with_membership<Value, IndexFunction, Compare> self;
00070 typedef typename IndexFunction::result_type index_type;
00071 typedef Value value_type;
00072 typedef typename vector<Value>::size_type size_type;
00073 typedef typename vector<index_type>::size_type index_size_type;
00074 typedef push_iterator<self> insert_iterator;
00075 protected:
00076 typedef typename vector<Value>::iterator heap_iterator;
00077 vector<Value> heap;
00078 vector<index_type> index;
00079 IndexFunction i_fun;
00080 Compare comp;
00081
00082 void assign(heap_iterator to, const Value& from) {
00083 *to = from;
00084 index[i_fun(from)] = index_type((to - heap.begin()) + 2);
00085 }
00086 void sift_up(heap_iterator current, Value value) {
00087 heap_iterator first = heap.begin();
00088 size_type hole_index = current - first;
00089 size_type parent = (hole_index - 1) >> 1;
00090 while (hole_index > 0 && comp(*(first + parent), value)) {
00091 assign(first + hole_index, *(first + parent));
00092 hole_index = parent;
00093 parent = (hole_index - 1) >> 1;
00094 }
00095 assign(first + hole_index, value);
00096 }
00097 void fix_hole(size_type hole_index) {
00098 heap_iterator first = heap.begin();
00099 size_type len = heap.size() - 1;
00100 size_type second_child = (hole_index + 1) << 1;
00101 while (second_child < len) {
00102 if (comp(*(first + second_child), *(first + (second_child - 1))))
00103 --second_child;
00104 assign( first + hole_index, *(first + second_child));
00105 hole_index = second_child;
00106 second_child = (second_child + 1) << 1;
00107 }
00108 if (second_child == len) {
00109 assign(first + hole_index, *(first + (second_child - 1)));
00110 hole_index = second_child - 1;
00111 }
00112 sift_up(first + hole_index, heap.back());
00113 heap.pop_back();
00114 }
00115
00116 public:
00117 heap_with_membership(size_type n) : index(n, index_type(0)) {}
00118 heap_with_membership(size_type n, Compare c)
00119 : index(n, index_type(0)), comp(c) {}
00120 heap_with_membership(size_type n, IndexFunction i_f)
00121 : index(n, index_type(0)), i_fun(i_f) {}
00122 heap_with_membership(size_type n, IndexFunction i_f, Compare c)
00123 : index(n, index_type(0)), comp(c), i_fun(i_f) {}
00124
00125 bool empty() const { return heap.empty(); }
00126 size_type size() const { return heap.size(); }
00127 const Value& top() const { return *heap.begin(); }
00128 void push(const Value& x) {
00129 size_type i_x = i_fun(x);
00130 index_type ii_x = index[i_x];
00131 if (ii_x == 0) {
00132 heap.push_back(x);
00133 sift_up(heap.end()-1, x);
00134 } else if (ii_x != 1) {
00135 heap_iterator p_x = heap.begin() + size_type(ii_x - 2);
00136 if (comp(x, *p_x)) {
00137 sift_up(p_x, x);
00138 }
00139 }
00140 }
00141 void pop() {
00142 index[i_fun(heap.front())] = 1;
00143 fix_hole(size_type(0));
00144 }
00145 void remove(index_type i) {
00146 size_type tmp = index[i];
00147 if (0 == tmp) index[i] = 1;
00148 if (1 < tmp) {
00149 fix_hole(size_type(tmp - 2));
00150 index[i] = 2;
00151 }
00152 }
00153 int status(index_type i) {
00154 int tmp = index[i];
00155 if (0 == tmp) return 0;
00156 if (1 == tmp) return - 1;
00157 return 1;
00158 }
00159 const Value& operator[](index_size_type n) { return heap[index[n] - 2]; }
00160
00161 };
00162
00163 }
00164
00165 #endif
00166
00167
00168
00169