/src/ntopng/include/Bitmap.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * (C) 2013-26 - ntop.org |
4 | | * |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, write to the Free Software Foundation, |
18 | | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 | | * |
20 | | */ |
21 | | |
22 | | #ifndef _BITMAP_H_ |
23 | | #define _BITMAP_H_ |
24 | | |
25 | | #include "ntop_includes.h" |
26 | | |
27 | | /** |
28 | | * @brief Generic bitmap class template building a bitmap as multiple 64-bit |
29 | | * bitmaps |
30 | | * @tparam N Number of u_int64_t elements in the bitmap array |
31 | | */ |
32 | | template <size_t N> |
33 | | class Bitmap { |
34 | | private: |
35 | | u_int64_t bitmap[N]; |
36 | | |
37 | | public: |
38 | 22.9k | Bitmap() { reset(); }Unexecuted instantiation: Bitmap<1ul>::Bitmap() Line | Count | Source | 38 | 22.9k | Bitmap() { reset(); } |
Unexecuted instantiation: Bitmap<64ul>::Bitmap() |
39 | 0 | Bitmap(char* list) { |
40 | 0 | reset(); |
41 | 0 | setBits(list); |
42 | 0 | }; Unexecuted instantiation: Bitmap<2ul>::Bitmap(char*) Unexecuted instantiation: Bitmap<64ul>::Bitmap(char*) |
43 | | |
44 | 0 | static inline u_int numBits() { return N * 64; };Unexecuted instantiation: Bitmap<2ul>::numBits() Unexecuted instantiation: Bitmap<64ul>::numBits() |
45 | | |
46 | 22.9k | void reset() { memset(bitmap, 0, sizeof(bitmap)); }Unexecuted instantiation: Bitmap<1ul>::reset() Unexecuted instantiation: Bitmap<64ul>::reset() Line | Count | Source | 46 | 22.9k | void reset() { memset(bitmap, 0, sizeof(bitmap)); } |
|
47 | | |
48 | 0 | void setBit(u_int16_t id) { |
49 | 0 | size_t idx = id >> 6; |
50 | 0 | u_int8_t offset = id & 0x3F; |
51 | 0 | if (idx < N) bitmap[idx] |= ((u_int64_t)1) << offset; |
52 | 0 | } Unexecuted instantiation: Bitmap<1ul>::setBit(unsigned short) Unexecuted instantiation: Bitmap<2ul>::setBit(unsigned short) Unexecuted instantiation: Bitmap<64ul>::setBit(unsigned short) |
53 | | |
54 | 0 | void setBits(char* list) { |
55 | 0 | char *item, *tmp = NULL; |
56 | |
|
57 | 0 | item = strtok_r(list, ",", &tmp); |
58 | |
|
59 | 0 | while (item) { |
60 | 0 | int id = atoi(item); |
61 | 0 | setBit(id); |
62 | 0 | item = strtok_r(NULL, ",", &tmp); |
63 | 0 | } |
64 | 0 | } Unexecuted instantiation: Bitmap<1ul>::setBits(char*) Unexecuted instantiation: Bitmap<2ul>::setBits(char*) Unexecuted instantiation: Bitmap<64ul>::setBits(char*) |
65 | | |
66 | 0 | void clearBit(u_int16_t id) { |
67 | 0 | size_t idx = id >> 6; /* id / 64 */ |
68 | 0 | u_int8_t offset = id & 0x3F; /* id % 64 */ |
69 | 0 | if (idx < N) bitmap[idx] &= ~(((u_int64_t)1) << offset); |
70 | 0 | } Unexecuted instantiation: Bitmap<2ul>::clearBit(unsigned short) Unexecuted instantiation: Bitmap<64ul>::clearBit(unsigned short) |
71 | | |
72 | 0 | bool isSetBit(u_int16_t id) const { |
73 | 0 | size_t idx = id >> 6; |
74 | 0 | u_int8_t offset = id & 0x3F; |
75 | 0 | if (idx < N) return (((bitmap[idx] >> offset) & 1) ? true : false); |
76 | 0 | return false; |
77 | 0 | } Unexecuted instantiation: Bitmap<2ul>::isSetBit(unsigned short) const Unexecuted instantiation: Bitmap<64ul>::isSetBit(unsigned short) const |
78 | | |
79 | | /* Return the first bit set, starting from start */ |
80 | 0 | int getNext(u_int16_t start) { |
81 | 0 | size_t start_idx = start >> 6; |
82 | 0 | u_int8_t start_offset = start & 0x3F; |
83 | |
|
84 | 0 | for (size_t i = start_idx; i < N; i++) { |
85 | 0 | int bit = |
86 | 0 | Utils::bitmapGetNext(bitmap[i], (i == start_idx) ? start_offset : 0); |
87 | 0 | if (bit >= 0) { |
88 | 0 | return (i << 6) + bit; |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | 0 | return -1; |
93 | 0 | } Unexecuted instantiation: Bitmap<2ul>::getNext(unsigned short) Unexecuted instantiation: Bitmap<64ul>::getNext(unsigned short) |
94 | | |
95 | 0 | void bitmapOr(const Bitmap<N> b) { |
96 | 0 | for (size_t i = 0; i < N; i++) { |
97 | 0 | bitmap[i] |= b.bitmap[i]; |
98 | 0 | } |
99 | 0 | } Unexecuted instantiation: Bitmap<2ul>::bitmapOr(Bitmap<2ul>) Unexecuted instantiation: Bitmap<64ul>::bitmapOr(Bitmap<64ul>) |
100 | | |
101 | 0 | void set(const Bitmap<N>* b) { memcpy(bitmap, b->bitmap, sizeof(bitmap)); }Unexecuted instantiation: Bitmap<2ul>::set(Bitmap<2ul> const*) Unexecuted instantiation: Bitmap<64ul>::set(Bitmap<64ul> const*) |
102 | | |
103 | 0 | bool equal(const Bitmap<N>* b) const { |
104 | 0 | return (memcmp(bitmap, b->bitmap, sizeof(bitmap)) == 0); |
105 | 0 | } Unexecuted instantiation: Bitmap<2ul>::equal(Bitmap<2ul> const*) const Unexecuted instantiation: Bitmap<64ul>::equal(Bitmap<64ul> const*) const |
106 | | |
107 | | /* Returns the 64-bit value at position idx */ |
108 | 0 | u_int64_t getBits64(size_t idx = 0) const { |
109 | 0 | return (idx < N) ? bitmap[idx] : 0; |
110 | 0 | } Unexecuted instantiation: Bitmap<2ul>::getBits64(unsigned long) const Unexecuted instantiation: Bitmap<64ul>::getBits64(unsigned long) const Unexecuted instantiation: Bitmap<1ul>::getBits64(unsigned long) const |
111 | | |
112 | | /* Sets the 64-bit value at position idx */ |
113 | 0 | void setBits64(u_int64_t val, size_t idx = 0) { |
114 | 0 | if (idx < N) bitmap[idx] = val; |
115 | 0 | } Unexecuted instantiation: Bitmap<1ul>::setBits64(unsigned long, unsigned long) Unexecuted instantiation: Bitmap<2ul>::setBits64(unsigned long, unsigned long) Unexecuted instantiation: Bitmap<64ul>::setBits64(unsigned long, unsigned long) |
116 | | |
117 | | void lua(lua_State* vm, const char* label) const; |
118 | | |
119 | 0 | const char* toHexString(char* buf, ssize_t buf_len) const { |
120 | 0 | char* ptr = buf; |
121 | 0 | ssize_t remaining = buf_len; |
122 | | |
123 | | /* Write hex values in reverse order (most significant first) */ |
124 | 0 | for (int i = N - 1; i >= 0; i--) { |
125 | 0 | int written = |
126 | 0 | snprintf(ptr, remaining, "%016lX", (unsigned long)bitmap[i]); |
127 | 0 | if (written >= remaining) break; |
128 | 0 | ptr += written; |
129 | 0 | remaining -= written; |
130 | 0 | } |
131 | | |
132 | | /* Remove heading zeroes but keep HEX byte-aligned (SQLite doesn't like |
133 | | * heading zeroes when inserting blob literals) */ |
134 | 0 | u_int shifts = 0; |
135 | 0 | for (u_int pos = 0; pos < strlen(buf) - 2; pos += 2) { |
136 | 0 | u_int8_t cur_byte = 0; |
137 | |
|
138 | 0 | sscanf(&buf[pos], "%02hhX", &cur_byte); |
139 | 0 | if (cur_byte > 0) break; |
140 | 0 | shifts += 2; |
141 | 0 | } |
142 | |
|
143 | 0 | if (shifts > 0) memmove(buf, &buf[shifts], buf_len - shifts); |
144 | |
|
145 | 0 | return buf; |
146 | 0 | } Unexecuted instantiation: Bitmap<2ul>::toHexString(char*, long) const Unexecuted instantiation: Bitmap<64ul>::toHexString(char*, long) const |
147 | | }; |
148 | | |
149 | | #endif /* _BITMAP_H_ */ |