-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
247 lines (199 loc) · 7.17 KB
/
list.h
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
/*
* File: list.h
* - header file for datatype polymorph list
*
* Author: Hermann Stamm-Wilbrandt
* Institut fuer Informatik III
* Roemerstr. 164
* Bonn University
* D-53117 Bonn
* Germany
* email: [email protected]
* phone: 0228-550-260 internal: x260 or x28, Fax: 0228-550-382
*
* For my safety:
*
* 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.
*/
/*
* Diagram of datatype list (with items):
* ======================================
*
*
* +-----+
* +-----------------|first|
* | +-----+
* | | last|-----------------+
* | +-----+ |
* | | size| |
* | +-----+ |
* | list |
* | |
* | |
* V V
* +----+ +----+ +----+ +----+
* |succ|---> |succ|---> ....---> |succ|---> |succ|---> 0
* +----+ +----+ +----+ +----+
* 0 <---|pred| <---|pred| <---.... <---|pred| <---|pred|
* +----+ |____| |____| |____|
* | inf| | inf| | inf| | inf|
* +----+ +----+ +----+ +----+
* item item item item
*
* ^ ^
* | |
* | |
* (here Push/Pop) (here Append/Remove)
*
*
* Push-Pop or Append-Remove: stack
* Push-Remove or Append-Pop : queue
* alltogether : double ended queue
*
*
* +----+
* <---| |--->
* +----+
* ^ ^
* | |
* | |
* Directions: before after
*
*/
#ifndef def_list_h
#include "array.h"
#define t_none 0
#define t_list 1
#define t_num 2
#define t_universe 3
#define t_deleted 4
#define Inf(X) _U_[(X)]
#define _N_(X) _U_[(X)+1]
#define _P_(X) _U_[(X)+2]
#define _T_(X) _U_[(X)+3]
#define ACTEL _U_[0]
#define SEENEL _U_[1]
#define MAXEL _U_[2]
#define FREEEL _U_[4]
#define FREESZ _U_[5]
extern num TOPIT,TOPITH,TOPITH1;
#define IS_NUM(X) (_T_(X)==t_num)
#define IS_LIST(X) (_T_(X)==t_list)
#define IS_DELETED(X) ((_T_(X)!=t_num) && (_T_(X)!=t_list))
#define IS_DEFINED(X) ((((X)%4)==0) && ((X)<=SEENEL) && (! IS_DELETED(X)))
#define new_IT TOPITH = new_half_element(TOPIT), _U_[TOPITH]=TOPIT,\
TOPIT=TOPITH, act_IT
#define act_IT0 _U_[TOPIT+1]
#define act_L _U_[_U_[TOPIT]+1]
#define act_IT TOPITH1 = _U_[TOPIT+1], _U_[TOPIT+1]
#define act_IT1 TOPITH1
#define del_IT TOPITH = _U_[TOPIT], delete_half_element(TOPIT),\
TOPIT=TOPITH
#define push_ARG(x) new_IT = (num) (x)
#define top_ARG1() (_U_[TOPIT+1])
#define top_ARG2() (_U_[_U_[TOPIT]+1])
#define top_ARG3() (_U_[_U_[_U_[TOPIT]]+1])
#define top_ARG4() (_U_[_U_[_U_[_U_[TOPIT]]]+1])
#define pop_ARG() (TOPITH1 = _U_[TOPIT+1], del_IT, TOPITH1)
extern num uni_aux;
extern num *_U_;
typedef num list;
typedef num item;
typedef list list_of_num;
typedef list list_of_list_of_num;
#define Apply_forall_elements(e,L,Block) { num e=0; forall(e,L) { Block } }
#define forall_items(it,S) for( (it) = first(S); (it); (it) = succ(it,S))
#define forall(x,L) for(new_IT = _N_(L); \
x=(act_IT?Inf(act_IT1):(del_IT,x)),act_IT1; \
act_IT = _N_(act_IT)) \
/*if (printf("\nforall\n"),1)*/
#define forall_rev(x,L) for(new_IT = _P_(L); \
x=(act_IT?Inf(act_IT1):(del_IT,x)),act_IT1; \
act_IT = _P_(act_IT)) \
/*if (printf("\nforall_rev\n"),1)*/
#define forallH(x) for(new_IT = _N_(act_L); \
x=(act_IT?Inf(act_IT1):(del_IT,x)),act_IT1; \
act_IT = _N_(act_IT))/*if (printf("\nforall\n"),1)*/
#define next1(I,L0,L1) ( (((I)==(L0)) && ((L0)!=(L1))) ? (L1) : 0 )
#define next2(I,L0,L1,L2) ( (((I)==(L0)) && \
((L0)!=(L1))) ? (L1) : next1((I),(L1),(L2)) )
#define next3(I,L0,L1,L2,L3) ( (((I)==(L0)) && ((L0)!=(L1))) ? \
(L1) : next2((I),(L1),(L2),(L3)) )
#define next4(I,L0,L1,L2,L3,L4) ( (((I)==(L0)) && ((L0)!=(L1))) ? \
(L1) : next3((I),(L1),(L2),(L3),(L4)) )
#define next5(I,L0,L1,L2,L3,L4,L5) ( (((I)==(L0)) && ((L0)!=(L1))) ? \
(L1) : next4((I),(L1),(L2),(L3),(L4),(L5)) )
#define forall1(x,L1) forall((X),(L1))
#define forall2(x,L1,L2) for(new_IT = 0; \
act_IT0 = next2(act_IT0,0,(L1),(L2)),\
(act_IT0 ? 1 : (del_IT,0));\
) \
/*if (printf("\nact_IT0: %d(%d,%d)\n",act_IT0,(L1),(L2)),1)*/\
forallH((x))
#define forall3(x,L1,L2,L3) for(new_IT = 0; \
act_IT0 = next3(act_IT0,0,(L1),(L2),(L3)),\
(act_IT0 ? 1 : (del_IT,0));\
) \
/*if (printf("\nact_IT0: %d(%d,%d)\n",act_IT0,(L1),(L2)),1)*/\
forallH((x))
#define forall4(x,L1,L2,L3,L4) for(new_IT = 0; \
act_IT0 = next4(act_IT0,0,(L1),(L2),(L3),(L4)), \
(act_IT0 ? 1 : (del_IT,0)); \
) \
forallH((x))
#define forall5(x,L1,L2,L3,L4,L5) for(new_IT = 0; \
act_IT0 = next5(act_IT0,0,(L1),(L2),(L3),(L4),(L5)),\
(act_IT0 ? 1 : (del_IT,0)); \
) \
forallH((x))
void new_universe(num k);
num used_in_universe(void);
void universe_statistics(bool do_abort);
void delete_universe();
num new_element(void);
void delete_element(num e);
num new_half_element(num e);
void delete_half_element(num e);
list new_list(void);
void delete_list(list L);
num length(list L);
num size(list L);
bool empty(list L);
item first(list L);
item last(list L);
item succ(item it, list L);
item pred(item it, list L);
item cyclic_succ(item it, list L);
item cyclic_pred(item it, list L);
item search(num x, list L);
num contents(item it, list L);
num inf(item it, list L);
num head(list L);
num tail(list L);
num rank(num x, list L);
num choose(list L);
bool duplicates(list L);
item Insert(num x, item it, bool dir, list L);
item Push(num x, list L);
item Append(num x, list L);
item remove_item(item it, list L);
item old_item(item it, list L);
num delete_item(item it, list L);
num Remove(list L);
num Top(list L);
num Pop(list L);
void assign(item it, num x, list L);
item conc(list L1, list L);
list split(item it, list L);
void swap(item it, item it1, list L);
void apply(void (*f)(num*), list L);
void bucket_sort_F(num low, num high, num (*Map)(num), list L);
void bucket_sort(num low, num high, array_of_num Map, list L);
void clear(list L);
bool is_layered_list(num level, list L);
void print(list L);
item Append_list(list L1, list L);
#define def_list_h
#endif