Forward port changelog
[tor.git] / src / common / container.h
blob352433272af6902524ada7232019bb50a06683aa
1 /* Copyright 2003-2004 Roger Dingledine; Copyright 2004 Nick Mathewson */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
5 #ifndef __CONTAINER_H
6 #define __CONTAINER_H
7 #define CONTAINER_H_ID "$Id$"
9 /** Generic resizeable array. */
10 typedef struct smartlist_t smartlist_t;
11 #ifdef FAST_SMARTLIST
13 struct smartlist_t {
14 /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
15 * before it needs to be resized. Only the first <b>num_used</b> (\<=
16 * capacity) elements point to valid data.
18 void **list;
19 int num_used;
20 int capacity;
22 #endif
24 smartlist_t *smartlist_create(void);
25 void smartlist_free(smartlist_t *sl);
26 void smartlist_set_capacity(smartlist_t *sl, int n);
27 void smartlist_clear(smartlist_t *sl);
28 void smartlist_truncate(smartlist_t *sl, int n);
29 void smartlist_add(smartlist_t *sl, void *element);
30 void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
31 void smartlist_remove(smartlist_t *sl, void *element);
32 int smartlist_isin(const smartlist_t *sl, void *element);
33 int smartlist_string_isin(const smartlist_t *sl, const char *element);
34 int smartlist_string_num_isin(const smartlist_t *sl, int num);
35 int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
36 void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
37 void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
38 /* smartlist_choose() is defined in crypto.[ch] */
39 #ifndef FAST_SMARTLIST
40 void *smartlist_get(const smartlist_t *sl, int idx);
41 void smartlist_set(smartlist_t *sl, int idx, void *val);
42 int smartlist_len(const smartlist_t *sl);
43 #else
44 #define smartlist_get(sl,idx) ((sl)->list[(idx)])
45 #define smartlist_set(sl,idx,val) ((sl)->list[(idx)] = val)
46 #define smartlist_len(sl) ((sl)->num_used)
47 #endif
48 void smartlist_del(smartlist_t *sl, int idx);
49 void smartlist_del_keeporder(smartlist_t *sl, int idx);
50 void smartlist_insert(smartlist_t *sl, int idx, void *val);
52 #define SPLIT_SKIP_SPACE 0x01
53 #define SPLIT_IGNORE_BLANK 0x02
54 int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
55 int flags, int max);
56 char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
57 size_t *len_out);
58 char *smartlist_join_strings2(smartlist_t *sl, const char *join,
59 size_t join_len, int terminate, size_t *len_out);
61 #define SMARTLIST_FOREACH(sl, type, var, cmd) \
62 do { \
63 int var ## _sl_idx, var ## _sl_len=smartlist_len(sl); \
64 type var; \
65 for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
66 ++var ## _sl_idx) { \
67 var = smartlist_get((sl),var ## _sl_idx); \
68 cmd; \
69 } } while (0)
71 /* Map from const char * to void*. Implemented with a splay tree. */
72 typedef struct strmap_t strmap_t;
73 typedef struct strmap_entry_t strmap_entry_t;
74 typedef struct strmap_entry_t strmap_iter_t;
75 strmap_t* strmap_new(void);
76 void* strmap_set(strmap_t *map, const char *key, void *val);
77 void* strmap_get(strmap_t *map, const char *key);
78 void* strmap_remove(strmap_t *map, const char *key);
79 void* strmap_set_lc(strmap_t *map, const char *key, void *val);
80 void* strmap_get_lc(strmap_t *map, const char *key);
81 void* strmap_remove_lc(strmap_t *map, const char *key);
82 typedef void* (*strmap_foreach_fn)(const char *key, void *val, void *data);
83 void strmap_foreach(strmap_t *map, strmap_foreach_fn fn, void *data);
84 void strmap_free(strmap_t *map, void (*free_val)(void*));
85 int strmap_isempty(strmap_t *map);
87 strmap_iter_t *strmap_iter_init(strmap_t *map);
88 strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
89 strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
90 void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
92 int strmap_iter_done(strmap_iter_t *iter);
94 #endif