Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2004:
[Freeciv-Dev] (PR#8683) specvec patch v3
Home

[Freeciv-Dev] (PR#8683) specvec patch v3

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: jdorje@xxxxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] (PR#8683) specvec patch v3
From: "Vasco Alexandre da Silva Costa" <vasc@xxxxxxxxxxxxxx>
Date: Fri, 14 May 2004 19:51:56 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=8683 >

> [vasc - Sat May 15 02:22:16 2004]:
> 
> > [vasc - Sat May 15 02:11:49 2004]:
> > 
> > An iterator macro patch for specvec.
> 
> On second thought, it is better to pass a pointer to the element. This
> way you can do a write to the current element as well and is faster than
> doing a copy for vectors with large element types (i.e. structs).

New insert_back() method and make get() retrieve the last element when
-1 is passed just like the genlists do. Hopefully more to come tomorrow
after I get some sleep.

Index: utility/specvec.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/specvec.h,v
retrieving revision 1.5
diff -u -r1.5 specvec.h
--- utility/specvec.h   15 May 2004 00:48:17 -0000      1.5
+++ utility/specvec.h   15 May 2004 02:49:29 -0000
@@ -106,11 +106,18 @@
 static inline SPECVEC_TYPE *SPECVEC_FOO(_vector_get) (SPECVEC_VECTOR *tthis,
                                                      int index)
 {
-  if (index < 0 || index >= tthis->size) {
-    assert(index >= 0 && index < tthis->size);
-    return NULL;
+  if (index == -1) {
+    if (tthis->size > 0) {
+      return tthis->p + tthis->size - 1;
+    } else {
+      return NULL;
+    }
   } else {
-    return tthis->p + index;
+    if (index < 0 || index >= tthis->size) {
+      return NULL;
+    } else {
+      return tthis->p + index;
+    }
   }
 }
 
@@ -130,6 +137,27 @@
   SPECVEC_FOO(_vector_init)(tthis);
 }
 
+static inline void SPECVEC_FOO(_vector_insert_back) (SPECVEC_VECTOR *tthis,
+                                                    SPECVEC_TYPE *pfoo)
+{
+  const size_t last = tthis->size;
+
+  SPECVEC_FOO(_vector_reserve) (tthis, last + 1);
+  tthis->p[last] = *pfoo;
+}
+
+
+
+#define TYPED_VECTOR_ITERATE(atype, vector, var) {      \
+  int myiter;                                          \
+  atype *var;                                           \
+  for (myiter = 0; myiter < (vector)->size; myiter++) { \
+    var = &(vector)->p[myiter];                                \
+ 
+/* Balance for above: */
+#define VECTOR_ITERATE_END  }}
+
+
 #undef SPECVEC_TAG
 #undef SPECVEC_TYPE
 #undef SPECVEC_PASTE_

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