/src/htslib/htslib/khash_str2int.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* khash_str2int.h -- C-string to integer hash table. |
2 | | |
3 | | Copyright (C) 2013-2014,2020 Genome Research Ltd. |
4 | | |
5 | | Author: Petr Danecek <pd3@sanger.ac.uk> |
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 HTSLIB_KHASH_STR2INT_H |
26 | | #define HTSLIB_KHASH_STR2INT_H |
27 | | |
28 | | #include "khash.h" |
29 | | |
30 | | KHASH_MAP_INIT_STR(str2int, int) |
31 | | |
32 | | /* |
33 | | * Wrappers for khash dictionaries used by mpileup. |
34 | | */ |
35 | | |
36 | | static inline void *khash_str2int_init(void) |
37 | 0 | { |
38 | 0 | return kh_init(str2int); |
39 | 0 | } |
40 | | |
41 | | /* |
42 | | * Destroy the hash structure, but not the keys |
43 | | */ |
44 | | static inline void khash_str2int_destroy(void *_hash) |
45 | 0 | { |
46 | 0 | khash_t(str2int) *hash = (khash_t(str2int)*)_hash; |
47 | 0 | if (hash) kh_destroy(str2int, hash); // Note that strings are not freed. |
48 | 0 | } |
49 | | |
50 | | /* |
51 | | * Destroys both the hash structure and the keys |
52 | | */ |
53 | | static inline void khash_str2int_destroy_free(void *_hash) |
54 | 0 | { |
55 | 0 | khash_t(str2int) *hash = (khash_t(str2int)*)_hash; |
56 | 0 | khint_t k; |
57 | 0 | if (hash == 0) return; |
58 | 0 | for (k = 0; k < kh_end(hash); ++k) |
59 | 0 | if (kh_exist(hash, k)) free((char*)kh_key(hash, k)); |
60 | 0 | kh_destroy(str2int, hash); |
61 | 0 | } |
62 | | |
63 | | /* |
64 | | * Returns 1 if key exists or 0 if not |
65 | | */ |
66 | | static inline int khash_str2int_has_key(void *_hash, const char *str) |
67 | 0 | { |
68 | 0 | khash_t(str2int) *hash = (khash_t(str2int)*)_hash; |
69 | 0 | khint_t k = kh_get(str2int, hash, str); |
70 | 0 | if ( k == kh_end(hash) ) return 0; |
71 | 0 | return 1; |
72 | 0 | } |
73 | | |
74 | | /* |
75 | | * Returns 0 on success and -1 when the key is not present. On success, |
76 | | * *value is set, unless NULL is passed. |
77 | | */ |
78 | | static inline int khash_str2int_get(void *_hash, const char *str, int *value) |
79 | 0 | { |
80 | 0 | khash_t(str2int) *hash = (khash_t(str2int)*)_hash; |
81 | 0 | khint_t k; |
82 | 0 | if ( !hash ) return -1; |
83 | 0 | k = kh_get(str2int, hash, str); |
84 | 0 | if ( k == kh_end(hash) ) return -1; |
85 | 0 | if ( !value ) return 0; |
86 | 0 | *value = kh_val(hash, k); |
87 | 0 | return 0; |
88 | 0 | } |
89 | | |
90 | | /* |
91 | | * Add a new string to the dictionary, auto-incrementing the value. |
92 | | * On success returns the newly inserted integer id, on error -1 |
93 | | * is returned. Note that the key must continue to exist throughout |
94 | | * the whole life of _hash. |
95 | | */ |
96 | | static inline int khash_str2int_inc(void *_hash, const char *str) |
97 | 0 | { |
98 | 0 | khint_t k; |
99 | 0 | int ret; |
100 | 0 | khash_t(str2int) *hash = (khash_t(str2int)*)_hash; |
101 | 0 | if ( !hash ) return -1; |
102 | 0 | k = kh_put(str2int, hash, str, &ret); |
103 | 0 | if (ret < 0) return -1; |
104 | 0 | if (ret == 0) return kh_val(hash, k); |
105 | 0 | kh_val(hash, k) = kh_size(hash) - 1; |
106 | 0 | return kh_val(hash, k); |
107 | 0 | } |
108 | | |
109 | | /* |
110 | | * Set a new key,value pair. On success returns the bin index, on |
111 | | * error -1 is returned. Note that the key must continue to exist |
112 | | * throughout the whole life of _hash. |
113 | | */ |
114 | | static inline int khash_str2int_set(void *_hash, const char *str, int value) |
115 | 0 | { |
116 | 0 | khint_t k; |
117 | 0 | int ret; |
118 | 0 | khash_t(str2int) *hash = (khash_t(str2int)*)_hash; |
119 | 0 | if ( !hash ) return -1; |
120 | 0 | k = kh_put(str2int, hash, str, &ret); |
121 | 0 | if (ret < 0) return -1; |
122 | 0 | kh_val(hash,k) = value; |
123 | 0 | return k; |
124 | 0 | } |
125 | | |
126 | | /* |
127 | | * Return the number of keys in the hash table. |
128 | | */ |
129 | | static inline int khash_str2int_size(void *_hash) |
130 | 0 | { |
131 | 0 | khash_t(str2int) *hash = (khash_t(str2int)*)_hash; |
132 | 0 | return kh_size(hash); |
133 | 0 | } |
134 | | |
135 | | #endif |