clist.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA 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  * This program 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 MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef CLIST_HH
22 #define CLIST_HH
23 
24 #include <cassert>
25 
27 
28 template <class T>
29 class clist {
30 
31 public:
32  typedef T value_type;
33 
34 
35  struct node {
36  T value;
39  node(T v, node *p, node *s):
40  value(v), prev(p), succ(s)
41  {
42  }
43 
44  T& operator *() {
45  return value;
46  }
47  const T& operator *() const {
48  return value;
49  }
50 
51 
52  };
53 
54  typedef node *iterator;
55  typedef const node *const_iterator;
56 
57 
58  clist(): m_head(NULL){
59 
60  }
61  ~clist() {
62  if (m_head != NULL) {
63  node *head = m_head;
64  while (head != head->succ)
65  remove(head->succ);
66  delete m_head;
67  }
68  }
69 
70  iterator begin() {
71  return m_head;
72  }
73 
74  iterator end() {
75  return m_head;
76  }
77 
78  const_iterator begin() const {
79  return m_head;
80  }
81 
82  const_iterator end() const {
83  return m_head;
84  }
85 
86 
87  void remove(node *n){
88  if (n->prev != n) {
89  n->succ->prev = n->prev;
90  n->prev->succ = n->succ;
91  if (n == m_head) {
92  m_head = n->prev;
93  }
94  delete n;
95  }else { // only head left
96  assert(n == m_head);
97  delete n;
98  m_head = NULL;
99  }
100  }
101  void push_back(T val)
102  {
103  if (m_head) {
104  node *nn = new node(val, m_head, m_head->succ);
105  nn->prev->succ = nn;
106  nn->succ->prev = nn;
107  }else {
108  assert (m_head == NULL);
109  m_head = new node(val,NULL,NULL);
110  m_head->prev = m_head->succ = m_head;
111  }
112  }
113  int size() {
114  int s = 0;
115  if (m_head) {
116  node *n = m_head;
117  while (n->succ != m_head) {
118  n = n->succ;
119  ++s;
120  }
121  }
122  return s;
123  }
124 private:
125  node *m_head;
126 };
127 
129 
130 #endif
131 
132 /*
133  $Log$
134  Revision 1.3 2005/06/29 13:22:20 wollny
135  switch to version 0.7
136 
137  Revision 1.1.1.1 2005/03/17 13:44:20 gerddie
138  initial import
139 
140  Revision 1.2 2004/10/15 14:05:37 wollny
141  log entrys
142 
143 */
clist()
Definition: clist.hh:58
~clist()
Definition: clist.hh:61
T value_type
Definition: clist.hh:32
const_iterator begin() const
Definition: clist.hh:78
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
const node * const_iterator
Definition: clist.hh:55
node * prev
Definition: clist.hh:37
node * iterator
Definition: clist.hh:54
iterator end()
Definition: clist.hh:74
T & operator*()
Definition: clist.hh:44
node(T v, node *p, node *s)
Definition: clist.hh:39
Definition: clist.hh:29
iterator begin()
Definition: clist.hh:70
int size()
Definition: clist.hh:113
void push_back(T val)
Definition: clist.hh:101
node * succ
Definition: clist.hh:38
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
const_iterator end() const
Definition: clist.hh:82