4
4
# File: oldlist.cpp
5
5
# Description: List processing procedures.
6
6
# Author: Mark Seaman, Software Productivity
7
- # Created: Thu Jul 23 13:24:09 1987
8
- # Modified: Thu Dec 22 10:59:52 1988 (Mark Seaman) marks@hpgrlt
9
- # Language: C
10
- # Package: N/A
11
- # Status: Reusable Software Component
12
7
#
13
8
# (c) Copyright 1987, Hewlett-Packard Company.
14
9
** Licensed under the Apache License, Version 2.0 (the "License");
53
48
count LENGTH x = count(l);
54
49
search MEMBER if (search(l, x, nullptr))
55
50
56
- To implement SETS use:
57
-
58
- adjoin l = adjoin(l, x);
59
- set_union l = set_union(r, s);
60
- intersection l = intersection(r, s);
61
- set_difference l = set_difference(r, s);
62
- delete l = delete(s, x, nullptr);
63
- search if (search(l, x, nullptr))
64
-
65
- To Implement Associated LISTS use:
66
-
67
- lpush l = lpush(l, p);
68
- assoc s = assoc(l, x);
69
- adelete l = adelete(l, x);
70
-
71
51
The following rules of closure exist for the functions provided.
72
52
a = first_node (push (a, b))
73
53
b = list_rest (push (a, b))
87
67
#define add_on (l , x ) l = push(l, first_node(x))
88
68
#define next_one (l ) l = list_rest(l)
89
69
70
+ /**********************************************************************
71
+ * c o p y f i r s t
72
+ *
73
+ * Do the appropriate kind a push operation to copy the first node from
74
+ * one list to another.
75
+ *
76
+ **********************************************************************/
77
+
78
+ #define copy_first (l1 ,l2 ) \
79
+ (l2=push(l2, first_node(l1)))
80
+
90
81
/*----------------------------------------------------------------------
91
82
F u n c t i o n s
92
83
----------------------------------------------------------------------*/
84
+
85
+ /**********************************************************************
86
+ * i s s a m e
87
+ *
88
+ * Compare the list node with the key value return TRUE (non-zero)
89
+ * if they are equivalent strings. (Return FALSE if not)
90
+ **********************************************************************/
91
+ static int is_same (void * item1 , void * item2 ) {
92
+ return strcmp ((char * )item1 , (char * )item2 ) == 0 ;
93
+ }
94
+
93
95
/**********************************************************************
94
96
* c o u n t
95
97
*
@@ -108,8 +110,7 @@ int count(LIST var_list) {
108
110
* Delete all the elements out of the current list that match the key.
109
111
* This operation destroys the original list. The caller will supply a
110
112
* routine that will compare each node to the
111
- * key, and return a non-zero value when they match. If the value
112
- * nullptr is supplied for is_equal, the is_key routine will be used.
113
+ * key, and return a non-zero value when they match.
113
114
**********************************************************************/
114
115
LIST delete_d (LIST list , void * key , int_compare is_equal ) {
115
116
LIST result = NIL_LIST ;
@@ -137,31 +138,6 @@ LIST delete_d(LIST list, void *key, int_compare is_equal) {
137
138
return (result );
138
139
}
139
140
140
- LIST delete_d (LIST list , void * key ,
141
- TessResultCallback2 < int , void * , void * > * is_equal ) {
142
- LIST result = NIL_LIST ;
143
- LIST last_one = NIL_LIST ;
144
-
145
- while (list != NIL_LIST ) {
146
- if (!(* is_equal ).Run (first_node (list ), key )) {
147
- if (last_one == NIL_LIST ) {
148
- last_one = list ;
149
- list = list_rest (list );
150
- result = last_one ;
151
- set_rest (last_one , NIL_LIST );
152
- } else {
153
- set_rest (last_one , list );
154
- last_one = list ;
155
- list = list_rest (list );
156
- set_rest (last_one , NIL_LIST );
157
- }
158
- } else {
159
- list = pop (list );
160
- }
161
- }
162
- return (result );
163
- }
164
-
165
141
/**********************************************************************
166
142
* d e s t r o y
167
143
*
@@ -192,48 +168,6 @@ void destroy_nodes(LIST list, void_dest destructor) {
192
168
}
193
169
}
194
170
195
- /**********************************************************************
196
- * i n s e r t
197
- *
198
- * Create a list element and rearange the pointers so that the first
199
- * element in the list is the second aurgment.
200
- **********************************************************************/
201
- void insert (LIST list , void * node ) {
202
- LIST element ;
203
-
204
- if (list != NIL_LIST ) {
205
- element = push (NIL_LIST , node );
206
- set_rest (element , list_rest (list ));
207
- set_rest (list , element );
208
- node = first_node (list );
209
- list -> node = first_node (list_rest (list ));
210
- list -> next -> node = (LIST )node ;
211
- }
212
- }
213
-
214
- /**********************************************************************
215
- * i s s a m e
216
- *
217
- * Compare the list node with the key value return TRUE (non-zero)
218
- * if they are equivalent strings. (Return FALSE if not)
219
- **********************************************************************/
220
- int is_same (void * item1 , void * item2 ) {
221
- return strcmp ((char * )item1 , (char * )item2 ) == 0 ? 1 : 0 ;
222
- }
223
-
224
- /**********************************************************************
225
- * j o i n
226
- *
227
- * Join the two lists together. This function is similar to concat
228
- * except that concat creates a new list. This function returns the
229
- * first list updated.
230
- **********************************************************************/
231
- LIST join (LIST list1 , LIST list2 ) {
232
- if (list1 == NIL_LIST ) return (list2 );
233
- set_rest (last (list1 ), list2 );
234
- return (list1 );
235
- }
236
-
237
171
/**********************************************************************
238
172
* l a s t
239
173
*
@@ -244,19 +178,6 @@ LIST last(LIST var_list) {
244
178
return (var_list );
245
179
}
246
180
247
- /**********************************************************************
248
- * n t h c e l l
249
- *
250
- * Return nth list cell in the list.
251
- **********************************************************************/
252
- void * nth_cell (LIST var_list , int item_num ) {
253
- int x = 0 ;
254
- iterate (var_list ) {
255
- if (x ++ == item_num ) return (var_list );
256
- }
257
- return (var_list );
258
- }
259
-
260
181
/**********************************************************************
261
182
* p o p
262
183
*
@@ -305,73 +226,16 @@ LIST push_last(LIST list, void *item) {
305
226
return (push (NIL_LIST , item ));
306
227
}
307
228
308
- /**********************************************************************
309
- * r e v e r s e
310
- *
311
- * Create a new list with the elements reversed. The old list is not
312
- * destroyed.
313
- **********************************************************************/
314
- LIST reverse (LIST list ) {
315
- LIST newlist = NIL_LIST ;
316
-
317
- iterate (list ) copy_first (list , newlist );
318
- return (newlist );
319
- }
320
-
321
- /**********************************************************************
322
- * r e v e r s e d
323
- *
324
- * Create a new list with the elements reversed. The old list is
325
- * destroyed.
326
- **********************************************************************/
327
- LIST reverse_d (LIST list ) {
328
- LIST result = reverse (list );
329
- destroy (list );
330
- return (result );
331
- }
332
-
333
- /**********************************************************************
334
- * s a d j o i n
335
- *
336
- * Adjoin an element to an assorted list. The original list is
337
- * modified. Returns the modified list.
338
- **********************************************************************/
339
- LIST s_adjoin (LIST var_list , void * variable , int_compare compare ) {
340
- LIST l ;
341
- int result ;
342
-
343
- if (compare == nullptr ) compare = (int_compare )strcmp ;
344
-
345
- l = var_list ;
346
- iterate (l ) {
347
- result = (* compare )(variable , first_node (l ));
348
- if (result == 0 )
349
- return (var_list );
350
- else if (result < 0 ) {
351
- insert (l , variable );
352
- return (var_list );
353
- }
354
- }
355
- return (push_last (var_list , variable ));
356
- }
357
-
358
229
/**********************************************************************
359
230
* s e a r c h
360
231
*
361
232
* Search list, return NIL_LIST if not found. Return the list starting from
362
233
* the item if found. The compare routine "is_equal" is passed in as
363
- * the third parameter to this routine. If the value nullptr is supplied
364
- * for is_equal, the is_key routine will be used.
234
+ * the third parameter to this routine.
365
235
**********************************************************************/
366
236
LIST search (LIST list , void * key , int_compare is_equal ) {
367
237
if (is_equal == nullptr ) is_equal = is_same ;
368
238
369
239
iterate (list ) if ( (* is_equal )(first_node (list ), key )) return (list );
370
240
return (NIL_LIST );
371
241
}
372
-
373
- LIST search (LIST list , void * key ,
374
- TessResultCallback2 < int , void * , void * > * is_equal ) {
375
- iterate (list ) if ( (* is_equal ).Run (first_node (list ), key )) return (list );
376
- return (NIL_LIST );
377
- }
0 commit comments