Skip to content

Commit

Permalink
IntSet: O(1) maximum/minimum/issorted
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Jul 18, 2017
1 parent 8a18928 commit 79f89d6
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions base/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ done(s::IntSet, i) = i <= 0


@noinline _throw_intset_notempty_error() = throw(ArgumentError("collection must be non-empty"))

function first(s::IntSet)
idx = findfirst(s.bits)
idx == 0 ? _throw_intset_notempty_error() : idx
end

function last(s::IntSet)
idx = findprev(s.bits, length(s.bits))
idx == 0 ? _throw_intset_notempty_error() : idx
Expand Down Expand Up @@ -239,3 +245,8 @@ function hash(s::IntSet, h::UInt)
end
h
end

minimum(s::IntSet) = first(s)
maximum(s::IntSet) = last(s)
extrema(s::IntSet) = (first(s), last(s))
issorted(s::IntSet) = true

0 comments on commit 79f89d6

Please sign in to comment.