FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/daslib/bool_array.das at master · feiyunwill/daScript · GitHub
feiyunwill
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
daslib
/
bool_array.das
Copy path
More file actions
More file actions
Latest commit
History
History
History
251 lines (245 loc) · 9.06 KB
Breadcrumbs
daScript
/
daslib
/
bool_array.das
Copy path
File metadata and controls
251 lines (245 loc) · 9.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
options
gen2
options
indenting
=
4
options
strict_smart_pointers
=
true
module
bool_array
shared
public
//! Packed boolean array backed by uint32 storage.
//!
//! The ``BoolArray`` struct stores boolean flags as individual bits,
//! providing ``set``, ``get``, ``resize``, ``clear``, and iteration
//! methods. Memory usage is 32x smaller than an ``array<bool>``.
require
strings
struct
BoolArray
{
//! A dynamic array of booleans, stored as bits.
private bits : array<uint>
private real_size : uint
[
hint
(unsafe_range_check,
noalias
=
self)]
def
finalize
{
//! Releases the memory used by the BoolArray.
delete
bits
}
def
const
operator
[] (
index
:
int
) :
bool
{
//! Get the boolean value at the given index.
let
uindex
=
uint
(
index
)
if
(
uindex
>
=
real_size
)
panic
(
"index out of range,
{
index
}
"
)
let
offs
=
uindex
>>
5u
let
mask
=
(
1u
<<
(
uindex
&
31u
))
return
(
bits
[
offs
]
&
mask
)
!
=
0u
}
[
hint
(unsafe_range_check,
noalias
=
self)]
def
operator
[] =(
index
:
int
,
value
:
bool
) {
//! Set the boolean value at the given index.
let
uindex
=
uint
(
index
)
if
(
uindex
>
=
real_size
)
panic
(
"index out of range,
{
index
}
"
)
let
offs
=
uindex
>>
5u
let
mask
=
(
1u
<<
(
uindex
&
31u
))
if
(
value
) {
bits
[
offs
]
|
=
mask
}
else
{
bits
[
offs
]
&
=
~
mask
}
}
[
hint
(unsafe_range_check,
noalias
=
self)]
def
operator
[] ^^=(
index
:
int
,
value
:
bool
) {
//! Perform XOR operation on the boolean value at the given index.
if
(
value
) {
let
uindex
=
uint
(
index
)
if
(
uindex
>
=
real_size
)
panic
(
"index out of range,
{
index
}
"
)
let
offs
=
uindex
>>
5u
let
mask
=
(
1u
<<
(
uindex
&
31u
))
bits
[
offs
]
^
=
mask
}
}
[
hint
(unsafe_range_check,
noalias
=
self)]
def
operator
[] &&=(
index
:
int
,
value
:
bool
) {
//! Perform AND operation on the boolean value at the given index.
if
(
!
value
) {
let
uindex
=
uint
(
index
)
if
(
uindex
>
=
real_size
)
panic
(
"index out of range,
{
index
}
"
)
let
offs
=
uindex
>>
5u
let
mask
=
(
1u
<<
(
uindex
&
31u
))
bits
[
offs
]
&
=
~
mask
}
}
[
hint
(unsafe_range_check,
noalias
=
self)]
def
operator
[] ||=(
index
:
int
,
value
:
bool
) {
//! Perform OR operation on the boolean value at the given index.
if
(
value
) {
let
uindex
=
uint
(
index
)
if
(
uindex
>
=
real_size
)
panic
(
"index out of range,
{
index
}
"
)
let
offs
=
uindex
>>
5u
let
mask
=
(
1u
<<
(
uindex
&
31u
))
bits
[
offs
]
|
=
mask
}
}
def
static
clear
(
var
self
:
BoolArray
) {
//! Clear the BoolArray.
with
(
self
) {
real_size
=
0u
bits
.
clear
()
}
}
def
static
reserve
(
var
self
:
BoolArray
,
capacity
:
int
) {
//! Reserve capacity for the BoolArray.
with
(
self
) {
assert
(
capacity
>
=
0
)
let
needed_uints
=
(
uint
(
capacity
)
+
31u
)
>>
5u
bits
.
reserve
(
int
(
needed_uints
))
}
}
[
hint
(unsafe_range_check,
noalias
=
self)]
def
static
resize
(
var
self
:
BoolArray
,
newSize
:
int
) {
//! Resize the BoolArray to the new size.
with
(
self
) {
assert
(
newSize
>
=
0
)
let
new_real_size
=
uint
(
newSize
)
if
(
new_real_size
>
real_size
&&
(
real_size
&
31u
)
!
=
0u
) {
let
offs
=
real_size
>>
5u
let
tail_mask
=
(
1u
<<
(
real_size
&
31u
))
-
1u
bits
[
offs
]
&
=
tail_mask
}
real_size
=
new_real_size
let
newBitsCount
=
(
real_size
+
31u
)
>>
5u
bits
.
resize
(
int
(
newBitsCount
))
}
}
[
hint
(unsafe_range_check,
noalias
=
self)]
def
static
push
(
var
self
:
BoolArray
,
value
:
bool
) {
//! Push a new boolean value to the end of the BoolArray.
with
(
self
) {
if
((
real_size
&
31u
)
==
0u
) {
bits
.
push
(
0u
)
}
let
offs
=
real_size
>>
5u
let
mask
=
(
1u
<<
(
real_size
&
31u
))
if
(
value
) {
bits
[
offs
]
|
=
mask
}
else
{
bits
[
offs
]
&
=
~
mask
}
real_size
++
}
}
[
hint
(unsafe_range_check,
noalias
=
self)]
def
static
push
(
var
self
:
BoolArray
,
value
:
bool
,
at
:
int
) {
//! Push a new boolean value at the given index in the BoolArray.
if
(
at
!
=
int
(
self
.
real_size
)) {
self
.
insert
(
at
,
value
)
}
else
{
self
.
push
(
value
)
}
}
[
hint
(unsafe_range_check,
noalias
=
self)]
def
static
pop
(
var
self
:
BoolArray
) :
bool
{
//! Pop the last boolean value from the BoolArray and return it.
with
(
self
) {
if
(
real_size
==
0u
)
panic
(
"pop from empty BoolArray"
)
let
old_size
=
real_size
let
value
=
self
[
int
(
old_size
-
1u
) ]
resize
(
self
,
int
(
real_size
-
1u
))
return
value
}
}
def
static
length
(
self
:
BoolArray
) :
int
{
//! Get the length of the BoolArray.
return
int
(
self
.
real_size
)
}
[
hint
(
noalias
=
self)]
def
static
erase
(
var
self
:
BoolArray
,
index
:
int
) {
//! Erase the boolean value at the given index from the BoolArray.
with
(
self
) {
let
uindex
=
uint
(
index
)
if
(
uindex
>
=
real_size
)
panic
(
"index out of range,
{
index
}
"
)
let
start_offs
=
uindex
>>
5u
let
start_bit
=
uindex
&
31u
let
w
=
bits
[
start_offs
]
let
low_bits
=
w
&
((
1u
<<
start_bit
)
-
1u
)
let
upper_bits
=
start_bit
<
31u
? (
w
>>
(
start_bit
+
1u
)) :
0u
var
new_w
=
low_bits
|
(
upper_bits
<<
start_bit
)
let
total_uints
=
(
real_size
+
31u
)
>>
5u
for
(
offs
in
(
start_offs
+
1u
)
..
total_uints
) {
let
next_w
=
bits
[
offs
]
let
next_low_bit
=
next_w
&
1u
new_w
|
=
(
next_low_bit
<<
31u
)
bits
[
offs
- 1u]
=
new_w
new_w
=
next_w
>>
1u
}
bits
[
total_uints
- 1u]
=
new_w
resize
(
self
,
int
(
real_size
-
1u
))
}
}
[
hint
(unsafe_range_check,
noalias
=
self)]
def
static
insert
(
var
self
:
BoolArray
,
index
:
int
,
value
:
bool
) {
//! Insert a boolean value at the given index in the BoolArray.
with
(
self
) {
let
uindex
=
uint
(
index
)
if
(
uindex
>
real_size
)
panic
(
"index out of range,
{
index
}
"
)
if
(
uindex
==
real_size
) {
push
(
self
,
value
)
return
}
resize
(
self
,
int
(
real_size
+
1u
))
let
start_offs
=
uindex
>>
5u
let
start_bit
=
uindex
&
31u
var
w
=
bits
[
start_offs
]
var
carry_bit
=
w
>>
31u
let
lower_bits
=
w
&
((
1u
<<
start_bit
)
-
1u
)
var
upper_bits
=
w
&
~
((
1u
<<
start_bit
)
-
1u
)
upper_bits
<<
=
1u
let
set_bit
=
value
? (
1u
<<
start_bit
) :
0u
w
=
lower_bits
|
set_bit
|
upper_bits
bits
[
start_offs
]
=
w
let
total_uints
=
(
real_size
+
31u
)
>>
5u
for
(
offs
in
(
start_offs
+
1u
)
..
total_uints
) {
let
next_w
=
bits
[
offs
]
let
next_carry_bit
=
next_w
>>
31u
let
next_lower_bits
=
next_w
&
((
1u
<<
31u
)
-
1u
)
let
new_w
=
(
next_lower_bits
<<
1u
)
|
carry_bit
bits
[
offs
]
=
new_w
carry_bit
=
next_carry_bit
}
bits
[
total_uints
- 1u]
|
=
carry_bit
}
}
def
static
to_string
(
self
:
BoolArray
) {
//! Convert the BoolArray to a string representation.
return
build_string
()
$
(wr) {
wr
.
write
(
"["
)
for
(
bit
in
0
..
int
(
self
.
real_size
)) {
if
(
bit
!
=
0
)
wr
.
write
(
", "
)
wr
.
write
(
self
[
bit
] ?
"true"
:
"false"
)
}
wr
.
write
(
"]"
)
}
}
[
unsafe_operation
]
def
static
data_pointer
(
var
self
:
BoolArray
) :
uint
? {
//! Get the data pointer of the BoolArray.
return
unsafe
(
addr
(
self
.
bits
[0]))
}
[
unsafe_operation
]
def
static
data_pointer
(
self
:
BoolArray
) :
uint
const
? {
//! Get the data pointer of the BoolArray.
return
unsafe
(
addr
(
self
.
bits
[0]))
}
}
[
unsafe_outside_of_for
,
unsafe_deref
]
def
each
(
self
:
BoolArray
) :
iterator
<
bool
> {
//! Returns an iterator over all boolean values in the BoolArray.
let
pself
=
unsafe
(
addr
(
self
))
return
<
-
generator
<
bool
>
{
let
len
=
(
*
pself
).
length
()
return
false
if
(
len
==
0
)
let
stored_uints
=
(
uint
(
len
)
+
31u
)
>>
5u
let
last_uint_bits
=
uint
(
len
)
-
((
stored_uints
-
1u
)
<<
5u
)
let
data_ptr
=
unsafe
((
*
pself
).
data_pointer
())
for
(
i
in
0u
..
stored_uints
) {
var
bits_value
=
unsafe
(
data_ptr
[
i
])
let
bit_count
=
i
+
1u
<
stored_uints
?
32u
:
last_uint_bits
for
(
b
in
0u
..
bit_count
) {
yield
(
bits_value
&
1u
)
!
=
0u
bits_value
>>
=
1u
}
}
return
false
}
}
Back
|
FazBrowse Home
|
New Git URL