container.h 3.9 KB

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