-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,20 @@ | ||
""" | ||
diff(A::AbstractMatrix, B::AbstractMatrix) | ||
AbstractDifference | ||
Compute the difference between matrix `A` and matrix `B`, and return a tuple | ||
containing the elements that have been modified, added (per row and column), and | ||
removed (per row and column). | ||
Supertype for differences. | ||
""" | ||
function diff(A::AbstractMatrix, B::AbstractMatrix) | ||
iadict = OrderedDict{Int,Int}(i => i for i = 1:size(A, 1)) | ||
jadict = OrderedDict{Int,Int}(j => j for j = 1:size(A, 2)) | ||
ibdict = OrderedDict{Int,Int}(i => i for i = 1:size(B, 1)) | ||
jbdict = OrderedDict{Int,Int}(j => j for j = 1:size(B, 2)) | ||
return _diff(A, B, iadict, jadict, ibdict, jbdict) | ||
end | ||
abstract type AbstractDifference end | ||
|
||
""" | ||
diff(A::AbstractMatrix, B::AbstractMatrix, ia::AbstractVector, ja::AbstractVector, ib::AbstractVector, jb::AbstractVector) | ||
added(a::AbstractDifference) | ||
Like [`diff`](@ref), but provide integer vectors that number the rows and | ||
columns of the matrices `A` and `B`. The vector `ia` represents the row numbers | ||
of `A`, and the vector `jb` represents the column numbers of `B` etc. The | ||
position of each vector element refers to the row index (or column index | ||
respectively) of `A` or `B`. | ||
Access the added elements. | ||
""" | ||
function diff(A::AbstractMatrix, B::AbstractMatrix, ia::AbstractVector, | ||
ja::AbstractVector, ib::AbstractVector, jb::AbstractVector) | ||
iadict = OrderedDict(zip(ia, 1:length(ia))) | ||
jadict = OrderedDict(zip(ja, 1:length(ja))) | ||
ibdict = OrderedDict(zip(ib, 1:length(ib))) | ||
jbdict = OrderedDict(zip(jb, 1:length(jb))) | ||
return _diff(A, B, iadict, jadict, ibdict, jbdict) | ||
end | ||
|
||
# Core matrix difference implementation | ||
function _diff(A::AbstractMatrix, B::AbstractMatrix, ia::OrderedDict, | ||
ja::OrderedDict, ib::OrderedDict, jb::OrderedDict) | ||
T = promote_type(eltype(A), eltype(B)) | ||
modvals = sparse([], [], T[]) | ||
addvals = view(Vector{T}(undef, 0), :) | ||
remvals = view(Vector{T}(undef, 0), :) | ||
|
||
if size(A) == (0, 0) || size(B) == (0, 0) | ||
return modvals, addvals, remvals | ||
end | ||
|
||
iakeys = collect(keys(ia)) | ||
jakeys = collect(keys(ja)) | ||
ibkeys = collect(keys(ib)) | ||
jbkeys = collect(keys(jb)) | ||
|
||
# Compute modified values | ||
i = intersect(iakeys, ibkeys) | ||
j = intersect(jakeys, jbkeys) | ||
ia2 = replace(i, ia) | ||
ja2 = replace(j, ja) | ||
ib2 = replace(i, ib) | ||
jb2 = replace(j, jb) | ||
modvals = sparse(view(A, ia2, ja2) - view(B, ib2, jb2)) | ||
|
||
# Compute added values | ||
indicesb = CartesianIndices(B) | ||
modindicesb = CartesianIndex.(Iterators.product(ib2, jb2)) | ||
addindices = setdiff(indicesb, modindicesb) | ||
addvals = view(B, addindices) | ||
|
||
# Compute removed values | ||
indicesa = CartesianIndices(A) | ||
modindicesa = CartesianIndex.(Iterators.product(ia2, ja2)) | ||
remindices = setdiff(indicesa, modindicesa) | ||
remvals = view(A, remindices) | ||
|
||
return modvals, addvals, remvals | ||
end | ||
added(a::AbstractDifference) = a.addvals | ||
|
||
""" | ||
diff(a::AbstractSet, b::AbstractSet) | ||
Compute the difference between set `a` and set `b`, and return a tuple | ||
containing the unique elements that have been shared, added, and removed. | ||
removed(a::AbstractDifference) | ||
# Examples | ||
```jldoctest | ||
julia> diff(Set([1, 2, 3, 3]), Set([4, 2, 1])) | ||
(Set([1, 2]), Set([4]), Set([3])) | ||
``` | ||
Access the removed elements. | ||
""" | ||
diff(a::AbstractSet, b::AbstractSet) = | ||
intersect(a, b), setdiff(b, a), setdiff(a, b) | ||
removed(a::AbstractDifference) = a.remvals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters