~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-libs/tina/sys/sysLst_rec.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/sys/sysLst_rec.c,v $
 37  * Date    :  $Date: 2008/12/02 22:03:07 $
 38  * Version :  $Revision: 1.7 $
 39  * CVS Id  :  $Id: sysLst_rec.c,v 1.7 2008/12/02 22:03:07 paul Exp $
 40  *
 41 */
 42 /**
 43  *  @file Mutually recursive lists & strings handling.
 44  *  @brief Complex geomety can be stored as arbitrary lists of lists in order to encode 
 45  *  spatial relationships in the list structure or for algorithmic purposes. These lists
 46  *  are provided here with basic manipulation routines. Such data structures are used
 47  *  extensively in the wireframe model matcher and geometry fitting. 
 48  *
 49  *********
 50 */
 51 
 52 #include "sysLst_rec.h"
 53 
 54 #if HAVE_CONFIG_H
 55   #include <config.h>
 56 #endif
 57 
 58 #include <stdio.h>
 59 #include <tina/sys/sys_GenDef.h>
 60 #include <tina/sys/sys_LstDef.h>
 61 #include <tina/sys/sysLst_dd.h>
 62 #include <tina/sys/sysLst_ddstr.h>
 63 #include <tina/sys/sysLst_list.h>
 64 #include <tina/sys/sysLst_strings.h>
 65 
 66 
 67 void reclist_list_apply(List * list, void (*func) ( /* ??? */ ), int type, void *data)
 68 {
 69         List *ptr;
 70 
 71         for (ptr = list; ptr != NULL; ptr = ptr->next)
 72                 reclist_apply(ptr->to, ptr->type, func, type, data);
 73 }
 74 
 75 void reclist_string_apply(Tstring * str, void (*func) ( /* ??? */ ), int type, void *data)
 76 {
 77         List *ptr;
 78         List *end;
 79 
 80         if (str == NULL)
 81                 return;
 82 
 83         end = str->end;
 84 
 85         for (ptr = str->start;; ptr = ptr->next)
 86         {
 87                 reclist_apply(ptr->to, ptr->type, func, type, data);
 88 
 89                 if (ptr == end)
 90                         break;
 91         }
 92 }
 93 
 94 void reclist_apply(void *ptr, int ptype, void (*func) ( /* ??? */ ), int type, void *data)
 95 /* ptr type */
 96 /* test type */
 97 {
 98         switch (ptype)
 99         {
100                 case LIST:
101                         reclist_list_apply((List *) ptr, func, type, data);
102                         break;
103                 case STRING:
104                         reclist_string_apply((Tstring *) ptr, func, type, data);
105                         break;
106                 default:
107                         if ((type == 0) || (type == ptype))
108                                 func(ptr, ptype, data);
109                         break;
110         }
111 }
112 
113 /* reclist rm all occurences of to */
114 
115 void *reclist_rm_entry(void *ptr, int type, void *to, void (*freefunc) ( /* ??? */ ));
116 
117 List *reclist_list_rm_entry(List * list, void *to, void (*freefunc) ( /* ??? */ ))
118 {
119         List *ptr;
120         List *next;
121         List *newlist = NULL;
122 
123         for (ptr = list; ptr != NULL; ptr = next)
124         {
125                 void *temp;
126 
127                 next = ptr->next;
128                 temp = (void *) reclist_rm_entry(ptr->to, ptr->type, to, freefunc);
129                 if (temp != NULL)
130                         newlist = ref_addtostart(newlist, temp, ptr->type);
131         }
132         return (list_reverse(newlist));
133 }
134 
135 Tstring *reclist_string_rm_entry(Tstring * str, void *to, void (*freefunc) ( /* ??? */ ))
136 {
137         List *ptr;
138         List *end;
139         List *next;
140         List *newlist = NULL;
141 
142         if (str == NULL)
143                 return (NULL);
144 
145         end = str->end;
146 
147         for (ptr = str->start;; ptr = next)
148         {
149                 void *temp;
150 
151                 next = ptr->next;
152                 temp = (void *) reclist_rm_entry(ptr->to, ptr->type, to, freefunc);
153 
154                 if (temp != NULL)
155                         newlist = dd_ref_addtostart(newlist, temp, ptr->type);
156                 if (ptr == end)
157                         break;
158         }
159         if (newlist == NULL)
160                 return (NULL);
161         newlist = dd_list_reverse(newlist);
162         return (str_make(str->type, newlist, dd_get_end(newlist)));
163 }
164 
165 void *reclist_rm_entry(void *ptr, int type, void *to, void (*freefunc) ( /* ??? */ ))
166 {
167         switch (type)
168         {
169                 case LIST:
170                         return ((void *) reclist_list_rm_entry((List *) ptr, to, freefunc));
171                 case STRING:
172                         return ((void *)
173                                                         reclist_string_rm_entry((Tstring *) ptr, to, freefunc));
174                 default:
175                         if (to != ptr)
176                                 return (ptr);
177                         if (freefunc != NULL)
178                                 freefunc(ptr, type);
179                         return (NULL);
180                         break;
181         }
182 }
183 
184 /* copy recursive lists possibly updating their types */
185 List *reclist_list_update(List * list, void *(*func) ( /* ??? */ ), int type, void *data)
186 {
187         List *ptr;
188         List *newlist = NULL;
189 
190         for (ptr = list; ptr != NULL; ptr = ptr->next)
191         {
192                 void *copy;
193                 int ptype = ptr->type;
194 
195                 copy = reclist_update(ptr->to, &ptype, func, type, data);
196                 if (copy != NULL)
197                         newlist = ref_addtostart(newlist, copy, ptype);
198         }
199         return (list_reverse(newlist));
200 }
201 
202 Tstring *reclist_string_update(Tstring * str, void *(*func) ( /* ??? */ ), int type, void *data)
203 {
204         List *ptr;
205         List *end;
206         List *newlist = NULL;
207 
208         if (str == NULL)
209                 return (NULL);
210 
211         end = str->end;
212 
213         for (ptr = str->start;; ptr = ptr->next)
214         {
215                 void *copy;
216                 int ptype = ptr->type;
217 
218                 copy = reclist_update(ptr->to, &ptype, func, type, data);
219 
220                 if (copy != NULL)
221                         newlist = dd_ref_addtostart(newlist, copy, ptype);
222                 if (ptr == end)
223                         break;
224         }
225         if (newlist == NULL)
226                 return (NULL);
227         newlist = dd_list_reverse(newlist);
228         str = str_make(str->type, newlist, dd_get_end(newlist));
229         if (str->type == LOOP)
230         {
231                 str->end->next = str->start;
232                 str->start->last = str->end;
233         }
234         return (str);
235 }
236 
237 void *reclist_update(void *ptr, int *ptype, void *(*func) ( /* ??? */ ), int type, void *data)
238 /* ptr type */
239 /* test type */
240 {
241         switch (*ptype)
242         {
243                 case LIST:
244                         return ((void *)
245                                                         reclist_list_update((List *) ptr, func, type, data));
246                 case STRING:
247                         return ((void *)
248                                                         reclist_string_update((Tstring *) ptr, func, type, data));
249                 default:
250                         if ((type == 0) || (type == *ptype))
251                                 return ((func == NULL) ? ptr : func(ptr, ptype, data));
252                         break;
253         }
254         return (NULL);
255 }
256 
257 /* Update recursive lists to simple flat lists allow type of element to
258  * be updated */
259 List *reclist_flat(void *ptr, int *ptype, void *(*func) ( /* ??? */ ), int type, void *data);
260 
261 List *reclist_list_flat(List * list, void *(*func) ( /* ??? */ ), int type, void *data)
262 {
263         List *ptr;
264         List *listend = NULL;
265         List *flatlist = NULL;
266 
267         for (ptr = list; ptr != NULL; ptr = ptr->next)
268         {
269                 List *newlist;
270                 int ptype = ptr->type;
271 
272                 newlist = reclist_flat(ptr->to, &ptype, func, type, data);
273                 if (newlist == NULL)
274                         continue;
275                 if (flatlist == NULL)
276                         flatlist = newlist;
277                 else
278                         listend->next = newlist;
279                 listend = list_get_end(newlist);
280         }
281         return (flatlist);
282 }
283 
284 List *reclist_string_flat(Tstring * str, void *(*func) ( /* ??? */ ), int type, void *data)
285 {
286         List *ptr;
287         List *end;
288         List *listend = NULL;
289         List *flatlist = NULL;
290 
291         if (str == NULL)
292                 return (NULL);
293 
294         end = str->end;
295 
296         for (ptr = str->start;; ptr = ptr->next)
297         {
298                 List *newlist;
299                 int ptype = ptr->type;
300 
301                 newlist = reclist_flat(ptr->to, &ptype, func, type, data);
302                 if (newlist != NULL)
303                 {
304                         if (flatlist == NULL)
305                                 flatlist = newlist;
306                         else
307                                 listend->next = newlist;
308                         listend = list_get_end(newlist);
309                 }
310                 if (ptr == end)
311                         break;
312         }
313         return (flatlist);
314 }
315 
316 List *reclist_flat(void *ptr, int *ptype, void *(*func) ( /* ??? */ ), int type, void *data)
317 /* ptr type */
318 /* test type */
319 {
320         switch (*ptype)
321         {
322                 case LIST:
323                         return (reclist_list_flat((List *) ptr, func, type, data));
324                 case STRING:
325                         return (reclist_string_flat((Tstring *) ptr, func, type, data));
326                 default:
327                         if ((type == 0) || (type == *ptype))
328                         {
329                                 ptr = (func == NULL) ? ptr : func(ptr, ptype, data);
330                                 if (ptr == NULL)
331                                         return (NULL);
332                                 return (link_alloc((void *) ptr, *ptype));
333                         }
334                         break;
335         }
336         return (NULL);
337 }
338 
339 /* copy recursive lists without updating their types */
340 
341 void *reclist_copy(void *ptr, int ptype, void *(*func) ( /* ??? */ ), int type, void *data);
342 
343 List *reclist_list_copy(List * list, void *(*func) ( /* ??? */ ), int type, void *data)
344 {
345         List *ptr;
346         List *newlist = NULL;
347 
348         for (ptr = list; ptr != NULL; ptr = ptr->next)
349         {
350                 void *copy;
351 
352                 copy = reclist_copy(ptr->to, ptr->type, func, type, data);
353                 if (copy != NULL)
354                         newlist = ref_addtostart(newlist, copy, ptr->type);
355         }
356         return (list_reverse(newlist));
357 }
358 
359 Tstring *reclist_string_copy(Tstring * str, void *(*func) ( /* ??? */ ), int type, void *data)
360 {
361         List *ptr;
362         List *end;
363         List *newlist = NULL;
364 
365         if (str == NULL)
366                 return (NULL);
367 
368         end = str->end;
369 
370         for (ptr = str->start;; ptr = ptr->next)
371         {
372                 void *copy;
373 
374                 copy = reclist_copy(ptr->to, ptr->type, func, type, data);
375 
376                 if (copy != NULL)
377                         newlist = dd_ref_addtostart(newlist, copy, ptr->type);
378                 if (ptr == end)
379                         break;
380         }
381         if (newlist == NULL)
382                 return (NULL);
383         newlist = dd_list_reverse(newlist);
384         return (str_make(str->type, newlist, dd_get_end(newlist)));
385 }
386 
387 void *reclist_copy(void *ptr, int ptype, void *(*func) ( /* ??? */ ), int type, void *data)
388 /* ptr type */
389 /* test type */
390 {
391         switch (ptype)
392         {
393                 case LIST:
394                         return ((void *) reclist_list_copy((List *) ptr, func, type, data));
395                 case STRING:
396                         return ((void *)
397                                                         reclist_string_copy((Tstring *) ptr, func, type, data));
398                 default:
399                         if ((type == 0) || (type == ptype))
400                                 return ((func == NULL) ? ptr : func(ptr, ptype, data));
401                         break;
402         }
403         return (NULL);
404 }
405 
406 /* Recursive list free */
407 List *reclist_list_free(List * list, void (*freefunc) ( /* ??? */ ), int type, void *data)
408 {
409         List *ptr;
410         List *next;
411         List *last = NULL;
412 
413         for (ptr = list; ptr != NULL; ptr = next)
414         {
415                 next = ptr->next;
416                 ptr->to = reclist_free(ptr->to, ptr->type, freefunc, type, data);
417                 if (ptr->to == NULL)
418                 {
419                         if (last == NULL)
420                                 list = link_rm_el(list);
421                         else
422                                 (void) link_rm_next_el(last);
423                 } else
424                         last = ptr;
425         }
426         return (list);
427 }
428 
429 Tstring *reclist_string_free(Tstring * str, void (*freefunc) ( /* ??? */ ), int type, void *data)
430 {
431         List *ptr;
432         List *start;
433         List *end;
434         List *next;
435         int more = 1;
436 
437         if (str == NULL)
438                 return (NULL);
439 
440         ptr = start = str->start;
441         end = str->end;
442 
443         do
444         {
445                 next = ptr->next;
446                 if (ptr == end)
447                         more = 0;
448                 ptr->to = reclist_free(ptr->to, ptr->type, freefunc, type, data);
449                 if (ptr->to == NULL)
450                 {
451                         if (ptr == end)
452                         {
453                                 if (ptr == start)
454                                         start = end = NULL;
455                                 else
456                                         end = end->last;
457                         }
458                         if (ptr == start)
459                                 start = dd_link_rm_el(start);
460                         else
461                                 ptr = dd_link_rm_el(ptr);
462                 }
463                 ptr = next;
464         }
465         while (more);
466 
467         if (start == NULL)
468         {
469                 str_rm_only_str(str);
470                 return (NULL);
471         }
472         str->start = start;
473         str->end = end;
474         str->count = ddstr_count(start, end);
475         return (str);
476 }
477 
478 void *reclist_free(void *ptr, int ptype, void (*freefunc) ( /* ??? */ ), int type, void *data)
479 /* ptr type */
480 /* test type */
481 {
482         switch (ptype)
483         {
484                 case LIST:
485                         return ((void *)
486                                                         reclist_list_free((List *) ptr, freefunc, type, data));
487                 case STRING:
488                         return ((void *)
489                                                         reclist_string_free((Tstring *) ptr, freefunc, type, data));
490                 default:
491                         if ((type != 0) && (type != ptype))
492                                 return (ptr);
493                         if (freefunc != NULL)
494                                 freefunc(ptr, ptype, data);
495                         return (NULL);
496         }
497 }
498 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.