1 /*
2
3 VSEARCH: a versatile open source tool for metagenomics
4
5 Copyright (C) 2014-2021, Torbjorn Rognes, Frederic Mahe and Tomas Flouri
6 All rights reserved.
7
8 Contact: Torbjorn Rognes <torognes@ifi.uio.no>,
9 Department of Informatics, University of Oslo,
10 PO Box 1080 Blindern, NO-0316 Oslo, Norway
11
12 This software is dual-licensed and available under a choice
13 of one of two licenses, either under the terms of the GNU
14 General Public License version 3 or the BSD 2-Clause License.
15
16
17 GNU General Public License version 3
18
19 This program is free software: you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation, either version 3 of the License, or
22 (at your option) any later version.
23
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
28
29 You should have received a copy of the GNU General Public License
30 along with this program. If not, see <http://www.gnu.org/licenses/>.
31
32
33 The BSD 2-Clause License
34
35 Redistribution and use in source and binary forms, with or without
36 modification, are permitted provided that the following conditions
37 are met:
38
39 1. Redistributions of source code must retain the above copyright
40 notice, this list of conditions and the following disclaimer.
41
42 2. Redistributions in binary form must reproduce the above copyright
43 notice, this list of conditions and the following disclaimer in the
44 documentation and/or other materials provided with the distribution.
45
46 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
48 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
49 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
50 COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
51 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
52 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
54 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
56 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 POSSIBILITY OF SUCH DAMAGE.
58
59 */
60
61 typedef struct bitmap_s
62 {
63 unsigned char * bitmap; /* the actual bitmap */
64 unsigned int size; /* size in bits */
65 } bitmap_t;
66
67 bitmap_t * bitmap_init(unsigned int size);
68
69 void bitmap_free(bitmap_t* b);
70
bitmap_get(bitmap_t * b,unsigned int x)71 inline unsigned char bitmap_get(bitmap_t * b, unsigned int x)
72 {
73 return (b->bitmap[x >> 3] >> (x & 7)) & 1;
74 }
75
bitmap_reset_all(bitmap_t * b)76 inline void bitmap_reset_all(bitmap_t * b)
77 {
78 memset(b->bitmap, 0, (b->size+7)/8);
79 }
80
bitmap_set_all(bitmap_t * b)81 inline void bitmap_set_all(bitmap_t * b)
82 {
83 memset(b->bitmap, 255, (b->size+7)/8);
84 }
85
bitmap_reset(bitmap_t * b,unsigned int x)86 inline void bitmap_reset(bitmap_t * b, unsigned int x)
87 {
88 b->bitmap[x >> 3] &= ~ (1 << (x & 7));
89 }
90
bitmap_set(bitmap_t * b,unsigned int x)91 inline void bitmap_set(bitmap_t * b, unsigned int x)
92 {
93 b->bitmap[x >> 3] |= 1 << (x & 7);
94 }
95
bitmap_flip(bitmap_t * b,unsigned int x)96 inline void bitmap_flip(bitmap_t * b, unsigned int x)
97 {
98 b->bitmap[x >> 3] ^= 1 << (x & 7);
99 }
100