gnunet_container_lib.h 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-2015 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Christian Grothoff
  18. * @author Nils Durner
  19. *
  20. * @file
  21. * Container classes for GNUnet
  22. *
  23. * @defgroup hashmap Container library: MultiHashMap
  24. * Hash map with multiple values per key.
  25. *
  26. * @see [Documentation](https://gnunet.org/util_multihashmap)
  27. *
  28. * @defgroup heap Container library: Heap
  29. * Min- or max-heap with arbitrary element removal
  30. *
  31. * @defgroup bloomfilter Container library: Bloom filter
  32. * Probabilistic set tests
  33. *
  34. * @defgroup dll Container library: Doubly-linked list
  35. *
  36. * @see [Documentation](https://gnunet.org/mdll-api)
  37. *
  38. * @defgroup metadata Container library: Metadata
  39. * GNU libextractor key-value pairs
  40. */
  41. #ifndef GNUNET_CONTAINER_LIB_H
  42. #define GNUNET_CONTAINER_LIB_H
  43. /* add error and config prototypes */
  44. #include "gnunet_crypto_lib.h"
  45. /**
  46. * Try to compress the given block of data using libz. Only returns
  47. * the compressed block if compression worked and the new block is
  48. * actually smaller. Decompress using #GNUNET_decompress().
  49. *
  50. * @param data block to compress; if compression
  51. * resulted in a smaller block, the first
  52. * bytes of data are updated to the compressed
  53. * data
  54. * @param old_size number of bytes in data
  55. * @param[out] result set to the compressed data, if compression worked
  56. * @param[out] new_size set to size of result, if compression worked
  57. * @return #GNUNET_YES if compression reduce the size,
  58. * #GNUNET_NO if compression did not help
  59. */
  60. int
  61. GNUNET_try_compression (const char *data,
  62. size_t old_size,
  63. char **result,
  64. size_t *new_size);
  65. /**
  66. * Decompress input, return the decompressed data as output. Dual to
  67. * #GNUNET_try_compression(). Caller must set @a output_size to the
  68. * number of bytes that were originally compressed.
  69. *
  70. * @param input compressed data
  71. * @param input_size number of bytes in input
  72. * @param output_size expected size of the output
  73. * @return NULL on error, buffer of @a output_size decompressed bytes otherwise
  74. */
  75. char *
  76. GNUNET_decompress (const char *input, size_t input_size, size_t output_size);
  77. #if HAVE_EXTRACTOR_H
  78. #include <extractor.h>
  79. #else
  80. /* definitions from extractor.h we need for the build */
  81. /**
  82. * Enumeration defining various sources of keywords. See also
  83. * http://dublincore.org/documents/1998/09/dces/
  84. */
  85. enum EXTRACTOR_MetaType
  86. {
  87. EXTRACTOR_METATYPE_RESERVED = 0,
  88. EXTRACTOR_METATYPE_MIMETYPE = 1,
  89. EXTRACTOR_METATYPE_FILENAME = 2,
  90. EXTRACTOR_METATYPE_COMMENT = 3,
  91. EXTRACTOR_METATYPE_TITLE = 4,
  92. EXTRACTOR_METATYPE_BOOK_TITLE = 5,
  93. EXTRACTOR_METATYPE_JOURNAL_NAME = 8,
  94. EXTRACTOR_METATYPE_AUTHOR_NAME = 13,
  95. EXTRACTOR_METATYPE_PUBLICATION_DATE = 24,
  96. EXTRACTOR_METATYPE_URL = 29,
  97. EXTRACTOR_METATYPE_URI = 30,
  98. EXTRACTOR_METATYPE_ISRC = 31,
  99. EXTRACTOR_METATYPE_UNKNOWN = 45,
  100. EXTRACTOR_METATYPE_DESCRIPTION = 46,
  101. EXTRACTOR_METATYPE_KEYWORDS = 49,
  102. EXTRACTOR_METATYPE_SUBJECT = 52,
  103. EXTRACTOR_METATYPE_PACKAGE_NAME = 69,
  104. EXTRACTOR_METATYPE_THUMBNAIL = 114,
  105. EXTRACTOR_METATYPE_ALBUM = 129,
  106. EXTRACTOR_METATYPE_ARTIST = 130,
  107. EXTRACTOR_METATYPE_ORIGINAL_TITLE = 162,
  108. EXTRACTOR_METATYPE_GNUNET_FULL_DATA = 174,
  109. EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME = 180,
  110. };
  111. /**
  112. * Format in which the extracted meta data is presented.
  113. */
  114. enum EXTRACTOR_MetaFormat
  115. {
  116. /**
  117. * Format is unknown.
  118. */
  119. EXTRACTOR_METAFORMAT_UNKNOWN = 0,
  120. /**
  121. * 0-terminated, UTF-8 encoded string. "data_len"
  122. * is strlen(data)+1.
  123. */
  124. EXTRACTOR_METAFORMAT_UTF8 = 1,
  125. /**
  126. * Some kind of binary format, see given Mime type.
  127. */
  128. EXTRACTOR_METAFORMAT_BINARY = 2,
  129. /**
  130. * 0-terminated string. The specific encoding is unknown.
  131. * "data_len" is strlen (data)+1.
  132. */
  133. EXTRACTOR_METAFORMAT_C_STRING = 3
  134. };
  135. /**
  136. * Type of a function that libextractor calls for each
  137. * meta data item found.
  138. *
  139. * @param cls closure (user-defined)
  140. * @param plugin_name name of the plugin that produced this value;
  141. * special values can be used (i.e. '&lt;zlib&gt;' for zlib being
  142. * used in the main libextractor library and yielding
  143. * meta data).
  144. * @param type libextractor-type describing the meta data
  145. * @param format basic format information about @a data
  146. * @param data_mime_type mime-type of @a data (not of the original file);
  147. * can be NULL (if mime-type is not known)
  148. * @param data actual meta-data found
  149. * @param data_len number of bytes in @a data
  150. * @return 0 to continue extracting, 1 to abort
  151. */
  152. typedef int (*EXTRACTOR_MetaDataProcessor) (void *cls,
  153. const char *plugin_name,
  154. enum EXTRACTOR_MetaType type,
  155. enum EXTRACTOR_MetaFormat format,
  156. const char *data_mime_type,
  157. const char *data,
  158. size_t data_len);
  159. #endif
  160. #ifndef EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME
  161. /* hack for LE < 0.6.3 */
  162. #define EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME 180
  163. #endif
  164. #ifdef __cplusplus
  165. extern "C" {
  166. #if 0 /* keep Emacsens' auto-indent happy */
  167. }
  168. #endif
  169. #endif
  170. /* ******************* bloomfilter ***************** */
  171. /**
  172. * @brief bloomfilter representation (opaque)
  173. * @ingroup bloomfilter
  174. */
  175. struct GNUNET_CONTAINER_BloomFilter;
  176. /**
  177. * @ingroup bloomfilter
  178. * Iterator over `struct GNUNET_HashCode`.
  179. *
  180. * @param cls closure
  181. * @param next set to the next hash code
  182. * @return #GNUNET_YES if next was updated
  183. * #GNUNET_NO if there are no more entries
  184. */
  185. typedef int (*GNUNET_CONTAINER_HashCodeIterator) (void *cls,
  186. struct GNUNET_HashCode *next);
  187. /**
  188. * @ingroup bloomfilter
  189. * Load a Bloom filter from a file.
  190. *
  191. * @param filename the name of the file (or the prefix)
  192. * @param size the size of the bloom-filter (number of
  193. * bytes of storage space to use); will be rounded up
  194. * to next power of 2
  195. * @param k the number of #GNUNET_CRYPTO_hash-functions to apply per
  196. * element (number of bits set per element in the set)
  197. * @return the bloomfilter
  198. */
  199. struct GNUNET_CONTAINER_BloomFilter *
  200. GNUNET_CONTAINER_bloomfilter_load (const char *filename,
  201. size_t size,
  202. unsigned int k);
  203. /**
  204. * @ingroup bloomfilter
  205. * Create a Bloom filter from raw bits.
  206. *
  207. * @param data the raw bits in memory (maybe NULL,
  208. * in which case all bits should be considered
  209. * to be zero).
  210. * @param size the size of the bloom-filter (number of
  211. * bytes of storage space to use); also size of @a data
  212. * -- unless data is NULL. Must be a power of 2.
  213. * @param k the number of #GNUNET_CRYPTO_hash-functions to apply per
  214. * element (number of bits set per element in the set)
  215. * @return the bloomfilter
  216. */
  217. struct GNUNET_CONTAINER_BloomFilter *
  218. GNUNET_CONTAINER_bloomfilter_init (const char *data,
  219. size_t size,
  220. unsigned int k);
  221. /**
  222. * @ingroup bloomfilter
  223. * Copy the raw data of this Bloom filter into
  224. * the given data array.
  225. *
  226. * @param data where to write the data
  227. * @param size the size of the given @a data array
  228. * @return #GNUNET_SYSERR if the data array of the wrong size
  229. */
  230. int
  231. GNUNET_CONTAINER_bloomfilter_get_raw_data (
  232. const struct GNUNET_CONTAINER_BloomFilter *bf,
  233. char *data,
  234. size_t size);
  235. /**
  236. * @ingroup bloomfilter
  237. * Test if an element is in the filter.
  238. *
  239. * @param e the element
  240. * @param bf the filter
  241. * @return #GNUNET_YES if the element is in the filter, #GNUNET_NO if not
  242. */
  243. int
  244. GNUNET_CONTAINER_bloomfilter_test (
  245. const struct GNUNET_CONTAINER_BloomFilter *bf,
  246. const struct GNUNET_HashCode *e);
  247. /**
  248. * @ingroup bloomfilter
  249. * Add an element to the filter.
  250. *
  251. * @param bf the filter
  252. * @param e the element
  253. */
  254. void
  255. GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
  256. const struct GNUNET_HashCode *e);
  257. /**
  258. * @ingroup bloomfilter
  259. * Remove an element from the filter.
  260. *
  261. * @param bf the filter
  262. * @param e the element to remove
  263. */
  264. void
  265. GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
  266. const struct GNUNET_HashCode *e);
  267. /**
  268. * @ingroup bloomfilter
  269. * Create a copy of a bloomfilter.
  270. *
  271. * @param bf the filter
  272. * @return copy of bf
  273. */
  274. struct GNUNET_CONTAINER_BloomFilter *
  275. GNUNET_CONTAINER_bloomfilter_copy (
  276. const struct GNUNET_CONTAINER_BloomFilter *bf);
  277. /**
  278. * @ingroup bloomfilter
  279. * Free the space associcated with a filter
  280. * in memory, flush to drive if needed (do not
  281. * free the space on the drive).
  282. *
  283. * @param bf the filter
  284. */
  285. void
  286. GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf);
  287. /**
  288. * Get the number of the addresses set per element in the bloom filter.
  289. *
  290. * @param bf the filter
  291. * @return addresses set per element in the bf
  292. */
  293. size_t
  294. GNUNET_CONTAINER_bloomfilter_get_element_addresses (
  295. const struct GNUNET_CONTAINER_BloomFilter *bf);
  296. /**
  297. * @ingroup bloomfilter
  298. * Get size of the bloom filter.
  299. *
  300. * @param bf the filter
  301. * @return number of bytes used for the data of the bloom filter
  302. */
  303. size_t
  304. GNUNET_CONTAINER_bloomfilter_get_size (
  305. const struct GNUNET_CONTAINER_BloomFilter *bf);
  306. /**
  307. * @ingroup bloomfilter
  308. * Reset a Bloom filter to empty.
  309. *
  310. * @param bf the filter
  311. */
  312. void
  313. GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf);
  314. /**
  315. * @ingroup bloomfilter
  316. * "or" the entries of the given raw data array with the
  317. * data of the given Bloom filter. Assumes that
  318. * the @a size of the @a data array and the current filter
  319. * match.
  320. *
  321. * @param bf the filter
  322. * @param data data to OR-in
  323. * @param size size of @a data
  324. * @return #GNUNET_OK on success
  325. */
  326. int
  327. GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
  328. const char *data,
  329. size_t size);
  330. /**
  331. * @ingroup bloomfilter
  332. * "or" the entries of the given raw data array with the
  333. * data of the given Bloom filter. Assumes that
  334. * the size of the two filters matches.
  335. *
  336. * @param bf the filter
  337. * @param to_or the bloomfilter to or-in
  338. * @return #GNUNET_OK on success
  339. */
  340. int
  341. GNUNET_CONTAINER_bloomfilter_or2 (
  342. struct GNUNET_CONTAINER_BloomFilter *bf,
  343. const struct GNUNET_CONTAINER_BloomFilter *to_or);
  344. /**
  345. * @ingroup bloomfilter
  346. * Resize a bloom filter. Note that this operation
  347. * is pretty costly. Essentially, the Bloom filter
  348. * needs to be completely re-build.
  349. *
  350. * @param bf the filter
  351. * @param iterator an iterator over all elements stored in the BF
  352. * @param iterator_cls closure for @a iterator
  353. * @param size the new size for the filter
  354. * @param k the new number of #GNUNET_CRYPTO_hash-function to apply per element
  355. */
  356. void
  357. GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
  358. GNUNET_CONTAINER_HashCodeIterator iterator,
  359. void *iterator_cls,
  360. size_t size,
  361. unsigned int k);
  362. /* ****************** metadata ******************* */
  363. /**
  364. * @ingroup metadata
  365. * Meta data to associate with a file, directory or namespace.
  366. */
  367. struct GNUNET_CONTAINER_MetaData;
  368. /**
  369. * @ingroup metadata
  370. * Create a fresh meta data container.
  371. *
  372. * @return empty meta-data container
  373. */
  374. struct GNUNET_CONTAINER_MetaData *
  375. GNUNET_CONTAINER_meta_data_create (void);
  376. /**
  377. * @ingroup metadata
  378. * Duplicate a MetaData token.
  379. *
  380. * @param md what to duplicate
  381. * @return duplicate meta-data container
  382. */
  383. struct GNUNET_CONTAINER_MetaData *
  384. GNUNET_CONTAINER_meta_data_duplicate (
  385. const struct GNUNET_CONTAINER_MetaData *md);
  386. /**
  387. * @ingroup metadata
  388. * Free meta data.
  389. *
  390. * @param md what to free
  391. */
  392. void
  393. GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md);
  394. /**
  395. * @ingroup metadata
  396. * Test if two MDs are equal. We consider them equal if
  397. * the meta types, formats and content match (we do not
  398. * include the mime types and plugins names in this
  399. * consideration).
  400. *
  401. * @param md1 first value to check
  402. * @param md2 other value to check
  403. * @return #GNUNET_YES if they are equal
  404. */
  405. int
  406. GNUNET_CONTAINER_meta_data_test_equal (
  407. const struct GNUNET_CONTAINER_MetaData *md1,
  408. const struct GNUNET_CONTAINER_MetaData *md2);
  409. /**
  410. * @ingroup metadata
  411. * Extend metadata.
  412. *
  413. * @param md metadata to extend
  414. * @param plugin_name name of the plugin that produced this value;
  415. * special values can be used (i.e. '&lt;zlib&gt;' for zlib being
  416. * used in the main libextractor library and yielding
  417. * meta data).
  418. * @param type libextractor-type describing the meta data
  419. * @param format basic format information about data
  420. * @param data_mime_type mime-type of data (not of the original file);
  421. * can be NULL (if mime-type is not known)
  422. * @param data actual meta-data found
  423. * @param data_size number of bytes in data
  424. * @return #GNUNET_OK on success, #GNUNET_SYSERR if this entry already exists
  425. * data_mime_type and plugin_name are not considered for "exists" checks
  426. */
  427. int
  428. GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
  429. const char *plugin_name,
  430. enum EXTRACTOR_MetaType type,
  431. enum EXTRACTOR_MetaFormat format,
  432. const char *data_mime_type,
  433. const char *data,
  434. size_t data_size);
  435. /**
  436. * @ingroup metadata
  437. * Extend metadata. Merges the meta data from the second argument
  438. * into the first, discarding duplicate key-value pairs.
  439. *
  440. * @param md metadata to extend
  441. * @param in metadata to merge
  442. */
  443. void
  444. GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
  445. const struct GNUNET_CONTAINER_MetaData *in);
  446. /**
  447. * @ingroup metadata
  448. * Remove an item.
  449. *
  450. * @param md metadata to manipulate
  451. * @param type type of the item to remove
  452. * @param data specific value to remove, NULL to remove all
  453. * entries of the given type
  454. * @param data_size number of bytes in data
  455. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the item does not exist in md
  456. */
  457. int
  458. GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
  459. enum EXTRACTOR_MetaType type,
  460. const char *data,
  461. size_t data_size);
  462. /**
  463. * @ingroup metadata
  464. * Remove all items in the container.
  465. *
  466. * @param md metadata to manipulate
  467. */
  468. void
  469. GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md);
  470. /**
  471. * @ingroup metadata
  472. * Add the current time as the publication date
  473. * to the meta-data.
  474. *
  475. * @param md metadata to modify
  476. */
  477. void
  478. GNUNET_CONTAINER_meta_data_add_publication_date (
  479. struct GNUNET_CONTAINER_MetaData *md);
  480. /**
  481. * @ingroup metadata
  482. * Iterate over MD entries.
  483. *
  484. * @param md metadata to inspect
  485. * @param iter function to call on each entry, return 0 to continue to iterate
  486. * and 1 to abort iteration in this function (GNU libextractor API!)
  487. * @param iter_cls closure for @a iter
  488. * @return number of entries
  489. */
  490. int
  491. GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md,
  492. EXTRACTOR_MetaDataProcessor iter,
  493. void *iter_cls);
  494. /**
  495. * @ingroup metadata
  496. * Get the first MD entry of the given type. Caller
  497. * is responsible for freeing the return value.
  498. * Also, only meta data items that are strings (0-terminated)
  499. * are returned by this function.
  500. *
  501. * @param md metadata to inspect
  502. * @param type type to look for
  503. * @return NULL if no entry was found
  504. */
  505. char *
  506. GNUNET_CONTAINER_meta_data_get_by_type (
  507. const struct GNUNET_CONTAINER_MetaData *md,
  508. enum EXTRACTOR_MetaType type);
  509. /**
  510. * @ingroup metadata
  511. * Get the first matching MD entry of the given types. Caller is
  512. * responsible for freeing the return value. Also, only meta data
  513. * items that are strings (0-terminated) are returned by this
  514. * function.
  515. *
  516. * @param md metadata to inspect
  517. * @param ... -1-terminated list of types
  518. * @return NULL if we do not have any such entry,
  519. * otherwise client is responsible for freeing the value!
  520. */
  521. char *
  522. GNUNET_CONTAINER_meta_data_get_first_by_types (
  523. const struct GNUNET_CONTAINER_MetaData *md,
  524. ...);
  525. /**
  526. * @ingroup metadata
  527. * Get a thumbnail from the meta-data (if present). Only matches meta
  528. * data with mime type "image" and binary format.
  529. *
  530. * @param md metadata to inspect
  531. * @param thumb will be set to the thumbnail data. Must be
  532. * freed by the caller!
  533. * @return number of bytes in thumbnail, 0 if not available
  534. */
  535. size_t
  536. GNUNET_CONTAINER_meta_data_get_thumbnail (
  537. const struct GNUNET_CONTAINER_MetaData *md,
  538. unsigned char **thumb);
  539. /**
  540. * @ingroup metadata
  541. * Options for metadata serialization.
  542. */
  543. enum GNUNET_CONTAINER_MetaDataSerializationOptions
  544. {
  545. /**
  546. * @ingroup metadata
  547. * Serialize all of the data.
  548. */
  549. GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL = 0,
  550. /**
  551. * @ingroup metadata
  552. * If not enough space is available, it is acceptable
  553. * to only serialize some of the metadata.
  554. */
  555. GNUNET_CONTAINER_META_DATA_SERIALIZE_PART = 1,
  556. /**
  557. * @ingroup metadata
  558. * Speed is of the essence, do not allow compression.
  559. */
  560. GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS = 2
  561. };
  562. /**
  563. * @ingroup metadata
  564. * Serialize meta-data to target.
  565. *
  566. * @param md metadata to serialize
  567. * @param target where to write the serialized metadata;
  568. * *target can be NULL, in which case memory is allocated
  569. * @param max maximum number of bytes available
  570. * @param opt is it ok to just write SOME of the
  571. * meta-data to match the size constraint,
  572. * possibly discarding some data?
  573. * @return number of bytes written on success,
  574. * -1 on error (typically: not enough
  575. * space)
  576. */
  577. ssize_t
  578. GNUNET_CONTAINER_meta_data_serialize (
  579. const struct GNUNET_CONTAINER_MetaData *md,
  580. char **target,
  581. size_t max,
  582. enum GNUNET_CONTAINER_MetaDataSerializationOptions opt);
  583. /**
  584. * @ingroup metadata
  585. * Get the size of the full meta-data in serialized form.
  586. *
  587. * @param md metadata to inspect
  588. * @return number of bytes needed for serialization, -1 on error
  589. */
  590. ssize_t
  591. GNUNET_CONTAINER_meta_data_get_serialized_size (
  592. const struct GNUNET_CONTAINER_MetaData *md);
  593. /**
  594. * @ingroup metadata
  595. * Deserialize meta-data. Initializes md.
  596. *
  597. * @param input serialized meta-data.
  598. * @param size number of bytes available
  599. * @return MD on success, NULL on error (i.e.
  600. * bad format)
  601. */
  602. struct GNUNET_CONTAINER_MetaData *
  603. GNUNET_CONTAINER_meta_data_deserialize (const char *input, size_t size);
  604. /* ******************************* HashMap **************************** */
  605. /**
  606. * @ingroup hashmap
  607. * Opaque handle for a HashMap.
  608. */
  609. struct GNUNET_CONTAINER_MultiHashMap;
  610. /**
  611. * @ingroup hashmap
  612. * Opaque handle to an iterator over
  613. * a multihashmap.
  614. */
  615. struct GNUNET_CONTAINER_MultiHashMapIterator;
  616. /**
  617. * @ingroup hashmap
  618. * Options for storing values in the HashMap.
  619. */
  620. enum GNUNET_CONTAINER_MultiHashMapOption
  621. {
  622. /**
  623. * @ingroup hashmap
  624. * If a value with the given key exists, replace it. Note that the
  625. * old value would NOT be freed by replace (the application has to
  626. * make sure that this happens if required).
  627. */
  628. GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE,
  629. /**
  630. * @ingroup hashmap
  631. * Allow multiple values with the same key.
  632. */
  633. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE,
  634. /**
  635. * @ingroup hashmap
  636. * There must only be one value per key; storing a value should fail
  637. * if a value under the same key already exists.
  638. */
  639. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY,
  640. /**
  641. * @ingroup hashmap There must only be one value per key, but don't
  642. * bother checking if a value already exists (faster than
  643. * #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY; implemented
  644. * just like #GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE but this
  645. * option documents better what is intended if
  646. * #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY is what is
  647. * desired).
  648. */
  649. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
  650. };
  651. /**
  652. * @ingroup hashmap
  653. * Iterator over hash map entries.
  654. *
  655. * @param cls closure
  656. * @param key current key code
  657. * @param value value in the hash map
  658. * @return #GNUNET_YES if we should continue to
  659. * iterate,
  660. * #GNUNET_NO if not.
  661. */
  662. typedef int (*GNUNET_CONTAINER_MulitHashMapIteratorCallback) (
  663. void *cls,
  664. const struct GNUNET_HashCode *key,
  665. void *value);
  666. /**
  667. * @ingroup hashmap
  668. * Create a multi hash map.
  669. *
  670. * @param len initial size (map will grow as needed)
  671. * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
  672. * #GNUNET_YES means that on 'put', the 'key' does not have
  673. * to be copied as the destination of the pointer is
  674. * guaranteed to be life as long as the value is stored in
  675. * the hashmap. This can significantly reduce memory
  676. * consumption, but of course is also a recipie for
  677. * heap corruption if the assumption is not true. Only
  678. * use this if (1) memory use is important in this case and
  679. * (2) you have triple-checked that the invariant holds
  680. * @return NULL on error
  681. */
  682. struct GNUNET_CONTAINER_MultiHashMap *
  683. GNUNET_CONTAINER_multihashmap_create (unsigned int len,
  684. int do_not_copy_keys);
  685. /**
  686. * @ingroup hashmap
  687. * Destroy a hash map. Will not free any values
  688. * stored in the hash map!
  689. *
  690. * @param map the map
  691. */
  692. void
  693. GNUNET_CONTAINER_multihashmap_destroy (struct
  694. GNUNET_CONTAINER_MultiHashMap *map);
  695. /**
  696. * @ingroup hashmap
  697. * Given a key find a value in the map matching the key.
  698. *
  699. * @param map the map
  700. * @param key what to look for
  701. * @return NULL if no value was found; note that
  702. * this is indistinguishable from values that just
  703. * happen to be NULL; use "contains" to test for
  704. * key-value pairs with value NULL
  705. */
  706. void *
  707. GNUNET_CONTAINER_multihashmap_get (
  708. const struct GNUNET_CONTAINER_MultiHashMap *map,
  709. const struct GNUNET_HashCode *key);
  710. /**
  711. * @ingroup hashmap
  712. * Remove the given key-value pair from the map. Note that if the
  713. * key-value pair is in the map multiple times, only one of the pairs
  714. * will be removed.
  715. *
  716. * @param map the map
  717. * @param key key of the key-value pair
  718. * @param value value of the key-value pair
  719. * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
  720. * is not in the map
  721. */
  722. int
  723. GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
  724. const struct GNUNET_HashCode *key,
  725. const void *value);
  726. /**
  727. * @ingroup hashmap
  728. * Remove all entries for the given key from the map.
  729. * Note that the values would not be "freed".
  730. *
  731. * @param map the map
  732. * @param key identifies values to be removed
  733. * @return number of values removed
  734. */
  735. int
  736. GNUNET_CONTAINER_multihashmap_remove_all (
  737. struct GNUNET_CONTAINER_MultiHashMap *map,
  738. const struct GNUNET_HashCode *key);
  739. /**
  740. * @ingroup hashmap
  741. * Remove all entries from the map.
  742. * Note that the values would not be "freed".
  743. *
  744. * @param map the map
  745. * @return number of values removed
  746. */
  747. unsigned int
  748. GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map);
  749. /**
  750. * @ingroup hashmap
  751. * Check if the map contains any value under the given
  752. * key (including values that are NULL).
  753. *
  754. * @param map the map
  755. * @param key the key to test if a value exists for it
  756. * @return #GNUNET_YES if such a value exists,
  757. * #GNUNET_NO if not
  758. */
  759. int
  760. GNUNET_CONTAINER_multihashmap_contains (
  761. const struct GNUNET_CONTAINER_MultiHashMap *map,
  762. const struct GNUNET_HashCode *key);
  763. /**
  764. * @ingroup hashmap
  765. * Check if the map contains the given value under the given
  766. * key.
  767. *
  768. * @param map the map
  769. * @param key the key to test if a value exists for it
  770. * @param value value to test for
  771. * @return #GNUNET_YES if such a value exists,
  772. * #GNUNET_NO if not
  773. */
  774. int
  775. GNUNET_CONTAINER_multihashmap_contains_value (
  776. const struct GNUNET_CONTAINER_MultiHashMap *map,
  777. const struct GNUNET_HashCode *key,
  778. const void *value);
  779. /**
  780. * @ingroup hashmap
  781. * Store a key-value pair in the map.
  782. *
  783. * @param map the map
  784. * @param key key to use
  785. * @param value value to use
  786. * @param opt options for put
  787. * @return #GNUNET_OK on success,
  788. * #GNUNET_NO if a value was replaced (with REPLACE)
  789. * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
  790. * value already exists
  791. */
  792. int
  793. GNUNET_CONTAINER_multihashmap_put (
  794. struct GNUNET_CONTAINER_MultiHashMap *map,
  795. const struct GNUNET_HashCode *key,
  796. void *value,
  797. enum GNUNET_CONTAINER_MultiHashMapOption opt);
  798. /**
  799. * @ingroup hashmap
  800. * Get the number of key-value pairs in the map.
  801. *
  802. * @param map the map
  803. * @return the number of key value pairs
  804. */
  805. unsigned int
  806. GNUNET_CONTAINER_multihashmap_size (
  807. const struct GNUNET_CONTAINER_MultiHashMap *map);
  808. /**
  809. * @ingroup hashmap
  810. * Iterate over all entries in the map.
  811. *
  812. * @param map the map
  813. * @param it function to call on each entry
  814. * @param it_cls extra argument to @a it
  815. * @return the number of key value pairs processed,
  816. * #GNUNET_SYSERR if it aborted iteration
  817. */
  818. int
  819. GNUNET_CONTAINER_multihashmap_iterate (
  820. struct GNUNET_CONTAINER_MultiHashMap *map,
  821. GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
  822. void *it_cls);
  823. /**
  824. * @ingroup hashmap
  825. * Create an iterator for a multihashmap.
  826. * The iterator can be used to retrieve all the elements in the multihashmap
  827. * one by one, without having to handle all elements at once (in contrast to
  828. * #GNUNET_CONTAINER_multihashmap_iterate). Note that the iterator can not be
  829. * used anymore if elements have been removed from 'map' after the creation of
  830. * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
  831. * result in skipped or repeated elements.
  832. *
  833. * @param map the map to create an iterator for
  834. * @return an iterator over the given multihashmap @a map
  835. */
  836. struct GNUNET_CONTAINER_MultiHashMapIterator *
  837. GNUNET_CONTAINER_multihashmap_iterator_create (
  838. const struct GNUNET_CONTAINER_MultiHashMap *map);
  839. /**
  840. * @ingroup hashmap
  841. * Retrieve the next element from the hash map at the iterator's
  842. * position. If there are no elements left, #GNUNET_NO is returned,
  843. * and @a key and @a value are not modified. This operation is only
  844. * allowed if no elements have been removed from the multihashmap
  845. * since the creation of @a iter, and the map has not been destroyed.
  846. * Adding elements may result in repeating or skipping elements.
  847. *
  848. * @param iter the iterator to get the next element from
  849. * @param key pointer to store the key in, can be NULL
  850. * @param value pointer to store the value in, can be NULL
  851. * @return #GNUNET_YES we returned an element,
  852. * #GNUNET_NO if we are out of elements
  853. */
  854. int
  855. GNUNET_CONTAINER_multihashmap_iterator_next (
  856. struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
  857. struct GNUNET_HashCode *key,
  858. const void **value);
  859. /**
  860. * @ingroup hashmap
  861. * Destroy a multihashmap iterator.
  862. *
  863. * @param iter the iterator to destroy
  864. */
  865. void
  866. GNUNET_CONTAINER_multihashmap_iterator_destroy (
  867. struct GNUNET_CONTAINER_MultiHashMapIterator *iter);
  868. /**
  869. * @ingroup hashmap
  870. * Iterate over all entries in the map that match a particular key.
  871. *
  872. * @param map the map
  873. * @param key key that the entries must correspond to
  874. * @param it function to call on each entry
  875. * @param it_cls extra argument to @a it
  876. * @return the number of key value pairs processed,
  877. * #GNUNET_SYSERR if it aborted iteration
  878. */
  879. int
  880. GNUNET_CONTAINER_multihashmap_get_multiple (
  881. struct GNUNET_CONTAINER_MultiHashMap *map,
  882. const struct GNUNET_HashCode *key,
  883. GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
  884. void *it_cls);
  885. /**
  886. * @ingroup hashmap
  887. * Call @a it on a random value from the map, or not at all
  888. * if the map is empty. Note that this function has linear
  889. * complexity (in the size of the map).
  890. *
  891. * @param map the map
  892. * @param it function to call on a random entry
  893. * @param it_cls extra argument to @a it
  894. * @return the number of key value pairs processed, zero or one.
  895. */
  896. unsigned int
  897. GNUNET_CONTAINER_multihashmap_get_random (
  898. const struct GNUNET_CONTAINER_MultiHashMap *map,
  899. GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
  900. void *it_cls);
  901. /* ***************** Version of Multihashmap for peer identities ****************** */
  902. /**
  903. * @ingroup hashmap
  904. * Iterator over hash map entries.
  905. *
  906. * @param cls closure
  907. * @param key current public key
  908. * @param value value in the hash map
  909. * @return #GNUNET_YES if we should continue to
  910. * iterate,
  911. * #GNUNET_NO if not.
  912. */
  913. typedef int (*GNUNET_CONTAINER_PeerMapIterator) (
  914. void *cls,
  915. const struct GNUNET_PeerIdentity *key,
  916. void *value);
  917. /**
  918. * Hash map from peer identities to values.
  919. */
  920. struct GNUNET_CONTAINER_MultiPeerMap;
  921. /**
  922. * @ingroup hashmap
  923. * Create a multi peer map (hash map for public keys of peers).
  924. *
  925. * @param len initial size (map will grow as needed)
  926. * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
  927. * #GNUNET_YES means that on 'put', the 'key' does not have
  928. * to be copied as the destination of the pointer is
  929. * guaranteed to be life as long as the value is stored in
  930. * the hashmap. This can significantly reduce memory
  931. * consumption, but of course is also a recipie for
  932. * heap corruption if the assumption is not true. Only
  933. * use this if (1) memory use is important in this case and
  934. * (2) you have triple-checked that the invariant holds
  935. * @return NULL on error
  936. */
  937. struct GNUNET_CONTAINER_MultiPeerMap *
  938. GNUNET_CONTAINER_multipeermap_create (unsigned int len, int do_not_copy_keys);
  939. /**
  940. * @ingroup hashmap
  941. * Destroy a hash map. Will not free any values
  942. * stored in the hash map!
  943. *
  944. * @param map the map
  945. */
  946. void
  947. GNUNET_CONTAINER_multipeermap_destroy (
  948. struct GNUNET_CONTAINER_MultiPeerMap *map);
  949. /**
  950. * @ingroup hashmap
  951. * Given a key find a value in the map matching the key.
  952. *
  953. * @param map the map
  954. * @param key what to look for
  955. * @return NULL if no value was found; note that
  956. * this is indistinguishable from values that just
  957. * happen to be NULL; use "contains" to test for
  958. * key-value pairs with value NULL
  959. */
  960. void *
  961. GNUNET_CONTAINER_multipeermap_get (
  962. const struct GNUNET_CONTAINER_MultiPeerMap *map,
  963. const struct GNUNET_PeerIdentity *key);
  964. /**
  965. * @ingroup hashmap
  966. * Remove the given key-value pair from the map. Note that if the
  967. * key-value pair is in the map multiple times, only one of the pairs
  968. * will be removed.
  969. *
  970. * @param map the map
  971. * @param key key of the key-value pair
  972. * @param value value of the key-value pair
  973. * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
  974. * is not in the map
  975. */
  976. int
  977. GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
  978. const struct GNUNET_PeerIdentity *key,
  979. const void *value);
  980. /**
  981. * @ingroup hashmap
  982. * Remove all entries for the given key from the map.
  983. * Note that the values would not be "freed".
  984. *
  985. * @param map the map
  986. * @param key identifies values to be removed
  987. * @return number of values removed
  988. */
  989. int
  990. GNUNET_CONTAINER_multipeermap_remove_all (
  991. struct GNUNET_CONTAINER_MultiPeerMap *map,
  992. const struct GNUNET_PeerIdentity *key);
  993. /**
  994. * @ingroup hashmap
  995. * Check if the map contains any value under the given
  996. * key (including values that are NULL).
  997. *
  998. * @param map the map
  999. * @param key the key to test if a value exists for it
  1000. * @return #GNUNET_YES if such a value exists,
  1001. * #GNUNET_NO if not
  1002. */
  1003. int
  1004. GNUNET_CONTAINER_multipeermap_contains (
  1005. const struct GNUNET_CONTAINER_MultiPeerMap *map,
  1006. const struct GNUNET_PeerIdentity *key);
  1007. /**
  1008. * @ingroup hashmap
  1009. * Check if the map contains the given value under the given
  1010. * key.
  1011. *
  1012. * @param map the map
  1013. * @param key the key to test if a value exists for it
  1014. * @param value value to test for
  1015. * @return #GNUNET_YES if such a value exists,
  1016. * #GNUNET_NO if not
  1017. */
  1018. int
  1019. GNUNET_CONTAINER_multipeermap_contains_value (
  1020. const struct GNUNET_CONTAINER_MultiPeerMap *map,
  1021. const struct GNUNET_PeerIdentity *key,
  1022. const void *value);
  1023. /**
  1024. * @ingroup hashmap
  1025. * Store a key-value pair in the map.
  1026. *
  1027. * @param map the map
  1028. * @param key key to use
  1029. * @param value value to use
  1030. * @param opt options for put
  1031. * @return #GNUNET_OK on success,
  1032. * #GNUNET_NO if a value was replaced (with REPLACE)
  1033. * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
  1034. * value already exists
  1035. */
  1036. int
  1037. GNUNET_CONTAINER_multipeermap_put (
  1038. struct GNUNET_CONTAINER_MultiPeerMap *map,
  1039. const struct GNUNET_PeerIdentity *key,
  1040. void *value,
  1041. enum GNUNET_CONTAINER_MultiHashMapOption opt);
  1042. /**
  1043. * @ingroup hashmap
  1044. * Get the number of key-value pairs in the map.
  1045. *
  1046. * @param map the map
  1047. * @return the number of key value pairs
  1048. */
  1049. unsigned int
  1050. GNUNET_CONTAINER_multipeermap_size (
  1051. const struct GNUNET_CONTAINER_MultiPeerMap *map);
  1052. /**
  1053. * @ingroup hashmap
  1054. * Iterate over all entries in the map.
  1055. *
  1056. * @param map the map
  1057. * @param it function to call on each entry
  1058. * @param it_cls extra argument to @a it
  1059. * @return the number of key value pairs processed,
  1060. * #GNUNET_SYSERR if it aborted iteration
  1061. */
  1062. int
  1063. GNUNET_CONTAINER_multipeermap_iterate (
  1064. struct GNUNET_CONTAINER_MultiPeerMap *map,
  1065. GNUNET_CONTAINER_PeerMapIterator it,
  1066. void *it_cls);
  1067. struct GNUNET_CONTAINER_MultiPeerMapIterator;
  1068. /**
  1069. * @ingroup hashmap
  1070. * Create an iterator for a multihashmap.
  1071. * The iterator can be used to retrieve all the elements in the multihashmap
  1072. * one by one, without having to handle all elements at once (in contrast to
  1073. * #GNUNET_CONTAINER_multipeermap_iterate). Note that the iterator can not be
  1074. * used anymore if elements have been removed from @a map after the creation of
  1075. * the iterator, or 'map' has been destroyed. Adding elements to @a map may
  1076. * result in skipped or repeated elements.
  1077. *
  1078. * @param map the map to create an iterator for
  1079. * @return an iterator over the given multihashmap @a map
  1080. */
  1081. struct GNUNET_CONTAINER_MultiPeerMapIterator *
  1082. GNUNET_CONTAINER_multipeermap_iterator_create (
  1083. const struct GNUNET_CONTAINER_MultiPeerMap *map);
  1084. /**
  1085. * @ingroup hashmap
  1086. * Retrieve the next element from the hash map at the iterator's
  1087. * position. If there are no elements left, #GNUNET_NO is returned,
  1088. * and @a key and @a value are not modified. This operation is only
  1089. * allowed if no elements have been removed from the multihashmap
  1090. * since the creation of @a iter, and the map has not been destroyed.
  1091. * Adding elements may result in repeating or skipping elements.
  1092. *
  1093. * @param iter the iterator to get the next element from
  1094. * @param key pointer to store the key in, can be NULL
  1095. * @param value pointer to store the value in, can be NULL
  1096. * @return #GNUNET_YES we returned an element,
  1097. * #GNUNET_NO if we are out of elements
  1098. */
  1099. int
  1100. GNUNET_CONTAINER_multipeermap_iterator_next (
  1101. struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
  1102. struct GNUNET_PeerIdentity *key,
  1103. const void **value);
  1104. /**
  1105. * @ingroup hashmap
  1106. * Destroy a multipeermap iterator.
  1107. *
  1108. * @param iter the iterator to destroy
  1109. */
  1110. void
  1111. GNUNET_CONTAINER_multipeermap_iterator_destroy (
  1112. struct GNUNET_CONTAINER_MultiPeerMapIterator *iter);
  1113. /**
  1114. * @ingroup hashmap
  1115. * Iterate over all entries in the map that match a particular key.
  1116. *
  1117. * @param map the map
  1118. * @param key public key that the entries must correspond to
  1119. * @param it function to call on each entry
  1120. * @param it_cls extra argument to @a it
  1121. * @return the number of key value pairs processed,
  1122. * #GNUNET_SYSERR if it aborted iteration
  1123. */
  1124. int
  1125. GNUNET_CONTAINER_multipeermap_get_multiple (
  1126. struct GNUNET_CONTAINER_MultiPeerMap *map,
  1127. const struct GNUNET_PeerIdentity *key,
  1128. GNUNET_CONTAINER_PeerMapIterator it,
  1129. void *it_cls);
  1130. /**
  1131. * @ingroup hashmap
  1132. * Call @a it on a random value from the map, or not at all
  1133. * if the map is empty. Note that this function has linear
  1134. * complexity (in the size of the map).
  1135. *
  1136. * @param map the map
  1137. * @param it function to call on a random entry
  1138. * @param it_cls extra argument to @a it
  1139. * @return the number of key value pairs processed, zero or one.
  1140. */
  1141. unsigned int
  1142. GNUNET_CONTAINER_multipeermap_get_random (
  1143. const struct GNUNET_CONTAINER_MultiPeerMap *map,
  1144. GNUNET_CONTAINER_PeerMapIterator it,
  1145. void *it_cls);
  1146. /* ***************** Version of Multihashmap for short hashes ****************** */
  1147. /**
  1148. * @ingroup hashmap
  1149. * Iterator over hash map entries.
  1150. *
  1151. * @param cls closure
  1152. * @param key current public key
  1153. * @param value value in the hash map
  1154. * @return #GNUNET_YES if we should continue to
  1155. * iterate,
  1156. * #GNUNET_NO if not.
  1157. */
  1158. typedef int (*GNUNET_CONTAINER_ShortmapIterator) (
  1159. void *cls,
  1160. const struct GNUNET_ShortHashCode *key,
  1161. void *value);
  1162. /**
  1163. * Hash map from peer identities to values.
  1164. */
  1165. struct GNUNET_CONTAINER_MultiShortmap;
  1166. /**
  1167. * @ingroup hashmap
  1168. * Create a multi peer map (hash map for public keys of peers).
  1169. *
  1170. * @param len initial size (map will grow as needed)
  1171. * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
  1172. * #GNUNET_YES means that on 'put', the 'key' does not have
  1173. * to be copied as the destination of the pointer is
  1174. * guaranteed to be life as long as the value is stored in
  1175. * the hashmap. This can significantly reduce memory
  1176. * consumption, but of course is also a recipie for
  1177. * heap corruption if the assumption is not true. Only
  1178. * use this if (1) memory use is important in this case and
  1179. * (2) you have triple-checked that the invariant holds
  1180. * @return NULL on error
  1181. */
  1182. struct GNUNET_CONTAINER_MultiShortmap *
  1183. GNUNET_CONTAINER_multishortmap_create (unsigned int len, int do_not_copy_keys);
  1184. /**
  1185. * @ingroup hashmap
  1186. * Destroy a hash map. Will not free any values
  1187. * stored in the hash map!
  1188. *
  1189. * @param map the map
  1190. */
  1191. void
  1192. GNUNET_CONTAINER_multishortmap_destroy (
  1193. struct GNUNET_CONTAINER_MultiShortmap *map);
  1194. /**
  1195. * @ingroup hashmap
  1196. * Given a key find a value in the map matching the key.
  1197. *
  1198. * @param map the map
  1199. * @param key what to look for
  1200. * @return NULL if no value was found; note that
  1201. * this is indistinguishable from values that just
  1202. * happen to be NULL; use "contains" to test for
  1203. * key-value pairs with value NULL
  1204. */
  1205. void *
  1206. GNUNET_CONTAINER_multishortmap_get (
  1207. const struct GNUNET_CONTAINER_MultiShortmap *map,
  1208. const struct GNUNET_ShortHashCode *key);
  1209. /**
  1210. * @ingroup hashmap
  1211. * Remove the given key-value pair from the map. Note that if the
  1212. * key-value pair is in the map multiple times, only one of the pairs
  1213. * will be removed.
  1214. *
  1215. * @param map the map
  1216. * @param key key of the key-value pair
  1217. * @param value value of the key-value pair
  1218. * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
  1219. * is not in the map
  1220. */
  1221. int
  1222. GNUNET_CONTAINER_multishortmap_remove (
  1223. struct GNUNET_CONTAINER_MultiShortmap *map,
  1224. const struct GNUNET_ShortHashCode *key,
  1225. const void *value);
  1226. /**
  1227. * @ingroup hashmap
  1228. * Remove all entries for the given key from the map.
  1229. * Note that the values would not be "freed".
  1230. *
  1231. * @param map the map
  1232. * @param key identifies values to be removed
  1233. * @return number of values removed
  1234. */
  1235. int
  1236. GNUNET_CONTAINER_multishortmap_remove_all (
  1237. struct GNUNET_CONTAINER_MultiShortmap *map,
  1238. const struct GNUNET_ShortHashCode *key);
  1239. /**
  1240. * @ingroup hashmap
  1241. * Check if the map contains any value under the given
  1242. * key (including values that are NULL).
  1243. *
  1244. * @param map the map
  1245. * @param key the key to test if a value exists for it
  1246. * @return #GNUNET_YES if such a value exists,
  1247. * #GNUNET_NO if not
  1248. */
  1249. int
  1250. GNUNET_CONTAINER_multishortmap_contains (
  1251. const struct GNUNET_CONTAINER_MultiShortmap *map,
  1252. const struct GNUNET_ShortHashCode *key);
  1253. /**
  1254. * @ingroup hashmap
  1255. * Check if the map contains the given value under the given
  1256. * key.
  1257. *
  1258. * @param map the map
  1259. * @param key the key to test if a value exists for it
  1260. * @param value value to test for
  1261. * @return #GNUNET_YES if such a value exists,
  1262. * #GNUNET_NO if not
  1263. */
  1264. int
  1265. GNUNET_CONTAINER_multishortmap_contains_value (
  1266. const struct GNUNET_CONTAINER_MultiShortmap *map,
  1267. const struct GNUNET_ShortHashCode *key,
  1268. const void *value);
  1269. /**
  1270. * @ingroup hashmap
  1271. * Store a key-value pair in the map.
  1272. *
  1273. * @param map the map
  1274. * @param key key to use
  1275. * @param value value to use
  1276. * @param opt options for put
  1277. * @return #GNUNET_OK on success,
  1278. * #GNUNET_NO if a value was replaced (with REPLACE)
  1279. * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
  1280. * value already exists
  1281. */
  1282. int
  1283. GNUNET_CONTAINER_multishortmap_put (
  1284. struct GNUNET_CONTAINER_MultiShortmap *map,
  1285. const struct GNUNET_ShortHashCode *key,
  1286. void *value,
  1287. enum GNUNET_CONTAINER_MultiHashMapOption opt);
  1288. /**
  1289. * @ingroup hashmap
  1290. * Get the number of key-value pairs in the map.
  1291. *
  1292. * @param map the map
  1293. * @return the number of key value pairs
  1294. */
  1295. unsigned int
  1296. GNUNET_CONTAINER_multishortmap_size (
  1297. const struct GNUNET_CONTAINER_MultiShortmap *map);
  1298. /**
  1299. * @ingroup hashmap
  1300. * Iterate over all entries in the map.
  1301. *
  1302. * @param map the map
  1303. * @param it function to call on each entry
  1304. * @param it_cls extra argument to @a it
  1305. * @return the number of key value pairs processed,
  1306. * #GNUNET_SYSERR if it aborted iteration
  1307. */
  1308. int
  1309. GNUNET_CONTAINER_multishortmap_iterate (
  1310. struct GNUNET_CONTAINER_MultiShortmap *map,
  1311. GNUNET_CONTAINER_ShortmapIterator it,
  1312. void *it_cls);
  1313. struct GNUNET_CONTAINER_MultiShortmapIterator;
  1314. /**
  1315. * @ingroup hashmap
  1316. * Create an iterator for a multihashmap.
  1317. * The iterator can be used to retrieve all the elements in the multihashmap
  1318. * one by one, without having to handle all elements at once (in contrast to
  1319. * #GNUNET_CONTAINER_multishortmap_iterate). Note that the iterator can not be
  1320. * used anymore if elements have been removed from @a map after the creation of
  1321. * the iterator, or 'map' has been destroyed. Adding elements to @a map may
  1322. * result in skipped or repeated elements.
  1323. *
  1324. * @param map the map to create an iterator for
  1325. * @return an iterator over the given multihashmap @a map
  1326. */
  1327. struct GNUNET_CONTAINER_MultiShortmapIterator *
  1328. GNUNET_CONTAINER_multishortmap_iterator_create (
  1329. const struct GNUNET_CONTAINER_MultiShortmap *map);
  1330. /**
  1331. * @ingroup hashmap
  1332. * Retrieve the next element from the hash map at the iterator's
  1333. * position. If there are no elements left, #GNUNET_NO is returned,
  1334. * and @a key and @a value are not modified. This operation is only
  1335. * allowed if no elements have been removed from the multihashmap
  1336. * since the creation of @a iter, and the map has not been destroyed.
  1337. * Adding elements may result in repeating or skipping elements.
  1338. *
  1339. * @param iter the iterator to get the next element from
  1340. * @param key pointer to store the key in, can be NULL
  1341. * @param value pointer to store the value in, can be NULL
  1342. * @return #GNUNET_YES we returned an element,
  1343. * #GNUNET_NO if we are out of elements
  1344. */
  1345. int
  1346. GNUNET_CONTAINER_multishortmap_iterator_next (
  1347. struct GNUNET_CONTAINER_MultiShortmapIterator *iter,
  1348. struct GNUNET_ShortHashCode *key,
  1349. const void **value);
  1350. /**
  1351. * @ingroup hashmap
  1352. * Destroy a multishortmap iterator.
  1353. *
  1354. * @param iter the iterator to destroy
  1355. */
  1356. void
  1357. GNUNET_CONTAINER_multishortmap_iterator_destroy (
  1358. struct GNUNET_CONTAINER_MultiShortmapIterator *iter);
  1359. /**
  1360. * @ingroup hashmap
  1361. * Iterate over all entries in the map that match a particular key.
  1362. *
  1363. * @param map the map
  1364. * @param key public key that the entries must correspond to
  1365. * @param it function to call on each entry
  1366. * @param it_cls extra argument to @a it
  1367. * @return the number of key value pairs processed,
  1368. * #GNUNET_SYSERR if it aborted iteration
  1369. */
  1370. int
  1371. GNUNET_CONTAINER_multishortmap_get_multiple (
  1372. struct GNUNET_CONTAINER_MultiShortmap *map,
  1373. const struct GNUNET_ShortHashCode *key,
  1374. GNUNET_CONTAINER_ShortmapIterator it,
  1375. void *it_cls);
  1376. /**
  1377. * @ingroup hashmap
  1378. * Call @a it on a random value from the map, or not at all
  1379. * if the map is empty. Note that this function has linear
  1380. * complexity (in the size of the map).
  1381. *
  1382. * @param map the map
  1383. * @param it function to call on a random entry
  1384. * @param it_cls extra argument to @a it
  1385. * @return the number of key value pairs processed, zero or one.
  1386. */
  1387. unsigned int
  1388. GNUNET_CONTAINER_multishortmap_get_random (
  1389. const struct GNUNET_CONTAINER_MultiShortmap *map,
  1390. GNUNET_CONTAINER_ShortmapIterator it,
  1391. void *it_cls);
  1392. /* ***************** Version of Multihashmap for UUIDs ****************** */
  1393. /**
  1394. * @ingroup hashmap
  1395. * Iterator over uuid map entries.
  1396. *
  1397. * @param cls closure
  1398. * @param key current public key
  1399. * @param value value in the hash map
  1400. * @return #GNUNET_YES if we should continue to
  1401. * iterate,
  1402. * #GNUNET_NO if not.
  1403. */
  1404. typedef int (*GNUNET_CONTAINER_MultiUuidmapIteratorCallback) (
  1405. void *cls,
  1406. const struct GNUNET_Uuid *key,
  1407. void *value);
  1408. /**
  1409. * Hash map from peer identities to values.
  1410. */
  1411. struct GNUNET_CONTAINER_MultiUuidmap;
  1412. /**
  1413. * @ingroup hashmap
  1414. * Create a multi peer map (hash map for public keys of peers).
  1415. *
  1416. * @param len initial size (map will grow as needed)
  1417. * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
  1418. * #GNUNET_YES means that on 'put', the 'key' does not have
  1419. * to be copied as the destination of the pointer is
  1420. * guaranteed to be life as long as the value is stored in
  1421. * the hashmap. This can significantly reduce memory
  1422. * consumption, but of course is also a recipie for
  1423. * heap corruption if the assumption is not true. Only
  1424. * use this if (1) memory use is important in this case and
  1425. * (2) you have triple-checked that the invariant holds
  1426. * @return NULL on error
  1427. */
  1428. struct GNUNET_CONTAINER_MultiUuidmap *
  1429. GNUNET_CONTAINER_multiuuidmap_create (unsigned int len, int do_not_copy_keys);
  1430. /**
  1431. * @ingroup hashmap
  1432. * Destroy a hash map. Will not free any values
  1433. * stored in the hash map!
  1434. *
  1435. * @param map the map
  1436. */
  1437. void
  1438. GNUNET_CONTAINER_multiuuidmap_destroy (
  1439. struct GNUNET_CONTAINER_MultiUuidmap *map);
  1440. /**
  1441. * @ingroup hashmap
  1442. * Given a key find a value in the map matching the key.
  1443. *
  1444. * @param map the map
  1445. * @param key what to look for
  1446. * @return NULL if no value was found; note that
  1447. * this is indistinguishable from values that just
  1448. * happen to be NULL; use "contains" to test for
  1449. * key-value pairs with value NULL
  1450. */
  1451. void *
  1452. GNUNET_CONTAINER_multiuuidmap_get (
  1453. const struct GNUNET_CONTAINER_MultiUuidmap *map,
  1454. const struct GNUNET_Uuid *key);
  1455. /**
  1456. * @ingroup hashmap
  1457. * Remove the given key-value pair from the map. Note that if the
  1458. * key-value pair is in the map multiple times, only one of the pairs
  1459. * will be removed.
  1460. *
  1461. * @param map the map
  1462. * @param key key of the key-value pair
  1463. * @param value value of the key-value pair
  1464. * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
  1465. * is not in the map
  1466. */
  1467. int
  1468. GNUNET_CONTAINER_multiuuidmap_remove (struct GNUNET_CONTAINER_MultiUuidmap *map,
  1469. const struct GNUNET_Uuid *key,
  1470. const void *value);
  1471. /**
  1472. * @ingroup hashmap
  1473. * Remove all entries for the given key from the map.
  1474. * Note that the values would not be "freed".
  1475. *
  1476. * @param map the map
  1477. * @param key identifies values to be removed
  1478. * @return number of values removed
  1479. */
  1480. int
  1481. GNUNET_CONTAINER_multiuuidmap_remove_all (
  1482. struct GNUNET_CONTAINER_MultiUuidmap *map,
  1483. const struct GNUNET_Uuid *key);
  1484. /**
  1485. * @ingroup hashmap
  1486. * Check if the map contains any value under the given
  1487. * key (including values that are NULL).
  1488. *
  1489. * @param map the map
  1490. * @param key the key to test if a value exists for it
  1491. * @return #GNUNET_YES if such a value exists,
  1492. * #GNUNET_NO if not
  1493. */
  1494. int
  1495. GNUNET_CONTAINER_multiuuidmap_contains (
  1496. const struct GNUNET_CONTAINER_MultiUuidmap *map,
  1497. const struct GNUNET_Uuid *key);
  1498. /**
  1499. * @ingroup hashmap
  1500. * Check if the map contains the given value under the given
  1501. * key.
  1502. *
  1503. * @param map the map
  1504. * @param key the key to test if a value exists for it
  1505. * @param value value to test for
  1506. * @return #GNUNET_YES if such a value exists,
  1507. * #GNUNET_NO if not
  1508. */
  1509. int
  1510. GNUNET_CONTAINER_multiuuidmap_contains_value (
  1511. const struct GNUNET_CONTAINER_MultiUuidmap *map,
  1512. const struct GNUNET_Uuid *key,
  1513. const void *value);
  1514. /**
  1515. * @ingroup hashmap
  1516. * Store a key-value pair in the map.
  1517. *
  1518. * @param map the map
  1519. * @param key key to use
  1520. * @param value value to use
  1521. * @param opt options for put
  1522. * @return #GNUNET_OK on success,
  1523. * #GNUNET_NO if a value was replaced (with REPLACE)
  1524. * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
  1525. * value already exists
  1526. */
  1527. int
  1528. GNUNET_CONTAINER_multiuuidmap_put (
  1529. struct GNUNET_CONTAINER_MultiUuidmap *map,
  1530. const struct GNUNET_Uuid *key,
  1531. void *value,
  1532. enum GNUNET_CONTAINER_MultiHashMapOption opt);
  1533. /**
  1534. * @ingroup hashmap
  1535. * Get the number of key-value pairs in the map.
  1536. *
  1537. * @param map the map
  1538. * @return the number of key value pairs
  1539. */
  1540. unsigned int
  1541. GNUNET_CONTAINER_multiuuidmap_size (
  1542. const struct GNUNET_CONTAINER_MultiUuidmap *map);
  1543. /**
  1544. * @ingroup hashmap
  1545. * Iterate over all entries in the map.
  1546. *
  1547. * @param map the map
  1548. * @param it function to call on each entry
  1549. * @param it_cls extra argument to @a it
  1550. * @return the number of key value pairs processed,
  1551. * #GNUNET_SYSERR if it aborted iteration
  1552. */
  1553. int
  1554. GNUNET_CONTAINER_multiuuidmap_iterate (
  1555. struct GNUNET_CONTAINER_MultiUuidmap *map,
  1556. GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
  1557. void *it_cls);
  1558. struct GNUNET_CONTAINER_MultiUuidmapIterator;
  1559. /**
  1560. * @ingroup hashmap
  1561. * Create an iterator for a multihashmap.
  1562. * The iterator can be used to retrieve all the elements in the multihashmap
  1563. * one by one, without having to handle all elements at once (in contrast to
  1564. * #GNUNET_CONTAINER_multiuuidmap_iterate). Note that the iterator can not be
  1565. * used anymore if elements have been removed from @a map after the creation of
  1566. * the iterator, or 'map' has been destroyed. Adding elements to @a map may
  1567. * result in skipped or repeated elements.
  1568. *
  1569. * @param map the map to create an iterator for
  1570. * @return an iterator over the given multihashmap @a map
  1571. */
  1572. struct GNUNET_CONTAINER_MultiUuidmapIterator *
  1573. GNUNET_CONTAINER_multiuuidmap_iterator_create (
  1574. const struct GNUNET_CONTAINER_MultiUuidmap *map);
  1575. /**
  1576. * @ingroup hashmap
  1577. * Retrieve the next element from the hash map at the iterator's
  1578. * position. If there are no elements left, #GNUNET_NO is returned,
  1579. * and @a key and @a value are not modified. This operation is only
  1580. * allowed if no elements have been removed from the multihashmap
  1581. * since the creation of @a iter, and the map has not been destroyed.
  1582. * Adding elements may result in repeating or skipping elements.
  1583. *
  1584. * @param iter the iterator to get the next element from
  1585. * @param key pointer to store the key in, can be NULL
  1586. * @param value pointer to store the value in, can be NULL
  1587. * @return #GNUNET_YES we returned an element,
  1588. * #GNUNET_NO if we are out of elements
  1589. */
  1590. int
  1591. GNUNET_CONTAINER_multiuuidmap_iterator_next (
  1592. struct GNUNET_CONTAINER_MultiUuidmapIterator *iter,
  1593. struct GNUNET_Uuid *key,
  1594. const void **value);
  1595. /**
  1596. * @ingroup hashmap
  1597. * Destroy a multiuuidmap iterator.
  1598. *
  1599. * @param iter the iterator to destroy
  1600. */
  1601. void
  1602. GNUNET_CONTAINER_multiuuidmap_iterator_destroy (
  1603. struct GNUNET_CONTAINER_MultiUuidmapIterator *iter);
  1604. /**
  1605. * @ingroup hashmap
  1606. * Iterate over all entries in the map that match a particular key.
  1607. *
  1608. * @param map the map
  1609. * @param key public key that the entries must correspond to
  1610. * @param it function to call on each entry
  1611. * @param it_cls extra argument to @a it
  1612. * @return the number of key value pairs processed,
  1613. * #GNUNET_SYSERR if it aborted iteration
  1614. */
  1615. int
  1616. GNUNET_CONTAINER_multiuuidmap_get_multiple (
  1617. struct GNUNET_CONTAINER_MultiUuidmap *map,
  1618. const struct GNUNET_Uuid *key,
  1619. GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
  1620. void *it_cls);
  1621. /**
  1622. * @ingroup hashmap
  1623. * Call @a it on a random value from the map, or not at all
  1624. * if the map is empty. Note that this function has linear
  1625. * complexity (in the size of the map).
  1626. *
  1627. * @param map the map
  1628. * @param it function to call on a random entry
  1629. * @param it_cls extra argument to @a it
  1630. * @return the number of key value pairs processed, zero or one.
  1631. */
  1632. unsigned int
  1633. GNUNET_CONTAINER_multiuuidmap_get_random (
  1634. const struct GNUNET_CONTAINER_MultiUuidmap *map,
  1635. GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
  1636. void *it_cls);
  1637. /* Version of multihashmap with 32 bit keys */
  1638. /**
  1639. * @ingroup hashmap
  1640. * Opaque handle for the 32-bit key HashMap.
  1641. */
  1642. struct GNUNET_CONTAINER_MultiHashMap32;
  1643. /**
  1644. * @ingroup hashmap
  1645. * Opaque handle to an iterator over
  1646. * a 32-bit key multihashmap.
  1647. */
  1648. struct GNUNET_CONTAINER_MultiHashMap32Iterator;
  1649. /**
  1650. * @ingroup hashmap
  1651. * Iterator over hash map entries.
  1652. *
  1653. * @param cls closure
  1654. * @param key current key code
  1655. * @param value value in the hash map
  1656. * @return #GNUNET_YES if we should continue to
  1657. * iterate,
  1658. * #GNUNET_NO if not.
  1659. */
  1660. typedef int (*GNUNET_CONTAINER_MulitHashMapIterator32Callback) (void *cls,
  1661. uint32_t key,
  1662. void *value);
  1663. /**
  1664. * @ingroup hashmap
  1665. * Create a 32-bit key multi hash map.
  1666. *
  1667. * @param len initial size (map will grow as needed)
  1668. * @return NULL on error
  1669. */
  1670. struct GNUNET_CONTAINER_MultiHashMap32 *
  1671. GNUNET_CONTAINER_multihashmap32_create (unsigned int len);
  1672. /**
  1673. * @ingroup hashmap
  1674. * Destroy a 32-bit key hash map. Will not free any values
  1675. * stored in the hash map!
  1676. *
  1677. * @param map the map
  1678. */
  1679. void
  1680. GNUNET_CONTAINER_multihashmap32_destroy (
  1681. struct GNUNET_CONTAINER_MultiHashMap32 *map);
  1682. /**
  1683. * @ingroup hashmap
  1684. * Get the number of key-value pairs in the map.
  1685. *
  1686. * @param map the map
  1687. * @return the number of key value pairs
  1688. */
  1689. unsigned int
  1690. GNUNET_CONTAINER_multihashmap32_size (
  1691. const struct GNUNET_CONTAINER_MultiHashMap32 *map);
  1692. /**
  1693. * @ingroup hashmap
  1694. * Given a key find a value in the map matching the key.
  1695. *
  1696. * @param map the map
  1697. * @param key what to look for
  1698. * @return NULL if no value was found; note that
  1699. * this is indistinguishable from values that just
  1700. * happen to be NULL; use "contains" to test for
  1701. * key-value pairs with value NULL
  1702. */
  1703. void *
  1704. GNUNET_CONTAINER_multihashmap32_get (
  1705. const struct GNUNET_CONTAINER_MultiHashMap32 *map,
  1706. uint32_t key);
  1707. /**
  1708. * @ingroup hashmap
  1709. * Iterate over all entries in the map.
  1710. *
  1711. * @param map the map
  1712. * @param it function to call on each entry
  1713. * @param it_cls extra argument to @a it
  1714. * @return the number of key value pairs processed,
  1715. * #GNUNET_SYSERR if it aborted iteration
  1716. */
  1717. int
  1718. GNUNET_CONTAINER_multihashmap32_iterate (
  1719. struct GNUNET_CONTAINER_MultiHashMap32 *map,
  1720. GNUNET_CONTAINER_MulitHashMapIterator32Callback it,
  1721. void *it_cls);
  1722. /**
  1723. * @ingroup hashmap
  1724. * Remove the given key-value pair from the map. Note that if the
  1725. * key-value pair is in the map multiple times, only one of the pairs
  1726. * will be removed.
  1727. *
  1728. * @param map the map
  1729. * @param key key of the key-value pair
  1730. * @param value value of the key-value pair
  1731. * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
  1732. * is not in the map
  1733. */
  1734. int
  1735. GNUNET_CONTAINER_multihashmap32_remove (
  1736. struct GNUNET_CONTAINER_MultiHashMap32 *map,
  1737. uint32_t key,
  1738. const void *value);
  1739. /**
  1740. * @ingroup hashmap
  1741. * Remove all entries for the given key from the map.
  1742. * Note that the values would not be "freed".
  1743. *
  1744. * @param map the map
  1745. * @param key identifies values to be removed
  1746. * @return number of values removed
  1747. */
  1748. int
  1749. GNUNET_CONTAINER_multihashmap32_remove_all (
  1750. struct GNUNET_CONTAINER_MultiHashMap32 *map,
  1751. uint32_t key);
  1752. /**
  1753. * @ingroup hashmap
  1754. * Check if the map contains any value under the given
  1755. * key (including values that are NULL).
  1756. *
  1757. * @param map the map
  1758. * @param key the key to test if a value exists for it
  1759. * @return #GNUNET_YES if such a value exists,
  1760. * #GNUNET_NO if not
  1761. */
  1762. int
  1763. GNUNET_CONTAINER_multihashmap32_contains (
  1764. const struct GNUNET_CONTAINER_MultiHashMap32 *map,
  1765. uint32_t key);
  1766. /**
  1767. * @ingroup hashmap
  1768. * Check if the map contains the given value under the given
  1769. * key.
  1770. *
  1771. * @param map the map
  1772. * @param key the key to test if a value exists for it
  1773. * @param value value to test for
  1774. * @return #GNUNET_YES if such a value exists,
  1775. * #GNUNET_NO if not
  1776. */
  1777. int
  1778. GNUNET_CONTAINER_multihashmap32_contains_value (
  1779. const struct GNUNET_CONTAINER_MultiHashMap32 *map,
  1780. uint32_t key,
  1781. const void *value);
  1782. /**
  1783. * @ingroup hashmap
  1784. * Store a key-value pair in the map.
  1785. *
  1786. * @param map the map
  1787. * @param key key to use
  1788. * @param value value to use
  1789. * @param opt options for put
  1790. * @return #GNUNET_OK on success,
  1791. * #GNUNET_NO if a value was replaced (with REPLACE)
  1792. * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
  1793. * value already exists
  1794. */
  1795. int
  1796. GNUNET_CONTAINER_multihashmap32_put (
  1797. struct GNUNET_CONTAINER_MultiHashMap32 *map,
  1798. uint32_t key,
  1799. void *value,
  1800. enum GNUNET_CONTAINER_MultiHashMapOption opt);
  1801. /**
  1802. * @ingroup hashmap
  1803. * Iterate over all entries in the map that match a particular key.
  1804. *
  1805. * @param map the map
  1806. * @param key key that the entries must correspond to
  1807. * @param it function to call on each entry
  1808. * @param it_cls extra argument to @a it
  1809. * @return the number of key value pairs processed,
  1810. * #GNUNET_SYSERR if it aborted iteration
  1811. */
  1812. int
  1813. GNUNET_CONTAINER_multihashmap32_get_multiple (
  1814. struct GNUNET_CONTAINER_MultiHashMap32 *map,
  1815. uint32_t key,
  1816. GNUNET_CONTAINER_MulitHashMapIterator32Callback it,
  1817. void *it_cls);
  1818. /**
  1819. * Create an iterator for a 32-bit multihashmap.
  1820. * The iterator can be used to retrieve all the elements in the multihashmap
  1821. * one by one, without having to handle all elements at once (in contrast to
  1822. * #GNUNET_CONTAINER_multihashmap32_iterate). Note that the iterator can not be
  1823. * used anymore if elements have been removed from 'map' after the creation of
  1824. * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
  1825. * result in skipped or repeated elements.
  1826. *
  1827. * @param map the map to create an iterator for
  1828. * @return an iterator over the given multihashmap map
  1829. */
  1830. struct GNUNET_CONTAINER_MultiHashMap32Iterator *
  1831. GNUNET_CONTAINER_multihashmap32_iterator_create (
  1832. const struct GNUNET_CONTAINER_MultiHashMap32 *map);
  1833. /**
  1834. * Retrieve the next element from the hash map at the iterator's position.
  1835. * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
  1836. * are not modified.
  1837. * This operation is only allowed if no elements have been removed from the
  1838. * multihashmap since the creation of 'iter', and the map has not been destroyed.
  1839. * Adding elements may result in repeating or skipping elements.
  1840. *
  1841. * @param iter the iterator to get the next element from
  1842. * @param key pointer to store the key in, can be NULL
  1843. * @param value pointer to store the value in, can be NULL
  1844. * @return #GNUNET_YES we returned an element,
  1845. * #GNUNET_NO if we are out of elements
  1846. */
  1847. int
  1848. GNUNET_CONTAINER_multihashmap32_iterator_next (
  1849. struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter,
  1850. uint32_t *key,
  1851. const void **value);
  1852. /**
  1853. * Destroy a 32-bit multihashmap iterator.
  1854. *
  1855. * @param iter the iterator to destroy
  1856. */
  1857. void
  1858. GNUNET_CONTAINER_multihashmap32_iterator_destroy (
  1859. struct GNUNET_CONTAINER_MultiHashMapIterator *iter);
  1860. /* ******************** doubly-linked list *************** */
  1861. /* To avoid mistakes: head->prev == tail->next == NULL */
  1862. /**
  1863. * @ingroup dll
  1864. * Insert an element at the head of a DLL. Assumes that head, tail and
  1865. * element are structs with prev and next fields.
  1866. *
  1867. * @param head pointer to the head of the DLL
  1868. * @param tail pointer to the tail of the DLL
  1869. * @param element element to insert
  1870. */
  1871. #define GNUNET_CONTAINER_DLL_insert(head, tail, element) \
  1872. do \
  1873. { \
  1874. GNUNET_assert (((element)->prev == NULL) && ((head) != (element))); \
  1875. GNUNET_assert (((element)->next == NULL) && ((tail) != (element))); \
  1876. (element)->next = (head); \
  1877. (element)->prev = NULL; \
  1878. if ((tail) == NULL) \
  1879. (tail) = element; \
  1880. else \
  1881. (head)->prev = element; \
  1882. (head) = (element); \
  1883. } while (0)
  1884. /**
  1885. * @ingroup dll
  1886. * Insert an element at the tail of a DLL. Assumes that head, tail and
  1887. * element are structs with prev and next fields.
  1888. *
  1889. * @param head pointer to the head of the DLL
  1890. * @param tail pointer to the tail of the DLL
  1891. * @param element element to insert
  1892. */
  1893. #define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element) \
  1894. do \
  1895. { \
  1896. GNUNET_assert (((element)->prev == NULL) && ((head) != (element))); \
  1897. GNUNET_assert (((element)->next == NULL) && ((tail) != (element))); \
  1898. (element)->prev = (tail); \
  1899. (element)->next = NULL; \
  1900. if ((head) == NULL) \
  1901. (head) = element; \
  1902. else \
  1903. (tail)->next = element; \
  1904. (tail) = (element); \
  1905. } while (0)
  1906. /**
  1907. * @ingroup dll
  1908. * Insert an element into a DLL after the given other element. Insert
  1909. * at the head if the other element is NULL.
  1910. *
  1911. * @param head pointer to the head of the DLL
  1912. * @param tail pointer to the tail of the DLL
  1913. * @param other prior element, NULL for insertion at head of DLL
  1914. * @param element element to insert
  1915. */
  1916. #define GNUNET_CONTAINER_DLL_insert_after(head, tail, other, element) \
  1917. do \
  1918. { \
  1919. GNUNET_assert (((element)->prev == NULL) && ((head) != (element))); \
  1920. GNUNET_assert (((element)->next == NULL) && ((tail) != (element))); \
  1921. (element)->prev = (other); \
  1922. if (NULL == other) \
  1923. { \
  1924. (element)->next = (head); \
  1925. (head) = (element); \
  1926. } \
  1927. else \
  1928. { \
  1929. (element)->next = (other)->next; \
  1930. (other)->next = (element); \
  1931. } \
  1932. if (NULL == (element)->next) \
  1933. (tail) = (element); \
  1934. else \
  1935. (element)->next->prev = (element); \
  1936. } while (0)
  1937. /**
  1938. * @ingroup dll
  1939. * Insert an element into a DLL before the given other element. Insert
  1940. * at the tail if the other element is NULL.
  1941. *
  1942. * @param head pointer to the head of the DLL
  1943. * @param tail pointer to the tail of the DLL
  1944. * @param other prior element, NULL for insertion at head of DLL
  1945. * @param element element to insert
  1946. */
  1947. #define GNUNET_CONTAINER_DLL_insert_before(head, tail, other, element) \
  1948. do \
  1949. { \
  1950. GNUNET_assert (((element)->prev == NULL) && ((head) != (element))); \
  1951. GNUNET_assert (((element)->next == NULL) && ((tail) != (element))); \
  1952. (element)->next = (other); \
  1953. if (NULL == other) \
  1954. { \
  1955. (element)->prev = (tail); \
  1956. (tail) = (element); \
  1957. } \
  1958. else \
  1959. { \
  1960. (element)->prev = (other)->prev; \
  1961. (other)->prev = (element); \
  1962. } \
  1963. if (NULL == (element)->prev) \
  1964. (head) = (element); \
  1965. else \
  1966. (element)->prev->next = (element); \
  1967. } while (0)
  1968. /**
  1969. * @ingroup dll
  1970. * Remove an element from a DLL. Assumes that head, tail and
  1971. * element point to structs with prev and next fields.
  1972. *
  1973. * Using the head or tail pointer as the element
  1974. * argument does NOT work with this macro.
  1975. * Make sure to store head/tail in another pointer
  1976. * and use it to remove the head or tail of the list.
  1977. *
  1978. * @param head pointer to the head of the DLL
  1979. * @param tail pointer to the tail of the DLL
  1980. * @param element element to remove
  1981. */
  1982. #define GNUNET_CONTAINER_DLL_remove(head, tail, element) \
  1983. do \
  1984. { \
  1985. GNUNET_assert (((element)->prev != NULL) || ((head) == (element))); \
  1986. GNUNET_assert (((element)->next != NULL) || ((tail) == (element))); \
  1987. if ((element)->prev == NULL) \
  1988. (head) = (element)->next; \
  1989. else \
  1990. (element)->prev->next = (element)->next; \
  1991. if ((element)->next == NULL) \
  1992. (tail) = (element)->prev; \
  1993. else \
  1994. (element)->next->prev = (element)->prev; \
  1995. (element)->next = NULL; \
  1996. (element)->prev = NULL; \
  1997. } while (0)
  1998. /* ************ Multi-DLL interface, allows DLL elements to be
  1999. in multiple lists at the same time *********************** */
  2000. /**
  2001. * @ingroup dll
  2002. * Insert an element at the head of a MDLL. Assumes that head, tail and
  2003. * element are structs with prev and next fields.
  2004. *
  2005. * @param mdll suffix name for the next and prev pointers in the element
  2006. * @param head pointer to the head of the MDLL
  2007. * @param tail pointer to the tail of the MDLL
  2008. * @param element element to insert
  2009. */
  2010. #define GNUNET_CONTAINER_MDLL_insert(mdll, head, tail, element) \
  2011. do \
  2012. { \
  2013. GNUNET_assert (((element)->prev_ ## mdll == NULL) && ((head) != (element))); \
  2014. GNUNET_assert (((element)->next_ ## mdll == NULL) && ((tail) != (element))); \
  2015. (element)->next_ ## mdll = (head); \
  2016. (element)->prev_ ## mdll = NULL; \
  2017. if ((tail) == NULL) \
  2018. (tail) = element; \
  2019. else \
  2020. (head)->prev_ ## mdll = element; \
  2021. (head) = (element); \
  2022. } while (0)
  2023. /**
  2024. * @ingroup dll
  2025. * Insert an element at the tail of a MDLL. Assumes that head, tail and
  2026. * element are structs with prev and next fields.
  2027. *
  2028. * @param mdll suffix name for the next and prev pointers in the element
  2029. * @param head pointer to the head of the MDLL
  2030. * @param tail pointer to the tail of the MDLL
  2031. * @param element element to insert
  2032. */
  2033. #define GNUNET_CONTAINER_MDLL_insert_tail(mdll, head, tail, element) \
  2034. do \
  2035. { \
  2036. GNUNET_assert (((element)->prev_ ## mdll == NULL) && ((head) != (element))); \
  2037. GNUNET_assert (((element)->next_ ## mdll == NULL) && ((tail) != (element))); \
  2038. (element)->prev_ ## mdll = (tail); \
  2039. (element)->next_ ## mdll = NULL; \
  2040. if ((head) == NULL) \
  2041. (head) = element; \
  2042. else \
  2043. (tail)->next_ ## mdll = element; \
  2044. (tail) = (element); \
  2045. } while (0)
  2046. /**
  2047. * @ingroup dll
  2048. * Insert an element into a MDLL after the given other element. Insert
  2049. * at the head if the other element is NULL.
  2050. *
  2051. * @param mdll suffix name for the next and prev pointers in the element
  2052. * @param head pointer to the head of the MDLL
  2053. * @param tail pointer to the tail of the MDLL
  2054. * @param other prior element, NULL for insertion at head of MDLL
  2055. * @param element element to insert
  2056. */
  2057. #define GNUNET_CONTAINER_MDLL_insert_after(mdll, head, tail, other, element) \
  2058. do \
  2059. { \
  2060. GNUNET_assert (((element)->prev_ ## mdll == NULL) && ((head) != (element))); \
  2061. GNUNET_assert (((element)->next_ ## mdll == NULL) && ((tail) != (element))); \
  2062. (element)->prev_ ## mdll = (other); \
  2063. if (NULL == other) \
  2064. { \
  2065. (element)->next_ ## mdll = (head); \
  2066. (head) = (element); \
  2067. } \
  2068. else \
  2069. { \
  2070. (element)->next_ ## mdll = (other)->next_ ## mdll; \
  2071. (other)->next_ ## mdll = (element); \
  2072. } \
  2073. if (NULL == (element)->next_ ## mdll) \
  2074. (tail) = (element); \
  2075. else \
  2076. (element)->next_ ## mdll->prev_ ## mdll = (element); \
  2077. } while (0)
  2078. /**
  2079. * @ingroup dll
  2080. * Insert an element into a MDLL before the given other element. Insert
  2081. * at the tail if the other element is NULL.
  2082. *
  2083. * @param mdll suffix name for the next and prev pointers in the element
  2084. * @param head pointer to the head of the MDLL
  2085. * @param tail pointer to the tail of the MDLL
  2086. * @param other prior element, NULL for insertion at head of MDLL
  2087. * @param element element to insert
  2088. */
  2089. #define GNUNET_CONTAINER_MDLL_insert_before(mdll, head, tail, other, element) \
  2090. do \
  2091. { \
  2092. GNUNET_assert (((element)->prev_ ## mdll == NULL) && ((head) != (element))); \
  2093. GNUNET_assert (((element)->next_ ## mdll == NULL) && ((tail) != (element))); \
  2094. (element)->next_ ## mdll = (other); \
  2095. if (NULL == other) \
  2096. { \
  2097. (element)->prev = (tail); \
  2098. (tail) = (element); \
  2099. } \
  2100. else \
  2101. { \
  2102. (element)->prev_ ## mdll = (other)->prev_ ## mdll; \
  2103. (other)->prev_ ## mdll = (element); \
  2104. } \
  2105. if (NULL == (element)->prev_ ## mdll) \
  2106. (head) = (element); \
  2107. else \
  2108. (element)->prev_ ## mdll->next_ ## mdll = (element); \
  2109. } while (0)
  2110. /**
  2111. * @ingroup dll
  2112. * Remove an element from a MDLL. Assumes
  2113. * that head, tail and element are structs
  2114. * with prev and next fields.
  2115. *
  2116. * @param mdll suffix name for the next and prev pointers in the element
  2117. * @param head pointer to the head of the MDLL
  2118. * @param tail pointer to the tail of the MDLL
  2119. * @param element element to remove
  2120. */
  2121. #define GNUNET_CONTAINER_MDLL_remove(mdll, head, tail, element) \
  2122. do \
  2123. { \
  2124. GNUNET_assert (((element)->prev_ ## mdll != NULL) || ((head) == (element))); \
  2125. GNUNET_assert (((element)->next_ ## mdll != NULL) || ((tail) == (element))); \
  2126. if ((element)->prev_ ## mdll == NULL) \
  2127. (head) = (element)->next_ ## mdll; \
  2128. else \
  2129. (element)->prev_ ## mdll->next_ ## mdll = (element)->next_ ## mdll; \
  2130. if ((element)->next_ ## mdll == NULL) \
  2131. (tail) = (element)->prev_ ## mdll; \
  2132. else \
  2133. (element)->next_ ## mdll->prev_ ## mdll = (element)->prev_ ## mdll; \
  2134. (element)->next_ ## mdll = NULL; \
  2135. (element)->prev_ ## mdll = NULL; \
  2136. } while (0)
  2137. /**
  2138. * Insertion sort of @a element into DLL from @a head to @a tail
  2139. * sorted by @a comparator.
  2140. *
  2141. * @param TYPE element type of the elements, i.e. `struct ListElement`
  2142. * @param comparator function like memcmp() to compare elements; takes
  2143. * three arguments, the @a comparator_cls and two elements,
  2144. * returns an `int` (-1, 0 or 1)
  2145. * @param comparator_cls closure for @a comparator
  2146. * @param[in,out] head head of DLL
  2147. * @param[in,out] tail tail of DLL
  2148. * @param element element to insert
  2149. */
  2150. #define GNUNET_CONTAINER_DLL_insert_sorted(TYPE, \
  2151. comparator, \
  2152. comparator_cls, \
  2153. head, \
  2154. tail, \
  2155. element) \
  2156. do \
  2157. { \
  2158. if ((NULL == head) || (0 < comparator (comparator_cls, element, head))) \
  2159. { \
  2160. /* insert at head, element < head */ \
  2161. GNUNET_CONTAINER_DLL_insert (head, tail, element); \
  2162. } \
  2163. else \
  2164. { \
  2165. TYPE *pos; \
  2166. \
  2167. for (pos = head; NULL != pos; pos = pos->next) \
  2168. if (0 < comparator (comparator_cls, element, pos)) \
  2169. break; /* element < pos */ \
  2170. if (NULL == pos) /* => element > tail */ \
  2171. { \
  2172. GNUNET_CONTAINER_DLL_insert_tail (head, tail, element); \
  2173. } \
  2174. else /* prev < element < pos */ \
  2175. { \
  2176. GNUNET_CONTAINER_DLL_insert_after (head, tail, pos->prev, element); \
  2177. } \
  2178. } \
  2179. } while (0)
  2180. /* ******************** Heap *************** */
  2181. /**
  2182. * @ingroup heap
  2183. * Cost by which elements in a heap can be ordered.
  2184. */
  2185. typedef uint64_t GNUNET_CONTAINER_HeapCostType;
  2186. /**
  2187. * @ingroup heap
  2188. * Heap type, either max or min.
  2189. */
  2190. enum GNUNET_CONTAINER_HeapOrder
  2191. {
  2192. /**
  2193. * @ingroup heap
  2194. * Heap with the maximum cost at the root.
  2195. */
  2196. GNUNET_CONTAINER_HEAP_ORDER_MAX,
  2197. /**
  2198. * @ingroup heap
  2199. * Heap with the minimum cost at the root.
  2200. */
  2201. GNUNET_CONTAINER_HEAP_ORDER_MIN
  2202. };
  2203. /**
  2204. * @ingroup heap
  2205. * Handle to a Heap.
  2206. */
  2207. struct GNUNET_CONTAINER_Heap;
  2208. /**
  2209. * @ingroup heap
  2210. * Handle to a node in a heap.
  2211. */
  2212. struct GNUNET_CONTAINER_HeapNode;
  2213. /**
  2214. * @ingroup heap
  2215. * Create a new heap.
  2216. *
  2217. * @param order how should the heap be sorted?
  2218. * @return handle to the heap
  2219. */
  2220. struct GNUNET_CONTAINER_Heap *
  2221. GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order);
  2222. /**
  2223. * @ingroup heap
  2224. * Destroys the heap. Only call on a heap that
  2225. * is already empty.
  2226. *
  2227. * @param heap heap to destroy
  2228. */
  2229. void
  2230. GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap);
  2231. /**
  2232. * @ingroup heap
  2233. * Get element stored at the root of @a heap.
  2234. *
  2235. * @param heap Heap to inspect.
  2236. * @return Element at the root, or NULL if heap is empty.
  2237. */
  2238. void *
  2239. GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap);
  2240. /**
  2241. * Get @a element and @a cost stored at the root of @a heap.
  2242. *
  2243. * @param[in] heap Heap to inspect.
  2244. * @param[out] element Root element is returned here.
  2245. * @param[out] cost Cost of @a element is returned here.
  2246. * @return #GNUNET_YES if an element is returned,
  2247. * #GNUNET_NO if the heap is empty.
  2248. */
  2249. int
  2250. GNUNET_CONTAINER_heap_peek2 (const struct GNUNET_CONTAINER_Heap *heap,
  2251. void **element,
  2252. GNUNET_CONTAINER_HeapCostType *cost);
  2253. /**
  2254. * @ingroup heap
  2255. * Get the current size of the heap
  2256. *
  2257. * @param heap the heap to get the size of
  2258. * @return number of elements stored
  2259. */
  2260. unsigned int
  2261. GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap);
  2262. /**
  2263. * @ingroup heap
  2264. * Get the current cost of the node
  2265. *
  2266. * @param node the node to get the cost of
  2267. * @return cost of the node
  2268. */
  2269. GNUNET_CONTAINER_HeapCostType
  2270. GNUNET_CONTAINER_heap_node_get_cost (
  2271. const struct GNUNET_CONTAINER_HeapNode *node);
  2272. /**
  2273. * @ingroup heap
  2274. * Iterator for heap
  2275. *
  2276. * @param cls closure
  2277. * @param node internal node of the heap
  2278. * @param element value stored at the node
  2279. * @param cost cost associated with the node
  2280. * @return #GNUNET_YES if we should continue to iterate,
  2281. * #GNUNET_NO if not.
  2282. */
  2283. typedef int (*GNUNET_CONTAINER_HeapIterator) (
  2284. void *cls,
  2285. struct GNUNET_CONTAINER_HeapNode *node,
  2286. void *element,
  2287. GNUNET_CONTAINER_HeapCostType cost);
  2288. /**
  2289. * @ingroup heap
  2290. * Iterate over all entries in the heap.
  2291. *
  2292. * @param heap the heap
  2293. * @param iterator function to call on each entry
  2294. * @param iterator_cls closure for @a iterator
  2295. */
  2296. void
  2297. GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
  2298. GNUNET_CONTAINER_HeapIterator iterator,
  2299. void *iterator_cls);
  2300. /**
  2301. * @ingroup heap
  2302. * Perform a random walk of the tree. The walk is biased
  2303. * towards elements closer to the root of the tree (since
  2304. * each walk starts at the root and ends at a random leaf).
  2305. * The heap internally tracks the current position of the
  2306. * walk.
  2307. *
  2308. * @param heap heap to walk
  2309. * @return data stored at the next random node in the walk;
  2310. * NULL if the tree is empty.
  2311. */
  2312. void *
  2313. GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap);
  2314. /**
  2315. * @ingroup heap
  2316. * Inserts a new element into the heap.
  2317. *
  2318. * @param heap heap to modify
  2319. * @param element element to insert
  2320. * @param cost cost for the element
  2321. * @return node for the new element
  2322. */
  2323. struct GNUNET_CONTAINER_HeapNode *
  2324. GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap,
  2325. void *element,
  2326. GNUNET_CONTAINER_HeapCostType cost);
  2327. /**
  2328. * @ingroup heap
  2329. * Remove root of the heap.
  2330. *
  2331. * @param heap heap to modify
  2332. * @return element data stored at the root node
  2333. */
  2334. void *
  2335. GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap);
  2336. /**
  2337. * @ingroup heap
  2338. * Removes a node from the heap.
  2339. *
  2340. * @param node node to remove
  2341. * @return element data stored at the node, NULL if heap is empty
  2342. */
  2343. void *
  2344. GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node);
  2345. /**
  2346. * @ingroup heap
  2347. * Updates the cost of any node in the tree
  2348. *
  2349. * @param node node for which the cost is to be changed
  2350. * @param new_cost new cost for the node
  2351. */
  2352. void
  2353. GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_HeapNode *node,
  2354. GNUNET_CONTAINER_HeapCostType new_cost);
  2355. #if 0 /* keep Emacsens' auto-indent happy */
  2356. {
  2357. #endif
  2358. #ifdef __cplusplus
  2359. }
  2360. #endif
  2361. /* ifndef GNUNET_CONTAINER_LIB_H */
  2362. #endif
  2363. /* end of gnunet_container_lib.h */