Sha256: 173f63d2e8893ce1bc27e10999c88d61ee675eab74f44e187ef4e408a19c8681

Contents?: true

Size: 1.53 KB

Versions: 106

Compression:

Stored size: 1.53 KB

Contents

import Base: AbstractSet, isempty, length, in, issubset, start, next, done,
             push!, ==, copy, intersect!, intersect, union!, union

type CustomSet{T} <: AbstractSet
    elements::Array{T, 1}
end

isempty(s::CustomSet) = isempty(s.elements)
length(s::CustomSet) = length(s.elements)
in(element, s::CustomSet) = in(element, s.elements)  # this also defines issubset(::CustomSet, ::CustomSet)
==(s1::CustomSet, s2::CustomSet) = issubset(s1, s2) && issubset(s2, s1)
copy(s::CustomSet) = CustomSet(copy(s.elements))
push!(s::CustomSet, element) = push!(s.elements, element)

# Iterator protocol
start(::CustomSet) = 1
next(s::CustomSet, state) = s.elements[state], state + 1
done(s::CustomSet, state) = state > length(s)

function disjoint(s1::CustomSet, s2::CustomSet)
    for element in s1
        element in s2 && return false
    end
    return true
end

function complement!(s1::CustomSet, s2::CustomSet)
    for (i, element) in enumerate(s1)
        element in s2 && deleteat!(s1.elements, i)
    end
    return s1
end
complement(s1::CustomSet, s2::CustomSet) = complement!(copy(s1), s2)

function intersect!(s1::CustomSet, s2::CustomSet)
    tbd = Int[]
    for (i, element) in enumerate(s1)
        element in s2 || push!(tbd, i)
    end
    deleteat!(s1.elements, tbd)
    return s1
end
intersect(s1::CustomSet, s2::CustomSet) = intersect!(copy(s1), s2)

function union!(s1::CustomSet, s2::CustomSet)
    for element in s2
        element in s1 || push!(s1, element)
    end
    return s1
end
union(s1::CustomSet, s2::CustomSet) = union!(copy(s1), s2)

Version data entries

106 entries across 106 versions & 1 rubygems

Version Path
trackler-2.1.0.41 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.40 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.39 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.38 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.37 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.36 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.34 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.33 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.32 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.31 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.30 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.29 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.28 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.27 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.26 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.25 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.24 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.23 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.22 tracks/julia/exercises/custom-set/example.jl
trackler-2.1.0.21 tracks/julia/exercises/custom-set/example.jl