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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
// This may look like C code, but it is really -*- C++ -*-
/*
Copyright (C) 1988 Free Software Foundation
written by Doug Lea (dl@rocky.oswego.edu)
This file is part of the GNU C++ Library. This library is free
software; you can redistribute it and/or modify it under the terms of
the GNU Library General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version. This library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _<T>List_h
#ifdef __GNUG__
#pragma interface
#endif
#define _<T>List_h 1
#include <Pix.h>
#include "<T>.defs.h"
#ifndef _<T>_typedefs
#define _<T>_typedefs 1
typedef void (*<T>Procedure)(<T&>);
typedef <T> (*<T>Mapper)(<T&>);
typedef <T> (*<T>Combiner)(<T&>, <T&>);
typedef int (*<T>Predicate)(<T&>);
typedef int (*<T>Comparator)(<T&>, <T&>);
#endif
struct <T>ListNode
{
<T>ListNode* tl;
short ref;
<T> hd;
};
extern <T>ListNode Nil<T>ListNode;
class <T>List
{
protected:
<T>ListNode* P;
<T>List(<T>ListNode* p);
public:
<T>List();
<T>List(<T&> head);
<T>List(<T&> head, const <T>List& tl);
<T>List(<T>List& a);
<T>List(Pix p);
~<T>List();
<T>List& operator = (const <T>List& a);
int null();
int valid();
operator const void* ();
int operator ! ();
int length();
int list_length();
<T>& get();
<T>& head();
<T>& operator [] (int n);
<T>List nth(int n);
<T>List tail();
<T>List last();
<T>List find(<T&> targ);
<T>List find(<T>List& targ);
int contains(<T&> targ);
int contains(<T>List& targ);
int position(<T&> targ);
friend <T>List copy(const <T>List& a);
friend <T>List concat(<T>List& a, <T>List& b);
friend <T>List append(<T>List& a, <T>List& b);
friend <T>List map(<T>Mapper f, <T>List& a);
friend <T>List merge(<T>List& a, <T>List& b, <T>Comparator f);
friend <T>List combine(<T>Combiner f, <T>List& a, <T>List& b);
friend <T>List reverse(<T>List& a);
friend <T>List select(<T>Predicate f, <T>List& a);
#undef remove
friend <T>List remove(<T&> targ, <T>List& a);
friend <T>List remove(<T>Predicate f, <T>List& a);
friend <T>List subst(<T&> old, <T&> repl, <T>List& a);
void push(<T&> x);
<T> pop();
void set_tail(<T>List& p);
void append(<T>List& p);
void prepend(<T>List& p);
void del(<T&> targ);
void del(<T>Predicate f);
void select(<T>Predicate f);
void subst(<T&> old, <T&> repl);
void reverse();
void sort(<T>Comparator f);
void apply(<T>Procedure f);
<T> reduce(<T>Combiner f, <T&> base);
friend int operator == (const <T>List& a, const <T>List& b);
friend inline int operator != (const <T>List& a, const <T>List& b);
Pix first();
void next(Pix& p);
Pix seek(<T&> item) const;
<T>& operator () (Pix p);
const <T>& operator () (Pix p) const;
int owns(Pix p);
void error(const char*);
int OK();
};
inline void reference(<T>ListNode* p)
{
if (p->ref >= 0) ++p->ref;
}
inline void dereference(<T>ListNode* p)
{
while (p->ref > 0 && --p->ref == 0)
{
<T>ListNode* n = p->tl;
delete(p);
p = n;
}
}
inline <T>ListNode* new<T>ListNode(const <T&> h)
{
<T>ListNode* p = new <T>ListNode;
p->ref = 1;
p->hd = h;
return p;
}
inline <T>ListNode* new<T>ListNode(<T&> h, <T>ListNode* t)
{
<T>ListNode* p = new <T>ListNode;
p->ref = 1;
p->hd = h;
p->tl = t;
return p;
}
inline <T>List::~<T>List()
{
dereference(P);
}
inline <T>List::<T>List()
{
P = &Nil<T>ListNode;
}
inline <T>List::<T>List(<T>ListNode* p)
{
P = p;
}
inline <T>List::<T>List(<T&> head)
{
P = new<T>ListNode(head);
P->tl = &Nil<T>ListNode;
}
inline <T>List::<T>List(<T&> head, const <T>List& tl)
{
P = new<T>ListNode(head, tl.P);
reference(P->tl);
}
inline <T>List::<T>List(<T>List& a)
{
reference(a.P);
P = a.P;
}
inline <T>& <T>List::get()
{
return P->hd;
}
inline <T>& <T>List::head()
{
return P->hd;
}
inline <T>List <T>List::tail()
{
reference(P->tl);
return <T>List(P->tl);
}
inline int <T>List::null()
{
return P == &Nil<T>ListNode;
}
inline int <T>List::valid()
{
return P != &Nil<T>ListNode;
}
inline <T>List::operator const void* ()
{
return (P == &Nil<T>ListNode)? 0 : this;
}
inline int <T>List::operator ! ()
{
return (P == &Nil<T>ListNode);
}
inline void <T>List::push(<T&> head)
{
<T>ListNode* oldp = P;
P = new<T>ListNode(head, oldp);
}
inline int operator != (const <T>List& x, const <T>List& y)
{
return !(x == y);
}
inline Pix <T>List::first()
{
return (P == &Nil<T>ListNode)? 0 : Pix(P);
}
inline <T>& <T>List::operator () (Pix p)
{
return ((<T>ListNode*)p)->hd;
}
inline const <T>& <T>List::operator () (Pix p) const
{
return ((const <T>ListNode*)p)->hd;
}
inline void <T>List::next(Pix& p)
{
if (p != 0)
{
p = Pix(((<T>ListNode*)p)->tl);
if (p == &Nil<T>ListNode) p = 0;
}
}
inline <T>List::<T>List(Pix p)
{
P = (<T>ListNode*)p;
reference(P);
}
#endif
|