-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSet.cpp
232 lines (198 loc) · 5.14 KB
/
Set.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
The MIT License (MIT)
Copyright (c) 2019 ogatatsu.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "Set.h"
#include "ArduinoMacro.h"
#include <string.h>
namespace hidpg
{
Set::Set() : _data(), _count(0)
{
}
bool Set::add(uint8_t val)
{
if (contains(val))
{
return false;
}
bitSet(_data[val / 8], val % 8);
_count++;
return true;
}
void Set::addAll(const uint8_t vals[], size_t len)
{
for (size_t i = 0; i < len; i++)
{
add(vals[i]);
}
}
Set &Set::operator|=(const Set &rhs)
{
uint32_t *data_32 = reinterpret_cast<uint32_t *>(_data);
uint32_t *rhs_data_32 = reinterpret_cast<uint32_t *>(const_cast<uint8_t *>(rhs._data));
_count = 0;
for (int i = 0; i < 8; i++)
{
data_32[i] |= rhs_data_32[i];
_count += __builtin_popcountl(data_32[i]);
}
return *this;
}
bool Set::remove(uint8_t val)
{
if (contains(val) == false)
{
return false;
}
bitClear(_data[val / 8], val % 8);
_count--;
return true;
}
void Set::removeAll(const uint8_t vals[], size_t len)
{
for (size_t i = 0; i < len; i++)
{
remove(vals[i]);
}
}
Set &Set::operator-=(const Set &rhs)
{
uint32_t *data_32 = reinterpret_cast<uint32_t *>(_data);
uint32_t *rhs_data_32 = reinterpret_cast<uint32_t *>(const_cast<uint8_t *>(rhs._data));
_count = 0;
for (int i = 0; i < 8; i++)
{
data_32[i] &= ~(rhs_data_32[i]);
_count += __builtin_popcountl(data_32[i]);
}
return *this;
}
bool Set::update(uint8_t val, bool b)
{
if (b)
{
return add(val);
}
else
{
return remove(val);
}
}
void Set::clear()
{
memset(_data, 0, sizeof(_data));
_count = 0;
}
bool Set::contains(uint8_t val) const
{
return bitRead(_data[val / 8], val % 8);
}
bool Set::containsAll(const uint8_t vals[], size_t len) const
{
for (size_t i = 0; i < len; i++)
{
if (contains(vals[i]) == false)
{
return false;
}
}
return true;
}
bool Set::containsAny(const uint8_t vals[], size_t len) const
{
for (size_t i = 0; i < len; i++)
{
if (contains(vals[i]))
{
return true;
}
}
return false;
}
void Set::toArray(uint8_t buf[]) const
{
if (_count == 0)
return;
uint16_t buf_cnt = 0;
uint32_t *data_32 = reinterpret_cast<uint32_t *>(const_cast<uint8_t *>(_data));
for (int i = 0; i < 8; i++)
{
if (data_32[i] == 0)
{
continue;
}
for (int j = 0; j < 32; j++)
{
uint8_t val = i * 32 + j;
if (contains(val))
{
buf[buf_cnt++] = val;
if (buf_cnt == _count)
{
return;
}
}
}
}
}
uint16_t Set::count() const
{
return _count;
}
bool operator==(const Set &a, const Set &b)
{
if (a.count() != b.count())
{
return false;
}
return memcmp(a._data, b._data, 32) ? false : true;
}
bool operator!=(const Set &a, const Set &b)
{
return !(a == b);
}
Set operator|(const Set &a, const Set &b)
{
Set result;
uint32_t *a_data_32 = reinterpret_cast<uint32_t *>(const_cast<uint8_t *>(a._data));
uint32_t *b_data_32 = reinterpret_cast<uint32_t *>(const_cast<uint8_t *>(b._data));
uint32_t *r_data_32 = reinterpret_cast<uint32_t *>(const_cast<uint8_t *>(result._data));
for (int i = 0; i < 8; i++)
{
r_data_32[i] |= a_data_32[i];
r_data_32[i] |= b_data_32[i];
result._count += __builtin_popcountl(r_data_32[i]);
}
return result;
}
Set operator-(const Set &a, const Set &b)
{
Set result;
uint32_t *a_data_32 = reinterpret_cast<uint32_t *>(const_cast<uint8_t *>(a._data));
uint32_t *b_data_32 = reinterpret_cast<uint32_t *>(const_cast<uint8_t *>(b._data));
uint32_t *r_data_32 = reinterpret_cast<uint32_t *>(const_cast<uint8_t *>(result._data));
for (int i = 0; i < 8; i++)
{
r_data_32[i] |= a_data_32[i];
r_data_32[i] &= ~(b_data_32[i]);
result._count += __builtin_popcountl(r_data_32[i]);
}
return result;
}
} // namespace hidpg