40 inline T
operator*()
const {
return _current->element; };
41 inline bool operator !=(
const void* end)
const {
return _current != end; };
42 inline bool operator ==(
const void* end)
const {
return _current == end; };
50 template <
typename T>
class List {
60 return _first->element;
84 if (!_last) _last=_first;
89 while (it != this->end()) {
98 void inline remove(T elem) {
99 if (_first->element == elem) {
101 _first = _first->
next;
118 inline void*
end()
const {
return NULL; };
121 this->push_back(*it);
131 cerr <<
" print list " << endl;
151 BinaryHeap(
int size) { _last=-1; _values=
new T[size]; _id=
new int[size]; _position=
new int[size];
_size=size;};
152 ~BinaryHeap() {
delete[](_values);
delete[](_id);
delete[](_position); };
154 bool inline is_empty()
const {
return _last==-1; };
156 node=_id[0]; val=_values[node]; };
157 void inline insert(
const int node,
const T val) {
159 assert(_last <
_size);
161 _position[node]=_last;
166 _position[_id[_last]]=0;
172 assert(val <= _values[node]);
174 this->siftup(_position[node]);
177 for (
int i = 0; i<=_last; ++i) {
178 cerr << _id[i] <<
" ";
181 for (
int i = 0; i<=_last; ++i) {
182 cerr << _values[_id[i]] <<
" ";
190 int parent=(current_pos-1)/2;
191 while (current_pos != 0 && _values[_id[current_pos]] < _values[_id[parent]]) {
192 this->swapping(current_pos,parent);
193 parent=(current_pos-1)/2;
198 int first_succ=pos+pos+1;
199 int second_succ=first_succ+1;
202 if (first_succ == _last) {
203 if (_values[_id[current_pos]] > _values[_id[first_succ]])
204 this->swapping(current_pos,first_succ);
206 }
else if (second_succ <= _last) {
207 if (_values[_id[first_succ]] > _values[_id[second_succ]]) {
208 if (_values[_id[current_pos]] > _values[_id[second_succ]]) {
209 this->swapping(current_pos,second_succ);
210 first_succ=current_pos+current_pos+1;
211 second_succ=first_succ+1;
216 if (_values[_id[current_pos]] > _values[_id[first_succ]]) {
217 this->swapping(current_pos,first_succ);
218 first_succ=current_pos+current_pos+1;
219 second_succ=first_succ+1;
230 swap(_position[_id[pos1]],_position[_id[pos2]]);
231 swap(_id[pos1],_id[pos2]);
void siftdown(const int pos)
void decrease_key(const int node, const T val)
void reverse(List< T > &list)
void set(Element< T > *elem)
ListIterator< T > & begin() const
void insert(const int node, const T val)
ListIterator< T > * _iterator
void siftup(const int pos)
void swapping(int &pos1, int &pos2)
ListIterator< int > const_iterator_int
void find_min(int &node, T &val) const
void fusion(const List< T > &list)