container.h 3.9 KB

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