strmap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * strmap version 2.0.1
  3. *
  4. * ANSI C hash table for strings.
  5. *
  6. * Version history:
  7. * 1.0.0 - initial release
  8. * 2.0.0 - changed function prefix from strmap to sm to ensure
  9. * ANSI C compatibility
  10. * 2.0.1 - improved documentation
  11. *
  12. * strmap.c
  13. *
  14. * Copyright (c) 2009, 2011, 2013 Per Ola Kristensson.
  15. *
  16. * Per Ola Kristensson <pok21@cam.ac.uk>
  17. * Inference Group, Department of Physics
  18. * University of Cambridge
  19. * Cavendish Laboratory
  20. * JJ Thomson Avenue
  21. * CB3 0HE Cambridge
  22. * United Kingdom
  23. *
  24. * strmap is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU Lesser General Public License as published by
  26. * the Free Software Foundation, either version 3 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * strmap is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU Lesser General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Lesser General Public License
  35. * along with strmap. If not, see <http://www.gnu.org/licenses/>.
  36. */
  37. #include "strmap.h"
  38. typedef struct Pair Pair;
  39. typedef struct Bucket Bucket;
  40. struct Pair {
  41. char *key;
  42. void *value;
  43. };
  44. struct Bucket {
  45. unsigned int count;
  46. Pair *pairs;
  47. };
  48. struct StrMap {
  49. unsigned int count;
  50. Bucket *buckets;
  51. };
  52. static Pair * get_pair(Bucket *bucket, const char *key);
  53. static unsigned long hash(const char *str);
  54. StrMap * sm_new(unsigned int capacity)
  55. {
  56. StrMap *map;
  57. map = malloc(sizeof(StrMap));
  58. if (map == NULL) {
  59. return NULL;
  60. }
  61. map->count = capacity;
  62. map->buckets = malloc(map->count * sizeof(Bucket));
  63. if (map->buckets == NULL) {
  64. free(map);
  65. return NULL;
  66. }
  67. memset(map->buckets, 0, map->count * sizeof(Bucket));
  68. return map;
  69. }
  70. void sm_delete(StrMap *map)
  71. {
  72. unsigned int i, j, n, m;
  73. Bucket *bucket;
  74. Pair *pair;
  75. if (map == NULL) {
  76. return;
  77. }
  78. n = map->count;
  79. bucket = map->buckets;
  80. i = 0;
  81. while (i < n) {
  82. m = bucket->count;
  83. pair = bucket->pairs;
  84. j = 0;
  85. while(j < m) {
  86. free(pair->key);
  87. //free(pair->value);
  88. pair++;
  89. j++;
  90. }
  91. free(bucket->pairs);
  92. bucket++;
  93. i++;
  94. }
  95. free(map->buckets);
  96. free(map);
  97. }
  98. int sm_get(const StrMap *map, const char *key, void **out_buf)
  99. {
  100. unsigned int index;
  101. Bucket *bucket;
  102. Pair *pair;
  103. if (map == NULL) {
  104. return 0;
  105. }
  106. if (key == NULL) {
  107. return 0;
  108. }
  109. index = hash(key) % map->count;
  110. bucket = &(map->buckets[index]);
  111. pair = get_pair(bucket, key);
  112. if (pair == NULL) {
  113. return 0;
  114. }
  115. *out_buf = pair->value;
  116. return 1;
  117. }
  118. int sm_exists(const StrMap *map, const char *key)
  119. {
  120. unsigned int index;
  121. Bucket *bucket;
  122. Pair *pair;
  123. if (map == NULL) {
  124. return 0;
  125. }
  126. if (key == NULL) {
  127. return 0;
  128. }
  129. index = hash(key) % map->count;
  130. bucket = &(map->buckets[index]);
  131. pair = get_pair(bucket, key);
  132. if (pair == NULL) {
  133. return 0;
  134. }
  135. return 1;
  136. }
  137. int sm_put(StrMap *map, const char *key, void* value)
  138. {
  139. unsigned int key_len, index;
  140. Bucket *bucket;
  141. Pair *tmp_pairs, *pair;
  142. //char *tmp_value;
  143. char *new_key; //, *new_value;
  144. if (map == NULL) {
  145. return 0;
  146. }
  147. if (key == NULL || value == NULL) {
  148. return 0;
  149. }
  150. key_len = strlen(key);
  151. /* Get a pointer to the bucket the key string hashes to */
  152. index = hash(key) % map->count;
  153. bucket = &(map->buckets[index]);
  154. /* Check if we can handle insertion by simply replacing
  155. * an existing value in a key-value pair in the bucket.
  156. */
  157. if ((pair = get_pair(bucket, key)) != NULL) {
  158. /* The bucket contains a pair that matches the provided key,
  159. * change the value for that pair to the new value.
  160. */
  161. /*if (strlen(pair->value) < value_len) {
  162. tmp_value = realloc(pair->value, (value_len + 1) * sizeof(char));
  163. if (tmp_value == NULL) {
  164. return 0;
  165. }
  166. pair->value = tmp_value;
  167. }*/
  168. /* Copy the new value into the pair that matches the key */
  169. pair->value = value;
  170. return 1;
  171. }
  172. /* Allocate space for a new key and value */
  173. new_key = malloc((key_len + 1) * sizeof(char));
  174. if (new_key == NULL) {
  175. return 0;
  176. }
  177. /*new_value = malloc((value_len) * sizeof(char));
  178. if (new_value == NULL) {
  179. free(new_key);
  180. return 0;
  181. }*/
  182. /* Create a key-value pair */
  183. if (bucket->count == 0) {
  184. /* The bucket is empty, lazily allocate space for a single
  185. * key-value pair.
  186. */
  187. bucket->pairs = malloc(sizeof(Pair));
  188. if (bucket->pairs == NULL) {
  189. free(new_key);
  190. //free(new_value);
  191. return 0;
  192. }
  193. bucket->count = 1;
  194. }
  195. else {
  196. /* The bucket wasn't empty but no pair existed that matches the provided
  197. * key, so create a new key-value pair.
  198. */
  199. tmp_pairs = realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair));
  200. if (tmp_pairs == NULL) {
  201. free(new_key);
  202. //free(new_value);
  203. return 0;
  204. }
  205. bucket->pairs = tmp_pairs;
  206. bucket->count++;
  207. }
  208. /* Get the last pair in the chain for the bucket */
  209. pair = &(bucket->pairs[bucket->count - 1]);
  210. pair->key = new_key;
  211. //pair->value = new_value;
  212. /* Copy the key and its value into the key-value pair */
  213. strcpy(pair->key, key);
  214. pair->value = value;
  215. return 1;
  216. }
  217. int sm_get_count(const StrMap *map)
  218. {
  219. unsigned int i, j, n, m;
  220. unsigned int count;
  221. Bucket *bucket;
  222. Pair *pair;
  223. if (map == NULL) {
  224. return 0;
  225. }
  226. bucket = map->buckets;
  227. n = map->count;
  228. i = 0;
  229. count = 0;
  230. while (i < n) {
  231. pair = bucket->pairs;
  232. m = bucket->count;
  233. j = 0;
  234. while (j < m) {
  235. count++;
  236. pair++;
  237. j++;
  238. }
  239. bucket++;
  240. i++;
  241. }
  242. return count;
  243. }
  244. int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj)
  245. {
  246. unsigned int i, j, n, m;
  247. Bucket *bucket;
  248. Pair *pair;
  249. if (map == NULL) {
  250. return 0;
  251. }
  252. if (enum_func == NULL) {
  253. return 0;
  254. }
  255. bucket = map->buckets;
  256. n = map->count;
  257. i = 0;
  258. while (i < n) {
  259. pair = bucket->pairs;
  260. m = bucket->count;
  261. j = 0;
  262. while (j < m) {
  263. enum_func(pair->key, pair->value, obj);
  264. pair++;
  265. j++;
  266. }
  267. bucket++;
  268. i++;
  269. }
  270. return 1;
  271. }
  272. /*
  273. * Returns a pair from the bucket that matches the provided key,
  274. * or null if no such pair exist.
  275. */
  276. static Pair * get_pair(Bucket *bucket, const char *key)
  277. {
  278. unsigned int i, n;
  279. Pair *pair;
  280. n = bucket->count;
  281. if (n == 0) {
  282. return NULL;
  283. }
  284. pair = bucket->pairs;
  285. i = 0;
  286. while (i < n) {
  287. if (pair->key != NULL && pair->value != NULL) {
  288. if (strcmp(pair->key, key) == 0) {
  289. return pair;
  290. }
  291. }
  292. pair++;
  293. i++;
  294. }
  295. return NULL;
  296. }
  297. /*
  298. * Returns a hash code for the provided string.
  299. */
  300. static unsigned long hash(const char *str)
  301. {
  302. unsigned long hash = 5381;
  303. int c;
  304. while ((c = *str++)) {
  305. hash = ((hash << 5) + hash) + c;
  306. }
  307. return hash;
  308. }
  309. /*
  310. GNU LESSER GENERAL PUBLIC LICENSE
  311. Version 3, 29 June 2007
  312. Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
  313. Everyone is permitted to copy and distribute verbatim copies
  314. of this license document, but changing it is not allowed.
  315. This version of the GNU Lesser General Public License incorporates
  316. the terms and conditions of version 3 of the GNU General Public
  317. License, supplemented by the additional permissions listed below.
  318. 0. Additional Definitions.
  319. As used herein, "this License" refers to version 3 of the GNU Lesser
  320. General Public License, and the "GNU GPL" refers to version 3 of the GNU
  321. General Public License.
  322. "The Library" refers to a covered work governed by this License,
  323. other than an Application or a Combined Work as defined below.
  324. An "Application" is any work that makes use of an interface provided
  325. by the Library, but which is not otherwise based on the Library.
  326. Defining a subclass of a class defined by the Library is deemed a mode
  327. of using an interface provided by the Library.
  328. A "Combined Work" is a work produced by combining or linking an
  329. Application with the Library. The particular version of the Library
  330. with which the Combined Work was made is also called the "Linked
  331. Version".
  332. The "Minimal Corresponding Source" for a Combined Work means the
  333. Corresponding Source for the Combined Work, excluding any source code
  334. for portions of the Combined Work that, considered in isolation, are
  335. based on the Application, and not on the Linked Version.
  336. The "Corresponding Application Code" for a Combined Work means the
  337. object code and/or source code for the Application, including any data
  338. and utility programs needed for reproducing the Combined Work from the
  339. Application, but excluding the System Libraries of the Combined Work.
  340. 1. Exception to Section 3 of the GNU GPL.
  341. You may convey a covered work under sections 3 and 4 of this License
  342. without being bound by section 3 of the GNU GPL.
  343. 2. Conveying Modified Versions.
  344. If you modify a copy of the Library, and, in your modifications, a
  345. facility refers to a function or data to be supplied by an Application
  346. that uses the facility (other than as an argument passed when the
  347. facility is invoked), then you may convey a copy of the modified
  348. version:
  349. a) under this License, provided that you make a good faith effort to
  350. ensure that, in the event an Application does not supply the
  351. function or data, the facility still operates, and performs
  352. whatever part of its purpose remains meaningful, or
  353. b) under the GNU GPL, with none of the additional permissions of
  354. this License applicable to that copy.
  355. 3. Object Code Incorporating Material from Library Header Files.
  356. The object code form of an Application may incorporate material from
  357. a header file that is part of the Library. You may convey such object
  358. code under terms of your choice, provided that, if the incorporated
  359. material is not limited to numerical parameters, data structure
  360. layouts and accessors, or small macros, inline functions and templates
  361. (ten or fewer lines in length), you do both of the following:
  362. a) Give prominent notice with each copy of the object code that the
  363. Library is used in it and that the Library and its use are
  364. covered by this License.
  365. b) Accompany the object code with a copy of the GNU GPL and this license
  366. document.
  367. 4. Combined Works.
  368. You may convey a Combined Work under terms of your choice that,
  369. taken together, effectively do not restrict modification of the
  370. portions of the Library contained in the Combined Work and reverse
  371. engineering for debugging such modifications, if you also do each of
  372. the following:
  373. a) Give prominent notice with each copy of the Combined Work that
  374. the Library is used in it and that the Library and its use are
  375. covered by this License.
  376. b) Accompany the Combined Work with a copy of the GNU GPL and this license
  377. document.
  378. c) For a Combined Work that displays copyright notices during
  379. execution, include the copyright notice for the Library among
  380. these notices, as well as a reference directing the user to the
  381. copies of the GNU GPL and this license document.
  382. d) Do one of the following:
  383. 0) Convey the Minimal Corresponding Source under the terms of this
  384. License, and the Corresponding Application Code in a form
  385. suitable for, and under terms that permit, the user to
  386. recombine or relink the Application with a modified version of
  387. the Linked Version to produce a modified Combined Work, in the
  388. manner specified by section 6 of the GNU GPL for conveying
  389. Corresponding Source.
  390. 1) Use a suitable shared library mechanism for linking with the
  391. Library. A suitable mechanism is one that (a) uses at run time
  392. a copy of the Library already present on the user's computer
  393. system, and (b) will operate properly with a modified version
  394. of the Library that is interface-compatible with the Linked
  395. Version.
  396. e) Provide Installation Information, but only if you would otherwise
  397. be required to provide such information under section 6 of the
  398. GNU GPL, and only to the extent that such information is
  399. necessary to install and execute a modified version of the
  400. Combined Work produced by recombining or relinking the
  401. Application with a modified version of the Linked Version. (If
  402. you use option 4d0, the Installation Information must accompany
  403. the Minimal Corresponding Source and Corresponding Application
  404. Code. If you use option 4d1, you must provide the Installation
  405. Information in the manner specified by section 6 of the GNU GPL
  406. for conveying Corresponding Source.)
  407. 5. Combined Libraries.
  408. You may place library facilities that are a work based on the
  409. Library side by side in a single library together with other library
  410. facilities that are not Applications and are not covered by this
  411. License, and convey such a combined library under terms of your
  412. choice, if you do both of the following:
  413. a) Accompany the combined library with a copy of the same work based
  414. on the Library, uncombined with any other library facilities,
  415. conveyed under the terms of this License.
  416. b) Give prominent notice with the combined library that part of it
  417. is a work based on the Library, and explaining where to find the
  418. accompanying uncombined form of the same work.
  419. 6. Revised Versions of the GNU Lesser General Public License.
  420. The Free Software Foundation may publish revised and/or new versions
  421. of the GNU Lesser General Public License from time to time. Such new
  422. versions will be similar in spirit to the present version, but may
  423. differ in detail to address new problems or concerns.
  424. Each version is given a distinguishing version number. If the
  425. Library as you received it specifies that a certain numbered version
  426. of the GNU Lesser General Public License "or any later version"
  427. applies to it, you have the option of following the terms and
  428. conditions either of that published version or of any later version
  429. published by the Free Software Foundation. If the Library as you
  430. received it does not specify a version number of the GNU Lesser
  431. General Public License, you may choose any version of the GNU Lesser
  432. General Public License ever published by the Free Software Foundation.
  433. If the Library as you received it specifies that a proxy can decide
  434. whether future versions of the GNU Lesser General Public License shall
  435. apply, that proxy's public statement of acceptance of any version is
  436. permanent authorization for you to choose that version for the
  437. Library.
  438. */