Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LittleSet (follow-up on #95) #107

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/OrderedCollections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ module OrderedCollections
valtype, lastindex, nextind,
copymutable, emptymutable, dict_with_eltype

export OrderedDict, OrderedSet, LittleDict
export OrderedDict, OrderedSet, LittleDict, LittleSet
export freeze

include("dict_support.jl")
include("ordered_dict.jl")
include("little_dict.jl")
include("little_set.jl")
include("ordered_set.jl")
include("dict_sorting.jl")

Expand Down
4 changes: 4 additions & 0 deletions src/dict_support.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
# so they are redefined here.
_tablesz(x::Integer) = x < 16 ? 16 : one(x)<<((sizeof(x)<<3)-leading_zeros(x-1))
hashindex(key, sz) = (reinterpret(Int,(hash(key))) & (sz-1)) + 1

const orderedset_seed = UInt === UInt64 ? 0x2114638a942a91a5 : 0xd86bdbf1

struct NotFoundSentinel end # Struct to mark not not found
5 changes: 2 additions & 3 deletions src/little_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const StoreType{T} = Union{Tuple{Vararg{T}}, AbstractVector{T}}
end

"""
LittleDict(keys, vals)<:AbstractDict
LittleDict(keys, vals) <: AbstractDict

An ordered dictionary type for small numbers of keys.
Rather than using `hash` or some other sophisticated measure
Expand Down Expand Up @@ -57,7 +57,7 @@ end
# Other iterators should be copied to a Vector
LittleDict(ks, vs) = LittleDict(collect(ks), collect(vs))

function LittleDict{K,V}(itr) where {K,V}
function LittleDict{K, V}(itr) where {K,V}
ks = K[]
vs = V[]
for val in itr
Expand Down Expand Up @@ -132,7 +132,6 @@ function Base.map!(f, iter::Base.ValueIterator{<:LittleDict})
return iter
end

struct NotFoundSentinel end # Struct to mark not not found
function Base.get(dd::LittleDict, key, default)
@assert length(dd.keys) == length(dd.vals)
for ii in 1:length(dd.keys)
Expand Down
286 changes: 286 additions & 0 deletions src/little_set.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@

"""
LittleSet([itr]) <: AbstractSet

Constructs an ordered set optimized for a small number of elements, given the
iterable `itr`. The underlying data is stored as either an `AbstractVector` or
a `Tuple` and is optimal for 30-50 elements, similar to [`LittleDict`](@ref).
"""
struct LittleSet{T, D<:StoreType{T}} <: AbstractSet{T}
data::D

global const UnfrozenLittleSet{T} = LittleSet{T, <:AbstractVector{T}}
global const FrozenLittleSet{T} = LittleSet{T, <:Tuple}
global const OpaqueLittleSet{T} = LittleSet{T, Tuple{Vararg{T}}}

LittleSet{T, D}(data) where {T,D} = new{T, D}(data)
function OpaqueLittleSet{T}(@nospecialize(data)) where {T}
new_data = isa(data, Tuple) ? data : Tuple(data)
new{T, Tuple{Vararg{T}}}(new_data)
end
function OpaqueLittleSet(@nospecialize(data))
T = eltype(data)
new{T, Tuple{Vararg{T}}}(data)
end
function LittleSet{T}(data::AbstractVector) where {T}
if eltype(data) == T
return new{T, typeof(data)}(data)
else
d = convert(AbstractVector{T}, data)
return new{T, typeof(d)}(d)
end
end
LittleSet{T}(data::Tuple{Vararg{T}}) where {T} = new{T, typeof(data)}(data)
LittleSet{T}(itr) where {T} = union!(LittleSet{T}(), itr)
LittleSet{T}() where {T} = new{T, Vector{T}}(Vector{T}())
LittleSet() = LittleSet{Any}()
function LittleSet(itr)
IET = Base.IteratorEltype(itr)
if isa(IET, Base.HasEltype)
LittleSet{eltype(itr)}(itr)
else
T = Base.@default_eltype(itr)
if (Base.isconcretetype(T) || T === Union{})
return LittleSet{T}(itr)
else
return Base.grow_to!(LittleSet{T}(), itr)
end
end
end

function Base.empty(s::LittleSet{T, D}) where {T, D}
if isa(s, OpaqueLittleSet)
return new{T, Tuple{Vararg{T}}}(())
elseif D <: Tuple
return new{T, Tuple{}}(())
else
return new{T, D}(empty(getfield(s, :data)))
end
end
function Base.emptymutable(s::LittleSet{T, D}, ::Type{U}=T) where {T, D, U}
if D <: Tuple
new_data = U[]
else
new_data = Base.emptymutable(getfield(s, :data), U)
end
return new{U, typeof(new_data)}(new_data)
end
end

function Base.Tuple(s::LittleSet)
data = getfield(s, :data)
isa(data, Tuple) ? data : Tuple(data)
end

freeze(s::AbstractSet{T}) where {T} = LittleSet{T}(Tuple(s))

hash(s::LittleSet, h::UInt) = hash(getfield(s, :data), hash(orderedset_seed, h))

function Base.length(s::LittleSet)
data = getfield(s, :data)
isa(data, Tuple) ? nfields(data) : length(data)
end

function Base.isempty(s::LittleSet)
data = getfield(s, :data)
isa(data, Tuple) ? nfields(data) == 0 : isempty(data)
end

function Base.copy(s::LittleSet{T, D}) where {T, D}
# since `Base.copy` is a shallow copy on collections, an immutable collection
# like `Tuple` is treated the same
if D <: Tuple
return s
else
return LittleSet{T, D}(copy(getfield(s, :data)))
end
end

function Base.copymutable(s::LittleSet{T}) where {T}
data = getfield(s, :data)
if isa(data, Tuple)
i = nfields(data)
new_data = Vector{T}(undef, n)
while i != 0
@inbounds new_data[i] = getfield(data, i)
i -= 1
end
else
new_data = Base.copymutable(data)
end
LittleSet{T}(new_data)
end

function Base.sizehint!(s::UnfrozenLittleSet, sz)
sizehint!(getfield(s, :data), sz)
return s
end
Base.iterate(s::LittleSet, state...) = iterate(getfield(s, :data), state...)

function Base.in(x, s::LittleSet)
data = getfield(s, :data)
if isa(data, Tuple)
n = nfields(data)
while n > 0
isequal(x, getfield(data, n)) && return true
n -= 1
end
return false
else
return in(x, data)
end
end

# HACK: this is a temporary hack to get around the lack of `sort` available for tuples
function Base.sort(s::FrozenLittleSet{T}; ks...) where {T}
LittleSet{T}(sort(T[getfield(s, :data)...]; ks...))
end
function Base.sort(s::UnfrozenLittleSet{T}; ks...) where {T}
LittleSet{T}(sort(getfield(s, :data); ks...))
end

function Base.sort!(s::UnfrozenLittleSet; ks...)
sort!(getfield(s, :data); ks...)
return s
end

Base.first(s::UnfrozenLittleSet, n::Integer) = LittleSet(first(getfield(s, :data), n))
function Base.first(s::FrozenLittleSet{T}, n::Integer) where {T}
N = Int(n)
N < 0 && throw(ArgumentError("Number of elements must be nonnegative"))
data = getfield(s, :data)
stop = nfields(data)
if stop <= N
# max number of elements is everything so it's equivalent to `copy`
return s
elseif isa(s, LittleSet{T, Tuple{Vararg{T}}})
return LittleSet{T, Tuple{Vararg{T}}}(Tuple(@inbounds(collect(data)[1:N])))
else
return LittleSet{T}(ntuple(i->getfield(data, i), min(nfields(data), n)))
end
end

Base.last(s::UnfrozenLittleSet, n::Integer) = LittleSet(last(getfield(s, :data), n))
function Base.last(s::FrozenLittleSet{T}, n::Integer) where {T}
N = Int(n)
N < 0 && throw(ArgumentError("Number of elements must be nonnegative"))
data = getfield(s, :data)
stop = nfields(data)
offset = stop - N
if offset < 1
# offset less than one means each element is returned so it's equivalent to `copy`
return s
elseif isa(s, LittleSet{T, Tuple{Vararg{T}}})
return LittleSet{T, Tuple{Vararg{T}}}(Tuple(@inbounds(collect(data)[1:N])))
else
return LittleSet{T}(ntuple(i->getfield(data, offset + i), N))
end
end

function Base.union(x::FrozenLittleSet{T1}, y::FrozenLittleSet{T2}) where {T1, T2}
xdata = getfield(x, :data)
nx = nfields(xdata)
ydata = getfield(y, :data)
ny = nfields(ydata)
if isa(x, LittleSet{T1, Tuple{Vararg{T1}}}) || isa(y, LittleSet{T2, Tuple{Vararg{T2}}})
if nx < ny # nx is smaller so search for x items in y
newdata = (filter(!in(y), x)..., ydata...)
else # ny is smaller so search for y items in x
newdata = (xdata..., getfield(filter(!in(x), y), :data)...)
end
T = Union{T1, T2}
return LittleSet{T, Tuple{Vararg{T}}}(newdata)
else
if nx < ny # nx is smaller so search for x items in y
return LittleSet((filter(!in(ydata), xdata)..., ydata...))
else # ny is smaller so search for y items in x
return LittleSet((xdata..., filter(!in(xdata), ydata)...))
end
end
end

function Base.push!(s::UnfrozenLittleSet, val)
data = getfield(s, :data)
if !in(val, data)
push!(data, val)
end
return s
end

Base.pop!(s::UnfrozenLittleSet) = pop!(getfield(s, :data))
function Base.pop!(s::UnfrozenLittleSet, key)
data = getfield(s, :data)
for i in eachindex(data)
k = @inbounds(data[i])
if (key === k || isequal(key, k))
deleteat!(data, i)
return k
end
end
throw(KeyError(key))
end
function Base.pop!(s::UnfrozenLittleSet, key, default)
data = getfield(s, :data)
for i in eachindex(data)
k = @inbounds(data[i])
if (key === k || isequal(key, k))
deleteat!(data, i)
return k
end
end
return default
end

Base.empty!(s::UnfrozenLittleSet) = (empty!(getfield(s, :data)); s)

function Base.delete!(s::UnfrozenLittleSet, key)
data = getfield(s, :data)
for i in eachindex(data)
k = @inbounds(data[i])
if (key === k || isequal(key, k))
deleteat!(data, i)
break
end
end
return s
end

function Base.replace(
f::Union{Function, Type},
s::LittleSet{T};
count::Integer=typemax(Int)
) where {T}
newdata = replace(f, getfield(s, :data); count=count)
if isa(s, LittleSet{T, Tuple{Vararg{T}}})
T2 = eltype(newdata)
return LittleSet{T2, Tuple{Vararg{T2}}}(newdata)
else
return LittleSet(newdata)
end
end
function Base.replace(
s::LittleSet{T},
old_new::Pair{F, S}...;
count::Integer=typemax(Int)
) where {T, F, S}
newdata = replace(getfield(s, :data), old_new...; count=count)
if isa(s, LittleSet{T, Tuple{Vararg{T}}})
T2 = Union{T, S}
return LittleSet{T2, Tuple{Vararg{T2}}}(newdata)
else
return LittleSet(newdata)
end
end

function Base.filter(f, s::LittleSet{T}) where {T}
newdata = filter(f, getfield(s, :data))
if isa(s, OpaqueLittleSet)
return OpaqueLittleSet{T}(newdata)
else
return LittleSet(newdata)
end
end
function Base.filter!(f, s::UnfrozenLittleSet)
filter!(f, getfield(s, :data))
return s
end
1 change: 0 additions & 1 deletion src/ordered_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ function filter!(f::Function, s::OrderedSet)
return s
end

const orderedset_seed = UInt === UInt64 ? 0x2114638a942a91a5 : 0xd86bdbf1
function hash(s::OrderedSet, h::UInt)
h = hash(orderedset_seed, h)
s.dict.ndel > 0 && rehash!(s.dict)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Random, Serialization

tests = [
"little_dict",
"little_set",
"ordered_dict",
"ordered_set",
]
Expand Down
Loading
Loading