-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NDTensors] Add SortedSets and new TagSet prototype (#1204)
- Loading branch information
Showing
32 changed files
with
1,127 additions
and
6 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,9 @@ | ||
# Trait determining the style of inserting into a structure | ||
abstract type InsertStyle end | ||
struct IsInsertable <: InsertStyle end | ||
struct NotInsertable <: InsertStyle end | ||
struct FastCopy <: InsertStyle end | ||
|
||
# Assume is insertable | ||
@inline InsertStyle(::Type) = IsInsertable() | ||
@inline InsertStyle(x) = InsertStyle(typeof(x)) |
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,242 @@ | ||
# Union two unique sorted collections into an | ||
# output buffer, returning a unique sorted collection. | ||
|
||
using Base: Ordering, ord, lt | ||
|
||
function unionsortedunique!( | ||
itr1, | ||
itr2; | ||
lt=isless, | ||
by=identity, | ||
rev::Union{Bool,Nothing}=nothing, | ||
order::Ordering=Forward, | ||
) | ||
return unionsortedunique!(itr1, itr2, ord(lt, by, rev, order)) | ||
end | ||
|
||
function unionsortedunique!(itr1, itr2, order::Ordering) | ||
i1 = firstindex(itr1) | ||
i2 = firstindex(itr2) | ||
stop1 = lastindex(itr1) | ||
stop2 = lastindex(itr2) | ||
@inbounds while i1 ≤ stop1 && i2 ≤ stop2 | ||
item1 = itr1[i1] | ||
item2 = itr2[i2] | ||
if lt(order, item1, item2) | ||
i1 += 1 | ||
elseif lt(order, item2, item1) | ||
# TODO: Use `insertat!`? | ||
resize!(itr1, length(itr1) + 1) | ||
for j in length(itr1):-1:(i1 + 1) | ||
itr1[j] = itr1[j - 1] | ||
end | ||
itr1[i1] = item2 | ||
i1 += 1 | ||
i2 += 1 | ||
stop1 += 1 | ||
else # They are equal | ||
i1 += 1 | ||
i2 += 1 | ||
end | ||
end | ||
# TODO: Use `insertat!`? | ||
resize!(itr1, length(itr1) + (stop2 - i2 + 1)) | ||
@inbounds for j2 in i2:stop2 | ||
itr1[i1] = itr2[j2] | ||
i1 += 1 | ||
end | ||
return itr1 | ||
end | ||
|
||
function unionsortedunique( | ||
itr1, | ||
itr2; | ||
lt=isless, | ||
by=identity, | ||
rev::Union{Bool,Nothing}=nothing, | ||
order::Ordering=Forward, | ||
) | ||
return unionsortedunique(itr1, itr2, ord(lt, by, rev, order)) | ||
end | ||
|
||
# Union two unique sorted collections into an | ||
# output buffer, returning a unique sorted collection. | ||
function unionsortedunique(itr1, itr2, order::Ordering) | ||
out = thaw_type(itr1)(undef, length(itr1)) | ||
i1 = firstindex(itr1) | ||
i2 = firstindex(itr2) | ||
iout = firstindex(out) | ||
stop1 = lastindex(itr1) | ||
stop2 = lastindex(itr2) | ||
stopout = lastindex(out) | ||
@inbounds while i1 ≤ stop1 && i2 ≤ stop2 | ||
iout > stopout && resize!(out, iout) | ||
item1 = itr1[i1] | ||
item2 = itr2[i2] | ||
if lt(order, item1, item2) | ||
out[iout] = item1 | ||
iout += 1 | ||
i1 += 1 | ||
elseif lt(order, item2, item1) | ||
out[iout] = item2 | ||
iout += 1 | ||
i2 += 1 | ||
else # They are equal | ||
out[iout] = item1 | ||
iout += 1 | ||
i1 += 1 | ||
i2 += 1 | ||
end | ||
end | ||
# In case `out` was too long to begin with. | ||
## resize!(out, iout - 1) | ||
# TODO: Use `insertat!`? | ||
r1 = i1:stop1 | ||
resize!(out, length(out) + length(r1)) | ||
@inbounds for j1 in r1 | ||
out[iout] = itr1[j1] | ||
iout += 1 | ||
end | ||
# TODO: Use `insertat!`? | ||
r2 = i2:stop2 | ||
resize!(out, length(out) + length(r2)) | ||
@inbounds for j2 in r2 | ||
out[iout] = itr2[j2] | ||
iout += 1 | ||
end | ||
return freeze(out) | ||
end | ||
|
||
function setdiffsortedunique!( | ||
itr1, | ||
itr2; | ||
lt=isless, | ||
by=identity, | ||
rev::Union{Bool,Nothing}=nothing, | ||
order::Ordering=Forward, | ||
) | ||
return setdiffsortedunique!(itr1, itr2, ord(lt, by, rev, order)) | ||
end | ||
|
||
function setdiffsortedunique!(itr1, itr2, order::Ordering) | ||
i1 = firstindex(itr1) | ||
i2 = firstindex(itr2) | ||
stop1 = lastindex(itr1) | ||
stop2 = lastindex(itr2) | ||
@inbounds while i1 ≤ stop1 && i2 ≤ stop2 | ||
item1 = itr1[i1] | ||
item2 = itr2[i2] | ||
if lt(order, item1, item2) | ||
i1 += 1 | ||
elseif lt(order, item2, item1) | ||
i2 += 1 | ||
else # They are equal | ||
# TODO: Use `deletate!`? | ||
for j1 in i1:(length(itr1) - 1) | ||
itr1[j1] = itr1[j1 + 1] | ||
end | ||
resize!(itr1, length(itr1) - 1) | ||
stop1 = lastindex(itr1) | ||
i2 += 1 | ||
end | ||
end | ||
return itr1 | ||
end | ||
|
||
function setdiffsortedunique( | ||
itr1, | ||
itr2; | ||
lt=isless, | ||
by=identity, | ||
rev::Union{Bool,Nothing}=nothing, | ||
order::Ordering=Forward, | ||
) | ||
return setdiffsortedunique(itr1, itr2, ord(lt, by, rev, order)) | ||
end | ||
|
||
function setdiffsortedunique(itr1, itr2, order::Ordering) | ||
out = thaw_type(itr1)() | ||
i1 = firstindex(itr1) | ||
i2 = firstindex(itr2) | ||
iout = firstindex(out) | ||
stop1 = lastindex(itr1) | ||
stop2 = lastindex(itr2) | ||
stopout = lastindex(out) | ||
@inbounds while i1 ≤ stop1 && i2 ≤ stop2 | ||
item1 = itr1[i1] | ||
item2 = itr2[i2] | ||
if lt(order, item1, item2) | ||
iout > stopout && resize!(out, iout) | ||
out[iout] = item1 | ||
iout += 1 | ||
i1 += 1 | ||
elseif lt(order, item2, item1) | ||
i2 += 1 | ||
else # They are equal | ||
i1 += 1 | ||
i2 += 1 | ||
end | ||
end | ||
resize!(out, iout - 1) | ||
return freeze(out) | ||
end | ||
|
||
function intersectsortedunique!( | ||
itr1, | ||
itr2; | ||
lt=isless, | ||
by=identity, | ||
rev::Union{Bool,Nothing}=nothing, | ||
order::Ordering=Forward, | ||
) | ||
return intersectsortedunique!(itr1, itr2, ord(lt, by, rev, order)) | ||
end | ||
|
||
function intersectsortedunique!(itr1, itr2, order::Ordering) | ||
return error("Not implemented") | ||
end | ||
|
||
function intersectsortedunique( | ||
itr1, | ||
itr2; | ||
lt=isless, | ||
by=identity, | ||
rev::Union{Bool,Nothing}=nothing, | ||
order::Ordering=Forward, | ||
) | ||
return intersectsortedunique(itr1, itr2, ord(lt, by, rev, order)) | ||
end | ||
|
||
function intersectsortedunique(itr1, itr2, order::Ordering) | ||
return error("Not implemented") | ||
end | ||
|
||
function symdiffsortedunique!( | ||
itr1, | ||
itr2; | ||
lt=isless, | ||
by=identity, | ||
rev::Union{Bool,Nothing}=nothing, | ||
order::Ordering=Forward, | ||
) | ||
return symdiffsortedunique!(itr1, itr2, ord(lt, by, rev, order)) | ||
end | ||
|
||
function symdiffsortedunique!(itr1, itr2, order::Ordering) | ||
return error("Not implemented") | ||
end | ||
|
||
function symdiffsortedunique( | ||
itr1, | ||
itr2; | ||
lt=isless, | ||
by=identity, | ||
rev::Union{Bool,Nothing}=nothing, | ||
order::Ordering=Forward, | ||
) | ||
return symdiffsortedunique(itr1, itr2, ord(lt, by, rev, order)) | ||
end | ||
|
||
function symdiffsortedunique(itr1, itr2, order::Ordering) | ||
return error("Not implemented") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
thaw(x) = copy(x) | ||
freeze(x) = x | ||
|
||
thaw_type(::Type{<:AbstractArray{<:Any,N}}, ::Type{T}) where {T,N} = Array{T,N} | ||
thaw_type(x::AbstractArray, ::Type{T}) where {T} = thaw_type(typeof(x), T) | ||
thaw_type(x::AbstractArray{T}) where {T} = thaw_type(typeof(x), T) |
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,16 +1,44 @@ | ||
module SmallVectors | ||
using StaticArrays | ||
|
||
export SmallVector, MSmallVector, SubSmallVector | ||
export AbstractSmallVector, | ||
SmallVector, | ||
MSmallVector, | ||
SubSmallVector, | ||
FastCopy, | ||
InsertStyle, | ||
IsInsertable, | ||
NotInsertable, | ||
insert, | ||
delete, | ||
thaw, | ||
freeze, | ||
maxlength, | ||
unionsortedunique, | ||
unionsortedunique!, | ||
setdiffsortedunique, | ||
setdiffsortedunique!, | ||
intersectsortedunique, | ||
intersectsortedunique!, | ||
symdiffsortedunique, | ||
symdiffsortedunique!, | ||
thaw_type | ||
|
||
struct NotImplemented <: Exception | ||
msg::String | ||
end | ||
NotImplemented() = NotImplemented("Not implemented.") | ||
|
||
include("BaseExt/insertstyle.jl") | ||
include("BaseExt/thawfreeze.jl") | ||
include("BaseExt/sortedunique.jl") | ||
include("abstractarray/insert.jl") | ||
include("abstractsmallvector/abstractsmallvector.jl") | ||
include("abstractsmallvector/deque.jl") | ||
include("msmallvector/msmallvector.jl") | ||
include("smallvector/smallvector.jl") | ||
include("smallvector/insertstyle.jl") | ||
include("msmallvector/thawfreeze.jl") | ||
include("smallvector/thawfreeze.jl") | ||
include("subsmallvector/subsmallvector.jl") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SmallVectors.insert(a::Vector, index::Integer, item) = insert!(copy(a), index, item) | ||
delete(d::AbstractDict, key) = delete!(copy(d), key) |
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
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,2 @@ | ||
thaw(vec::MSmallVector) = copy(vec) | ||
freeze(vec::MSmallVector) = SmallVector(vec) |
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 @@ | ||
InsertStyle(::Type{<:SmallVector}) = FastCopy() |
Oops, something went wrong.