-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitset.jai
58 lines (48 loc) · 1.22 KB
/
bitset.jai
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
/// Simple Odin-like bitset
Bit_Set :: struct(T: Type)
#modify {
info := cast(*Type_Info_Enum)T;
if info.type != .ENUM {
compiler_report("T must be an enum!", #location(T));
return false;
}
if info.enum_type_flags & .FLAGS != 0 {
return true;
}
#import "Compiler";
compiler_report("T must be of type 'enum_flags'!", #location(T));
return false;
}{
values: T;
}
reset :: (set: *Bit_Set) {
set.values = 0;
}
operator + :: (set: Bit_Set, value: set.T) -> Bit_Set(set.T) {
s := set;
s.values |= value;
return s;
}
operator - :: (set: Bit_Set, value: set.T) -> Bit_Set(set.T) {
s := set;
s.values &= ~value;
return s;
}
operator == :: inline (lhs: Bit_Set, rhs: Bit_Set) -> bool {
return lhs.values == rhs.values;
}
operator == :: inline (lhs: Bit_Set, rhs: lhs.T) -> bool {
return lhs.values == rhs;
}
operator [] :: inline (set: Bit_Set, value: set.T) -> bool {
return set.values & value != 0;
}
operator []= :: inline (set: *Bit_Set, value: set.T, $$toggle: bool) {
#if is_constant(toggle) {
#if toggle set.values |= value;
else set.values &= ~value;
} else {
if toggle set.values |= value;
else set.values &= ~value;
}
}