HarrySun

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1 /*
2 * declare_list_class.cpp
3 *
4 * Created on: 2011-12-22
5 * Author: Administrator
6 */
7
8 class glist_e {
9 friend class glist;
10 friend class mtflist;
11 friend class dlist;
12
13 protected:
14
15 glist_e(const glist_e &);
16 glist_e &operator=(const glist_e &);
17
18 protected:
19 glist_e *next_e;
20
21 public:
22 glist_e() :
23 next_e(0) {
24 }
25 glist_e *next() const {
26 return next_e;
27 }
28 };
29
30
31 class glist {
32 private:
33
34 glist(const glist &);
35 glist &operator=(const glist &);
36
37 protected:
38 glist_e *head_e, *tail_e;
39
40 public:
41 glist() :
42 head_e(0), tail_e(0) {
43 }
44 virtual ~glist();
45
46 boolean is_empty() const {
47 return head_e == NULL;
48 }
49 glist_e *head() const {
50 return head_e;
51 }
52 glist_e *tail() const {
53 return tail_e;
54 }
55 glist_e *push(glist_e *e);
56 glist_e *pop();
57 glist_e *append(glist_e *e);
58 glist_e *insert_before(glist_e *e, glist_e *pos);
59 glist_e *insert_after(glist_e *e, glist_e *pos);
60 glist_e *remove(glist_e *e);
61 void clear() {
62 head_e = tail_e = NULL;
63 }
64 void erase();
65 void grab_from(glist *l);
66 void push(glist *l);
67 void append(glist *l);
68 void insert_before(glist *l, glist_e *pos);
69 void insert_after(glist *l, glist_e *pos);
70 int count() const;
71 boolean contains(const glist_e *e) const;
72 glist_e *operator[](int ndx) const;
73 };
74
75
76 class glist_iter {
77 private:
78
79 glist_iter(const glist_iter &);
80 glist_iter &operator=(const glist_iter &);
81
82 protected:
83 glist_e *cur, *nxt;
84
85 public:
86 glist_iter() :
87 cur(0), nxt(0) {
88 }
89 glist_iter(const glist *gl) :
90 cur(0), nxt(0) {
91 reset(gl);
92 }
93
94 void reset(const glist *gl);
95 void set(glist_e *e);
96
97 boolean is_empty() const {
98 return nxt == NULL;
99 }
100 glist_e *step();
101 glist_e *peek() const {
102 return nxt;
103 }
104 glist_e *cur_elem() const {
105 return cur;
106 }
107 };
108
109
110 class super_block_list_e: public glist_e {
111 private:
112 super_block_list_e(const super_block_list_e &o) :
113 contents(o.contents) {
114 assert(0);
115 }
116 super_block_list_e &operator=(const super_block_list_e &) {
117 assert(0);
118 return *this;
119 }
120 public:
121 super_block * contents;
122 super_block_list_e(super_block * t) :
123 contents(t) {
124 }
125 super_block_list_e *next() const {
126 return (super_block_list_e*) glist_e::next();
127 }
128 ;
129 };
130
131
132 class super_block_list: public glist {
133 protected:
134 virtual void set_elem(super_block_list_e *) {
135 }
136 void set_elems(super_block_list *l) {
137 super_block_list_e *i = l->head();
138 while (i) {
139 set_elem(i);
140 i = i->next();
141 }
142 }
143 private:
144 super_block_list(const super_block_list &) {
145 assert(0);
146 }
147 super_block_list &operator=(const super_block_list &) {
148 assert(0);
149 return *this;
150 }
151 public:
152 super_block_list() {
153 }
154 super_block_list(super_block * t) {
155 append(t);
156 }
157 super_block_list(super_block * t1, super_block * t2) {
158 append(t1);
159 append(t2);
160 }
161 super_block_list(super_block * t1, super_block * t2, super_block * t3) {
162 append(t1);
163 append(t2);
164 append(t3);
165 }
166 virtual ~super_block_list() {
167 }
168 super_block_list_e *head() const {
169 return (super_block_list_e*) glist::head();
170 }
171 super_block_list_e *tail() const {
172 return (super_block_list_e*) glist::tail();
173 }
174 super_block * push(super_block * t) {
175 return push(new super_block_list_e(t))->contents;
176 }
177 super_block_list_e *push(super_block_list_e *e) {
178 set_elem(e);
179 return (super_block_list_e*) glist::push(e);
180 }
181 super_block * pop() {
182 super_block_list_e *e = (super_block_list_e *) glist::pop();
183 super_block * t = e->contents;
184 delete e;
185 return t;
186 }
187 super_block * append(super_block * t) {
188 return append(new super_block_list_e(t))->contents;
189 }
190 super_block_list_e *append(super_block_list_e *e) {
191 set_elem(e);
192 return (super_block_list_e*) glist::append(e);
193 }
194 super_block * insert_before(super_block * t, super_block_list_e *pos) {
195 return insert_before(new super_block_list_e(t), pos)->contents;
196 }
197 super_block_list_e *insert_before(super_block_list_e *e,
198 super_block_list_e *pos) {
199 set_elem(e);
200 return (super_block_list_e*) glist::insert_before(e, pos);
201 }
202 super_block * insert_after(super_block * t, super_block_list_e *pos) {
203 return insert_after(new super_block_list_e(t), pos)->contents;
204 }
205 super_block_list_e *insert_after(super_block_list_e *e,
206 super_block_list_e *pos) {
207 set_elem(e);
208 return (super_block_list_e*) glist::insert_after(e, pos);
209 }
210 super_block_list_e *remove(super_block_list_e *e) {
211 return (super_block_list_e*) glist::remove(e);
212 }
213 void copy(const super_block_list *l) {
214 super_block_list_e *i = l->head();
215 while (i) {
216 append(i->contents);
217 i = i->next();
218 }
219 }
220 void grab_from(super_block_list *l) {
221 set_elems(l);
222 glist::grab_from(l);
223 }
224 void push(super_block_list *l) {
225 set_elems(l);
226 glist::push(l);
227 }
228 void append(super_block_list *l) {
229 set_elems(l);
230 glist::append(l);
231 }
232 void insert_before(super_block_list *l, super_block_list_e *pos) {
233 set_elems(l);
234 glist::insert_before(l, pos);
235 }
236 void insert_after(super_block_list *l, super_block_list_e *pos) {
237 set_elems(l);
238 glist::insert_after(l, pos);
239 }
240 super_block * operator[](int ndx) const {
241 return ((super_block_list_e*) glist::operator[](ndx))->contents;
242 }
243 super_block_list_e *lookup(const super_block * t) const {
244 super_block_list_e *i = head();
245 while (i) {
246 if (i->contents == t)
247 break;
248 i = i->next();
249 }
250 return i;
251 }
252 ;
253 };
254
255
256 class super_block_list_iter: public glist_iter {
257 public:
258 super_block_list_iter() :
259 glist_iter() {
260 }
261 super_block_list_iter(const super_block_list *l) :
262 glist_iter(l) {
263 }
264 super_block * step() {
265 return ((super_block_list_e*) glist_iter::step())->contents;
266 }
267 super_block * peek() const {
268 assert_msg(!is_empty(),
269 ("%s_iter::peek - no next element", "super_block_list"));
270 return ((super_block_list_e*) glist_iter::peek())->contents;
271 }
272 super_block_list_e *cur_elem() const {
273 return (super_block_list_e*) glist_iter::cur_elem();
274 }
275 };

 

posted on 2011-12-26 19:55  HarrySun  阅读(232)  评论(0编辑  收藏  举报