/src/ntopng/include/Cardinality.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * (C) 2020-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 _CARDINALITY_H_ |
23 | | #define _CARDINALITY_H_ |
24 | | |
25 | | #include "ntop_includes.h" |
26 | | |
27 | | /* ******************************* */ |
28 | | |
29 | | class Cardinality { |
30 | | private: |
31 | | struct ndpi_hll hll; |
32 | | |
33 | | public: |
34 | 298k | Cardinality() { memset(&hll, 0, sizeof(hll)); } |
35 | | |
36 | 298k | ~Cardinality() { ndpi_hll_destroy(&hll); } |
37 | | |
38 | 298k | void init(u_int8_t bits) { |
39 | 298k | if (ndpi_hll_init(&hll, bits)) throw "init error"; |
40 | 298k | } |
41 | | |
42 | 8.42k | bool addElement(const char* value, size_t value_len) { |
43 | 8.42k | return (ndpi_hll_add(&hll, value, value_len) ? true : false); |
44 | 8.42k | } |
45 | | |
46 | 73.0k | bool addElement(u_int32_t value) { |
47 | 73.0k | return (ndpi_hll_add_number(&hll, value) ? true : false); |
48 | 73.0k | } |
49 | | |
50 | 19.6k | u_int32_t getEstimate() { return ((u_int32_t)ndpi_hll_count(&hll)); } |
51 | | |
52 | 26.1k | void reset() { |
53 | 26.1k | memset(hll.registers, 0, hll.size); /* A lock might help here... */ |
54 | 26.1k | } |
55 | | }; |
56 | | |
57 | | #endif /* _CARDINALITY_H_ */ |