Module Name:    othersrc
Committed By:   dholland
Date:           Mon Mar  4 07:27:54 UTC 2013

Modified Files:
        othersrc/usr.bin/dholland-make2: array.c array.h

Log Message:
add a couple more array ops we look likely to use a lot


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 othersrc/usr.bin/dholland-make2/array.c
cvs rdiff -u -r1.3 -r1.4 othersrc/usr.bin/dholland-make2/array.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: othersrc/usr.bin/dholland-make2/array.c
diff -u othersrc/usr.bin/dholland-make2/array.c:1.1 othersrc/usr.bin/dholland-make2/array.c:1.2
--- othersrc/usr.bin/dholland-make2/array.c:1.1	Mon Mar  4 05:47:15 2013
+++ othersrc/usr.bin/dholland-make2/array.c	Mon Mar  4 07:27:54 2013
@@ -71,6 +71,32 @@ array_cleanup(struct array *a)
 }
 
 int
+array_contains(struct array *a, void *val)
+{
+	unsigned i;
+
+	for (i=0; i<a->num; i++) {
+		if (a->v[i] == val) {
+			return 1;
+		}
+	}
+	return 0;
+}
+
+unsigned
+array_find(struct array *a, void *val)
+{
+	unsigned i;
+
+	for (i=0; i<a->num; i++) {
+		if (a->v[i] == val) {
+			return i;
+		}
+	}
+	return ARRAY_NOTFOUND;
+}
+
+int
 array_setsize(struct array *a, unsigned num)
 {
 	unsigned newmax;
@@ -122,3 +148,14 @@ array_remove(struct array *a, unsigned i
 	memmove(a->v + index_, a->v + index_+1, movers*sizeof(*a->v));
 	a->num--;
 }
+
+void
+array_removeval(struct array *a, void *val)
+{
+	unsigned ix;
+
+	ix = array_find(a, val);
+	if (ix != ARRAY_NOTFOUND) {
+		array_remove(a, ix);
+	}
+}

Index: othersrc/usr.bin/dholland-make2/array.h
diff -u othersrc/usr.bin/dholland-make2/array.h:1.3 othersrc/usr.bin/dholland-make2/array.h:1.4
--- othersrc/usr.bin/dholland-make2/array.h:1.3	Mon Mar  4 06:48:03 2013
+++ othersrc/usr.bin/dholland-make2/array.h	Mon Mar  4 07:27:54 2013
@@ -58,10 +58,16 @@ void array_cleanup(struct array *);
 ARRAYINLINE unsigned array_num(const struct array *);
 ARRAYINLINE void *array_get(const struct array *, unsigned index_);
 ARRAYINLINE void array_set(const struct array *, unsigned index_, void *val);
+int array_contains(struct array *, void *val);
+unsigned array_find(struct array *, void *val);
 int array_setsize(struct array *, unsigned num);
 ARRAYINLINE int array_add(struct array *, void *val, unsigned *index_ret);
 int array_insert(struct array *a, unsigned index_);
 void array_remove(struct array *a, unsigned index_);
+void array_removeval(struct array *a, void *val);
+
+/* failure return for array_find() */
+#define ARRAY_NOTFOUND ((unsigned)-1)
 
 ////////////////////////////////////////////////////////////
 // inlining for base operations
@@ -173,10 +179,13 @@ array_add(struct array *a, void *val, un
 	INLINE unsigned ARRAY##_num(const struct ARRAY *a);		\
 	INLINE T *ARRAY##_get(const struct ARRAY *a, unsigned index_);	\
 	INLINE void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \
+	INLINE int ARRAY##_contains(struct ARRAY *a, T *val);		\
+	INLINE unsigned ARRAY##_find(struct ARRAY *a, T *val);		\
 	INLINE int ARRAY##_setsize(struct ARRAY *a, unsigned num);	\
 	INLINE int ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \
 	INLINE int ARRAY##_insert(struct ARRAY *a, unsigned index_);	\
-	INLINE void ARRAY##_remove(struct ARRAY *a, unsigned index_)
+	INLINE void ARRAY##_remove(struct ARRAY *a, unsigned index_);	\
+	INLINE void ARRAY##_removeval(struct ARRAY *a, T *val)
 
 
 #define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \
@@ -231,6 +240,18 @@ array_add(struct array *a, void *val, un
 	}							\
 								\
 	INLINE int						\
+	ARRAY##_contains(struct ARRAY *a, T *val)		\
+	{				 			\
+		return array_contains(&a->arr, val);		\
+	}							\
+								\
+	INLINE unsigned						\
+	ARRAY##_find(struct ARRAY *a, T *val)			\
+	{				 			\
+		return array_find(&a->arr, val);		\
+	}							\
+								\
+	INLINE int						\
 	ARRAY##_setsize(struct ARRAY *a, unsigned num)		\
 	{				 			\
 		return array_setsize(&a->arr, num);		\
@@ -252,6 +273,12 @@ array_add(struct array *a, void *val, un
 	ARRAY##_remove(struct ARRAY *a, unsigned index_)	\
 	{				 			\
 		return array_remove(&a->arr, index_);		\
+	}							\
+								\
+	INLINE void						\
+	ARRAY##_removeval(struct ARRAY *a, T *val)		\
+	{				 			\
+		return array_removeval(&a->arr, val);		\
 	}
 
 #define DECLARRAY(T, INLINE) DECLARRAY_BYTYPE(T##array, struct T, INLINE)

Reply via email to