blob: 5adf5b71fab7f625008383230763ca20c63618c4 (
plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#ifndef __XLIST_H
#define __XLIST_H
template<typename T>
class xlist {
public:
struct item {
T _item;
item *_prev, *_next;
xlist *_list;
item(T i) : _item(i), _prev(0), _next(0), _list(0) {}
~item() {
assert(!is_on_list());
//remove_myself();
}
// no copying!
item(const item& other);
const item& operator= (const item& right);
xlist* get_list() { return _list; }
bool is_on_list() { return _list ? true:false; }
bool remove_myself() {
if (_list) {
_list->remove(this);
assert(_list == 0);
return true;
} else
return false;
}
void move_to_front() {
assert(_list);
_list->push_front(this);
}
void move_to_back() {
assert(_list);
_list->push_back(this);
}
};
private:
item *_front, *_back;
int _size;
public:
xlist() : _front(0), _back(0), _size(0) {}
~xlist() {
assert(_size == 0);
assert(_front == 0);
assert(_back == 0);
}
int size() { return _size; }
bool empty() {
assert((bool)_front == (bool)_size);
return _front == 0;
}
void clear() {
while (_front) remove(_front);
}
void push_front(item *item) {
if (item->_list)
item->_list->remove(item);
item->_list = this;
item->_next = _front;
item->_prev = 0;
if (_front)
_front->_prev = item;
else
_back = item;
_front = item;
_size++;
}
void push_back(item *item) {
if (item->_list)
item->_list->remove(item);
item->_list = this;
item->_next = 0;
item->_prev = _back;
if (_back)
_back->_next = item;
else
_front = item;
_back = item;
_size++;
}
void remove(item *item) {
assert(item->_list == this);
if (item->_prev)
item->_prev->_next = item->_next;
else
_front = item->_next;
if (item->_next)
item->_next->_prev = item->_prev;
else
_back = item->_prev;
_size--;
item->_list = 0;
item->_next = item->_prev = 0;
}
T front() { return (T)_front->_item; }
T back() { return (T)_back->_item; }
void pop_front() {
assert(!empty());
remove(_front);
}
void pop_back() {
assert(!empty());
remove(_back);
}
class iterator {
private:
item *cur;
public:
iterator(item *i = 0) : cur(i) {}
T operator*() { return (T)cur->_item; }
iterator& operator++() {
assert(cur);
assert(cur->_list);
cur = cur->_next;
return *this;
}
bool end() { return cur == 0; }
};
iterator begin() { return iterator(_front); }
iterator end() { return iterator(NULL); }
};
#endif
|