Skip to content

Commit

Permalink
Adds pop and isless to sets
Browse files Browse the repository at this point in the history
  • Loading branch information
binarybana committed Dec 12, 2012
1 parent e896153 commit 389d2ee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ done(s::Set, state) = done(s.hash, state)
# NOTE: manually optimized to take advantage of Dict representation
next(s::Set, i) = (s.hash.keys[i], skip_deleted(s.hash,i+1))

pop(s::Set) = (val = s.hash.keys[start(s.hash)]; del(s.hash, val); val)

union() = Set()
union(s::Set) = copy(s)
function union(s::Set, sets::Set...)
Expand Down Expand Up @@ -82,5 +84,7 @@ end
-(a::Set, b::Set) = setdiff(a,b)

isequal(l::Set, r::Set) = length(l) == length(r) == length(intersect(l,r))
isless(l::Set, r::Set) = (length(l) < length(r)) && ((l&r) == l)
<=(l::Set, r::Set) = isless(l,r) || isequal(l,r)

unique(C) = elements(add_each(Set{eltype(C)}(), C))
24 changes: 24 additions & 0 deletions test/corelib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@ d4[1001] = randstring(3)
@test !isempty(Set("banana", "apple"))
@test !isempty(Set(1, 1:10, "pear"))

# isless
@test isless(Set(), Set(1))
@test isless(Set(1), Set(1,2))
@test !isless(Set(3), Set(1,2))
@test !(Set(3) > Set(1,2))
@test Set(1,2,3) > Set(1,2)
@test !(Set(3) <= Set(1,2))
@test !(Set(3) >= Set(1,2))
@test Set(1) <= Set(1,2)
@test Set(1,2) <= Set(1,2)
@test Set(1,2) >= Set(1,2)
@test Set(1,2,3) >= Set(1,2)
@test !(Set(1,2,3) >= Set(1,2,4))
@test !(Set(1,2,3) <= Set(1,2,4))

# add, length
s = Set()
@test isempty(s)
Expand Down Expand Up @@ -323,6 +338,15 @@ for data_in in ((7,8,4,5),
end
end

# pop
origs = Set(1,2,3,"apple")
s = copy(origs)
for i in 1:numel(origs)
el = pop(s)
@test !has(s, el)
@test has(origs, el)
end
@test isempty(s)
# isequal
@test isequal(Set(), Set())
@test !isequal(Set(), Set(1))
Expand Down

0 comments on commit 389d2ee

Please sign in to comment.