/
iterate_control.jl
130 lines (111 loc) · 3.26 KB
/
iterate_control.jl
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
using StaticArrays
export IterControl, itercontrol, controldo, group_shift!
# NOTE: use SortedVector in Blocks would help benchmarks
"""
IterControl{S}
IterControl(n::Int, base::Int, masks, factors) -> IterControl
Iterator to iterate through controlled subspace. See also [`itercontrol`](@ref).
`S` is the number of chunks,
`n` is the size of Hilbert space,
`base` is the base of counter,
`masks` and `factors` are helpers for enumerating over the target Hilbert Space.
"""
struct IterControl{S}
n::Int
base::Int
masks::NTuple{S,Int}
factors::NTuple{S,Int}
function IterControl(n::Int, base::Int, masks::NTuple{S,Int}, factors::NTuple{S,Int}) where {S}
new{S}(n, base, masks, factors)
end
end
# NOTE: positions should be vector (MVector is the best), since it need to be sorted
# do not use Tuple, or other immutables, it increases the sorting time.
"""
itercontrol([T=Int], nbits, positions, bit_configs)
Returns an iterator which iterate through controlled subspace of bits.
# Example
To iterate through all the bits satisfy `0xx10x1` where `x` means an arbitrary bit.
```jldoctest
julia> for each in itercontrol(7, [1, 3, 4, 7], (1, 0, 1, 0))
println(string(each, base=2, pad=7))
end
0001001
0001011
0011001
0011011
0101001
0101011
0111001
0111011
```
"""
function itercontrol(nbits::Int, positions::AbstractVector, bit_configs)
@assert all(x->iszero(x) || isone(x), bit_configs) "Bit configurations should be 0 or 1"
base = bmask(Int, positions[i] for (i, u) in enumerate(bit_configs) if u != 0)
masks, factors = group_shift!(nbits, positions)
S = length(masks)
return IterControl(1 << (nbits - length(positions)), base, Tuple(masks), Tuple(factors))
end
"""
controldo(f, itr::IterControl)
Execute `f` while iterating `itr`.
!!! note
this is faster but equivalent than using `itr` as an iterator.
See also [`itercontrol`](@ref).
"""
function controldo(f::Base.Callable, ic::IterControl{S}) where {S}
for i in 0:ic.n-1
out = 0
for s in 1:S
@inbounds out += (i & ic.masks[s]) * ic.factors[s]
end
f(out + ic.base)
end
return nothing
end
Base.length(it::IterControl) = it.n
Base.eltype(it::IterControl) = Int
function Base.getindex(it::IterControl{S}, k::Int) where {S}
out = 0
k -= 1
for s in 1:S
@inbounds out += (k & it.masks[s]) * it.factors[s]
end
return out + it.base
end
Base.lastindex(it::IterControl) = it.n
function Base.iterate(it::IterControl, state = 1)
if state > length(it)
return nothing
else
return it[state], state + 1
end
end
"""
group_shift!(nbits, positions)
Shift bits on `positions` together.
"""
function group_shift!(nbits::Int, positions::AbstractVector{Int})
sort!(positions)
masks = Int[]
factors = Int[]
k_prv = 0
i = 0
for k in positions
@assert k > k_prv "Conflict at location: $k"
if k != k_prv + 1
push!(factors, 1<<(k_prv-i))
gap = k - k_prv-1
push!(masks, bmask(i+1:i+gap))
i += gap
end
k_prv = k
end
# the last block
if i != nbits
push!(factors, 1<<(k_prv-i))
push!(masks, bmask(i+1:nbits))
end
return masks, factors
end