Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
khash_str2int.h
Go to the documentation of this file.
1 /* khash_str2int.h -- C-string to integer hash table.
2 
3  Copyright (C) 2013 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 <htslib/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 {
38  return kh_init(str2int);
39 }
40 
41 /*
42  * Destroy the hash structure, but not the keys
43  */
44 static inline void khash_str2int_destroy(void *_hash)
45 {
46  khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
47  if (hash) kh_destroy(str2int, 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_str2int_destroy_free(void *_hash)
54 {
55  khash_t(str2int) *hash = (khash_t(str2int)*)_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(str2int, hash);
61 }
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 {
68  khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
69  khint_t k = kh_get(str2int, hash, str);
70  if ( k == kh_end(hash) ) return 0;
71  return 1;
72 }
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 {
80  khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
81  khint_t k;
82  if ( !hash ) return -1;
83  k = kh_get(str2int, hash, str);
84  if ( k == kh_end(hash) ) return -1;
85  if ( !value ) return 0;
86  *value = kh_val(hash, k);
87  return 0;
88 }
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 {
98  khint_t k;
99  int ret;
100  khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
101  if ( !hash ) return -1;
102  k = kh_put(str2int, hash, str, &ret);
103  if (ret == 0) return kh_val(hash, k);
104  kh_val(hash, k) = kh_size(hash) - 1;
105  return kh_val(hash, k);
106 }
107 
108 /*
109  * Set a new key,value pair. On success returns the bin index, on
110  * error -1 is returned. Note that the key must contnue to exist
111  * throughout the whole life of _hash.
112  */
113 static inline int khash_str2int_set(void *_hash, const char *str, int value)
114 {
115  khint_t k;
116  int ret;
117  khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
118  if ( !hash ) return -1;
119  k = kh_put(str2int, hash, str, &ret);
120  kh_val(hash,k) = value;
121  return k;
122 }
123 
124 /*
125  * Return the number of keys in the hash table.
126  */
127 static inline int khash_str2int_size(void *_hash)
128 {
129  khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
130  return kh_size(hash);
131 }
132 
133 #endif
#define kh_val(h, x)
Definition: khash.h:506
#define kh_exist(h, x)
Definition: khash.h:489
khint32_t khint_t
Definition: khash.h:152
#define kh_end(h)
Definition: khash.h:525
#define kh_put(name, h, k, r)
Definition: khash.h:464
#define KHASH_MAP_INIT_STR(name, khval_t)
Definition: khash.h:614
#define kh_init(name)
Definition: khash.h:429
#define kh_size(h)
Definition: khash.h:532
#define kh_destroy(name, h)
Definition: khash.h:436
#define str(x)
Definition: sam.c:66
#define khash_t(name)
Definition: khash.h:422
#define kh_key(h, x)
Definition: khash.h:497
#define kh_get(name, h, k)
Definition: khash.h:473