Skip to content

Commit 24d47c3

Browse files
committed
Replace iterate... macros with apply2... functions
1 parent 42c9ee3 commit 24d47c3

File tree

8 files changed

+217
-200
lines changed

8 files changed

+217
-200
lines changed

Diff for: NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## PairwiseListMatrices.jl Release Notes
22

3+
## Changes from v0.7 to v0.8
4+
5+
* The `@iterateupper,`, `@iteratelist` and `@iteratediag` macros have
6+
been replaced by the `apply2upper`, `apply2list` and `apply2diag` functions.
7+
38
## Changes from v0.6 to v0.7
49

510
PairwiseListMatrices v0.7 requires Julia 0.7/1.0.

Diff for: Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1010
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1111

1212
[compat]
13-
julia = "1"
1413
NamedArrays = "0.9"
1514
RecipesBase = "1"
15+
julia = "1"
1616

1717
[extras]
1818
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"

Diff for: docs/src/api.md

+3-8
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,9 @@ getdiag
2020
lengthlist
2121
ij2k
2222
diagonal
23-
```
24-
25-
## Macros
26-
27-
```@docs
28-
@iteratelist
29-
@iteratediag
30-
@iterateupper
23+
apply2list
24+
apply2diag
25+
apply2upper
3126
```
3227

3328
## Labels/Names

Diff for: src/PairwiseListMatrices.jl

+6-7
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ using Statistics
1818
using NamedArrays
1919
using RecipesBase
2020

21-
export @iterateupper,
22-
@iteratelist,
23-
@iteratediag,
24-
25-
PairwiseListMatrix,
21+
export PairwiseListMatrix,
2622
hasdiagonal,
2723
getlist, getdiag,
2824
diagonal,
@@ -34,10 +30,13 @@ export @iterateupper,
3430
from_table, to_table,
3531
to_dict,
3632
join,
37-
writedlm
33+
writedlm,
34+
apply2upper,
35+
apply2list,
36+
apply2diag
3837

39-
include("macros.jl")
4038
include("pairwiselistmatrix.jl")
39+
include("apply.jl")
4140
include("plotrecipes.jl")
4241

4342
end

Diff for: src/apply.jl

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
"""
2+
The `apply2list` function applies the function `fun`, the first argument, for
3+
each element on `list` but avoiding `getfield` calls inside the loop.
4+
The second argument of the macro is the `PairwiseListMatrix` that will be
5+
iterated. `fun` should take two arguments; the first is the `list` field of
6+
the `PairwiseListMatrix`, and the second is the index over that list at a
7+
given iteration.
8+
9+
```jldoctest
10+
julia> using PairwiseListMatrices
11+
12+
julia> plm = PairwiseListMatrix([1,2,3], false)
13+
3×3 PairwiseListMatrix{Int64,false,Array{Int64,1}}:
14+
0 1 2
15+
1 0 3
16+
2 3 0
17+
18+
julia> apply2list((list, k) -> println(list[k]), plm)
19+
1
20+
2
21+
3
22+
23+
```
24+
"""
25+
function apply2list(fun, plm)
26+
list = plm.list
27+
for k in 1:length(list)
28+
fun(list, k)
29+
end
30+
end
31+
32+
"""
33+
The `apply2diag` function applies the function `fun`, the first argument,
34+
for each element on the `diag` field of a`PairwiseListMatrix{T,false,VT}`
35+
but avoiding `getfield` calls inside the loop. The second argument of the
36+
macro is the `PairwiseListMatrix` that will be iterated. `fun` should take
37+
two arguments; the first is the `diag` field of the `PairwiseListMatrix`,
38+
and the second is the index over that vector of diagonal elements at a
39+
given iteration.
40+
41+
```jldoctest
42+
julia> using PairwiseListMatrices
43+
44+
julia> plm = PairwiseListMatrix([1,2,3], false)
45+
3×3 PairwiseListMatrix{Int64,false,Array{Int64,1}}:
46+
0 1 2
47+
1 0 3
48+
2 3 0
49+
50+
julia>apply2diag((diag, k) -> diag[k] += 10k, plm)
51+
52+
julia> plm
53+
3×3 PairwiseListMatrix{Int64,false,Array{Int64,1}}:
54+
10 1 2
55+
1 20 3
56+
2 3 30
57+
58+
```
59+
"""
60+
function apply2diag(fun, plm::PairwiseListMatrix{T,false,VT}) where {T, VT}
61+
diag = plm.diag
62+
for k in 1:length(diag)
63+
fun(diag, k)
64+
end
65+
end
66+
67+
"""
68+
The `apply2upper` function applies the `fun` function over the upper triangular
69+
part of the `PairwiseListMatrix`. Set `use_diag` to `true` if the diagonal
70+
needs to be included in the iteration. The function should take four
71+
arguments, first the `list` and diag fields of the `PairwiseListMatrix`,
72+
the second is the index `k` over that `list`, and the third and fourth are
73+
the `i` and `j` indexes for the position `k` in the upper triangular
74+
part of the matrix.
75+
76+
```jldoctest
77+
julia> using PairwiseListMatrices
78+
79+
julia> plm = PairwiseListMatrix([1,2,3], true)
80+
2×2 PairwiseListMatrix{Int64,true,Array{Int64,1}}:
81+
1 2
82+
2 3
83+
84+
julia> mat = zeros(Int, 2, 2)
85+
2×2 Array{Int64,2}:
86+
0 0
87+
0 0
88+
89+
julia> apply2upper((list, k, i, j) -> mat[i, j] = list[k], plm; use_diag = true)
90+
91+
julia> mat
92+
2×2 Array{Int64,2}:
93+
1 2
94+
0 3
95+
96+
```
97+
"""
98+
function apply2upper(fun, plm::PairwiseListMatrix{T,false,VT}; use_diag::Bool=false) where {T, VT}
99+
N = plm.nelements
100+
if use_diag
101+
k = 0
102+
diag = plm.diag
103+
list = plm.list
104+
for i in 1:N
105+
for j in i:N
106+
if i != j
107+
k += 1
108+
fun(list, k, i, j)
109+
else
110+
let list = diag, k = i
111+
fun(list, k, i, j)
112+
end
113+
end
114+
end
115+
end
116+
else
117+
k = 0
118+
list = plm.list
119+
for i in 1:(N-1)
120+
for j in (i+1):N
121+
k += 1
122+
fun(list, k, i, j)
123+
end
124+
end
125+
end
126+
end
127+
128+
function apply2upper(fun, plm::PairwiseListMatrix{T,true,VT}; use_diag::Bool=false) where {T, VT}
129+
N = plm.nelements
130+
if hasdiagonal(plm)
131+
if use_diag
132+
k = 0
133+
list = plm.list
134+
for i in 1:N
135+
for j in i:N
136+
k += 1
137+
fun(list, k, i, j)
138+
end
139+
end
140+
else
141+
k = 0
142+
list = plm.list
143+
for i in 1:N
144+
for j in i:N
145+
k += 1
146+
if i != j
147+
fun(list, k, i, j)
148+
end
149+
end
150+
end
151+
end
152+
end
153+
end

Diff for: src/macros.jl

-160
This file was deleted.

0 commit comments

Comments
 (0)