container.h 6.21 KB
Newer Older
Nick Mathewson's avatar
Nick Mathewson committed
1
/* Copyright 2003-2004 Roger Dingledine
Roger Dingledine's avatar
Roger Dingledine committed
2
 * Copyright 2004-2006 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
/** A resizeable list of pointers, with associated helpful functionality. */
typedef struct smartlist_t {
16
17
18
19
20
21
22
  /** <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;
23
} smartlist_t;
24
25
26
27
28
29
30

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);
31
void smartlist_remove(smartlist_t *sl, const void *element);
32
33
void *smartlist_pop_last(smartlist_t *sl);
void smartlist_reverse(smartlist_t *sl);
34
void smartlist_string_remove(smartlist_t *sl, const char *element);
35
36
37
38
int smartlist_isin(const smartlist_t *sl, const void *element) ATTR_PURE;
int smartlist_string_isin(const smartlist_t *sl, const char *element) ATTR_PURE;
int smartlist_string_num_isin(const smartlist_t *sl, int num) ATTR_PURE;
int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2) ATTR_PURE;
39
40
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
41

42
/* smartlist_choose() is defined in crypto.[ch] */
43
#ifdef DEBUG_SMARTLIST
44
45
/** Return the number of items in sl.
 */
46
extern INLINE int smartlist_len(const smartlist_t *sl) ATTR_PURE {
47
48
49
50
51
  tor_assert(sl);
  return (sl)->num_used;
}
/** Return the <b>idx</b>th element of sl.
 */
52
extern INLINE void *smartlist_get(const smartlist_t *sl, int idx) ATTR_PURE {
53
54
55
56
57
58
59
60
61
62
63
  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;
}
64
65
66
67
68
69
#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

70
71
void smartlist_del(smartlist_t *sl, int idx);
void smartlist_del_keeporder(smartlist_t *sl, int idx);
72
void smartlist_insert(smartlist_t *sl, int idx, void *val);
73
74
void smartlist_sort(smartlist_t *sl,
                    int (*compare)(const void **a, const void **b));
75
void smartlist_sort_strings(smartlist_t *sl);
76
void smartlist_sort_digests(smartlist_t *sl);
77
void *smartlist_bsearch(smartlist_t *sl, const void *key,
78
79
                        int (*compare)(const void *key, const void **member))
 ATTR_PURE;
80

81
82
83
84
85
86
87
88
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));

89
90
91
92
#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);
93
char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
94
                             size_t *len_out) ATTR_MALLOC;
95
char *smartlist_join_strings2(smartlist_t *sl, const char *join,
96
97
                              size_t join_len, int terminate, size_t *len_out)
  ATTR_MALLOC;
98

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/** 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
 * be accessed as <b>var</b>_sl_idx.
 *
 * 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);
 *   });
 *   smarlitst_free(list);
 * </pre>
 */
115
116
#define SMARTLIST_FOREACH(sl, type, var, cmd)                   \
  do {                                                          \
117
    int var ## _sl_idx, var ## _sl_len=(sl)->num_used;          \
118
    type var;                                                   \
119
    for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len;   \
120
         ++var ## _sl_idx) {                                    \
121
      var = (sl)->list[var ## _sl_idx];                         \
122
123
124
      cmd;                                                      \
    } } while (0)

125
126
#define DECLARE_MAP_FNS(maptype, keytype, prefix)                       \
  typedef struct maptype maptype;                                       \
127
  typedef struct prefix##entry_t *prefix##iter_t;                       \
128
129
130
131
132
133
  maptype* prefix##new(void);                                           \
  void* prefix##set(maptype *map, keytype key, void *val);              \
  void* prefix##get(maptype *map, keytype key);                         \
  void* prefix##remove(maptype *map, keytype key);                      \
  void prefix##free(maptype *map, void (*free_val)(void*));             \
  int prefix##isempty(maptype *map);                                    \
134
  int prefix##size(maptype *map);                                       \
135
136
137
138
  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); \
139
140
  int prefix##iter_done(prefix##iter_t *iter);                          \
  void prefix##assert_ok(maptype *map);
141

142
/* Map from const char * to void *. Implemented with a hash table. */
143
144
145
DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);

146
147
148
149
150
void* strmap_set_lc(strmap_t *map, const char *key, void *val);
void* strmap_get_lc(strmap_t *map, const char *key);
void* strmap_remove_lc(strmap_t *map, const char *key);

#endif
151