diff options
author | Roger Dingledine <arma@torproject.org> | 2003-12-13 23:32:03 +0000 |
---|---|---|
committer | Roger Dingledine <arma@torproject.org> | 2003-12-13 23:32:03 +0000 |
commit | 4d3d99fa0c5215bbaec94ba51424cdcedda3417a (patch) | |
tree | 9a67deb5f65adc0beecea153db113ca3349cd055 /src/common/util.c | |
parent | 325935b1c63de1874d8b7d7669fe543e26441b4a (diff) | |
download | tor-4d3d99fa0c5215bbaec94ba51424cdcedda3417a.tar tor-4d3d99fa0c5215bbaec94ba51424cdcedda3417a.tar.gz |
extend smartlist with a few smarter operations
svn:r910
Diffstat (limited to 'src/common/util.c')
-rw-r--r-- | src/common/util.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c index dcce69bc1..d4708006f 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -92,6 +92,7 @@ void smartlist_add(smartlist_t *sl, void *element) { 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 @@ void smartlist_remove(smartlist_t *sl, void *element) { 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) |