Debian Codegraph @

bcftools/khash_str2str.h

raw symbols SCIP
1 /* khash_str2str.h -- C-string to C-string hash table.
2
3 Copyright (C) 2014,2016 Genome Research Ltd.
4
5 Author: Petr Danecek <[email protected]>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE. */
24
25 #ifndef KHASH_STR2STR_H
26 #define KHASH_STR2STR_H
27
28 #include <htslib/khash.h>
29
30 KHASH_MAP_INIT_STR(str2str, const char*)
31
32 /*
33 * Wrappers for khash dictionaries used by mpileup.
34 */
35
36 static inline void *khash_str2str_init(void)
37 {
38 return kh_init(str2str);
39 }
40
41 /*
42 * Destroy the hash structure, but not the keys
43 */
44 static inline void khash_str2str_destroy(void *_hash)
45 {
46 khash_t(str2str) *hash = (khash_t(str2str)*)_hash;
47 if (hash) kh_destroy(str2str, hash); // Note that strings are not freed.
48 }
49
50 /*
51 * Destroys both the hash structure and the keys
52 */
53 static inline void khash_str2str_destroy_free(void *_hash)
54 {
55 khash_t(str2str) *hash = (khash_t(str2str)*)_hash;
56 khint_t k;
57 if (hash == 0) return;
58 for (k = 0; k < kh_end(hash); ++k)
59 if (kh_exist(hash, k)) free((char*)kh_key(hash, k));
60 kh_destroy(str2str, hash);
61 }
62
63 /*
64 * Destroys the hash structure, the keys and the values
65 */
66 static inline void khash_str2str_destroy_free_all(void *_hash)
67 {
68 khash_t(str2str) *hash = (khash_t(str2str)*)_hash;
69 khint_t k;
70 if (hash == 0) return;
71 for (k = 0; k < kh_end(hash); ++k)
72 if (kh_exist(hash, k))
73 {
74 free((char*)kh_key(hash, k));
75 free((char*)kh_val(hash, k));
76 }
77 kh_destroy(str2str, hash);
78 }
79
80 /*
81 * Returns value if key exists or NULL if not
82 */
83 static inline char *khash_str2str_get(void *_hash, const char *str)
84 {
85 khash_t(str2str) *hash = (khash_t(str2str)*)_hash;
86 khint_t k = kh_get(str2str, hash, str);
87 if ( k == kh_end(hash) ) return NULL;
88 return (char*)kh_val(hash, k);
89 }
90
91 /*
92 * Set a new key,value pair. On success returns the bin index, on
93 * error -1 is returned.
94 */
95 static inline int khash_str2str_set(void *_hash, const char *str, const char *value)
96 {
97 khint_t k;
98 int ret;
99 khash_t(str2str) *hash = (khash_t(str2str)*)_hash;
100 if ( !hash ) return -1;
101 k = kh_put(str2str, hash, str, &ret);
102 kh_val(hash,k) = value;
103 return k;
104 }
105
106 #endif