container.h 12.5 KB
Newer Older
Nick Mathewson's avatar
Nick Mathewson committed
1
/* Copyright 2003-2004 Roger Dingledine
2
 * Copyright 2004-2007 Roger Dingledine, Nick Mathewson */
3
4
5
6
7
/* See LICENSE for licensing information */
/* $Id$ */

#ifndef __CONTAINER_H
#define __CONTAINER_H
8
9
#define CONTAINER_H_ID \
  "$Id$"
10

Nick Mathewson's avatar
Nick Mathewson committed
11
12
#include "compat.h"
#include "util.h"
13

14
15
16
17
18
19
/** A resizeable list of pointers, with associated helpful functionality.
 *
 * The members of this struct are exposed only so that macros and inlines can
 * use them; all access to smartlist internals should go throuch the functions
 * and macros defined here.
 **/
20
typedef struct smartlist_t {
21
22
23
24
25
26
27
  /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
   * before it needs to be resized.  Only the first <b>num_used</b> (\<=
   * capacity) elements point to valid data.
   */
  void **list;
  int num_used;
  int capacity;
28
} smartlist_t;
29
30
31
32
33
34
35

smartlist_t *smartlist_create(void);
void smartlist_free(smartlist_t *sl);
void smartlist_set_capacity(smartlist_t *sl, int n);
void smartlist_clear(smartlist_t *sl);
void smartlist_add(smartlist_t *sl, void *element);
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
36
void smartlist_remove(smartlist_t *sl, const void *element);
37
38
void *smartlist_pop_last(smartlist_t *sl);
void smartlist_reverse(smartlist_t *sl);
39
void smartlist_string_remove(smartlist_t *sl, const char *element);
40
int smartlist_isin(const smartlist_t *sl, const void *element) ATTR_PURE;
41
42
int smartlist_string_isin(const smartlist_t *sl, const char *element)
  ATTR_PURE;
43
int smartlist_string_pos(const smartlist_t *, const char *elt) ATTR_PURE;
44
45
int smartlist_string_isin_case(const smartlist_t *sl, const char *element)
  ATTR_PURE;
46
int smartlist_string_num_isin(const smartlist_t *sl, int num) ATTR_PURE;
47
48
int smartlist_digest_isin(const smartlist_t *sl, const char *element)
  ATTR_PURE;
49
50
int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2)
  ATTR_PURE;
51
52
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
53

54
/* smartlist_choose() is defined in crypto.[ch] */
55
#ifdef DEBUG_SMARTLIST
56
57
/** Return the number of items in sl.
 */
58
extern INLINE int smartlist_len(const smartlist_t *sl) ATTR_PURE {
59
60
61
62
63
  tor_assert(sl);
  return (sl)->num_used;
}
/** Return the <b>idx</b>th element of sl.
 */
64
extern INLINE void *smartlist_get(const smartlist_t *sl, int idx) ATTR_PURE {
65
66
67
68
69
70
71
72
73
74
75
  tor_assert(sl);
  tor_assert(idx>=0);
  tor_assert(sl->num_used < idx);
  return sl->list[idx];
}
extern INLINE void smartlist_set(smartlist_t *sl, int idx, void *val) {
  tor_assert(sl);
  tor_assert(idx>=0);
  tor_assert(sl->num_used < idx);
  sl->list[idx] = val;
}
76
77
78
79
80
81
#else
#define smartlist_len(sl) ((sl)->num_used)
#define smartlist_get(sl, idx) ((sl)->list[idx])
#define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
#endif

82
83
/** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
 * smartlist <b>sl</b>. */
84
static INLINE void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
85
86
87
88
89
90
91
92
{
  if (idx1 != idx2) {
    void *elt = smartlist_get(sl, idx1);
    smartlist_set(sl, idx1, smartlist_get(sl, idx2));
    smartlist_set(sl, idx2, elt);
  }
}

93
94
void smartlist_del(smartlist_t *sl, int idx);
void smartlist_del_keeporder(smartlist_t *sl, int idx);
95
void smartlist_insert(smartlist_t *sl, int idx, void *val);
96
97
void smartlist_sort(smartlist_t *sl,
                    int (*compare)(const void **a, const void **b));
98
99
100
void smartlist_uniq(smartlist_t *sl,
                    int (*compare)(const void **a, const void **b),
                    void (*free_fn)(void *elt));
101
void smartlist_sort_strings(smartlist_t *sl);
102
void smartlist_sort_digests(smartlist_t *sl);
103
104
void smartlist_uniq_strings(smartlist_t *sl);
void smartlist_uniq_digests(smartlist_t *sl);
105
void *smartlist_bsearch(smartlist_t *sl, const void *key,
106
107
                        int (*compare)(const void *key, const void **member))
 ATTR_PURE;
108

109
110
111
112
113
114
115
116
void smartlist_pqueue_add(smartlist_t *sl,
                          int (*compare)(const void *a, const void *b),
                          void *item);
void *smartlist_pqueue_pop(smartlist_t *sl,
                           int (*compare)(const void *a, const void *b));
void smartlist_pqueue_assert_ok(smartlist_t *sl,
                                int (*compare)(const void *a, const void *b));

117
118
119
120
#define SPLIT_SKIP_SPACE   0x01
#define SPLIT_IGNORE_BLANK 0x02
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
                           int flags, int max);
121
char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
122
                             size_t *len_out) ATTR_MALLOC;
123
char *smartlist_join_strings2(smartlist_t *sl, const char *join,
124
125
                              size_t join_len, int terminate, size_t *len_out)
  ATTR_MALLOC;
126

127
128
129
/** Iterate over the items in a smartlist <b>sl</b>, in order.  For each item,
 * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
 * execute the statement <b>cmd</b>.  Inside the loop, the loop index can
130
131
132
133
134
135
 * be accessed as <b>var</b>_sl_idx and the length of the list can be accessed
 * as <b>var</b>_sl_len.
 *
 * NOTE: Do not change the length of the list while the loop is in progress,
 * unless you adjust the _sl_len variable correspondingly.  See second example
 * below.
136
137
138
139
140
141
142
143
144
 *
 * Example use:
 * <pre>
 *   smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
 *   SMARTLIST_FOREACH(list, char *, cp,
 *   {
 *     printf("%d: %s\n", cp_sl_idx, cp);
 *     tor_free(cp);
 *   });
145
146
147
148
149
150
151
152
153
154
155
156
157
158
 *   smartlist_free(list);
 * </pre>
 *
 * Example use (advanced):
 * <pre>
 *   SMARTLIST_FOREACH(list, char *, cp,
 *   {
 *     if (!strcmp(cp, "junk")) {
 *       smartlist_del(list, cp_sl_idx);
 *       tor_free(cp);
 *       --cp_sl_len; // decrement length of list so we don't run off the end
 *       --cp_sl_idx; // decrement idx so we consider the item that moved here
 *     }
 *   });
159
160
 * </pre>
 */
161
162
#define SMARTLIST_FOREACH(sl, type, var, cmd)                   \
  do {                                                          \
163
    int var ## _sl_idx, var ## _sl_len=(sl)->num_used;          \
164
    type var;                                                   \
165
    for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len;   \
166
         ++var ## _sl_idx) {                                    \
167
      var = (sl)->list[var ## _sl_idx];                         \
168
169
170
      cmd;                                                      \
    } } while (0)

171
/** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
172
 * with the variable <b>var</b>, remove the current element in a way that
173
174
175
176
177
178
179
180
 * won't confuse the loop. */
#define SMARTLIST_DEL_CURRENT(sl, var)          \
  do {                                          \
    smartlist_del(sl, var ## _sl_idx);          \
    --var ## _sl_idx;                           \
    --var ## _sl_len;                           \
  } while (0);

181
182
#define DECLARE_MAP_FNS(maptype, keytype, prefix)                       \
  typedef struct maptype maptype;                                       \
183
  typedef struct prefix##entry_t *prefix##iter_t;                       \
184
185
  maptype* prefix##new(void);                                           \
  void* prefix##set(maptype *map, keytype key, void *val);              \
186
  void* prefix##get(const maptype *map, keytype key);                   \
187
188
  void* prefix##remove(maptype *map, keytype key);                      \
  void prefix##free(maptype *map, void (*free_val)(void*));             \
189
190
  int prefix##isempty(const maptype *map);                              \
  int prefix##size(const maptype *map);                                 \
191
192
193
194
  prefix##iter_t *prefix##iter_init(maptype *map);                      \
  prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
  prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
  void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
195
  int prefix##iter_done(prefix##iter_t *iter);                          \
196
  void prefix##assert_ok(const maptype *map);
197

198
/* Map from const char * to void *. Implemented with a hash table. */
199
200
DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
201
#undef DECLARE_MAP_FNS
202

203
void* strmap_set_lc(strmap_t *map, const char *key, void *val);
204
void* strmap_get_lc(const strmap_t *map, const char *key);
205
206
void* strmap_remove_lc(strmap_t *map, const char *key);

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype)           \
  typedef struct maptype maptype;                                       \
  typedef struct prefix##iter_t prefix##iter_t;                         \
  static INLINE maptype* prefix##new(void)                              \
  {                                                                     \
    return (maptype*)digestmap_new();                                   \
  }                                                                     \
  static INLINE valtype* prefix##get(maptype *map, const char *key)     \
  {                                                                     \
    return (valtype*)digestmap_get((digestmap_t*)map, key);             \
  }                                                                     \
  static INLINE valtype* prefix##set(maptype *map, const char *key,     \
                                     valtype *val)                      \
  {                                                                     \
    return (valtype*)digestmap_set((digestmap_t*)map, key, val);        \
  }                                                                     \
  static INLINE valtype* prefix##remove(maptype *map, const char *key)  \
  {                                                                     \
225
    return (valtype*)digestmap_remove((digestmap_t*)map, key);          \
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  }                                                                     \
  static INLINE void prefix##free(maptype *map, void (*free_val)(void*)) \
  {                                                                     \
    digestmap_free((digestmap_t*)map, free_val);                        \
  }                                                                     \
  static INLINE int prefix##isempty(maptype *map)                       \
  {                                                                     \
    return digestmap_isempty((digestmap_t*)map);                        \
  }                                                                     \
  static INLINE int prefix##size(maptype *map)                          \
  {                                                                     \
    return digestmap_isempty((digestmap_t*)map);                        \
  }                                                                     \
  static INLINE prefix##iter_t *prefix##iter_init(maptype *map)         \
  {                                                                     \
    return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map);    \
  }                                                                     \
  static INLINE prefix##iter_t *prefix##iter_next(maptype *map,         \
                                                  prefix##iter_t *iter) \
  {                                                                     \
    return (prefix##iter_t*) digestmap_iter_next(                       \
                       (digestmap_t*)map, (digestmap_iter_t*)iter);     \
  }                                                                     \
  static INLINE prefix##iter_t *prefix##iter_next_rmv(maptype *map,     \
                                                  prefix##iter_t *iter) \
  {                                                                     \
    return (prefix##iter_t*) digestmap_iter_next_rmv(                   \
                       (digestmap_t*)map, (digestmap_iter_t*)iter);     \
  }                                                                     \
  static INLINE void prefix##iter_get(prefix##iter_t *iter,             \
                                      const char **keyp,                \
                                      valtype **valp)                   \
  {                                                                     \
    void *v;                                                            \
    digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v);             \
    *valp = v;                                                          \
  }                                                                     \
  static INLINE int prefix##iter_done(prefix##iter_t *iter)             \
  {                                                                     \
    return digestmap_iter_done((digestmap_iter_t*)iter);                \
  }

268
#endif
269