Skip to content

Commit

Permalink
Merge 425ff3d into 42c9ee3
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonschaub committed Oct 11, 2020
2 parents 42c9ee3 + 425ff3d commit fa569f9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Project.toml
@@ -1,6 +1,6 @@
name = "PairwiseListMatrices"
uuid = "f9da4da7-9382-5435-b973-175f5d8dfb32"
version = "0.8.0"
version = "0.8.1"

[deps]
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Expand Down
109 changes: 56 additions & 53 deletions src/macros.jl
Expand Up @@ -3,7 +3,7 @@ The macro `@iteratelist` writes a `for` loop over the `list` but avoiding `getfi
inside the loop. The first argument of the macro is the `PairwiseListMatrix` that is going
to be iterated and the second is the body of the loop.
In the body `list` will be the list field of the `PairwiseListMatrix` and `k` the index
over that list. Other variables should be interpolated in a quote. You must not modify the
over that list. Other variables are expanded in the current scope. You must not modify the
value of `k`.
```jldoctest
Expand All @@ -23,12 +23,13 @@ julia> @iteratelist PLM println(list[k])
```
"""
macro iteratelist(plm, exp)
quote
list = $(esc(plm)).list
for k in 1:length(list)
$exp
esc(quote
let list = $plm.list
for k in 1:length(list)
$exp
end
end
end
end)
end

"""
Expand All @@ -37,7 +38,7 @@ The macro `@iteratediag` writes a `for` loop over the `diag` field of a
argument of the macro is the `PairwiseListMatrix` that is going to be iterated and the
second is the body of the loop. In the body `diag` will be the diag field of the
`PairwiseListMatrix` and `k` the index over that vector.
Other variables should be interpolated in a quote. You must not modify the value of `k`.
Other variables are expanded in the current scope. You must not modify the value of `k`.
```jldoctest
julia> using PairwiseListMatrices
Expand All @@ -59,14 +60,14 @@ julia> PLM
```
"""
macro iteratediag(plm, exp)
quote
if !hasdiagonal($(esc(plm)))
diag = $(esc(plm)).diag
esc(quote
if !$hasdiagonal($plm)
local diag = $plm.diag
for k in 1:length(diag)
$(exp)
$exp
end
end
end
end)
end

"""
Expand All @@ -76,7 +77,7 @@ The macro `@iterateupper` iterates over the upper triangular part of the
The last argument is the body of the loop, where `list` is the list and diag fields of
the `PairwiseListMatrix` and `k` is the index over that `list`.
You can also use the respective `i` and `j` indexes for that position `k` in the upper
triangular part of the matrix. Other variables should be interpolated in a quote.
triangular part of the matrix. Other variables are expanded in the current scope.
You must not modify the values of `i`, `j` or `k`.
```jldoctest
Expand All @@ -93,7 +94,7 @@ julia> mat = zeros(Int, 2, 2)
0 0
julia> let mat = mat # To avoid using global
@iterateupper PLM true :(\$mat)[i,j] = list[k]
@iterateupper PLM true mat[i,j] = list[k]
end
julia> mat
Expand All @@ -104,57 +105,59 @@ julia> mat
```
"""
macro iterateupper(plm, use_diag, exp)
quote
N = $(esc(plm)).nelements
if hasdiagonal($(esc(plm)))
if $(esc(use_diag))
k = 0
list = $(esc(plm)).list
for i in 1:N
for j in i:N
k += 1
$exp
end
end
else
k = 0
list = $(esc(plm)).list
for i in 1:N
for j in i:N
k += 1
if i != j
esc(quote
let N = $plm.nelements
local k, diag, list
if $hasdiagonal($plm)
if $use_diag
k = 0
list = $plm.list
for i in 1:N
for j in i:N
k += 1
$exp
end
end
end
end
else
if $(esc(use_diag))
k = 0
diag = $(esc(plm)).diag
list = $(esc(plm)).list
for i in 1:N
for j in i:N
if i != j
else
k = 0
list = $plm.list
for i in 1:N
for j in i:N
k += 1
$exp
else
let list = diag, k = i
if i != j
$exp
end
end
end
end
else
k = 0
list = $(esc(plm)).list
for i in 1:(N-1)
for j in (i+1):N
k += 1
$exp
if $use_diag
k = 0
diag = $plm.diag
list = $plm.list
for i in 1:N
for j in i:N
if i != j
k += 1
$exp
else
let list = diag, k = i
$exp
end
end
end
end
else
k = 0
list = $plm.list
for i in 1:(N-1)
for j in (i+1):N
k += 1
$exp
end
end
end
end
end
end
end)
end
20 changes: 10 additions & 10 deletions src/pairwiselistmatrix.jl
Expand Up @@ -527,7 +527,7 @@ end
bc::Base.Broadcast.Broadcasted{Nothing}) where {T, D, VT}
axes(dest) == axes(bc) || Base.Broadcast.throwdm(axes(dest), axes(bc))
bc_ = Base.Broadcast.preprocess(dest, bc)
@iterateupper dest true list[k] = :($bc_)[CartesianIndex(i,j)] # slow: bc_ has a plm
@iterateupper dest true list[k] = bc_[CartesianIndex(i,j)] # slow: bc_ has a plm
return dest
end

Expand Down Expand Up @@ -1037,10 +1037,10 @@ function to_table(plm::PairwiseListMatrix; diagonal::Bool = true, labels = getla
table = Array{Any}(undef, diagonal ? div(N*(N+1),2) : div(N*(N-1),2), 3)
t = 0
@iterateupper plm diagonal begin
:($t) += 1
:($table)[:($t), 1] = :($labels)[i]
:($table)[:($t), 2] = :($labels)[j]
:($table)[:($t), 3] = list[k]
t += 1
table[t, 1] = labels[i]
table[t, 2] = labels[j]
table[t, 3] = list[k]
end
table
end
Expand Down Expand Up @@ -1093,10 +1093,10 @@ function to_dict(plm::PairwiseListMatrix{T,D,TV};
K = Array{T}(undef, L)
t = 0
@iterateupper plm diagonal begin
:($t) += 1
:($I)[:($t)] = :($labels)[i]
:($J)[:($t)] = :($labels)[j]
:($K)[:($t)] = list[k]
t += 1
I[t] = labels[i]
J[t] = labels[j]
K[t] = list[k]
end
Dict(:i => I, :j => J, :values => K)
end
Expand Down Expand Up @@ -1214,7 +1214,7 @@ function DelimitedFiles.writedlm(filename::String,
delim::Char = '\t',
labels::Vector{String} = getlabels(plm)) where {T,D,TV}
open(filename, "w") do fh
@iterateupper plm diagonal println(:($fh), :($labels)[i], :($delim), :($labels)[j], :($delim), list[k])
@iterateupper plm diagonal println(fh, labels[i], delim, labels[j], delim, list[k])
end
end

Expand Down
16 changes: 8 additions & 8 deletions test/runtests.jl
Expand Up @@ -512,20 +512,20 @@ end
full_t = Matrix(PLMtrue)
full_f = Matrix(PLMfalse)

@iteratelist PLMtrue Main.@test(list[k] == :($list_values)[k])
@iteratelist PLMfalse Main.@test(list[k] == :($list_values)[k])
@iteratelist PLMtrue Main.@test(list[k] == list_values[k])
@iteratelist PLMfalse Main.@test(list[k] == list_values[k])

@iteratediag PLMtrue Main.@test(false)
@iteratediag PLMfalse Main.@test(diag[k] == 0)

@iterateupper PLMtrue true list[k] = :($list_values)[k]
@iterateupper PLMfalse false list[k] = :($list_values)[k]
@iterateupper PLMtrue true list[k] = list_values[k]
@iterateupper PLMfalse false list[k] = list_values[k]

@iterateupper PLMtrue true list[k] = :($full_t)[i,j]
@iterateupper PLMtrue false list[k] = :($full_t)[i,j]
@iterateupper PLMtrue true list[k] = full_t[i,j]
@iterateupper PLMtrue false list[k] = full_t[i,j]

@iterateupper PLMtrue true list[k] = :($full_f)[i,j]
@iterateupper PLMfalse false list[k] = :($full_f)[i,j]
@iterateupper PLMtrue true list[k] = full_f[i,j]
@iterateupper PLMfalse false list[k] = full_f[i,j]
end

@testset "IO" begin
Expand Down

0 comments on commit fa569f9

Please sign in to comment.