Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit78e939f

Browse files
committed
Rough sketch
1 parent5360750 commit78e939f

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

‎src/BitBlockArrays.jl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module BitBlockArrays
22

3-
# Write your package code here.
3+
include("bitblockarray.jl")
4+
include("uniformblockarray.jl")
45

56
end

‎src/bitblockarray.jl‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
bits_sizeof(::Type{T})where {T}=8*sizeof(T)
2+
log2_bits_sizeof(::Type{T})where {T}=intlog2(bits_sizeof(T))
3+
4+
"""
5+
LD = log2.(size(::BitBlockArray))
6+
"""
7+
struct BitBlockArray{N, T, LD}<:AbstractArray{Bool, N}
8+
x::T
9+
end
10+
11+
functionBitBlockArray(x::T, dims::Vararg{Int,N})where {T<:Unsigned, N}
12+
log2_dims=intlog2.(dims)
13+
sum(log2_dims)==log2_bits_sizeof(T)||throw(ArgumentError("dims:$(dims) not compatible with$(T)"))
14+
BitBlockArray{N, T, log2_dims}(x)
15+
end
16+
17+
log2_Size(::Type{BitBlockArray{N, T, LD}})where {N, T, LD}= LD
18+
19+
# thought I might use the StaticArrays type trait, but so far no
20+
Base.size(A::Type{<:BitBlockArray})=2.^log2_Size(A)
21+
Base.size(::A)where {A<:BitBlockArray}=2.^log2_Size(A)
22+
23+
Base.getindex(A::BitBlockArray, i)=Bool((A.x>>> (i-1))&1)
24+
Base.IndexStyle(::BitBlockArray)=IndexLinear()
25+
26+
functionintlog2(x::Signed)
27+
x>0||throw(ArgumentError("x must be positive, got$x"))
28+
intlog2(unsigned(x))
29+
end
30+
31+
#TODO use `bsr` https://stackoverflow.com/a/994709/415404
32+
functionintlog2(x::Unsigned)
33+
count_ones(x)==1||throw(ArgumentError("x must be a positive power of 2, got$x"))
34+
i=0
35+
whiletrue
36+
(x&1==1)&&return i
37+
x= x>>>1
38+
i+=1
39+
end
40+
end

‎src/uniformblockarray.jl‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Arrays of arrays, where the inner arrays are "statically sized" i.e. size is encoded in the type via the Size trait.
3+
The inner arrays must all share the same type signature.
4+
"""
5+
struct UniformBlockArray{T, N, X}<:AbstractArray{T, N}
6+
A::X
7+
end
8+
9+
functionUniformBlockArray(A::X)where {X}
10+
@assertndims(eltype(X))==ndims(X)
11+
UniformBlockArray{eltype(eltype(X)), ndims(X), X}(A)
12+
end
13+
14+
functionindex_parts(l, i)
15+
i0= i-1
16+
inner= i0& (2^l-1)
17+
outer= i0>> l
18+
return (outer+1, inner+1)
19+
end
20+
21+
functionouterinner(Tblock, idxs)
22+
ls=log2_Size(Tblock)
23+
ps=index_parts.(ls, idxs)
24+
end
25+
26+
blocktype(::Type{UniformBlockArray{T, N, X}})where {T, N, X}=eltype(X)
27+
28+
Base.size(uba::UniformBlockArray)=size(uba.A).*size(blocktype(typeof(uba)))
29+
Base.length(uba::UniformBlockArray)=prod(size(uba))
30+
31+
function Base.getindex(uba::UniformBlockArray{T, N, X}, idxs::Vararg{Int, N})where {T, N, X}
32+
ps=outerinner(blocktype(typeof(uba)), idxs)
33+
outer=first.(ps)
34+
inner=last.(ps)
35+
(uba.A[outer...])[inner...]
36+
end

‎test/runtests.jl‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
using BitBlockArrays
2+
using BitBlockArrays: BitBlockArray, UniformBlockArray
23
using Test
34

45
@testset"BitBlockArrays.jl"begin
56
# Write your tests here.
7+
bba=BitBlockArray(UInt64(0),8,8)
8+
@test_throws ArgumentErrorBitBlockArray(UInt64(0),8,9)
9+
10+
A=fill(bba,2,2)
11+
12+
uba=UniformBlockArray(A)
13+
@testsum(uba)==0
14+
uba.A[1]=BitBlockArray(UInt64(0xF),8,8)
15+
@testsum(uba)==4
16+
uba.A[2]=BitBlockArray(UInt64(0xFF),8,8)
17+
@testsum(uba)==12
18+
uba.A[end]=BitBlockArray(typemax(UInt64),8,8)
19+
@testsum(uba)==76
20+
621
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp