strmap.c 15 KB

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