annotate ezBAMQC/src/htslib/htslib/khash_str2int.h @ 9:6610eedd9fae

Uploaded
author cshl-bsr
date Wed, 30 Mar 2016 12:11:46 -0400
parents dfa3745e5fd8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
1 /* khash_str2int.h -- C-string to integer hash table.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
2
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
3 Copyright (C) 2013 Genome Research Ltd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
4
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
5 Author: Petr Danecek <pd3@sanger.ac.uk>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
6
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
7 Permission is hereby granted, free of charge, to any person obtaining a copy
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
8 of this software and associated documentation files (the "Software"), to deal
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
9 in the Software without restriction, including without limitation the rights
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
11 copies of the Software, and to permit persons to whom the Software is
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
12 furnished to do so, subject to the following conditions:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
13
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
14 The above copyright notice and this permission notice shall be included in
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
15 all copies or substantial portions of the Software.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
16
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
23 DEALINGS IN THE SOFTWARE. */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
24
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
25 #ifndef HTSLIB_KHASH_STR2INT_H
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
26 #define HTSLIB_KHASH_STR2INT_H
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
27
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
28 #include <htslib/khash.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
29
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
30 KHASH_MAP_INIT_STR(str2int, int)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
31
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
32 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
33 * Wrappers for khash dictionaries used by mpileup.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
34 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
35
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
36 static inline void *khash_str2int_init(void)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
37 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
38 return kh_init(str2int);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
39 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
40
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
41 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
42 * Destroy the hash structure, but not the keys
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
43 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
44 static inline void khash_str2int_destroy(void *_hash)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
45 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
46 khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
47 if (hash) kh_destroy(str2int, hash); // Note that strings are not freed.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
48 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
49
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
50 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
51 * Destroys both the hash structure and the keys
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
52 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
53 static inline void khash_str2int_destroy_free(void *_hash)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
54 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
55 khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
56 khint_t k;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
57 if (hash == 0) return;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
58 for (k = 0; k < kh_end(hash); ++k)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
59 if (kh_exist(hash, k)) free((char*)kh_key(hash, k));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
60 kh_destroy(str2int, hash);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
61 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
62
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
63 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
64 * Returns 1 if key exists or 0 if not
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
65 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
66 static inline int khash_str2int_has_key(void *_hash, const char *str)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
67 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
68 khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
69 khint_t k = kh_get(str2int, hash, str);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
70 if ( k == kh_end(hash) ) return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
71 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
72 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
73
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
74 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
75 * Returns 0 on success and -1 when the key is not present. On success,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
76 * *value is set, unless NULL is passed.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
77 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
78 static inline int khash_str2int_get(void *_hash, const char *str, int *value)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
79 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
80 khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
81 khint_t k;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
82 if ( !hash ) return -1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
83 k = kh_get(str2int, hash, str);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
84 if ( k == kh_end(hash) ) return -1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
85 if ( !value ) return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
86 *value = kh_val(hash, k);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
87 return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
88 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
89
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
90 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
91 * Add a new string to the dictionary, auto-incrementing the value.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
92 * On success returns the newly inserted integer id, on error -1
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
93 * is returned. Note that the key must continue to exist throughout
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
94 * the whole life of _hash.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
95 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
96 static inline int khash_str2int_inc(void *_hash, const char *str)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
97 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
98 khint_t k;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
99 int ret;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
100 khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
101 if ( !hash ) return -1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
102 k = kh_put(str2int, hash, str, &ret);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
103 if (ret == 0) return kh_val(hash, k);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
104 kh_val(hash, k) = kh_size(hash) - 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
105 return kh_val(hash, k);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
106 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
107
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
108 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
109 * Set a new key,value pair. On success returns the bin index, on
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
110 * error -1 is returned. Note that the key must contnue to exist
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
111 * throughout the whole life of _hash.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
112 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
113 static inline int khash_str2int_set(void *_hash, const char *str, int value)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
114 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
115 khint_t k;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
116 int ret;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
117 khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
118 if ( !hash ) return -1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
119 k = kh_put(str2int, hash, str, &ret);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
120 kh_val(hash,k) = value;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
121 return k;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
122 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
123
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
124 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
125 * Return the number of keys in the hash table.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
126 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
127 static inline int khash_str2int_size(void *_hash)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
128 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
129 khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
130 return kh_size(hash);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
131 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
132
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
133 #endif