[or-cvs] extend smartlist with _remove() and _subtract()
Roger Dingledine
arma at seul.org
Sun Dec 14 04:57:49 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 _remove() and _subtract()
Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.47
retrieving revision 1.48
diff -u -d -r1.47 -r1.48
--- util.c 14 Dec 2003 00:11:48 -0000 1.47
+++ util.c 14 Dec 2003 04:57:47 -0000 1.48
@@ -92,6 +92,17 @@
log_fn(LOG_WARN,"We've already got %d elements, discarding.",sl->max);
}
+void smartlist_remove(smartlist_t *sl, void *element) {
+ int i;
+ if(element == NULL)
+ return;
+ for(i=0; i < sl->num_used; i++)
+ if(sl->list[i] == element) {
+ sl->list[i] = sl->list[--sl->num_used]; /* swap with the end */
+ i--; /* so we process the new i'th element */
+ }
+}
+
int smartlist_isin(smartlist_t *sl, void *element) {
int i;
for(i=0; i < sl->num_used; i++)
@@ -118,6 +129,13 @@
}
}
+/* remove all elements of sl2 from sl1 */
+void smartlist_subtract(smartlist_t *sl1, smartlist *sl2) {
+ int i;
+ for(i=0; i < sl2->num_used; i++)
+ smartlist_remove(sl1, sl2->list[i]);
+}
+
void *smartlist_choose(smartlist_t *sl) {
if(sl->num_used)
return sl->list[crypto_pseudo_rand_int(sl->num_used)];
Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -d -r1.26 -r1.27
--- util.h 13 Dec 2003 23:32:03 -0000 1.26
+++ util.h 14 Dec 2003 04:57:47 -0000 1.27
@@ -48,9 +48,11 @@
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_subtract(smartlist_t *sl1, smartlist *sl2);
void *smartlist_choose(smartlist_t *sl);
const char *eat_whitespace(const char *s);
More information about the tor-commits
mailing list