[or-cvs] extend smartlist with a few smarter operations

Roger Dingledine arma at seul.org
Sat Dec 13 23:32:05 UTC 2003


Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/home2/arma/work/onion/cvs/src/common

Modified Files:
	util.c util.h 
Log Message:
extend smartlist with a few smarter operations


Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -d -r1.45 -r1.46
--- util.c	13 Dec 2003 06:56:21 -0000	1.45
+++ util.c	13 Dec 2003 23:32:03 -0000	1.46
@@ -92,6 +92,7 @@
     log_fn(LOG_WARN,"We've already got %d elements, discarding.",sl->max);
 }
 
+#if 0
 void smartlist_remove(smartlist_t *sl, void *element) {
   int i;
   if(element == NULL)
@@ -102,6 +103,33 @@
       i--; /* so we process the new i'th element */
     }
 }
+#endif
+
+int smartlist_isin(smartlist_t *sl, void *element) {
+  int i;
+  for(i=0; i < sl->num_used; i++)
+    if(sl->list[i] == element)
+      return 1;
+  return 0;
+}
+
+int smartlist_overlap(smartlist_t *sl1, smartlist_t *sl2) {
+  int i;
+  for(i=0; i < sl2->num_used; i++)
+    if(smartlist_isin(sl1, sl2->list[i]))
+      return 1;
+  return 0;
+}
+
+/* remove elements of sl1 that aren't in sl2 */
+void smartlist_intersect(smartlist_t *sl1, smartlist_t *sl2) {
+  int i;
+  for(i=0; i < sl1->num_used; i++)
+    if(!smartlist_isin(sl2, sl1->list[i])) {
+      sl1->list[i] = sl1->list[--sl1->num_used]; /* swap with the end */
+      i--; /* so we process the new i'th element */
+    }
+}
 
 void *smartlist_choose(smartlist_t *sl) {
   if(sl->num_used)

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- util.h	13 Dec 2003 01:42:44 -0000	1.25
+++ util.h	13 Dec 2003 23:32:03 -0000	1.26
@@ -48,7 +48,9 @@
 smartlist_t *smartlist_create(int max_elements);
 void smartlist_free(smartlist_t *sl);
 void smartlist_add(smartlist_t *sl, void *element);
-void smartlist_remove(smartlist_t *sl, void *element);
+int smartlist_isin(smartlist_t *sl, void *element);
+int smartlist_overlap(smartlist_t *sl1, smartlist_t *sl2);
+void smartlist_intersect(smartlist_t *sl1, smartlist_t *sl2);
 void *smartlist_choose(smartlist_t *sl);
 
 const char *eat_whitespace(const char *s);



More information about the tor-commits mailing list