container.h 4.0 KB

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