-
Notifications
You must be signed in to change notification settings - Fork 3
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
12 changed files
with
155 additions
and
36 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
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
""" | ||
SortedVector | ||
A wrapper for sorted vectors, designed for fast unions. | ||
# Constructor | ||
SortedVector(data::AbstractVector; already_sorted=false) | ||
# Example | ||
```jldoctest | ||
x = SortedVector([3, 4, 2]) | ||
x = SortedVector([1, 3, 5]; already_sorted=true) | ||
z = union(x, y) | ||
# output | ||
SortedVector([1, 2, 3, 4, 5]) | ||
```` | ||
""" | ||
struct SortedVector{T<:Number} <: AbstractVector{T} | ||
data::Vector{T} | ||
|
||
function SortedVector{T}(data::AbstractVector{T}) where {T} | ||
return new{T}(convert(Vector{T}, data)) | ||
end | ||
|
||
function SortedVector{T}(x::Number) where {T} | ||
return new{T}([convert(T, x)]) | ||
end | ||
|
||
function SortedVector{T}() where {T} | ||
return new{T}(T[]) | ||
end | ||
end | ||
|
||
function SortedVector(data::AbstractVector{T}; already_sorted=false) where {T} | ||
sorted_data = ifelse(already_sorted, data, sort(data)) | ||
return SortedVector{T}(sorted_data) | ||
end | ||
|
||
function Base.convert(::Type{SortedVector{T}}, v::Vector{T}) where {T} | ||
return SortedVector(v; already_sorted=false) | ||
end | ||
|
||
Base.eltype(::SortedVector{T}) where {T} = T | ||
Base.size(v::SortedVector) = size(v.data) | ||
Base.getindex(v::SortedVector, i) = v.data[i] | ||
Base.IndexStyle(::Type{SortedVector{T}}) where {T} = IndexStyle(Vector{T}) | ||
Base.show(io::IO, v::SortedVector) = print(io, "SortedVector($(v.data))") | ||
|
||
function Base.union(v1::SortedVector{T}, v2::SortedVector{T}) where {T} | ||
left, right = v1.data, v2.data | ||
result = similar(left, length(left) + length(right)) | ||
left_index, right_index, result_index = 1, 1, 1 | ||
# common part of left and right | ||
@inbounds while ( | ||
left_index in eachindex(left) && | ||
right_index in eachindex(right) && | ||
result_index in eachindex(result) | ||
) | ||
left_item = left[left_index] | ||
right_item = right[right_index] | ||
left_smaller = left_item <= right_item | ||
right_smaller = right_item <= left_item | ||
result_item = ifelse(left_smaller, left_item, right_item) | ||
result[result_index] = result_item | ||
result_index += 1 | ||
left_index = ifelse(left_smaller, left_index + 1, left_index) | ||
right_index = ifelse(right_smaller, right_index + 1, right_index) | ||
end | ||
# either left or right has reached its end at this point | ||
@inbounds while left_index in eachindex(left) && result_index in eachindex(result) | ||
result[result_index] = left[left_index] | ||
left_index += 1 | ||
result_index += 1 | ||
end | ||
@inbounds while right_index in eachindex(right) && result_index in eachindex(result) | ||
result[result_index] = right[right_index] | ||
right_index += 1 | ||
result_index += 1 | ||
end | ||
resize!(result, result_index - 1) | ||
return SortedVector(result; already_sorted=true) | ||
end |
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
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 +1 @@ | ||
ConnectivityTracer{Set{UInt64}}(0x0000000000000002,) | ||
ConnectivityTracer{Set{UInt64}}(2,) |
1 change: 1 addition & 0 deletions
1
test/references/show/ConnectivityTracer_SortedVector{UInt64}.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
ConnectivityTracer{SortedVector{UInt64}}(2,) |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
HessianTracer{SortedVector{UInt64}}( | ||
2 => (), | ||
) |
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 +1 @@ | ||
JacobianTracer{Set{UInt64}}(0x0000000000000002,) | ||
JacobianTracer{Set{UInt64}}(2,) |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
JacobianTracer{SortedVector{UInt64}}(2,) |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using ADTypes | ||
using SparseArrays | ||
using SparseConnectivityTracer | ||
using SparseConnectivityTracer: SortedVector | ||
using Test | ||
|
||
@testset "Correctness" begin | ||
@testset "$T - ($k1, $k2)" for T in (Int32, Int64), | ||
k1 in (0, 10, 100, 1000), | ||
k2 in (0, 10, 100, 1000) | ||
|
||
for _ in 1:100 | ||
x = SortedVector(rand(T(1):T(1000), k1); already_sorted=false) | ||
y = SortedVector(sort(rand(T(1):T(1000), k2)); already_sorted=true) | ||
z = union(x, y) | ||
@test eltype(z) == T | ||
@test issorted(z.data) | ||
@test Set(z.data) == union(Set(x.data), Set(y.data)) | ||
if k1 > 0 && k2 > 0 | ||
@test z[1] == min(x[1], y[1]) | ||
@test z[end] == max(x[end], y[end]) | ||
end | ||
end | ||
end | ||
end; | ||
|
||
sd = TracerSparsityDetector(SortedVector{UInt}) | ||
@test ADTypes.jacobian_sparsity(diff, rand(10), sd) isa SparseMatrixCSC | ||
@test ADTypes.hessian_sparsity(x -> sum(abs2, diff(x)), rand(10), sd) isa SparseMatrixCSC |