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

[Freeciv-Dev] (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] (PR#6411) genlist cleanup
From: "Per I. Mathisen" <per@xxxxxxxxxxx>
Date: Mon, 6 Oct 2003 08:23:17 -0700
Reply-to: rt@xxxxxxxxxxxxxx

This patch removes the now unnecessary genlist iterator struct and
associated initializer function. This makes the genlist faster, since
starting an iterator no longer involves any function calls, and the
genlist codes become cleaner.

Requires PR#6408 and PR#6410.

  - 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    6 Oct 2003 15:19:42 -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 *mylink;
   int i, n;
 
   if(compar==NULL) {
@@ -251,15 +238,15 @@
     sortbuf = (void **)fc_realloc(sortbuf, n_alloc*sizeof(void*));
   }
   
-  genlist_iterator_init(&myiter, pgenlist, 0);
-  for(i=0; i<n; i++, ITERATOR_NEXT(myiter)) {
-    sortbuf[i] = ITERATOR_PTR(myiter);
+  mylink = find_genlist_position(pgenlist, 0);
+  for(i = 0; i < n; i++, mylink = mylink->next) {
+    sortbuf[i] = mylink->dataptr;
   }
   
   qsort(sortbuf, n, sizeof(void*), compar);
-  
-  genlist_iterator_init(&myiter, pgenlist, 0);
-  for(i=0; i<n; i++, ITERATOR_NEXT(myiter)) {
-     ITERATOR_PTR(myiter) = sortbuf[i];
+
+  mylink = find_genlist_position(pgenlist, 0);
+  for(i = 0; i < n; i++, mylink = mylink->next) {
+     mylink->dataptr = 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    6 Oct 2003 15:19:43 -0000
@@ -76,25 +76,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,39 +86,29 @@
 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)
-
-
 /* This is to iterate for a type defined like:
      struct unit_list { struct genlist list; };
    where the pointers in the list are really pointers to "atype".
    Eg, see speclist.h, which is what this is really for.
 */
-#define TYPED_LIST_ITERATE(atype, typed_list, var) {       \
-  struct genlist_iterator myiter;                          \
-  atype *var;                                              \
-  genlist_iterator_init(&myiter, &(typed_list).list, 0);   \
-  for(; ITERATOR_PTR(myiter);) {                           \
-    var=(atype *)ITERATOR_PTR(myiter);                     \
-    ITERATOR_NEXT(myiter);
+#define TYPED_LIST_ITERATE(atype, typed_list, var) {              \
+  struct genlist_link *link = (typed_list).list.head_link;        \
+  atype *var;                                                     \
+  while (link->dataptr) {                                         \
+    var = (atype *)link->dataptr;                                 \
+    link = link->next;
 
 /* Balance for above: */ 
 #define LIST_ITERATE_END  }}
 
 
 /* Same, but iterate backwards: */
-#define TYPED_LIST_ITERATE_REV(atype, typed_list, var) {   \
-  struct genlist_iterator myiter;                          \
-  atype *var;                                              \
-  genlist_iterator_init(&myiter, &(typed_list).list, -1);  \
-  for(; ITERATOR_PTR(myiter);) {                           \
-    var=(atype *)ITERATOR_PTR(myiter);                     \
-    ITERATOR_PREV(myiter);
+#define TYPED_LIST_ITERATE_REV(atype, typed_list, var) {           \
+  struct genlist_link *link = (typed_list).list.tail_link;         \
+  atype *var;                                                      \
+  while (link->dataptr) {                                          \
+    var = (atype *)link->dataptr;                                  \
+    link = link->next;
  
 /* Balance for above: */ 
 #define LIST_ITERATE_REV_END  }}
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   6 Oct 2003 15:19:44 -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_link, *save_link, *col_link;
   struct entry *pentry, *col_pentry;
 
   if (!fs)
@@ -631,9 +631,8 @@
     /* 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);
-       (pentry = ITERATOR_PTR(ent_iter));
-       ITERATOR_NEXT(ent_iter)) {
+    for(ent_link = genlist_get(&psection->entries.list, 0);
+       (pentry = ent_link->dataptr); ent_link = ent_link->next) {
 
       /* Tables: break out of this loop if this is a non-table
        * entry (pentry and ent_iter unchanged) or after table (pentry
@@ -672,12 +671,12 @@
        /* Save an iterator at this first entry, which we can later use
         * to repeatedly iterate over column names:
         */
-       save_iter = ent_iter;
+       save_link = ent_link;
 
        /* write the column names, and calculate ncol: */
        ncol = 0;
-       col_iter = save_iter;
-       for( ; (col_pentry = ITERATOR_PTR(col_iter)); ITERATOR_NEXT(col_iter)) {
+       col_link = save_link;
+       for( ; (col_pentry = col_link->dataptr); col_link = col_link->next) {
          if(strncmp(col_pentry->name, first, offset) != 0)
            break;
          fz_fprintf(fs, "%c\"%s\"", (ncol==0?' ':','), 
col_pentry->name+offset);
@@ -685,17 +684,17 @@
        }
        fz_fprintf(fs, "\n");
 
-       /* Iterate over rows and columns, incrementing ent_iter as we go,
+       /* Iterate over rows and columns, incrementing ent_link as we go,
         * and writing values to the table.  Have a separate iterator
         * to the column names to check they all match.
         */
        irow = icol = 0;
-       col_iter = save_iter;
+       col_link = save_link;
        for(;;) {
          char expect[128];     /* pentry->name we're expecting */
 
-         pentry = ITERATOR_PTR(ent_iter);
-         col_pentry = ITERATOR_PTR(col_iter);
+         pentry = ent_link->dataptr;
+         col_pentry = col_link->dataptr;
 
          my_snprintf(expect, sizeof(expect), "%s%d.%s",
                      base, irow, col_pentry->name+offset);
@@ -718,15 +717,15 @@
          else
            fz_fprintf(fs, "%d", pentry->ivalue);
          
-         ITERATOR_NEXT(ent_iter);
-         ITERATOR_NEXT(col_iter);
+         ent_link = ent_link->next;
+         col_link = col_link->next;
          
          icol++;
          if(icol==ncol) {
            fz_fprintf(fs, "\n");
            irow++;
            icol = 0;
-           col_iter = save_iter;
+           col_link = save_link;
          }
        }
        if(!pentry) break;

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