Author: sveinung
Date: Tue Oct 18 14:10:30 2016
New Revision: 34165

URL: http://svn.gna.org/viewcvs/freeciv?rev=34165&view=rev
Log:
specvec: introduce foo_vector_remove().

See patch #7835

Modified:
    trunk/utility/specvec.h

Modified: trunk/utility/specvec.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/utility/specvec.h?rev=34165&r1=34164&r2=34165&view=diff
==============================================================================
--- trunk/utility/specvec.h     (original)
+++ trunk/utility/specvec.h     Tue Oct 18 14:10:30 2016
@@ -34,6 +34,7 @@
                 const struct foo_vector *from);
       void foo_vector_free(struct foo_vector *tthis);
       void foo_vector_append(struct foo_vector *tthis, foo_t pfoo);
+      void foo_vector_remove(struct foo_vector *tthis, int svindex);
 
    Note this is not protected against multiple inclusions; this is
    so that you can have multiple different specvectors.  For each
@@ -131,6 +132,32 @@
   tthis->p[tthis->size - 1] = pfoo;
 }
 
+/**************************************************************************
+  Remove element number svindex from the vector.
+**************************************************************************/
+static inline void SPECVEC_FOO(_vector_remove) (SPECVEC_VECTOR *tthis,
+                                                const int svindex)
+{
+  size_t i;
+
+  /* Be consistent with SPECVEC_FOO_vector_get(): Allow negative element
+   * number. */
+  const size_t rmpos = (svindex < 0 ?
+                          SPECVEC_FOO(_vector_size)(tthis) - svindex :
+                          (size_t)svindex);
+
+  fc_assert_ret(0 <= rmpos
+                && rmpos < SPECVEC_FOO(_vector_size)(tthis));
+
+  for (i = rmpos; (i + 1) < SPECVEC_FOO(_vector_size)(tthis); i++) {
+    /* Relocate the elements following the deleted element. */
+    tthis->p[i] = tthis->p[i + 1];
+  }
+
+  SPECVEC_FOO(_vector_reserve)(tthis,
+                               SPECVEC_FOO(_vector_size)(tthis) - 1);
+}
+
 
 
 #define TYPED_VECTOR_ITERATE(atype, vector, var) {      \


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to