[go: up one dir, main page]

Menu

[r998]: / libmona / libmona / clist.hh  Maximize  Restore  History

Download this file

143 lines (117 with data), 2.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* -*- mona-c++ -*-
* Copyright (c) Leipzig, Madrid 2004 - 2008
* Max-Planck-Institute for Human Cognitive and Brain Science
* Max-Planck-Institute for Evolutionary Anthropology
* BIT, ETSI Telecomunicacion, UPM
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef CLIST_HH
#define CLIST_HH
#include <cassert>
template <class T>
class clist {
public:
typedef T value_type;
struct node {
T value;
node *prev;
node *succ;
node(T v, node *p, node *s):
value(v), prev(p), succ(s)
{
}
T& operator *() {
return value;
}
const T& operator *() const {
return value;
}
};
typedef node *iterator;
typedef const node *const_iterator;
clist(): _M_head(NULL){
}
~clist() {
if (_M_head != NULL) {
node *head = _M_head;
while (head != head->succ)
remove(head->succ);
delete _M_head;
}
}
iterator begin() {
return _M_head;
}
iterator end() {
return _M_head;
}
const_iterator begin() const {
return _M_head;
}
const_iterator end() const {
return _M_head;
}
void remove(node *n){
if (n->prev != n) {
n->succ->prev = n->prev;
n->prev->succ = n->succ;
if (n == _M_head) {
_M_head = n->prev;
}
delete n;
}else { // only head left
assert(n == _M_head);
delete n;
_M_head = NULL;
}
}
void push_back(T val)
{
if (_M_head) {
node *nn = new node(val, _M_head, _M_head->succ);
nn->prev->succ = nn;
nn->succ->prev = nn;
}else {
assert (_M_head == NULL);
_M_head = new node(val,NULL,NULL);
_M_head->prev = _M_head->succ = _M_head;
}
}
int size() {
int s = 0;
if (_M_head) {
node *n = _M_head;
while (n->succ != _M_head) {
n = n->succ;
++s;
}
}
return s;
}
private:
node *_M_head;
};
#endif
/*
$Log$
Revision 1.3 2005/06/29 13:22:20 wollny
switch to version 0.7
Revision 1.1.1.1 2005/03/17 13:44:20 gerddie
initial import
Revision 1.2 2004/10/15 14:05:37 wollny
log entrys
*/