diff options
author | Roger Dingledine <arma@torproject.org> | 2003-12-14 04:57:47 +0000 |
---|---|---|
committer | Roger Dingledine <arma@torproject.org> | 2003-12-14 04:57:47 +0000 |
commit | f4cb5d8c93d13976bdcdf9dbefc0f6e85b85212a (patch) | |
tree | 18a06d150273ec8c50545df95fe030e10a04325f /src | |
parent | 2997ef8dd9d0187d798bc69baf363b2dc8843fa0 (diff) | |
download | tor-f4cb5d8c93d13976bdcdf9dbefc0f6e85b85212a.tar tor-f4cb5d8c93d13976bdcdf9dbefc0f6e85b85212a.tar.gz |
extend smartlist with _remove() and _subtract()
svn:r922
Diffstat (limited to 'src')
-rw-r--r-- | src/common/util.c | 18 | ||||
-rw-r--r-- | src/common/util.h | 2 |
2 files changed, 20 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c index 0757474a3..a70f19170 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -92,6 +92,17 @@ void smartlist_add(smartlist_t *sl, void *element) { 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 @@ void smartlist_intersect(smartlist_t *sl1, smartlist_t *sl2) { } } +/* 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)]; diff --git a/src/common/util.h b/src/common/util.h index c6a22c13a..bfba0f131 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -48,9 +48,11 @@ typedef struct { 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); |