Parcourir la source

utvector_erase

Troy D. Hanson il y a 9 ans
Parent
commit
dd57602919

+ 12 - 0
libut/src/utvector.c

@@ -153,6 +153,18 @@ void utvector_shift(UT_vector *v) {
   utmm_init(&v->mm,b,1);
 }
 
+void utvector_erase(UT_vector *v, unsigned i) {
+  assert(v->i);
+  if (i >= v->i) return;
+  utmm_fini(&v->mm, v->d + (i * v->mm.sz), 1);
+  v->i--;
+  if (v->n - 1 > i) {
+    memmove(v->d + (i * v->mm.sz), v->d + ((i+1) * v->mm.sz), (((v->n-1) - i) * v->mm.sz));
+  }
+  char *b = v->d + ((v->n-1) * v->mm.sz);
+  utmm_init(&v->mm,b,1);
+}
+
 void *utvector_push(UT_vector *v, void *e) {
   void *b  = utvector_extend(v);
   utmm_copy(&v->mm, b, e, 1);

+ 1 - 1
libut/tests/Makefile

@@ -1,5 +1,5 @@
 PROGS=test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test11 test12 \
-      test13 test14 test15 test16
+      test13 test14 test15 test16 test17 test18 test19
 OBJS=$(patsubst %,%.o,$(PROGS))
 
 CFLAGS += -I../include

+ 3 - 0
libut/tests/README

@@ -14,3 +14,6 @@ test13: test utvector head/tail (utstring)
 test14: test utvector_shift (int)
 test15: test utvector_shift (utstring)
 test16: test utvector_elt
+test17: test utvector_erase
+test18: test utvector_erase
+test19: test utvector_erase

+ 27 - 0
libut/tests/test17.ans

@@ -0,0 +1,27 @@
+.
+..
+...
+....
+.....
+......
+.......
+........
+.........
+..........
+..
+...
+....
+.....
+......
+.......
+........
+.........
+..........
+..
+....
+.....
+......
+.......
+........
+.........
+..........

+ 27 - 0
libut/tests/test17.c

@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include "libut.h"
+
+int main() {
+  int i; UT_string *p;
+  UT_vector v; utvector_init(&v, utstring_mm);
+  UT_string s; utstring_init(&s);
+  for(i=0; i<10; i++) {
+    utstring_printf(&s, ".");
+    utvector_push(&v, &s);
+  }
+  p=NULL;
+  while ( (p=(UT_string*)utvector_next(&v,p))) printf("%s\n",utstring_body(p));
+
+  utvector_erase(&v, 0);
+  p=NULL;
+  while ( (p=(UT_string*)utvector_next(&v,p))) printf("%s\n",utstring_body(p));
+
+  utvector_erase(&v, 1);
+  p=NULL;
+  while ( (p=(UT_string*)utvector_next(&v,p))) printf("%s\n",utstring_body(p));
+
+
+  utvector_fini(&v);
+  utstring_done(&s);
+  return 0;
+}

+ 28 - 0
libut/tests/test18.ans

@@ -0,0 +1,28 @@
+.
+..
+...
+....
+.....
+......
+.......
+........
+.........
+..........
+.
+..
+...
+....
+.....
+......
+.......
+........
+.........
+.
+..
+...
+....
+.....
+......
+.......
+........
+.........

+ 27 - 0
libut/tests/test18.c

@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include "libut.h"
+
+int main() {
+  int i; UT_string *p;
+  UT_vector v; utvector_init(&v, utstring_mm);
+  UT_string s; utstring_init(&s);
+  for(i=0; i<10; i++) {
+    utstring_printf(&s, ".");
+    utvector_push(&v, &s);
+  }
+  p=NULL;
+  while ( (p=(UT_string*)utvector_next(&v,p))) printf("%s\n",utstring_body(p));
+
+  utvector_erase(&v, 9);
+  p=NULL;
+  while ( (p=(UT_string*)utvector_next(&v,p))) printf("%s\n",utstring_body(p));
+
+  utvector_erase(&v, 9);
+  p=NULL;
+  while ( (p=(UT_string*)utvector_next(&v,p))) printf("%s\n",utstring_body(p));
+
+
+  utvector_fini(&v);
+  utstring_done(&s);
+  return 0;
+}

+ 1 - 0
libut/tests/test19.ans

@@ -0,0 +1 @@
+.

+ 22 - 0
libut/tests/test19.c

@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include "libut.h"
+
+int main() {
+  int i; UT_string *p;
+  UT_vector v; utvector_init(&v, utstring_mm);
+  UT_string s; utstring_init(&s);
+  for(i=0; i<1; i++) {
+    utstring_printf(&s, ".");
+    utvector_push(&v, &s);
+  }
+  p=NULL;
+  while ( (p=(UT_string*)utvector_next(&v,p))) printf("%s\n",utstring_body(p));
+
+  utvector_erase(&v, 0);
+  p=NULL;
+  while ( (p=(UT_string*)utvector_next(&v,p))) printf("%s\n",utstring_body(p));
+
+  utvector_fini(&v);
+  utstring_done(&s);
+  return 0;
+}

+ 1 - 0
src/utvector.h

@@ -68,6 +68,7 @@ void *utvector_next(UT_vector *v, void *cur);
 void *utvector_pop(UT_vector *v);
 void *utvector_elt(UT_vector *v, unsigned i);
 void utvector_shift(UT_vector *v);
+void utvector_erase(UT_vector *v, unsigned i);
 void *utvector_push(UT_vector *v, void *e);
 unsigned utvector_len(UT_vector *v);