1 /* Copyright 2003-2004 Roger Dingledine
2 Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
3 /* See LICENSE for licensing information */
5 const char container_c_id
[] = "$Id$";
9 * \brief Implements a smartlist (a resizable array) along
10 * with helper functions to use smartlists. Also includes a
11 * splay-tree implementation of the string-to-void* map.
17 #include "../or/tree.h"
18 #include "container.h"
27 /* All newly allocated smartlists have this capacity.
29 #define SMARTLIST_DEFAULT_CAPACITY 32
31 #ifndef FAST_SMARTLIST
33 /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
34 * before it needs to be resized. Only the first <b>num_used</b> (\<=
35 * capacity) elements point to valid data.
43 /** Allocate and return an empty smartlist.
46 smartlist_create(void)
48 smartlist_t
*sl
= tor_malloc(sizeof(smartlist_t
));
50 sl
->capacity
= SMARTLIST_DEFAULT_CAPACITY
;
51 sl
->list
= tor_malloc(sizeof(void *) * sl
->capacity
);
55 /** Deallocate a smartlist. Does not release storage associated with the
59 smartlist_free(smartlist_t
*sl
)
65 /** Change the capacity of the smartlist to <b>n</b>, so that we can grow
66 * the list up to <b>n</b> elements with no further reallocation or wasted
67 * space. If <b>n</b> is less than or equal to the number of elements
68 * currently in the list, reduce the list's capacity as much as
69 * possible without losing elements.
72 smartlist_set_capacity(smartlist_t
*sl
, int n
)
76 if (sl
->capacity
!= n
) {
78 sl
->list
= tor_realloc(sl
->list
, sizeof(void*)*sl
->capacity
);
82 /** Remove all elements from the list.
85 smartlist_clear(smartlist_t
*sl
)
90 /** Set the list's new length to <b>len</b> (which must be \<= the list's
91 * current size). Remove the last smartlist_len(sl)-len elements from the
95 smartlist_truncate(smartlist_t
*sl
, int len
)
97 tor_assert(len
<= sl
->num_used
);
101 /** Append element to the end of the list. */
103 smartlist_add(smartlist_t
*sl
, void *element
)
105 if (sl
->num_used
>= sl
->capacity
) {
106 int higher
= sl
->capacity
* 2;
107 tor_assert(higher
> sl
->capacity
); /* detect overflow */
108 sl
->capacity
= higher
;
109 sl
->list
= tor_realloc(sl
->list
, sizeof(void*)*sl
->capacity
);
111 sl
->list
[sl
->num_used
++] = element
;
114 /** Append each element from S2 to the end of S1. */
116 smartlist_add_all(smartlist_t
*sl
, const smartlist_t
*s2
)
118 int n2
= sl
->num_used
+ s2
->num_used
;
119 if (n2
> sl
->capacity
) {
120 int higher
= sl
->capacity
* 2;
123 sl
->capacity
= higher
;
124 sl
->list
= tor_realloc(sl
->list
, sizeof(void*)*sl
->capacity
);
126 memcpy(sl
->list
+ sl
->num_used
, s2
->list
, s2
->num_used
*sizeof(void*));
127 sl
->num_used
+= s2
->num_used
;
130 /** Remove all elements E from sl such that E==element. Preserve
131 * the order of any elements before E, but elements after E can be
135 smartlist_remove(smartlist_t
*sl
, void *element
)
140 for (i
=0; i
< sl
->num_used
; i
++)
141 if (sl
->list
[i
] == element
) {
142 sl
->list
[i
] = sl
->list
[--sl
->num_used
]; /* swap with the end */
143 i
--; /* so we process the new i'th element */
147 /** If there are any strings in sl equal to element, remove and free them.
148 * Does not preserve order. */
150 smartlist_string_remove(smartlist_t
*sl
, const char *element
)
155 for (i
= 0; i
< sl
->num_used
; ++i
) {
156 if (!strcmp(element
, sl
->list
[i
])) {
157 tor_free(sl
->list
[i
]);
158 sl
->list
[i
] = sl
->list
[--sl
->num_used
]; /* swap with the end */
159 i
--; /* so we process the new i'th element */
164 /** Return true iff some element E of sl has E==element.
167 smartlist_isin(const smartlist_t
*sl
, void *element
)
170 for (i
=0; i
< sl
->num_used
; i
++)
171 if (sl
->list
[i
] == element
)
176 /** Return true iff <b>sl</b> has some element E such that
177 * !strcmp(E,<b>element</b>)
180 smartlist_string_isin(const smartlist_t
*sl
, const char *element
)
184 for (i
=0; i
< sl
->num_used
; i
++)
185 if (strcmp((const char*)sl
->list
[i
],element
)==0)
190 /** Return true iff <b>sl</b> has some element E such that E is equal
191 * to the decimal encoding of <b>num</b>.
194 smartlist_string_num_isin(const smartlist_t
*sl
, int num
)
197 tor_snprintf(buf
,sizeof(buf
),"%d", num
);
198 return smartlist_string_isin(sl
, buf
);
201 /** Return true iff some element E of sl2 has smartlist_isin(sl1,E).
204 smartlist_overlap(const smartlist_t
*sl1
, const smartlist_t
*sl2
)
207 for (i
=0; i
< sl2
->num_used
; i
++)
208 if (smartlist_isin(sl1
, sl2
->list
[i
]))
213 /** Remove every element E of sl1 such that !smartlist_isin(sl2,E).
214 * Does not preserve the order of sl1.
217 smartlist_intersect(smartlist_t
*sl1
, const smartlist_t
*sl2
)
220 for (i
=0; i
< sl1
->num_used
; i
++)
221 if (!smartlist_isin(sl2
, sl1
->list
[i
])) {
222 sl1
->list
[i
] = sl1
->list
[--sl1
->num_used
]; /* swap with the end */
223 i
--; /* so we process the new i'th element */
227 /** Remove every element E of sl1 such that smartlist_isin(sl2,E).
228 * Does not preserve the order of sl1.
231 smartlist_subtract(smartlist_t
*sl1
, const smartlist_t
*sl2
)
234 for (i
=0; i
< sl2
->num_used
; i
++)
235 smartlist_remove(sl1
, sl2
->list
[i
]);
238 #ifndef FAST_SMARTLIST
239 /** Return the <b>idx</b>th element of sl.
242 smartlist_get(const smartlist_t
*sl
, int idx
)
246 tor_assert(idx
< sl
->num_used
);
247 return sl
->list
[idx
];
249 /** Change the value of the <b>idx</b>th element of sl to <b>val</b>.
252 smartlist_set(smartlist_t
*sl
, int idx
, void *val
)
256 tor_assert(idx
< sl
->num_used
);
259 /** Return the number of items in sl.
262 smartlist_len(const smartlist_t
*sl
)
268 /** Remove the <b>idx</b>th element of sl; if idx is not the last
269 * element, swap the last element of sl into the <b>idx</b>th space.
270 * Return the old value of the <b>idx</b>th element.
273 smartlist_del(smartlist_t
*sl
, int idx
)
277 tor_assert(idx
< sl
->num_used
);
278 sl
->list
[idx
] = sl
->list
[--sl
->num_used
];
280 /** Remove the <b>idx</b>th element of sl; if idx is not the last element,
281 * moving all subsequent elements back one space. Return the old value
282 * of the <b>idx</b>th element.
285 smartlist_del_keeporder(smartlist_t
*sl
, int idx
)
289 tor_assert(idx
< sl
->num_used
);
291 if (idx
< sl
->num_used
)
292 memmove(sl
->list
+idx
, sl
->list
+idx
+1, sizeof(void*)*(sl
->num_used
-idx
));
294 /** Insert the value <b>val</b> as the new <b>idx</b>th element of
295 * <b>sl</b>, moving all items previously at <b>idx</b> or later
299 smartlist_insert(smartlist_t
*sl
, int idx
, void *val
)
303 tor_assert(idx
<= sl
->num_used
);
304 if (idx
== sl
->num_used
) {
305 smartlist_add(sl
, val
);
307 /* Ensure sufficient capacity */
308 if (sl
->num_used
>= sl
->capacity
) {
310 sl
->list
= tor_realloc(sl
->list
, sizeof(void*)*sl
->capacity
);
312 /* Move other elements away */
313 if (idx
< sl
->num_used
)
314 memmove(sl
->list
+ idx
+ 1, sl
->list
+ idx
,
315 sizeof(void*)*(sl
->num_used
-idx
));
322 * Split a string <b>str</b> along all occurrences of <b>sep</b>,
323 * adding the split strings, in order, to <b>sl</b>. If
324 * <b>flags</b>&SPLIT_SKIP_SPACE is true, remove initial and
325 * trailing space from each entry. If
326 * <b>flags</b>&SPLIT_IGNORE_BLANK is true, remove any entries of
327 * length 0. If max>0, divide the string into no more than <b>max</b>
328 * pieces. If <b>sep</b> is NULL, split on any sequence of horizontal space.
331 smartlist_split_string(smartlist_t
*sl
, const char *str
, const char *sep
,
334 const char *cp
, *end
, *next
;
342 if (flags
&SPLIT_SKIP_SPACE
) {
343 while (TOR_ISSPACE(*cp
)) ++cp
;
346 if (max
>0 && n
== max
-1) {
347 end
= strchr(cp
,'\0');
349 end
= strstr(cp
,sep
);
351 end
= strchr(cp
,'\0');
353 for (end
= cp
; *end
&& *end
!= '\t' && *end
!= ' '; ++end
)
360 next
= end
+strlen(sep
);
363 while (*next
== '\t' || *next
== ' ')
367 if (flags
&SPLIT_SKIP_SPACE
) {
368 while (end
> cp
&& TOR_ISSPACE(*(end
-1)))
371 if (end
!= cp
|| !(flags
&SPLIT_IGNORE_BLANK
)) {
372 smartlist_add(sl
, tor_strndup(cp
, end
-cp
));
383 /** Allocate and return a new string containing the concatenation of
384 * the elements of <b>sl</b>, in order, separated by <b>join</b>. If
385 * <b>terminate</b> is true, also terminate the string with <b>join</b>.
386 * If <b>len_out</b> is not NULL, set <b>len_out</b> to the length of
387 * the returned string. Requires that every element of <b>sl</b> is
388 * NUL-terminated string.
391 smartlist_join_strings(smartlist_t
*sl
, const char *join
,
392 int terminate
, size_t *len_out
)
394 return smartlist_join_strings2(sl
,join
,strlen(join
),terminate
,len_out
);
397 /** As smartlist_join_strings, but instead of separating/terminated with a
398 * NUL-terminated string <b>join</b>, uses the <b>join_len</b>-byte sequence
399 * at <b>join</b>. (Useful for generating a sequence of NUL-terminated
403 smartlist_join_strings2(smartlist_t
*sl
, const char *join
,
404 size_t join_len
, int terminate
, size_t *len_out
)
408 char *r
= NULL
, *dst
, *src
;
416 for (i
= 0; i
< sl
->num_used
; ++i
) {
417 n
+= strlen(sl
->list
[i
]);
418 if (i
+1 < sl
->num_used
) /* avoid double-counting the last one */
421 dst
= r
= tor_malloc(n
+1);
422 for (i
= 0; i
< sl
->num_used
; ) {
423 for (src
= sl
->list
[i
]; *src
; )
425 if (++i
< sl
->num_used
) {
426 memcpy(dst
, join
, join_len
);
431 memcpy(dst
, join
, join_len
);
441 /** Sort the members of <b>sl</b> into an order defined by
442 * the ordering function <b>compare</b>, which returns less then 0 if a
443 * precedes b, greater than 0 if b precedes a, and 0 if a 'equals' b.
446 smartlist_sort(smartlist_t
*sl
, int (*compare
)(const void **a
, const void **b
))
450 qsort(sl
->list
, sl
->num_used
, sizeof(void*),
451 (int (*)(const void *,const void*))compare
);
454 /** Assuming the members of <b>sl</b> are in order, return a pointer to the
455 * member which matches <b>key</b>. Ordering and matching are defined by a
456 * <b>compare</b> function, which returns 0 on a match; less than 0 if key is
457 * less than member, and greater than 0 if key is greater then member.
460 smartlist_bsearch(smartlist_t
*sl
, const void *key
,
461 int (*compare
)(const void *key
, const void **member
))
467 r
= bsearch(key
, sl
->list
, sl
->num_used
, sizeof(void*),
468 (int (*)(const void *, const void *))compare
);
469 return r
? *r
: NULL
;
472 /** Helper: compare two const char **s. */
474 _compare_string_ptrs(const void **_a
, const void **_b
)
476 return strcmp((const char*)*_a
, (const char*)*_b
);
479 /** Sort a smartlist <b>sl</b> containing strings into lexically ascending
482 smartlist_sort_strings(smartlist_t
*sl
)
484 smartlist_sort(sl
, _compare_string_ptrs
);
487 /** Splay-tree implementation of string-to-void* map
489 typedef struct strmap_entry_t
{
490 SPLAY_ENTRY(strmap_entry_t
) node
;
496 SPLAY_HEAD(strmap_tree
, strmap_entry_t
) head
;
499 /** Helper: compare strmap_entry_t objects by key value. */
501 compare_strmap_entries(strmap_entry_t
*a
,
504 return strcmp(a
->key
, b
->key
);
507 SPLAY_PROTOTYPE(strmap_tree
, strmap_entry_t
, node
, compare_strmap_entries
);
508 SPLAY_GENERATE(strmap_tree
, strmap_entry_t
, node
, compare_strmap_entries
);
510 /** Create a new empty map from strings to void*'s.
516 result
= tor_malloc(sizeof(strmap_t
));
517 SPLAY_INIT(&result
->head
);
521 /** Set the current value for <b>key</b> to <b>val</b>. Returns the previous
522 * value for <b>key</b> if one was set, or NULL if one was not.
524 * This function makes a copy of <b>key</b> if necessary, but not of <b>val</b>.
527 strmap_set(strmap_t
*map
, const char *key
, void *val
)
529 strmap_entry_t
*resolve
;
530 strmap_entry_t search
;
535 search
.key
= (char*)key
;
536 resolve
= SPLAY_FIND(strmap_tree
, &map
->head
, &search
);
538 oldval
= resolve
->val
;
542 resolve
= tor_malloc_zero(sizeof(strmap_entry_t
));
543 resolve
->key
= tor_strdup(key
);
545 SPLAY_INSERT(strmap_tree
, &map
->head
, resolve
);
550 /** Return the current value associated with <b>key</b>, or NULL if no
554 strmap_get(strmap_t
*map
, const char *key
)
556 strmap_entry_t
*resolve
;
557 strmap_entry_t search
;
560 search
.key
= (char*)key
;
561 resolve
= SPLAY_FIND(strmap_tree
, &map
->head
, &search
);
569 /** Remove the value currently associated with <b>key</b> from the map.
570 * Return the value if one was set, or NULL if there was no entry for
573 * Note: you must free any storage associated with the returned value.
576 strmap_remove(strmap_t
*map
, const char *key
)
578 strmap_entry_t
*resolve
;
579 strmap_entry_t search
;
583 search
.key
= (char*)key
;
584 resolve
= SPLAY_FIND(strmap_tree
, &map
->head
, &search
);
586 oldval
= resolve
->val
;
587 SPLAY_REMOVE(strmap_tree
, &map
->head
, resolve
);
588 tor_free(resolve
->key
);
596 /** Same as strmap_set, but first converts <b>key</b> to lowercase. */
598 strmap_set_lc(strmap_t
*map
, const char *key
, void *val
)
600 /* We could be a little faster by using strcasecmp instead, and a separate
601 * type, but I don't think it matters. */
603 char *lc_key
= tor_strdup(key
);
604 tor_strlower(lc_key
);
605 v
= strmap_set(map
,lc_key
,val
);
609 /** Same as strmap_get, but first converts <b>key</b> to lowercase. */
611 strmap_get_lc(strmap_t
*map
, const char *key
)
614 char *lc_key
= tor_strdup(key
);
615 tor_strlower(lc_key
);
616 v
= strmap_get(map
,lc_key
);
620 /** Same as strmap_remove, but first converts <b>key</b> to lowercase */
622 strmap_remove_lc(strmap_t
*map
, const char *key
)
625 char *lc_key
= tor_strdup(key
);
626 tor_strlower(lc_key
);
627 v
= strmap_remove(map
,lc_key
);
632 /** Invoke fn() on every entry of the map, in order. For every entry,
633 * fn() is invoked with that entry's key, that entry's value, and the
634 * value of <b>data</b> supplied to strmap_foreach. fn() must return a new
635 * (possibly unmodified) value for each entry: if fn() returns NULL, the
640 * static void* upcase_and_remove_empty_vals(const char *key, void *val,
642 * char *cp = (char*)val;
643 * if (!*cp) { // val is an empty string.
648 * *cp = toupper(*cp);
656 * strmap_foreach(map, upcase_and_remove_empty_vals, NULL);
660 strmap_foreach(strmap_t
*map
,
661 void* (*fn
)(const char *key
, void *val
, void *data
),
664 strmap_entry_t
*ptr
, *next
;
667 for (ptr
= SPLAY_MIN(strmap_tree
, &map
->head
); ptr
!= NULL
; ptr
= next
) {
668 /* This remove-in-place usage is specifically blessed in tree(3). */
669 next
= SPLAY_NEXT(strmap_tree
, &map
->head
, ptr
);
670 ptr
->val
= fn(ptr
->key
, ptr
->val
, data
);
672 SPLAY_REMOVE(strmap_tree
, &map
->head
, ptr
);
679 /** return an <b>iterator</b> pointer to the front of a map.
684 * // uppercase values in "map", removing empty values.
686 * strmap_iter_t *iter;
691 * for (iter = strmap_iter_init(map); !strmap_iter_done(iter); ) {
692 * strmap_iter_get(iter, &key, &val);
695 * iter = strmap_iter_next_rmv(iter);
698 * for (;*cp;cp++) *cp = toupper(*cp);
699 * iter = strmap_iter_next(iter);
706 strmap_iter_init(strmap_t
*map
)
709 return SPLAY_MIN(strmap_tree
, &map
->head
);
711 /** Advance the iterator <b>iter</b> for map a single step to the next entry.
714 strmap_iter_next(strmap_t
*map
, strmap_iter_t
*iter
)
718 return SPLAY_NEXT(strmap_tree
, &map
->head
, iter
);
720 /** Advance the iterator <b>iter</b> a single step to the next entry, removing
724 strmap_iter_next_rmv(strmap_t
*map
, strmap_iter_t
*iter
)
729 next
= SPLAY_NEXT(strmap_tree
, &map
->head
, iter
);
730 SPLAY_REMOVE(strmap_tree
, &map
->head
, iter
);
735 /** Set *keyp and *valp to the current entry pointed to by iter.
738 strmap_iter_get(strmap_iter_t
*iter
, const char **keyp
, void **valp
)
746 /** Return true iff iter has advanced past the last entry of map.
749 strmap_iter_done(strmap_iter_t
*iter
)
753 /** Remove all entries from <b>map</b>, and deallocate storage for those entries.
754 * If free_val is provided, it is invoked on every value in <b>map</b>.
757 strmap_free(strmap_t
*map
, void (*free_val
)(void*))
759 strmap_entry_t
*ent
, *next
;
760 for (ent
= SPLAY_MIN(strmap_tree
, &map
->head
); ent
!= NULL
; ent
= next
) {
761 next
= SPLAY_NEXT(strmap_tree
, &map
->head
, ent
);
762 SPLAY_REMOVE(strmap_tree
, &map
->head
, ent
);
768 tor_assert(SPLAY_EMPTY(&map
->head
));
772 /* Return true iff <b>map</b> has no entries.
775 strmap_isempty(strmap_t
*map
)
777 return SPLAY_EMPTY(&map
->head
);