My Project
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
28template <class T>
29class clist
30{
31
32public:
33 typedef T value_type;
34
35
36 struct node {
40 node(T v, node *p, node *s):
41 value(v), prev(p), succ(s)
42 {
43 }
44
46 {
47 return value;
48 }
49 const T& operator *() const
50 {
51 return value;
52 }
53
54
55 };
56
57 typedef node *iterator;
58 typedef const node *const_iterator;
59
60
61 clist(): m_head(NULL)
62 {
63 }
65 {
66 if (m_head != NULL) {
67 node *head = m_head;
68
69 while (head != head->succ)
70 remove(head->succ);
71
72 delete m_head;
73 }
74 }
75
77 {
78 return m_head;
79 }
80
82 {
83 return m_head;
84 }
85
87 {
88 return m_head;
89 }
90
92 {
93 return m_head;
94 }
95
96
97 void remove(node *n)
98 {
99 if (n->prev != n) {
100 n->succ->prev = n->prev;
101 n->prev->succ = n->succ;
102
103 if (n == m_head) {
104 m_head = n->prev;
105 }
106
107 delete n;
108 } else { // only head left
109 assert(n == m_head);
110 delete n;
111 m_head = NULL;
112 }
113 }
114 void push_back(T val)
115 {
116 if (m_head) {
117 node *nn = new node(val, m_head, m_head->succ);
118 nn->prev->succ = nn;
119 nn->succ->prev = nn;
120 } else {
121 assert (m_head == NULL);
122 m_head = new node(val, NULL, NULL);
123 m_head->prev = m_head->succ = m_head;
124 }
125 }
126 int size()
127 {
128 int s = 0;
129
130 if (m_head) {
131 node *n = m_head;
132
133 while (n->succ != m_head) {
134 n = n->succ;
135 ++s;
136 }
137 }
138
139 return s;
140 }
141private:
142 node *m_head;
143};
144
146
147#endif
148
149/*
150 $Log$
151 Revision 1.3 2005/06/29 13:22:20 wollny
152 switch to version 0.7
153
154 Revision 1.1.1.1 2005/03/17 13:44:20 gerddie
155 initial import
156
157 Revision 1.2 2004/10/15 14:05:37 wollny
158 log entrys
159
160*/
Definition clist.hh:30
void push_back(T val)
Definition clist.hh:114
int size()
Definition clist.hh:126
const_iterator begin() const
Definition clist.hh:86
~clist()
Definition clist.hh:64
clist()
Definition clist.hh:61
node * iterator
Definition clist.hh:57
const_iterator end() const
Definition clist.hh:91
iterator end()
Definition clist.hh:81
T value_type
Definition clist.hh:33
iterator begin()
Definition clist.hh:76
const node * const_iterator
Definition clist.hh:58
void remove(node *n)
Definition clist.hh:97
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition defines.hh:33
#define NS_MIA_END
conveniance define to end the mia namespace
Definition defines.hh:36
T & operator*()
Definition clist.hh:45
node * succ
Definition clist.hh:39
node * prev
Definition clist.hh:38
node(T v, node *p, node *s)
Definition clist.hh:40