My Project
Loading...
Searching...
No Matches
omList.c
Go to the documentation of this file.
1/*******************************************************************
2 * File: omList.c
3 * Purpose: implementation of routines for operations on linked lists
4 * Author: obachman (Olaf Bachmann)
5 * Created: 11/99
6 *******************************************************************/
7#include "omConfig.h"
8
9#ifdef HAVE_OMALLOC
10
11#ifndef NULL
12#define NULL ((void*) 0)
13#endif
14
15#define _VALUE(list, offset) *((void**) ((char *)list + offset))
16#define VALUE(list, offset) (unsigned long) _VALUE(list, offset)
17#define NEXT(list) _VALUE(list, next)
18#define ITER(list) list = NEXT(list)
19
20int _omListLength(void* list, int next)
21{
22 int l = 0;
23 while (list != NULL)
24 {
25 l++;
26 ITER(list);
27 }
28 return l;
29}
30
31
32void* _omListLast(void* list, int next)
33{
34 if (list == NULL) return NULL;
35
36 while (NEXT(list) != NULL) ITER(list);
37
38 return list;
39}
40
41
42void* _omListHasCycle(void* list, int next)
43{
44 void* l1 = list;
45 void* l2;
46
47 int l = 0, i;
48
49 while (l1 != NULL)
50 {
51 i = 0;
52 l2 = list;
53 while (l1 != l2)
54 {
55 i++;
56 ITER(l2);
57 }
58 if (i != l) return l1;
59 ITER(l1);
60 l++;
61 }
62 return NULL;
63}
64
65
66void* _omIsOnList(void* list, int next, void* addr)
67{
68 if (addr == NULL)
69 return (NULL);
70
71 while (list != NULL)
72 {
73 if (addr == list) return addr;
74 ITER(list);
75 }
76 return 0;
77}
78
79void* _omRemoveFromList(void* list, int next, void* addr)
80{
81 void* nlist;
82 void* olist;
83
84 if (list == NULL) return NULL;
85
86 nlist = NEXT(list);
87 if (list == addr) return nlist;
88
89 olist = list;
90 while (nlist != NULL && nlist != addr)
91 {
92 list = nlist;
93 ITER(nlist);
94 }
95
96 if (nlist != NULL) NEXT(list) = NEXT(nlist);
97 return olist;
98}
99
100void* _omFindInList(void* list, int next, int long_field, unsigned long what)
101{
102 while (list != NULL)
103 {
104 if (VALUE(list, long_field) == what) return list;
105 ITER(list);
106 }
107 return NULL;
108}
109
110void* _omFindInSortedList(void* list, int next, int long_field,
111 unsigned long what)
112{
113 while (list != NULL)
114 {
115 if (VALUE(list, long_field) >= what)
116 {
117 if (VALUE(list, long_field) == what) return list;
118 return NULL;
119 }
120 ITER(list);
121 }
122 return NULL;
123}
124
125void* _omRemoveFromSortedList(void* list, int next, int long_field, void* addr)
126{
127 void* nlist;
128 void* olist;
129 unsigned long what = VALUE(addr, long_field);
130
131 if (list == NULL) return NULL;
132 nlist = NEXT(list);
133 if (list == addr) return nlist;
134 if (VALUE(list, long_field) > what) return list;
135
136 olist = list;
137 while (nlist != NULL && nlist != addr)
138 {
139 if (VALUE(list, long_field) > what) return olist;
140 list = nlist;
141 ITER(nlist);
142 }
143
144 if (nlist != NULL) NEXT(list) = NEXT(nlist);
145 return olist;
146}
147
148void* _omInsertInSortedList(void* list, int next, int long_field, void* addr)
149{
150 unsigned long what = VALUE(addr, long_field);
151
152 if (list == NULL || what <= VALUE(list, long_field))
153 {
154 NEXT(addr) = list;
155 return addr;
156 }
157 else
158 {
159 void* prev = list;
160 void* curr = NEXT(list);
161
162 while (curr != NULL && VALUE(curr, long_field) < what)
163 {
164 prev = curr;
165 ITER(curr);
166 }
167 NEXT(prev) = addr;
168 NEXT(addr) = curr;
169 return list;
170 }
171}
172
173
174#ifndef OM_NDEBUG
175#include "omalloc.h"
176#include "omDebug.h"
177
178omError_t _omCheckList(void* list, int next, int level, omError_t report, OM_FLR_DECL)
179{
180 if (level < 1) return omError_NoError;
181
182 if (level == 1)
183 {
184 while (list != NULL)
185 {
186 omCheckReturn(omCheckPtr(list, report, OM_FLR_VAL));
187 ITER(list);
188 }
189 }
190 else
191 {
192 void* l1 = list;
193 void* l2;
194 int l = 0, i;
195
196 l1 = list;
197 while (l1 != NULL)
198 {
199 omCheckReturn(omCheckPtr(l1, report, OM_FLR_VAL));
200 i = 0;
201 l2 = list;
202 while (l1 != l2)
203 {
204 i++;
205 ITER(l2);
206 }
207 if (i != l)
208 return omReportError(omError_ListCycleError, report, OM_FLR_VAL, "");
209 ITER(l1);
210 l++;
211 }
212 }
213 return omError_NoError;
214}
215
216omError_t _omCheckSortedList(void* list, int next, int long_field, int level, omError_t report, OM_FLR_DECL)
217{
218 void* prev = NULL;
219
220 if (level <= 1) return omError_NoError;
221
222 if (level == 1)
223 {
224 while (list != NULL)
225 {
226 omCheckReturn(omCheckPtr(list, report, OM_FLR_VAL));
227 if (prev != NULL && VALUE(prev, long_field) > VALUE(list, long_field))
228 return omReportError(omError_SortedListError, report, OM_FLR_VAL,
229 "%d > %d", VALUE(prev, long_field), VALUE(list, long_field));
230 prev = list;
231 ITER(list);
232 }
233 }
234 else
235 {
236 void* l1 = list;
237 void* l2;
238 int l = 0, i;
239
240 while (l1 != NULL)
241 {
242 omCheckReturn(omCheckPtr(l1, report, OM_FLR_VAL));
243 if (prev != NULL && VALUE(prev, long_field) > VALUE(l1, long_field))
244 return omReportError(omError_SortedListError, report, OM_FLR_VAL,
245 "%d > %d", VALUE(prev, long_field), VALUE(l1, long_field));
246 i = 0;
247 l2 = list;
248 while (l1 != l2)
249 {
250 i++;
251 ITER(l2);
252 }
254 prev = l1;
255 ITER(l1);
256 l++;
257 }
258 }
259 return omError_NoError;
260}
261#endif
262#endif
int level(const CanonicalForm &f)
int l
Definition: cfEzgcd.cc:100
int i
Definition: cfEzgcd.cc:132
ListNode * next
Definition: janet.h:31
omError_t omCheckPtr(const void *ptr, omError_t report, OM_FLR_DECL)
Definition: omDebugCheck.c:136
#define omCheckReturnError(cond, error)
Definition: omDebug.h:172
#define omCheckReturn(cond)
Definition: omDebug.h:170
omError_t omReportError(omError_t error, omError_t report_error, OM_FLR_DECL, const char *fmt,...)
Definition: omError.c:80
@ omError_ListCycleError
Definition: omError.h:34
@ omError_SortedListError
Definition: omError.h:35
@ omError_NoError
Definition: omError.h:18
enum omError_e omError_t
Definition: omError.h:44
void * _omIsOnList(void *list, int next, void *addr)
Definition: omList.c:66
#define NULL
Definition: omList.c:12
void * _omRemoveFromList(void *list, int next, void *addr)
Definition: omList.c:79
void * _omListLast(void *list, int next)
Definition: omList.c:32
#define NEXT(list)
Definition: omList.c:17
void * _omInsertInSortedList(void *list, int next, int long_field, void *addr)
Definition: omList.c:148
#define ITER(list)
Definition: omList.c:18
void * _omListHasCycle(void *list, int next)
Definition: omList.c:42
#define VALUE(list, offset)
Definition: omList.c:16
omError_t _omCheckSortedList(void *list, int next, int long_field, int level, omError_t report, OM_FLR_DECL)
Definition: omList.c:216
void * _omFindInSortedList(void *list, int next, int long_field, unsigned long what)
Definition: omList.c:110
void * _omFindInList(void *list, int next, int long_field, unsigned long what)
Definition: omList.c:100
int _omListLength(void *list, int next)
Definition: omList.c:20
omError_t _omCheckList(void *list, int next, int level, omError_t report, OM_FLR_DECL)
Definition: omList.c:178
void * _omRemoveFromSortedList(void *list, int next, int long_field, void *addr)
Definition: omList.c:125