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: Fri, 24 Oct 2003 04:33:50 -0700
Reply-to: rt@xxxxxxxxxxxxxx

On Wed, 22 Oct 2003, Jason Short wrote:
> >>But gui-gtk at least doesn't compile with this; citydlg.c still uses
> >>genlist_iterator.

See attached patch. This approach is faster than the gtk2 solution, but
perhaps not as pretty (gtk2 does a unit_list_get() on each unit, which is
a genlist search function).

If this approach is fine, I'll do the same for the other clients that
have this code.

> > Does anybody have a list of code places where we use the genlist
> > directly i.e. without the iterate? Can these places be converted?

client/control.c uses it in process_diplomat_arrival and
process_caravan_arrival, and client/gui-gtk/chatline.c in history_list. It
may be possible to convert, but I am not sure it is worth the effort.

  - Per

Index: client/gui-gtk/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/citydlg.c,v
retrieving revision 1.172
diff -u -r1.172 citydlg.c
--- client/gui-gtk/citydlg.c    6 Aug 2003 07:22:44 -0000       1.172
+++ client/gui-gtk/citydlg.c    24 Oct 2003 11:30:16 -0000
@@ -2002,10 +2002,8 @@
 *****************************************************************/
 static void city_dialog_update_supported_units(struct city_dialog *pdialog)
 {
-  int i;
+  int i, j;
   struct unit_list *plist;
-  struct genlist_iterator myiter;
-  struct unit *punit;
   int size, mini_size;
   char buf[30];
 
@@ -2040,13 +2038,15 @@
 
   /* mini */
 
-  genlist_iterator_init(&myiter, &(plist->list),
-                       pdialog->overview.supported_unit_pos *
-                       MINI_NUM_UNITS);
-
-  for (i = 0; i < MINI_NUM_UNITS && ITERATOR_PTR(myiter);
-       ITERATOR_NEXT(myiter), i++) {
-    punit = (struct unit *) ITERATOR_PTR(myiter);
+  i = 0; /* number of displayed units */
+  j = 0; /* index into list */
+  unit_list_iterate(*plist, punit) {
+    if (j++ < pdialog->overview.supported_unit_pos * MINI_NUM_UNITS) {
+      continue;
+    }
+    if (i >= MINI_NUM_UNITS) {
+      break;
+    }
 
     gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->
                                  overview.supported_unit_pixmaps[i]),
@@ -2080,8 +2080,10 @@
                       GINT_TO_POINTER(punit->id));
     gtk_widget_set_sensitive(pdialog->overview.supported_unit_boxes[i],
                             TRUE);
-  }
+    i++;
+  } unit_list_iterate_end;
 
+  /* Disable any empty slots */
   for (; i < MINI_NUM_UNITS; i++) {
     gtk_pixcomm_clear(GTK_PIXCOMM
                      (pdialog->overview.supported_unit_pixmaps[i]), TRUE);
@@ -2092,13 +2094,15 @@
 
   /* normal */
 
-  genlist_iterator_init(&myiter, &(plist->list),
-                       pdialog->unit.supported_unit_pos *
-                       NUM_UNITS_SHOWN);
-
-  for (i = 0; i < NUM_UNITS_SHOWN && ITERATOR_PTR(myiter);
-       ITERATOR_NEXT(myiter), i++) {
-    punit = (struct unit *) ITERATOR_PTR(myiter);
+  i = 0; /* number of displayed units */
+  j = 0; /* index into list */
+  unit_list_iterate(*plist, punit) {
+    if (j++ < pdialog->overview.supported_unit_pos * MINI_NUM_UNITS) {
+      continue;
+    }
+    if (i >= MINI_NUM_UNITS) {
+      break;
+    }
 
     gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->unit.supported_unit_pixmaps[i]),
                      FALSE);
@@ -2126,7 +2130,8 @@
                       GTK_SIGNAL_FUNC(supported_unit_middle_callback),
                       GINT_TO_POINTER(punit->id));
     gtk_widget_set_sensitive(pdialog->unit.supported_unit_boxes[i], TRUE);
-  }
+    i++;
+  } unit_list_iterate_end;
 
   for (; i < NUM_UNITS_SHOWN; i++) {
     gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->unit.supported_unit_pixmaps[i]),
@@ -2147,10 +2152,8 @@
 *****************************************************************/
 static void city_dialog_update_present_units(struct city_dialog *pdialog)
 {
-  int i;
+  int i, j;
   struct unit_list *plist;
-  struct genlist_iterator myiter;
-  struct unit *punit;
   int size, mini_size;
   char buf[30];
 
@@ -2184,13 +2187,15 @@
 
   /* mini */
 
-  genlist_iterator_init(&myiter, &(plist->list),
-                       pdialog->overview.present_unit_pos *
-                       MINI_NUM_UNITS);
-
-  for (i = 0; i < MINI_NUM_UNITS && ITERATOR_PTR(myiter);
-       ITERATOR_NEXT(myiter), i++) {
-    punit = (struct unit *) ITERATOR_PTR(myiter);
+  i = 0; /* number of displayed units */
+  j = 0; /* index into list */
+  unit_list_iterate(*plist, punit) {
+    if (j++ < pdialog->overview.supported_unit_pos * MINI_NUM_UNITS) {
+      continue;
+    }
+    if (i >= MINI_NUM_UNITS) {
+      break;
+    }
 
     gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->overview.present_unit_pixmaps[i]),
                      FALSE);
@@ -2218,7 +2223,9 @@
                       GINT_TO_POINTER(punit->id));
     gtk_widget_set_sensitive(pdialog->overview.present_unit_boxes[i],
                             TRUE);
-  }
+    i++;
+  } unit_list_iterate_end;
+
   for (; i < MINI_NUM_UNITS; i++) {
     gtk_pixcomm_clear(GTK_PIXCOMM
                      (pdialog->overview.present_unit_pixmaps[i]), TRUE);
@@ -2229,13 +2236,15 @@
 
   /* normal */
 
-
-  genlist_iterator_init(&myiter, &(plist->list),
-                       pdialog->unit.present_unit_pos * NUM_UNITS_SHOWN);
-
-  for (i = 0; i < NUM_UNITS_SHOWN && ITERATOR_PTR(myiter);
-       ITERATOR_NEXT(myiter), i++) {
-    punit = (struct unit *) ITERATOR_PTR(myiter);
+  i = 0; /* number of displayed units */
+  j = 0; /* index into list */
+  unit_list_iterate(*plist, punit) {
+    if (j++ < pdialog->overview.supported_unit_pos * MINI_NUM_UNITS) {
+      continue;
+    }
+    if (i >= MINI_NUM_UNITS) {
+      break;
+    }
 
     gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->unit.present_unit_pixmaps[i]),
                      FALSE);
@@ -2259,7 +2268,9 @@
                       GTK_SIGNAL_FUNC(present_unit_middle_callback),
                       GINT_TO_POINTER(punit->id));
     gtk_widget_set_sensitive(pdialog->unit.present_unit_boxes[i], TRUE);
-  }
+    i++;
+  } unit_list_iterate_end;
+
   for (; i < NUM_UNITS_SHOWN; i++) {
     gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->unit.present_unit_pixmaps[i]),
                      TRUE);

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