DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
list.h
Go to the documentation of this file.
1 
2 /* Software SPAMS v2.1 - Copyright 2009-2011 Julien Mairal
3  *
4  * This file is part of SPAMS.
5  *
6  * SPAMS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * SPAMS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with SPAMS. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIST_H
21 #define LIST_H
22 
23 namespace spams
24 {
25 
26 template <typename T> class Element {
27  public:
28  Element(T el) { element=el; next=NULL; };
29  ~Element() { };
30  T element;
32 };
33 
34 template <typename T> class ListIterator {
35 
36  public:
37  ListIterator() { _current = NULL; };
39  inline void set(Element<T>* elem) { _current = elem; };
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; };
43  inline void operator++() { _current = _current->next; };
44  inline Element<T>* current() { return _current; };
45  inline T operator->() { return _current->element; };
46  private:
47  Element<T>* _current;
48 };
49 
50 template <typename T> class List {
51  public:
52 
53  List() { _first=NULL; _last=NULL; _size=0; _iterator = new ListIterator<T>(); };
54  ~List() {
55  this->clear();
56  delete(_iterator);
57  };
58  bool inline empty() const { return _size==0; };
59  inline T front() const {
60  return _first->element;
61  };
62  void inline pop_front() {
63  Element<T>* fr=_first;
64  _first=fr->next;
65  fr->next=NULL;
66  delete(fr);
67  --_size;
68  };
69  void inline push_back(T elem) {
70  if (_first) {
71  Element<T>* la=_last;
72  _last=new Element<T>(elem);
73  la->next=_last;
74  } else {
75  _first=new Element<T>(elem);
76  _last=_first;
77  }
78  ++_size;
79  }
80  void inline push_front(T elem) {
81  Element<T>* fr=_first;
82  _first=new Element<T>(elem);
83  _first->next=fr;
84  if (!_last) _last=_first;
85  ++_size;
86  }
87  void inline clear() {
88  ListIterator<T> it = this->begin();
89  while (it != this->end()) {
90  Element<T>* cur = it.current();
91  ++it;
92  delete(cur);
93  }
94  _size=0;
95  _first=NULL;
96  _last=NULL;
97  }
98  void inline remove(T elem) {
99  if (_first->element == elem) {
100  Element<T>* el = _first;
101  _first = _first->next;
102  delete(el);
103  } else {
104  Element<T>* old = _first;
105  for (ListIterator<T> it = this->begin(); it != this->end(); ++it) {
106  if (*it == elem) {
107  Element<T>* el = it.current();
108  old->next=el->next;
109  delete(el);
110  break;
111  }
112  old=it.current();
113  }
114  }
115  };
116  int inline size() const { return _size; };
117  inline ListIterator<T>& begin() const { _iterator->set(_first); return *_iterator; };
118  inline void* end() const { return NULL; };
119  inline void fusion(const List<T>& list) {
120  for (ListIterator<T> it = list.begin(); it != list.end(); ++it) {
121  this->push_back(*it);
122  }
123  }
124  inline void reverse(List<T>& list) {
125  list.clear();
126  for (ListIterator<T> it = this->begin(); it != this->end(); ++it) {
127  list.push_front(*it);
128  }
129  }
130  void inline print() const {
131  cerr << " print list " << endl;
132  for (ListIterator<T> it = this->begin(); it != this->end(); ++it) {
133  cerr << *it << " ";
134  }
135  cerr << endl;
136  }
137 
138  private:
139 
143  int _size;
144 };
145 
148 
149 template <typename T> class BinaryHeap {
150  public:
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); };
153 
154  bool inline is_empty() const { return _last==-1; };
155  void inline find_min(int& node, T& val) const {
156  node=_id[0]; val=_values[node]; };
157  void inline insert(const int node, const T val) {
158  ++_last;
159  assert(_last < _size);
160  _values[node]=val;
161  _position[node]=_last;
162  _id[_last]=node;
163  this->siftup(_last);
164  };
165  void inline delete_min() {
166  _position[_id[_last]]=0;
167  _id[0]=_id[_last];
168  _last--;
169  this->siftdown(0);
170  };
171  void inline decrease_key(const int node, const T val) {
172  assert(val <= _values[node]);
173  _values[node]=val;
174  this->siftup(_position[node]);
175  };
176  void inline print() const {
177  for (int i = 0; i<=_last; ++i) {
178  cerr << _id[i] << " ";
179  }
180  cerr << endl;
181  for (int i = 0; i<=_last; ++i) {
182  cerr << _values[_id[i]] << " ";
183  }
184  cerr << endl;
185  }
186 
187  private:
188  void inline siftup(const int pos) {
189  int current_pos=pos;
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;
194  }
195  };
196  void inline siftdown(const int pos) {
197  int current_pos=pos;
198  int first_succ=pos+pos+1;
199  int second_succ=first_succ+1;
200  bool lop=true;
201  while (lop) {
202  if (first_succ == _last) {
203  if (_values[_id[current_pos]] > _values[_id[first_succ]])
204  this->swapping(current_pos,first_succ);
205  lop=false;
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;
212  } else {
213  lop=false;
214  }
215  } else {
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;
220  } else {
221  lop=false;
222  }
223  }
224  } else {
225  lop=false;
226  }
227  }
228  };
229  void inline swapping(int& pos1, int& pos2) {
230  swap(_position[_id[pos1]],_position[_id[pos2]]);
231  swap(_id[pos1],_id[pos2]);
232  swap(pos1,pos2);
233  };
234 
235  T* _values;
236  int* _id;
237  int* _position;
238  int _last;
239  int _size;
240 };
241 
242 }
243 
244 #endif
Element< T > * _first
Definition: list.h:141
List< int > list_int
Definition: list.h:146
void pop_front()
Definition: list.h:62
Definition: dag.h:26
void print() const
Definition: list.h:176
Element< T > * current()
Definition: list.h:44
int * _position
Definition: list.h:237
void siftdown(const int pos)
Definition: list.h:196
Element< T > * _last
Definition: list.h:142
void decrease_key(const int node, const T val)
Definition: list.h:171
void operator++()
Definition: list.h:43
void reverse(List< T > &list)
Definition: list.h:124
Element(T el)
Definition: list.h:28
bool empty() const
Definition: list.h:58
~List()
Definition: list.h:54
void set(Element< T > *elem)
Definition: list.h:39
void push_back(T elem)
Definition: list.h:69
ListIterator< T > & begin() const
Definition: list.h:117
bool is_empty() const
Definition: list.h:154
void push_front(T elem)
Definition: list.h:80
void delete_min()
Definition: list.h:165
void insert(const int node, const T val)
Definition: list.h:157
ListIterator< T > * _iterator
Definition: list.h:140
BinaryHeap(int size)
Definition: list.h:151
T operator*() const
Definition: list.h:40
~Element()
Definition: list.h:29
List()
Definition: list.h:53
void clear()
Definition: list.h:87
void siftup(const int pos)
Definition: list.h:188
T front() const
Definition: list.h:59
void * end() const
Definition: list.h:118
void swapping(int &pos1, int &pos2)
Definition: list.h:229
ListIterator< int > const_iterator_int
Definition: list.h:147
void find_min(int &node, T &val) const
Definition: list.h:155
void fusion(const List< T > &list)
Definition: list.h:119
std::vector< int > _size
Definition: 4DImageMath.cxx:77
Element< T > * next
Definition: list.h:31
int size() const
Definition: list.h:116
int _size
Definition: list.h:143
void print() const
Definition: list.h:130