container.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. smartlist_t *smartlist_create(void);
  9. void smartlist_free(smartlist_t *sl);
  10. void smartlist_set_capacity(smartlist_t *sl, int n);
  11. void smartlist_clear(smartlist_t *sl);
  12. void smartlist_truncate(smartlist_t *sl, int n);
  13. void smartlist_add(smartlist_t *sl, void *element);
  14. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  15. void smartlist_remove(smartlist_t *sl, void *element);
  16. int smartlist_isin(const smartlist_t *sl, void *element);
  17. int smartlist_string_isin(const smartlist_t *sl, const char *element);
  18. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
  19. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  20. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  21. /* smartlist_choose() is defined in crypto.[ch] */
  22. void *smartlist_get(const smartlist_t *sl, int idx);
  23. void *smartlist_set(smartlist_t *sl, int idx, void *val);
  24. void *smartlist_del(smartlist_t *sl, int idx);
  25. void *smartlist_del_keeporder(smartlist_t *sl, int idx);
  26. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  27. int smartlist_len(const smartlist_t *sl);
  28. #define SPLIT_SKIP_SPACE 0x01
  29. #define SPLIT_IGNORE_BLANK 0x02
  30. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  31. int flags, int max);
  32. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
  33. size_t *len_out);
  34. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  35. size_t join_len, int terminate, size_t *len_out);
  36. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  37. do { \
  38. int var ## _sl_idx, var ## _sl_len=smartlist_len(sl); \
  39. type var; \
  40. for(var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
  41. ++var ## _sl_idx) { \
  42. var = smartlist_get((sl),var ## _sl_idx); \
  43. cmd; \
  44. } } while (0)
  45. /* Map from const char * to void*. Implemented with a splay tree. */
  46. typedef struct strmap_t strmap_t;
  47. typedef struct strmap_entry_t strmap_entry_t;
  48. typedef struct strmap_entry_t strmap_iter_t;
  49. strmap_t* strmap_new(void);
  50. void* strmap_set(strmap_t *map, const char *key, void *val);
  51. void* strmap_get(strmap_t *map, const char *key);
  52. void* strmap_remove(strmap_t *map, const char *key);
  53. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  54. void* strmap_get_lc(strmap_t *map, const char *key);
  55. void* strmap_remove_lc(strmap_t *map, const char *key);
  56. typedef void* (*strmap_foreach_fn)(const char *key, void *val, void *data);
  57. void strmap_foreach(strmap_t *map, strmap_foreach_fn fn, void *data);
  58. void strmap_free(strmap_t *map, void (*free_val)(void*));
  59. int strmap_isempty(strmap_t *map);
  60. strmap_iter_t *strmap_iter_init(strmap_t *map);
  61. strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
  62. strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
  63. void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
  64. int strmap_iter_done(strmap_iter_t *iter);
  65. #endif
  66. /*
  67. Local Variables:
  68. mode:c
  69. indent-tabs-mode:nil
  70. c-basic-offset:2
  71. End:
  72. */