1 /* $Id: intlist.h 218147 2019-01-16 21:28:41Z twu $ */
2 #ifndef INTLIST_INCLUDED
3 #define INTLIST_INCLUDED
4 #ifdef HAVE_CONFIG_H
5 #include <config.h> /* For HAVE_INLINE */
6 #endif
7
8 typedef struct Intlist_T *Intlist_T;
9
10 #include <stdlib.h>
11 #include "mem.h"
12 #include "bool.h"
13
14 #define T Intlist_T
15 struct T {
16 int first;
17 struct T *rest;
18 };
19
20
21 #if !defined(HAVE_INLINE)
22
23 extern T Intlist_push (T list, int x);
24 extern T Intlist_pop (T list, int *x);
25 extern int Intlist_head (T list);
26 extern T Intlist_next (T list);
27 extern T Intlist_reverse (T list);
28 extern int Intlist_length (T list);
29
30 #else
31 /* extern inline T Intlist_push (T list, int x); */
32
33 #if 0
34 /* Code to trace callers of Intlist_push */
35 static inline T
36 Intlist_push_actual (T list, int x, const char *file, int line) {
37 T new = (T) MALLOC(sizeof(*new));
38
39 printf("Intlist_push %s:%d\n",file,line);
40 new->first = x;
41 new->rest = list;
42 return new;
43 }
44
45 #define Intlist_push(list,x) Intlist_push_actual(list,x,__FILE__,__LINE__)
46 #else
47 static inline T
Intlist_push(T list,int x)48 Intlist_push (T list, int x) {
49 T new = (T) MALLOC(sizeof(*new));
50
51 new->first = x;
52 new->rest = list;
53 return new;
54 }
55 #endif
56
57
58 /* extern inline T Intlist_pop (T list, int *x); */
59 static inline T
Intlist_pop(T list,int * x)60 Intlist_pop (T list, int *x) {
61 T head;
62
63 if (list) {
64 head = list->rest;
65 *x = list->first;
66 FREE(list);
67 return head;
68 } else {
69 return list;
70 }
71 }
72
73 /* extern inline int Intlist_head (T list); */
74 static inline int
Intlist_head(T list)75 Intlist_head (T list) {
76 return list->first;
77 }
78
79 /* extern inline T Intlist_next (T list); */
80 static inline T
Intlist_next(T list)81 Intlist_next (T list) {
82 if (list) {
83 return list->rest;
84 } else {
85 return NULL;
86 }
87 }
88
89 /* extern inline void Intlist_free (T *list); */
90 static inline void
Intlist_free(T * list)91 Intlist_free (T *list) {
92 T prev;
93
94 while ((prev = *list) != NULL) {
95 *list = prev->rest;
96 FREE(prev);
97 }
98 }
99
100 /* extern inline T Intlist_reverse (T list); */
101 static inline T
Intlist_reverse(T list)102 Intlist_reverse (T list) {
103 T head = NULL, next;
104
105 for ( ; list; list = next) {
106 next = list->rest;
107 list->rest = head;
108 head = list;
109 }
110 return head;
111 }
112
113 /* extern inline int Intlist_length (T list); */
114 static inline int
Intlist_length(T list)115 Intlist_length (T list) {
116 int n;
117
118 for (n = 0; list; list = list->rest) {
119 n++;
120 }
121 return n;
122 }
123 #endif
124
125
126 extern T
127 Intlist_push_in (T list, int x);
128 extern T
129 Intlist_insert_second (T list, int x);
130 extern void
131 Intlist_delete (T prev, T this);
132 extern void
133 Intlist_head_set (T list, int x);
134 extern void
135 Intlist_free_in (T *list);
136 extern T
137 Intlist_keep_one (T list, int i);
138 extern int
139 Intlist_max (T list);
140 extern int
141 Intlist_min (T list);
142 extern int
143 Intlist_sum (T list);
144 extern bool
145 Intlist_vary (T list);
146 extern bool
147 Intlist_exists_p (T list, int x);
148 extern int *
149 Intlist_to_array (int *n, T list);
150 extern int *
151 Intlist_to_array_plus_extra (int *n, T list);
152 extern void
153 Intlist_fill_array (int *array, T list);
154 extern void
155 Intlist_fill_array_and_free (int *array, T *list);
156 extern int *
157 Intlist_to_array_out (int *n, T list);
158 extern char *
159 Intlist_to_char_array (int *n, T list);
160 extern char *
161 Intlist_to_char_array_in (int *n, T list);
162 extern T
163 Intlist_from_array (int *array, int n);
164 extern T
165 Intlist_copy (T list);
166 extern T
167 Intlist_append (T list, T tail);
168 extern int
169 Intlist_last_value (T this);
170 extern int
171 Intlist_penultimate_value (T this);
172 extern int
173 Intlist_index (T this, int index);
174 extern T
175 Intlist_from_string (char *string);
176 extern char *
177 Intlist_to_string (T this);
178 extern int *
179 Intlist_array_ascending_by_key (int *n, T this, T key);
180 extern void
181 Intlist_array_dual_ascending_by_key (int *sorted, int *keyarray, int n, T this, T key);
182 extern T
183 Intlist_list_ascending_by_key (T this, T key);
184 extern T
185 Intlist_list_descending_by_key (T this, T key);
186 extern T
187 Intlist_sort_ascending (T this);
188 extern bool
189 Intlist_equal (T x, T y);
190 extern bool
191 Intlist_intersect_p (T x, T y);
192 extern T
193 Intlist_filter (T list, bool *deletep);
194
195 #undef T
196 #endif
197