HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Modulenonempty-containers-0.3.6.0Haskell2010

Data.IntSet.NonEmpty

Non-Empty Finite Integer Sets

The NEIntSet type represents a non-empty set of integers.

See documentation for NEIntSet for information on how to convert and manipulate such non-empty set.

This module essentially re-imports the API of Data.IntSet and its IntSet type, along with semantics and asymptotics. In most situations, asymptotics are different only by a constant factor. In some situations, asmyptotics are even better (constant-time instead of log-time).

Because NEIntSet is implemented using IntSet, all of the caveats of using IntSet apply (such as the limitation of the maximum size of sets).

All functions take non-empty sets as inputs. In situations where their results can be guarunteed to also be non-empty, they also return non-empty sets. In situations where their results could potentially be empty, IntSet is returned instead.

Some functions (partition, split) have modified return types to account for possible configurations of non-emptiness.

This module is intended to be imported qualified, to avoid name clashes with Prelude and Data.IntSet functions:

import qualified Data.IntSet.NonEmpty as NEIS

Note that all asmyptotics O(f(n)) in this module are actually O(min(W, f(n))), where W is the number of bits in an Int (32 or 64). That is, if f(n) is greater than W, all operations are constant-time.

  • 2 types
  • 56 values

Non-Empty Set Type

2 declarations
datadata NEIntSet
#

A non-empty (by construction) set of integers. At least one value exists in an NEIntSet a at all times.

Functions that take an NEIntSet can safely operate on it with the assumption that it has at least one item.

Functions that return an NEIntSet provide an assurance that the result has at least one item.

Data.IntSet.NonEmpty re-exports the API of Data.IntSet, faithfully reproducing asymptotics, typeclass constraints, and semantics. Functions that ensure that input and output sets are both non-empty (like insert) return NEIntSet, but functions that might potentially return an empty map (like delete) return a IntSet instead.

You can directly construct an NEIntSet with the API from Data.IntSet.NonEmpty; it's more or less the same as constructing a normal IntSet, except you don't have access to empty. There are also a few ways to construct an NEIntSet from a IntSet:

  1. The nonEmptySet smart constructor will convert a IntSet a into a Maybe (NEIntSet a), returning Nothing if the original IntSet was empty.

  2. You can use the Data.IntSet.NonEmpty.insertIntSet family of functions to insert a value into a IntSet to create a guaranteed NEIntSet.

  3. You can use the IsNonEmpty and IsEmpty patterns to "pattern match" on a IntSet to reveal it as either containing a NEIntSet or an empty map.

  4. withNonEmpty offers a continuation-based interface for deconstructing a IntSet and treating it as if it were an NEIntSet.

You can convert an NEIntSet into a IntSet with toSet or IsNonEmpty, essentially "obscuring" the non-empty property from the type.

Instances12IsList, Eq, Data, Ord, Read, Show, …
  • IsList NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal
  • Eq NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal
  • Data NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal
  • Ord NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal
  • Read NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal
  • Show NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal
  • Semigroup NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal

    Left-biased union

  • NFData NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal
  • FromJSON NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal
  • ToJSON NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal
  • IsNonEmptyList NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty.List
  • type Item NEIntSet = KeyDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal

Conversions between empty and non-empty sets

patternpattern IsNonEmpty :: NEIntSet -> IntSet
#

O(1) match, O(log n) usage of contents. The IsNonEmpty and IsEmpty patterns allow you to treat a IntSet as if it were either a IsNonEmpty n (where n is a NEIntSet) or an IsEmpty.

For example, you can pattern match on a IntSet:

myFunc :: IntSet X -> Y
myFunc (IsNonEmpty n) =  -- here, the user provided a non-empty set, and n is the NEIntSet
myFunc IsEmpty        =  -- here, the user provided an empty set

Matching on IsNonEmpty n means that the original IntSet was not empty, and you have a verified-non-empty NEIntSet n to use.

Note that patching on this pattern is O(1). However, using the contents requires a O(log n) cost that is deferred until after the pattern is matched on (and is not incurred at all if the contents are never used).

A case statement handling both IsNonEmpty and IsEmpty provides complete coverage.

This is a bidirectional pattern, so you can use IsNonEmpty to convert a NEIntSet back into a IntSet, obscuring its non-emptiness (see toSet).

O(log n). Smart constructor for an NEIntSet from a IntSet. Returns Nothing if the IntSet was originally actually empty, and Just n with an NEIntSet, if the IntSet was not empty.

nonEmptySet and maybe empty toSet form an isomorphism: they are perfect structure-preserving inverses of eachother.

See IsNonEmpty for a pattern synonym that lets you "match on" the possiblity of a IntSet being an NEIntSet.

nonEmptySet (Data.IntSet.fromList [3,5]) == Just (fromList (3:|[5]))
valuetoSet :: NEIntSet -> IntSet
#

O(log n). Convert a non-empty set back into a normal possibly-empty map, for usage with functions that expect IntSet.

Can be thought of as "obscuring" the non-emptiness of the set in its type. See the Data.IntSet.NonEmpty.IsNotEmpty pattern.

nonEmptySet and maybe empty toSet form an isomorphism: they are perfect structure-preserving inverses of eachother.

toSet (fromList ((3,"a") :| [(5,"b")])) == Data.IntSet.fromList [(3,"a"), (5,"b")]
valueinsertSet :: Key -> IntSet -> NEIntSet
#

O(log n). Convert a IntSet into an NEIntSet by adding a value. Because of this, we know that the set must have at least one element, and so therefore cannot be empty.

See insertSetMin for a version that is constant-time if the new value is strictly smaller than all values in the original set

insertSet 4 (Data.IntSet.fromList [5, 3]) == fromList (3 :| [4, 5])
insertSet 4 Data.IntSet.empty == singleton 4 "c"
valueinsertSetMin :: Key -> IntSet -> NEIntSet
#

O(1) Convert a IntSet into an NEIntSet by adding a value where the value is strictly less than all values in the input set The values in the original map must all be strictly greater than the new value. The precondition is not checked.

insertSetMin 2 (Data.IntSet.fromList [5, 3]) == fromList (2 :| [3, 5])
valid (insertSetMin 2 (Data.IntSet.fromList [5, 3])) == True
valid (insertSetMin 7 (Data.IntSet.fromList [5, 3])) == False
valid (insertSetMin 3 (Data.IntSet.fromList [5, 3])) == False
valueinsertSetMax :: Key -> IntSet -> NEIntSet
#

O(log n) Convert a IntSet into an NEIntSet by adding a value where the value is strictly less than all values in the input set The values in the original map must all be strictly greater than the new value. The precondition is not checked.

At the current moment, this is identical simply insertSet; however, it is left both for consistency and as a placeholder for a future version where optimizations are implemented to allow for a faster implementation.

insertSetMin 7 (Data.IntSet.fromList [5, 3]) == fromList (3 :| [5, 7])

Construction

4 declarations

O(n). Build a set from an ascending list in linear time. /The precondition (input list is ascending) is not checked./

O(n). Build a set from an ascending list of distinct elements in linear time. The precondition (input list is strictly ascending) is not checked.

Insertion

1 declaration
valueinsert :: Key -> NEIntSet -> NEIntSet
#

O(log n). Insert an element in a set. If the set already contains an element equal to the given value, it is replaced with the new value.

Deletion

2 declarations

Query

10 declarations
valuelookupLT :: Key -> NEIntSet -> Maybe Key
#

O(log n). Find largest element smaller than the given one.

lookupLT 3 (fromList (3 :| [5])) == Nothing
lookupLT 5 (fromList (3 :| [5])) == Just 3
valuelookupGT :: Key -> NEIntSet -> Maybe Key
#

O(log n). Find smallest element greater than the given one.

lookupLT 4 (fromList (3 :| [5])) == Just 5
lookupLT 5 (fromList (3 :| [5])) == Nothing
valuelookupLE :: Key -> NEIntSet -> Maybe Key
#

O(log n). Find largest element smaller or equal to the given one.

lookupLT 2 (fromList (3 :| [5])) == Nothing
lookupLT 4 (fromList (3 :| [5])) == Just 3
lookupLT 5 (fromList (3 :| [5])) == Just 5
valuelookupGE :: Key -> NEIntSet -> Maybe Key
#

O(log n). Find smallest element greater or equal to the given one.

lookupLT 3 (fromList (3 :| [5])) == Just 3
lookupLT 4 (fromList (3 :| [5])) == Just 5
lookupLT 6 (fromList (3 :| [5])) == Nothing
valuesize :: NEIntSet -> Int
#

O(1). The number of elements in the set. Guaranteed to be greater than zero.

valuedisjoint :: NEIntSet -> NEIntSet -> Bool
#

O(n+m). Check whether two sets are disjoint (i.e. their intersection is empty).

disjoint (fromList (2:|[4,6]))   (fromList (1:|[3]))     == True
disjoint (fromList (2:|[4,6,8])) (fromList (2:|[3,5,7])) == False
disjoint (fromList (1:|[2]))     (fromList (1:|[2,3,4])) == False

Combine

7 declarations
valueunion :: NEIntSet -> NEIntSet -> NEIntSet
#

O(m*log(n/m + 1)), m <= n. The union of two sets, preferring the first set when equal elements are encountered.

valuedifference :: NEIntSet -> NEIntSet -> IntSet
#

O(m*log(n/m + 1)), m <= n. Difference of two sets.

Returns a potentially empty set (IntSet) because the first set might be a subset of the second set, and therefore have all of its elements removed.

O(m*log(n/m + 1)), m <= n. The intersection of two sets.

Returns a potentially empty set (IntSet), because the two sets might have an empty intersection.

Elements of the result come from the first set, so for example

import qualified Data.IntSet.NonEmpty as NES
data AB = A | B deriving Show
instance Ord AB where compare _ _ = EQ
instance Eq AB where _ == _ = True
main = print (NES.singleton A `NES.intersection` NES.singleton B,
              NES.singleton B `NES.intersection` NES.singleton A)

prints (fromList (A:|[]),fromList (B:|[])).

Filter

5 declarations
valuefilter :: (Key -> Bool) -> NEIntSet -> IntSet
#

O(n). Filter all elements that satisfy the predicate.

Returns a potentially empty set (IntSet) because the predicate might filter out all items in the original non-empty set.

O(n). Partition the map according to a predicate.

Returns a These with potentially two non-empty sets:

  • This n1 means that the predicate was true for all items.

  • That n2 means that the predicate was false for all items.

  • These n1 n2 gives n1 (all of the items that were true for the predicate) and n2 (all of the items that were false for the predicate).

See also split.

partition (> 3) (fromList (5 :| [3])) == These (singleton 5) (singleton 3)
partition (< 7) (fromList (5 :| [3])) == This  (fromList (3 :| [5]))
partition (> 7) (fromList (5 :| [3])) == That  (fromList (3 :| [5]))

O(log n). The expression (split x set) is potentially a These containing up to two NEIntSets based on splitting the set into sets containing items before and after the value x. It will never return a set that contains x itself.

  • Nothing means that x was the only value in the the original set, and so there are no items before or after it.

  • Just (This n1) means x was larger than or equal to all items in the set, and n1 is the entire original set (minus x, if it was present)

  • Just (That n2) means x was smaller than or equal to all items in the set, and n2 is the entire original set (minus x, if it was present)

  • Just (These n1 n2) gives n1 (the set of all values from the original set less than x) and n2 (the set of all values from the original set greater than x).

split 2 (fromList (5 :| [3])) == Just (That  (fromList (3 :| [5]))      )
split 3 (fromList (5 :| [3])) == Just (That  (singleton 5)              )
split 4 (fromList (5 :| [3])) == Just (These (singleton 3) (singleton 5))
split 5 (fromList (5 :| [3])) == Just (This  (singleton 3)              )
split 6 (fromList (5 :| [3])) == Just (This  (fromList (3 :| [5]))      )
split 5 (singleton 5)         == Nothing

O(log n). The expression (splitMember x set) splits a set just like split but also returns member x set (whether or not x was in set)

splitMember 2 (fromList (5 :| [3])) == (False, Just (That  (fromList (3 :| [5)]))))
splitMember 3 (fromList (5 :| [3])) == (True , Just (That  (singleton 5)))
splitMember 4 (fromList (5 :| [3])) == (False, Just (These (singleton 3) (singleton 5)))
splitMember 5 (fromList (5 :| [3])) == (True , Just (This  (singleton 3))
splitMember 6 (fromList (5 :| [3])) == (False, Just (This  (fromList (3 :| [5])))
splitMember 5 (singleton 5)         == (True , Nothing)

O(1). Decompose a set into pieces based on the structure of the underlying tree. This function is useful for consuming a set in parallel.

No guarantee is made as to the sizes of the pieces; an internal, but deterministic process determines this. However, it is guaranteed that the pieces returned will be in ascending order (all elements in the first subset less than all elements in the second, and so on).

Note that the current implementation does not return more than four subsets, but you should not depend on this behaviour because it can change in the future without notice.

Map

1 declaration
valuemap :: (Key -> Key) -> NEIntSet -> NEIntSet
#

O(n*log n). map f s is the set obtained by applying f to each element of s.

It's worth noting that the size of the result may be smaller if, for some (x,y), x /= y && f x == f y

Folds

4 declarations
valuefoldr :: (Key -> b -> b) -> b -> NEIntSet -> b
#

O(n). Fold the elements in the set using the given right-associative binary operator, such that foldr f z == foldr f z . toAscList.

For example,

elemsList set = foldr (:) [] set
valuefoldl :: (a -> Key -> a) -> a -> NEIntSet -> a
#

O(n). Fold the elements in the set using the given left-associative binary operator, such that foldl f z == foldl f z . toAscList.

For example,

descElemsList set = foldl (flip (:)) [] set
valuefoldr1 :: (Key -> Key -> Key) -> NEIntSet -> Key
#

O(n). A version of foldr that uses the value at the maximal value in the set as the starting value.

Note that, unlike foldr1 for IntSet, this function is total if the input function is total.

valuefoldl1 :: (Key -> Key -> Key) -> NEIntSet -> Key
#

O(n). A version of foldl that uses the value at the minimal value in the set as the starting value.

Note that, unlike foldl1 for IntSet, this function is total if the input function is total.

Strict folds

valuefoldr' :: (Key -> b -> b) -> b -> NEIntSet -> b
#

O(n). A strict version of foldr. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

valuefoldl' :: (a -> Key -> a) -> a -> NEIntSet -> a
#

O(n). A strict version of foldl. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

valuefoldr1' :: (Key -> Key -> Key) -> NEIntSet -> Key
#

O(n). A strict version of foldr1. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

valuefoldl1' :: (Key -> Key -> Key) -> NEIntSet -> Key
#

O(n). A strict version of foldl1. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

Min/Max

6 declarations
valuefindMin :: NEIntSet -> Key
#

O(1). The minimal element of a set. Note that this is total, making lookupMin obsolete. It is constant-time, so has better asymptotics than Data.IntSet.lookupMin and Data.Map.findMin as well.

findMin (fromList (5 :| [3])) == 3
valuefindMax :: NEIntSet -> Key
#

O(log n). The maximal key of a set Note that this is total, making lookupMin obsolete.

findMax (fromList (5 :| [3])) == 5
valuedeleteMin :: NEIntSet -> IntSet
#

O(1). Delete the minimal element. Returns a potentially empty set (IntSet), because we might delete the final item in a singleton set. It is constant-time, so has better asymptotics than Data.IntSet.deleteMin.

deleteMin (fromList (5 :| [3, 7])) == Data.IntSet.fromList [5, 7]
deleteMin (singleton 5) == Data.IntSet.empty
valuedeleteMax :: NEIntSet -> IntSet
#

O(log n). Delete the maximal element. Returns a potentially empty set (IntSet), because we might delete the final item in a singleton set.

deleteMax (fromList (5 :| [3, 7])) == Data.IntSet.fromList [3, 5]
deleteMax (singleton 5) == Data.IntSet.empty
valuedeleteFindMin :: NEIntSet -> (Key, IntSet)
#

O(1). Delete and find the minimal element. It is constant-time, so has better asymptotics that Data.IntSet.minView for IntSet.

Note that unlike Data.IntSet.deleteFindMin for IntSet, this cannot ever fail, and so is a total function. However, the result IntSet is potentially empty, since the original set might have contained just a single item.

deleteFindMin (fromList (5 :| [3, 10])) == (3, Data.IntSet.fromList [5, 10])
valuedeleteFindMax :: NEIntSet -> (Key, IntSet)
#

O(log n). Delete and find the minimal element.

Note that unlike Data.IntSet.deleteFindMax for IntSet, this cannot ever fail, and so is a total function. However, the result IntSet is potentially empty, since the original set might have contained just a single item.

deleteFindMax (fromList (5 :| [3, 10])) == (10, Data.IntSet.fromList [3, 5])

Conversion

0 declarations

List

Debugging

1 declaration