container.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_string_num_isin(const smartlist_t *sl, int num);
  31. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
  32. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  33. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  34. /* smartlist_choose() is defined in crypto.[ch] */
  35. #ifndef FAST_SMARTLIST
  36. void *smartlist_get(const smartlist_t *sl, int idx);
  37. void smartlist_set(smartlist_t *sl, int idx, void *val);
  38. int smartlist_len(const smartlist_t *sl);
  39. #else
  40. #define smartlist_get(sl,idx) ((sl)->list[(idx)])
  41. #define smartlist_set(sl,idx,val) ((sl)->list[(idx)] = val)
  42. #define smartlist_len(sl) ((sl)->num_used)
  43. #endif
  44. void smartlist_del(smartlist_t *sl, int idx);
  45. void smartlist_del_keeporder(smartlist_t *sl, int idx);
  46. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  47. #define SPLIT_SKIP_SPACE 0x01
  48. #define SPLIT_IGNORE_BLANK 0x02
  49. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  50. int flags, int max);
  51. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
  52. size_t *len_out);
  53. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  54. size_t join_len, int terminate, size_t *len_out);
  55. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  56. do { \
  57. int var ## _sl_idx, var ## _sl_len=smartlist_len(sl); \
  58. type var; \
  59. for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
  60. ++var ## _sl_idx) { \
  61. var = smartlist_get((sl),var ## _sl_idx); \
  62. cmd; \
  63. } } while (0)
  64. /* Map from const char * to void*. Implemented with a splay tree. */
  65. typedef struct strmap_t strmap_t;
  66. typedef struct strmap_entry_t strmap_entry_t;
  67. typedef struct strmap_entry_t strmap_iter_t;
  68. strmap_t* strmap_new(void);
  69. void* strmap_set(strmap_t *map, const char *key, void *val);
  70. void* strmap_get(strmap_t *map, const char *key);
  71. void* strmap_remove(strmap_t *map, const char *key);
  72. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  73. void* strmap_get_lc(strmap_t *map, const char *key);
  74. void* strmap_remove_lc(strmap_t *map, const char *key);
  75. typedef void* (*strmap_foreach_fn)(const char *key, void *val, void *data);
  76. void strmap_foreach(strmap_t *map, strmap_foreach_fn fn, void *data);
  77. void strmap_free(strmap_t *map, void (*free_val)(void*));
  78. int strmap_isempty(strmap_t *map);
  79. strmap_iter_t *strmap_iter_init(strmap_t *map);
  80. strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
  81. strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
  82. void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
  83. int strmap_iter_done(strmap_iter_t *iter);
  84. #endif