2005-04-01 22:15:56 +02:00
|
|
|
/* Copyright 2003-2004 Roger Dingledine
|
2006-02-09 06:46:49 +01:00
|
|
|
* Copyright 2004-2006 Roger Dingledine, Nick Mathewson */
|
2004-11-01 21:41:47 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#ifndef __CONTAINER_H
|
|
|
|
#define __CONTAINER_H
|
2005-12-14 21:40:40 +01:00
|
|
|
#define CONTAINER_H_ID \
|
|
|
|
"$Id$"
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2005-10-18 19:52:45 +02:00
|
|
|
#include "compat.h"
|
|
|
|
#include "util.h"
|
2004-11-12 21:41:52 +01:00
|
|
|
|
2005-10-18 06:51:07 +02:00
|
|
|
/** A resizeable list of pointers, with associated helpful functionality. */
|
|
|
|
typedef struct smartlist_t {
|
2004-11-12 21:41:52 +01:00
|
|
|
/** <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;
|
2005-10-18 06:51:07 +02:00
|
|
|
} smartlist_t;
|
2004-11-01 21:41:47 +01:00
|
|
|
|
|
|
|
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);
|
2005-12-03 03:00:51 +01:00
|
|
|
void smartlist_remove(smartlist_t *sl, const void *element);
|
2006-06-18 09:21:35 +02:00
|
|
|
void *smartlist_pop_last(smartlist_t *sl);
|
|
|
|
void smartlist_reverse(smartlist_t *sl);
|
2005-04-03 07:52:32 +02:00
|
|
|
void smartlist_string_remove(smartlist_t *sl, const char *element);
|
2006-08-11 09:09:17 +02:00
|
|
|
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;
|
2004-11-01 21:41:47 +01:00
|
|
|
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
|
|
|
|
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
|
2006-07-31 19:59:37 +02:00
|
|
|
|
2004-11-02 03:28:51 +01:00
|
|
|
/* smartlist_choose() is defined in crypto.[ch] */
|
2005-10-18 07:03:44 +02:00
|
|
|
#ifdef DEBUG_SMARTLIST
|
2005-10-18 06:51:07 +02:00
|
|
|
/** Return the number of items in sl.
|
|
|
|
*/
|
2006-08-11 09:09:17 +02:00
|
|
|
extern INLINE int smartlist_len(const smartlist_t *sl) ATTR_PURE {
|
2005-10-18 06:51:07 +02:00
|
|
|
tor_assert(sl);
|
|
|
|
return (sl)->num_used;
|
|
|
|
}
|
|
|
|
/** Return the <b>idx</b>th element of sl.
|
|
|
|
*/
|
2006-08-11 09:09:17 +02:00
|
|
|
extern INLINE void *smartlist_get(const smartlist_t *sl, int idx) ATTR_PURE {
|
2005-10-18 06:51:07 +02:00
|
|
|
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;
|
|
|
|
}
|
2005-10-18 07:03:44 +02:00
|
|
|
#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
|
|
|
|
|
2004-11-12 22:14:06 +01:00
|
|
|
void smartlist_del(smartlist_t *sl, int idx);
|
|
|
|
void smartlist_del_keeporder(smartlist_t *sl, int idx);
|
2004-11-01 21:41:47 +01:00
|
|
|
void smartlist_insert(smartlist_t *sl, int idx, void *val);
|
2005-09-10 01:12:54 +02:00
|
|
|
void smartlist_sort(smartlist_t *sl,
|
|
|
|
int (*compare)(const void **a, const void **b));
|
2005-09-12 10:27:01 +02:00
|
|
|
void smartlist_sort_strings(smartlist_t *sl);
|
2006-06-21 06:57:12 +02:00
|
|
|
void smartlist_sort_digests(smartlist_t *sl);
|
2005-09-10 01:12:54 +02:00
|
|
|
void *smartlist_bsearch(smartlist_t *sl, const void *key,
|
2006-08-11 09:09:17 +02:00
|
|
|
int (*compare)(const void *key, const void **member))
|
|
|
|
ATTR_PURE;
|
2004-11-12 21:41:52 +01:00
|
|
|
|
2006-07-31 19:59:37 +02:00
|
|
|
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));
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
#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);
|
2004-11-03 19:28:00 +01:00
|
|
|
char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
|
2006-08-11 09:09:17 +02:00
|
|
|
size_t *len_out) ATTR_MALLOC;
|
2004-11-03 19:28:00 +01:00
|
|
|
char *smartlist_join_strings2(smartlist_t *sl, const char *join,
|
2006-08-11 09:09:17 +02:00
|
|
|
size_t join_len, int terminate, size_t *len_out)
|
|
|
|
ATTR_MALLOC;
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2005-09-12 01:14:14 +02:00
|
|
|
/** 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>
|
|
|
|
*/
|
2004-11-01 21:41:47 +01:00
|
|
|
#define SMARTLIST_FOREACH(sl, type, var, cmd) \
|
|
|
|
do { \
|
2005-10-18 19:38:07 +02:00
|
|
|
int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
|
2004-11-01 21:41:47 +01:00
|
|
|
type var; \
|
2004-11-28 10:05:49 +01:00
|
|
|
for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
|
2004-11-28 12:39:53 +01:00
|
|
|
++var ## _sl_idx) { \
|
2005-10-18 19:38:07 +02:00
|
|
|
var = (sl)->list[var ## _sl_idx]; \
|
2004-11-01 21:41:47 +01:00
|
|
|
cmd; \
|
|
|
|
} } while (0)
|
|
|
|
|
2005-10-18 22:11:39 +02:00
|
|
|
#define DECLARE_MAP_FNS(maptype, keytype, prefix) \
|
|
|
|
typedef struct maptype maptype; \
|
2005-11-23 05:18:45 +01:00
|
|
|
typedef struct prefix##entry_t *prefix##iter_t; \
|
2005-10-18 22:11:39 +02:00
|
|
|
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); \
|
2005-11-23 08:48:13 +01:00
|
|
|
int prefix##size(maptype *map); \
|
2005-10-18 22:11:39 +02:00
|
|
|
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); \
|
2006-07-31 20:01:27 +02:00
|
|
|
int prefix##iter_done(prefix##iter_t *iter); \
|
|
|
|
void prefix##assert_ok(maptype *map);
|
2005-10-18 22:11:39 +02:00
|
|
|
|
2005-12-03 03:00:51 +01:00
|
|
|
/* Map from const char * to void *. Implemented with a hash table. */
|
2005-10-18 22:11:39 +02:00
|
|
|
DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
|
|
|
|
DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
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
|
2005-06-09 21:03:31 +02:00
|
|
|
|