Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
feather/feather/base/src/chunk/packed_array.rs at main · feather-rs/feather · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
feather-rs
/
feather
Public
Notifications
You must be signed in to change notification settings
Fork
142
Star
2.7k
Code
Issues
66
Pull requests
38
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
feather
/
feather
/
base
/
src
/
chunk
/
packed_array.rs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
298 lines (246 loc) · 7.84 KB
main
Breadcrumbs
feather
/
feather
/
base
/
src
/
chunk
/
packed_array.rs
Copy path
Top
File metadata and controls
Code
Blame
298 lines (246 loc) · 7.84 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/// A packed array of integers where each integer consumes
/// `n` bits. Used to store block data in chunks.
#
[
derive
(
Debug
,
Clone
)
]
pub
struct
PackedArray
{
length
:
usize
,
bits_per_value
:
usize
,
bits
:
Vec
<
u64
>
,
}
impl
PackedArray
{
/// Creates a new `PackedArray` with the given length
/// and number of bits per value. Values are initialized
/// to zero.
///
/// # Panics
/// Panics if `bits_per_value > 64`.
pub
fn
new
(
length
:
usize
,
bits_per_value
:
usize
)
->
Self
{
let
mut
this =
Self
{
length
,
bits_per_value
,
bits
:
Vec
::
new
(
)
,
}
;
let
needed_u64s = this
.
needed_u64s
(
)
;
this
.
bits
=
vec
!
[
0u64
;
needed_u64s
]
;
this
}
/// Creates a `PackedArray` from raw `u64` data
/// and a length.
pub
fn
from_u64_vec
(
bits
:
Vec
<
u64
>
,
length
:
usize
)
->
Self
{
let
bits_per_value = bits
.
len
(
)
*
64
/ length
;
Self
{
length
,
bits_per_value
,
bits
,
}
}
/// Gets the value at the given index.
#
[
inline
]
pub
fn
get
(
&
self
,
index
:
usize
)
->
Option
<
u64
>
{
if
index >=
self
.
len
(
)
{
return
None
;
}
let
(
u64_index
,
bit_index
)
=
self
.
indexes
(
index
)
;
let
u64 =
self
.
bits
[
u64_index
]
;
Some
(
(
u64 >> bit_index
)
&
self
.
mask
(
)
)
}
/// Sets the value at the given index.
///
/// # Panics
/// Panics if `index >= self.length()` or `value > self.max_value()`.
#
[
inline
]
pub
fn
set
(
&
mut
self
,
index
:
usize
,
value
:
u64
)
{
assert
!
(
index <
self
.
len
(
)
,
"index out of bounds: index is {}; length is {}"
,
index
,
self
.
len
(
)
)
;
let
mask =
self
.
mask
(
)
;
assert
!
(
value <= mask
)
;
let
(
u64_index
,
bit_index
)
=
self
.
indexes
(
index
)
;
let
u64 =
&
mut
self
.
bits
[
u64_index
]
;
*
u64 &= !
(
mask << bit_index
)
;
*
u64 |= value << bit_index
;
}
/// Sets all values is the packed array to `value`.
///
/// # Panics
/// Panics if `value > self.max_value()`.
pub
fn
fill
(
&
mut
self
,
value
:
u64
)
{
assert
!
(
value <=
self
.
max_value
(
)
)
;
let
mut
x =
0
;
for
i
in
0
..
self
.
values_per_u64
(
)
{
x |= value <<
(
i
*
self
.
bits_per_value
)
;
}
self
.
bits
.
fill
(
x
)
;
}
/// Returns an iterator over values in this array.
pub
fn
iter
(
&
self
)
->
impl
Iterator
<
Item
=
u64
>
+
'
_
{
let
values_per_u64 =
self
.
values_per_u64
(
)
;
let
bits_per_value =
self
.
bits_per_value
(
)
as
u64
;
let
mask =
self
.
mask
(
)
;
let
length =
self
.
len
(
)
;
self
.
bits
.
iter
(
)
.
flat_map
(
move
|
&
u64|
{
(
0
..values_per_u64
)
.
map
(
move
|i|
(
u64 >>
(
i
as
u64
*
bits_per_value
)
)
&
mask
)
}
)
.
take
(
length
)
}
/// Resizes this packed array to a new bits per value.
pub
fn
resized
(
&
mut
self
,
new_bits_per_value
:
usize
)
->
PackedArray
{
Self
::
from_iter
(
self
.
iter
(
)
,
new_bits_per_value
)
}
/// Collects an iterator into a `PackedArray`.
pub
fn
from_iter
(
iter
:
impl
IntoIterator
<
Item
=
u64
>
,
bits_per_value
:
usize
)
->
Self
{
assert
!
(
bits_per_value <=
64
)
;
let
iter = iter
.
into_iter
(
)
;
let
mut
bits =
Vec
::
with_capacity
(
iter
.
size_hint
(
)
.
0
)
;
let
mut
current_u64 =
0u64
;
let
mut
current_offset =
0
;
let
mut
length =
0
;
for
value
in
iter
{
debug_assert
!
(
value <
1
<< bits_per_value
)
;
current_u64 |= value << current_offset
;
current_offset += bits_per_value
;
if
current_offset >
64
- bits_per_value
{
bits
.
push
(
current_u64
)
;
current_offset =
0
;
current_u64 =
0
;
}
length +=
1
;
}
if
current_offset !=
0
{
bits
.
push
(
current_u64
)
;
}
Self
{
length
,
bits_per_value
,
bits
,
}
}
/// Returns the maximum value of an integer in this packed array.
#
[
inline
]
pub
fn
max_value
(
&
self
)
->
u64
{
self
.
mask
(
)
}
/// Returns the length of this packed array.
#
[
inline
]
pub
fn
len
(
&
self
)
->
usize
{
self
.
length
}
/// Determines whether the length of this array is 0.
#
[
inline
]
pub
fn
is_empty
(
&
self
)
->
bool
{
self
.
len
(
)
==
0
}
/// Returns the number of bits used to represent each value.
#
[
inline
]
pub
fn
bits_per_value
(
&
self
)
->
usize
{
self
.
bits_per_value
}
pub
fn
set_bits_per_value
(
&
mut
self
,
new_value
:
usize
)
{
self
.
bits_per_value
= new_value
;
}
/// Gets the raw `u64` data.
pub
fn
as_u64_slice
(
&
self
)
->
&
[
u64
]
{
&
self
.
bits
}
pub
fn
as_u64_mut_vec
(
&
mut
self
)
->
&
mut
Vec
<
u64
>
{
&
mut
self
.
bits
}
fn
mask
(
&
self
)
->
u64
{
(
1
<<
self
.
bits_per_value
)
-
1
}
fn
needed_u64s
(
&
self
)
->
usize
{
(
self
.
length
+
self
.
values_per_u64
(
)
-
1
)
/
self
.
values_per_u64
(
)
}
fn
values_per_u64
(
&
self
)
->
usize
{
64
/
self
.
bits_per_value
}
fn
indexes
(
&
self
,
index
:
usize
)
->
(
usize
,
usize
)
{
let
u64_index = index /
self
.
values_per_u64
(
)
;
let
bit_index =
(
index %
self
.
values_per_u64
(
)
)
*
self
.
bits_per_value
;
(
u64_index
,
bit_index
)
}
}
#
[
cfg
(
test
)
]
mod
tests
{
use
super
::
*
;
use
rand
::
{
Rng
,
SeedableRng
}
;
use
rand_pcg
::
Pcg64Mcg
;
#
[
test
]
fn
smoke
(
)
{
let
length =
100
;
let
mut
array =
PackedArray
::
new
(
length
,
10
)
;
assert_eq
!
(
array
.
len
(
)
,
length
)
;
assert_eq
!
(
array
.
bits_per_value
(
)
,
10
)
;
assert_eq
!
(
array
.
bits
.
len
(
)
,
17
)
;
for
i
in
0
..length
{
assert_eq
!
(
array
.
get
(
i
)
,
Some
(
0
)
)
;
array
.
set
(
i
,
(
i
*
10
)
as
u64
)
;
assert_eq
!
(
array
.
get
(
i
)
,
Some
(
(
i
*
10
)
as
u64
)
)
;
}
}
#
[
test
]
fn
out_of_bounds
(
)
{
let
array =
PackedArray
::
new
(
97
,
10
)
;
assert_eq
!
(
array
.
bits
.
len
(
)
,
17
)
;
assert_eq
!
(
array
.
get
(
96
)
,
Some
(
0
)
)
;
assert_eq
!
(
array
.
get
(
97
)
,
None
)
;
}
#
[
test
]
fn
iter
(
)
{
let
mut
array =
PackedArray
::
new
(
10_000
,
10
)
;
let
mut
rng =
Pcg64Mcg
::
seed_from_u64
(
10
)
;
let
mut
oracle =
Vec
::
new
(
)
;
for
i
in
0
..array
.
len
(
)
{
let
value = rng
.
gen_range
(
0
..
1024
)
;
oracle
.
push
(
value
)
;
array
.
set
(
i
,
value
)
;
assert_eq
!
(
array
.
get
(
i
)
,
Some
(
value
)
)
;
}
for
(
i
,
&
oracle_value
)
in
oracle
.
iter
(
)
.
enumerate
(
)
{
assert_eq
!
(
array
.
get
(
i
)
,
Some
(
oracle_value
)
)
;
}
for
(
value
,
&
oracle_value
)
in
array
.
iter
(
)
.
zip
(
oracle
.
iter
(
)
)
{
assert_eq
!
(
value
,
oracle_value
)
;
}
}
#
[
test
]
fn
resize
(
)
{
let
mut
rng =
Pcg64Mcg
::
seed_from_u64
(
11
)
;
let
length =
1024
;
let
mut
array =
PackedArray
::
new
(
length
,
1
)
;
let
mut
oracle =
Vec
::
new
(
)
;
for
new_bits_per_value
in
2
..=
16
{
for
i
in
0
..array
.
len
(
)
{
let
value = rng
.
gen_range
(
0
..array
.
max_value
(
)
+
1
)
;
array
.
set
(
i
,
value
)
;
oracle
.
push
(
value
)
;
}
for
(
i
,
&
oracle_value
)
in
oracle
.
iter
(
)
.
enumerate
(
)
{
assert_eq
!
(
array
.
get
(
i
)
,
Some
(
oracle_value
)
)
;
}
array = array
.
resized
(
new_bits_per_value
)
;
for
(
i
,
&
oracle_value
)
in
oracle
.
iter
(
)
.
enumerate
(
)
{
assert_eq
!
(
array
.
get
(
i
)
,
Some
(
oracle_value
)
)
;
}
oracle
.
clear
(
)
;
}
}
#
[
test
]
fn
fill
(
)
{
let
mut
array =
PackedArray
::
new
(
1024
,
10
)
;
array
.
fill
(
102
)
;
assert
!
(
array
.
iter
(
)
.
all
(
|x| x ==
102
)
)
;
array
.
fill
(
256
)
;
assert
!
(
array
.
iter
(
)
.
all
(
|x| x ==
256
)
)
;
}
#
[
test
]
#
[
should_panic
]
fn
fill_too_large
(
)
{
let
mut
array =
PackedArray
::
new
(
100
,
10
)
;
array
.
fill
(
1024
)
;
// 1024 == 2^10
}
}
You can’t perform that action at this time.