Complete.Org: Mailing Lists: Archives: freeciv-dev: October 2003:
[Freeciv-Dev] Re: (PR#6411) genlist cleanup
Home

[Freeciv-Dev] Re: (PR#6411) genlist cleanup

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#6411) genlist cleanup
From: "Per I. Mathisen" <per@xxxxxxxxxxx>
Date: Thu, 9 Oct 2003 14:25:06 -0700
Reply-to: rt@xxxxxxxxxxxxxx

Here is a new version. Less lines of changes, since I reused the old
iterator macros like Jason wanted. Also fixed a serious bug.

  - Per

Index: common/genlist.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/genlist.c,v
retrieving revision 1.13
diff -u -r1.13 genlist.c
--- common/genlist.c    4 Apr 2003 15:47:49 -0000       1.13
+++ common/genlist.c    9 Oct 2003 21:24:15 -0000
@@ -169,19 +169,6 @@
   pgenlist->nelements++;
 }
 
-/************************************************************************
-  Initialize a genlist_iterator, for specified genlist and position
-  of initial element.  If pos is out of range the link will be null_link
-  (which will generally be interpreted as the iterator being finished).
-  Recall 'pos' can be -1 meaning the last element.
-************************************************************************/
-void genlist_iterator_init(struct genlist_iterator *iter,
-                               struct genlist *pgenlist, int pos)
-{
-  iter->list=pgenlist;
-  iter->link=find_genlist_position(pgenlist, pos);
-}
-
 
 /************************************************************************
   Returns a pointer to the genlist link structure at the specified
@@ -232,7 +219,7 @@
   static void **sortbuf = 0;
   static int n_alloc = 0;
   
-  struct genlist_iterator myiter;
+  struct genlist_link *myiter;
   int i, n;
 
   if(compar==NULL) {
@@ -250,15 +237,15 @@
     n_alloc = n+10;
     sortbuf = (void **)fc_realloc(sortbuf, n_alloc*sizeof(void*));
   }
-  
-  genlist_iterator_init(&myiter, pgenlist, 0);
+
+  myiter = find_genlist_position(pgenlist, 0);  
   for(i=0; i<n; i++, ITERATOR_NEXT(myiter)) {
     sortbuf[i] = ITERATOR_PTR(myiter);
   }
   
   qsort(sortbuf, n, sizeof(void*), compar);
   
-  genlist_iterator_init(&myiter, pgenlist, 0);
+  myiter = find_genlist_position(pgenlist, 0);  
   for(i=0; i<n; i++, ITERATOR_NEXT(myiter)) {
      ITERATOR_PTR(myiter) = sortbuf[i];
   }
Index: common/genlist.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/genlist.h,v
retrieving revision 1.10
diff -u -r1.10 genlist.h
--- common/genlist.h    18 Dec 2002 17:36:19 -0000      1.10
+++ common/genlist.h    9 Oct 2003 21:24:15 -0000
@@ -42,11 +42,9 @@
     it is the responsibility of the user to call the unlink functions
     as necessary to avoid memory leaks.
 
-  This module also contains a "genlist_iterator" type and associated
-  macros to assist in traversing genlists.  A trap to beware of with
-  iterators is modifying the list while the iterator is active, in
-  particular removing the next element pointed to by the iterator
-  (see further comments below).
+  A trap to beware of with iterators is modifying the list while the 
+  iterator is active, in particular removing the next element pointed 
+  to by the iterator (see further comments below).
 
   See also the speclist module.
 ***********************************************************************/
@@ -76,25 +74,6 @@
   struct genlist_link *tail_link;
 };
 
-
-/* Used to iterate genlists: a pointer to the list we are iterating
-   (although I don't think this is ever used?), and a pointer to the
-   current position.  You can use the ITERATOR_* macros below to
-   access the data of the current link and traverse the list.
-   Be very careful if the genlist is modified while the iteration
-   is in progress.  The standard macros (based on TYPED_LIST_ITERATE
-   below) allow removing the "current" element, as the iterator
-   is advances at the start of the loop, but if the "next" element
-   is removed (including as a side effect of removing the current
-   element), expect core dumps.  See server/unitfunc.c:wipe_unit_safe()
-   as an example of protecting against this.
-*/
-struct genlist_iterator {
-  struct genlist *list;
-  struct genlist_link *link;
-};
-
-
 int genlist_size(struct genlist *pgenlist);
 void *genlist_get(struct genlist *pgenlist, int idx);
 void genlist_init(struct genlist *pgenlist);
@@ -105,12 +84,9 @@
 void genlist_sort(struct genlist *pgenlist,
                  int (*compar)(const void *, const void *));
 
-void genlist_iterator_init(struct genlist_iterator *iter,
-                               struct genlist *pgenlist, int pos);
-
-#define ITERATOR_PTR(X)  ((X).link->dataptr)
-#define ITERATOR_NEXT(X) ((X).link=(X).link->next)
-#define ITERATOR_PREV(X) ((X).link=(X).link->prev)
+#define ITERATOR_PTR(iter) ((iter)->dataptr)
+#define ITERATOR_NEXT(iter) (iter = (iter)->next)
+#define ITERATOR_PREV(iter) (iter = (iter)->prev)
 
 
 /* This is to iterate for a type defined like:
@@ -119,9 +95,8 @@
    Eg, see speclist.h, which is what this is really for.
 */
 #define TYPED_LIST_ITERATE(atype, typed_list, var) {       \
-  struct genlist_iterator myiter;                          \
+  struct genlist_link *myiter = (typed_list).list.head_link;\
   atype *var;                                              \
-  genlist_iterator_init(&myiter, &(typed_list).list, 0);   \
   for(; ITERATOR_PTR(myiter);) {                           \
     var=(atype *)ITERATOR_PTR(myiter);                     \
     ITERATOR_NEXT(myiter);
@@ -132,9 +107,8 @@
 
 /* Same, but iterate backwards: */
 #define TYPED_LIST_ITERATE_REV(atype, typed_list, var) {   \
-  struct genlist_iterator myiter;                          \
+  struct genlist_link *myiter = (typed_list).list.tail_link;\
   atype *var;                                              \
-  genlist_iterator_init(&myiter, &(typed_list).list, -1);  \
   for(; ITERATOR_PTR(myiter);) {                           \
     var=(atype *)ITERATOR_PTR(myiter);                     \
     ITERATOR_PREV(myiter);
Index: common/registry.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/registry.c,v
retrieving revision 1.59
diff -u -r1.59 registry.c
--- common/registry.c   4 Apr 2003 15:47:49 -0000       1.59
+++ common/registry.c   9 Oct 2003 21:24:16 -0000
@@ -619,7 +619,7 @@
 {
   fz_FILE *fs = fz_from_file(filename, "w", FZ_ZLIB, compression_level);
 
-  struct genlist_iterator ent_iter, save_iter, col_iter;
+  struct genlist_link *ent_iter, *save_iter, *col_iter;
   struct entry *pentry, *col_pentry;
 
   if (!fs)
@@ -631,7 +631,7 @@
     /* Following doesn't use entry_list_iterate() because we want to do
      * tricky things with the iterators...
      */
-    for(genlist_iterator_init(&ent_iter, &psection->entries.list, 0);
+    for(ent_iter = psection->entries.list.head_link;
        (pentry = ITERATOR_PTR(ent_iter));
        ITERATOR_NEXT(ent_iter)) {
 

[Prev in Thread] Current Thread [Next in Thread]