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 500b017
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 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,7 @@ 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))

0 comments on commit 500b017

Please sign in to comment.