2
0
Troy D. Hanson 10 жил өмнө
parent
commit
2ea2301645

+ 4 - 3
opt/bundle/include/utvector.h

@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2003-2014, Troy D. Hanson     http://troydhanson.github.com/uthash/
+Copyright (c) 2003-2015, Troy D. Hanson     http://troydhanson.github.com/uthash/
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -54,8 +54,8 @@ typedef struct _UT_vector {
 } UT_vector;
 
 
-UT_vector *utvector_new(UT_vector_mm *mm);
-void utvector_init(UT_vector *v, UT_vector_mm *mm);
+UT_vector *utvector_new(const UT_vector_mm *mm);
+void utvector_init(UT_vector *v, const UT_vector_mm *mm);
 void utvector_reserve(UT_vector *v, unsigned num);
 void utvector_fini(UT_vector *v);
 UT_vector * utvector_clone(UT_vector *src);
@@ -67,6 +67,7 @@ void *utvector_head(UT_vector *v);
 void *utvector_tail(UT_vector *v);
 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_push(UT_vector *v, void *e);
 unsigned utvector_len(UT_vector *v);

+ 8 - 3
opt/bundle/src/utvector.c

@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2003-2014, Troy D. Hanson     http://troydhanson.github.com/uthash/
+Copyright (c) 2003-2015, Troy D. Hanson     http://troydhanson.github.com/uthash/
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -42,7 +42,7 @@ void oom(void) {
   exit(-1);
 }
 
-UT_vector *utvector_new(UT_vector_mm *mm) {
+UT_vector *utvector_new(const UT_vector_mm *mm) {
   UT_vector *v = malloc(sizeof(UT_vector)); if (!v) return NULL;
   utvector_init(v,mm);
   return v;
@@ -52,7 +52,7 @@ unsigned utvector_len(UT_vector *v) {
   return v->i;
 }
 
-void utvector_init(UT_vector *v, UT_vector_mm *mm) {
+void utvector_init(UT_vector *v, const UT_vector_mm *mm) {
   v->mm = *mm;  // struct copy
   v->i = v->n = 0;
   v->d = NULL;
@@ -135,6 +135,11 @@ void *utvector_pop(UT_vector *v) {
   return v->d + (--(v->i) * v->mm.sz);
 }
 
+void *utvector_elt(UT_vector *v, unsigned i) {
+  if (i >= v->i) return NULL;
+  return v->d + (i * v->mm.sz);
+}
+
 /* shifting is not very efficient. we end up throwing away/fini'ing the
  * head of the vector, then doing a memmove, then having to init a new slot.
  * we don't return the shifted item because its been fini'd, and we have

+ 1 - 1
opt/bundle/tests/Makefile

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

+ 1 - 0
opt/bundle/tests/README

@@ -13,3 +13,4 @@ test12: test utvector head/tail (int)
 test13: test utvector head/tail (utstring)
 test14: test utvector_shift (int)
 test15: test utvector_shift (utstring)
+test16: test utvector_elt

+ 21 - 0
opt/bundle/tests/test16.ans

@@ -0,0 +1,21 @@
+0
+1
+2
+3
+4
+5
+6
+7
+8
+9
+elt 0: 0
+elt 1: 1
+elt 2: 2
+elt 3: 3
+elt 4: 4
+elt 5: 5
+elt 6: 6
+elt 7: 7
+elt 8: 8
+elt 9: 9
+p is null

+ 25 - 0
opt/bundle/tests/test16.c

@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include "utvector.h"
+
+int main() {
+  int i,*p=NULL;
+  UT_vector v,*k;
+  utvector_init(&v, utvector_int);
+  for(i=0; i<10; i++) utvector_push(&v, &i);
+
+  k = utvector_clone(&v);
+  while ( (p=(int*)utvector_next(k,p))) printf("%d\n",*p);
+
+  for(i=0; i<10; i++) {
+    p = (int*)utvector_elt(&v, i);
+    printf("elt %d: %d\n", i, *p);
+  }
+
+  /* expect null */
+  p = (int*)utvector_elt(&v, 11);
+  printf("p is %snull\n", p ? "not " : "");
+
+  utvector_fini(&v);
+  utvector_free(k);
+  return 0;
+}