Skip to content

Commit bd8f82c

Browse files
committed
tiered caching for fetching indices
1 parent fafdfcb commit bd8f82c

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DirectSum"
22
uuid = "22fd7b30-a8c0-5bf2-aabe-97783860d07c"
33
authors = ["Michael Reed"]
4-
version = "0.2.3"
4+
version = "0.2.4"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/indices.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
# This file is part of DirectSum.jl. It is licensed under the GPL license
33
# Grassmann Copyright (C) 2019 Michael Reed
44

5+
struct Dim{N}
6+
@pure Dim{N}() where N = new{N}()
7+
end
8+
9+
@pure Dim(N::Int) = Dim{N}()
10+
@pure Base.ndims(::Dim{N}) where N = N
11+
512
# vector and co-vector prefix
613
const pre = ("v","w","ϵ","")
714

@@ -82,8 +89,9 @@ const ndigits_extra = Dict{Bits,SVector}[]
8289
end
8390
end
8491

92+
const indices_cache = Dict{Bits,Vector}()
8593
@pure indices(b::Bits) = findall(digits(b,base=2).==1)
86-
@pure function indices(b::Bits,N::Int)
94+
@pure function indices_calc(b::Bits,N::Int)
8795
d = ndigits(b,N)
8896
l = length(d)
8997
a = Int[]
@@ -92,9 +100,13 @@ end
92100
end
93101
return a
94102
end
103+
@pure function indices(b::Bits,N::Int)
104+
!haskey(indices_cache,b) && push!(indices_cache,b=>indices_calc(b,N))
105+
return @inbounds indices_cache[b]
106+
end
95107

96-
@pure shift_indices(V::T,b::Bits) where T<:VectorSpace = shift_indices(V,indices(b,ndims(V)))
97-
function shift_indices(s::T,set::Vector{Int}) where T<:VectorSpace{N,M} where N where M
108+
@pure shift_indices(V::T,b::Bits) where T<:VectorSpace = shift_indices!(V,copy(indices(b,ndims(V))))
109+
function shift_indices!(s::T,set::Vector{Int}) where T<:VectorSpace{N,M} where N where M
98110
if !isempty(set)
99111
k = 1
100112
hasinf(s) && set[1] == 1 && (set[1] = -1; k += 1)
@@ -104,6 +116,7 @@ function shift_indices(s::T,set::Vector{Int}) where T<:VectorSpace{N,M} where N
104116
end
105117
return set
106118
end
119+
@deprecate(shift_indices(s::VectorSpace,set::Vector{Int}),shift_indices!(s::VectorSpace,set::Vector{Int}))
107120

108121
# printing of indices
109122
@inline function printindex(i,l::Bool=false,e::String=pre[1],t=i>36,j=t ? i-26 : i)

0 commit comments

Comments
 (0)