[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] Hide smartlist internals
Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv9546/src/common
Modified Files:
util.c util.h
Log Message:
Hide smartlist internals
Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.75
retrieving revision 1.76
diff -u -d -r1.75 -r1.76
--- util.c 2 Apr 2004 23:30:53 -0000 1.75
+++ util.c 3 Apr 2004 00:58:53 -0000 1.76
@@ -129,6 +129,13 @@
* _choose() returns a random element.
*/
#define SMARTLIST_DEFAULT_CAPACITY 32
+
+struct smartlist_t {
+ void **list;
+ int num_used;
+ int capacity;
+};
+
smartlist_t *smartlist_create() {
smartlist_t *sl = tor_malloc(sizeof(smartlist_t));
sl->num_used = 0;
@@ -143,6 +150,8 @@
}
void smartlist_set_capacity(smartlist_t *sl, int n) {
+ if (n<0)
+ n = sl->num_used;
if (sl->capacity != n && sl->num_used < n) {
sl->capacity = n;
sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
@@ -208,6 +217,29 @@
return NULL; /* no elements to choose from */
}
+void *smartlist_get(smartlist_t *sl, int idx)
+{
+ return sl->list[idx];
+}
+void *smartlist_set(smartlist_t *sl, int idx, void *val)
+{
+ void *old;
+ old = sl->list[idx];
+ sl->list[idx] = val;
+ return old;
+}
+void *smartlist_del(smartlist_t *sl, int idx)
+{
+ void *old;
+ old = sl->list[idx];
+ sl->list[idx] = sl->list[--sl->num_used];
+ return old;
+}
+int smartlist_len(smartlist_t *sl)
+{
+ return sl->num_used;
+}
+
/*
* Splay-tree implementation of string-to-void* map
*/
Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.46
retrieving revision 1.47
diff -u -d -r1.46 -r1.47
--- util.h 2 Apr 2004 23:30:53 -0000 1.46
+++ util.h 3 Apr 2004 00:58:53 -0000 1.47
@@ -83,11 +83,7 @@
void hex_encode(const char *from, int fromlen, char *to);
-typedef struct smartlist_t {
- void **list;
- int num_used;
- int capacity;
-} smartlist_t;
+typedef struct smartlist_t smartlist_t;
smartlist_t *smartlist_create();
void smartlist_free(smartlist_t *sl);
@@ -99,6 +95,18 @@
void smartlist_intersect(smartlist_t *sl1, smartlist_t *sl2);
void smartlist_subtract(smartlist_t *sl1, smartlist_t *sl2);
void *smartlist_choose(smartlist_t *sl);
+void *smartlist_get(smartlist_t *sl, int idx);
+void *smartlist_set(smartlist_t *sl, int idx, void *val);
+void *smartlist_del(smartlist_t *sl, int idx);
+int smartlist_len(smartlist_t *sl);
+#define SMARTLIST_FOREACH(sl, type, var, cmd) \
+ do { \
+ int sl_idx, sl_len=smartlist_len(sl); \
+ type var; \
+ for(sl_idx = 0; sl_idx < sl_len; ++sl_idx) { \
+ var = smartlist_get((sl),sl_idx); \
+ do {cmd;} while(0); \
+ } } while (0)
/* Map from const char * to void*. Implemented with a splay tree. */
typedef struct strmap_t strmap_t;