Data.SortedList

sorted-list-0.2.2.0: Type-enforced sorted lists and related functions.

Safe Haskell | None
Language | Haskell2010

Data.SortedList

Contents

- Type
- List conversions
- Construction
- Deconstruction
- Inserting
- Deleting
- Sublists
  - Filtering
- Queries
- `map` function
- Unfolding
- Others
- Set operations

Description

This module defines a type for sorted lists, together with several functions to create and use values of that type. Many operations are optimized to take advantage of the list being sorted.

Synopsis

- data SortedList a
- toSortedList :: Ord a => [a] -> SortedList a
- fromSortedList :: SortedList a -> [a]
- singleton :: a -> SortedList a
- repeat :: a -> SortedList a
- replicate :: Int -> a -> SortedList a
- iterate :: Ord a => (a -> a) -> a -> SortedList a
- uncons :: SortedList a -> Maybe (a, SortedList a)
- insert :: Ord a => a -> SortedList a -> SortedList a
- delete :: Ord a => a -> SortedList a -> SortedList a
- deleteAll :: Ord a => a -> SortedList a -> SortedList a
- take :: Int -> SortedList a -> SortedList a
- drop :: Int -> SortedList a -> SortedList a
- splitAt :: Int -> SortedList a -> (SortedList a, SortedList a)
- takeWhile :: (a -> Bool) -> SortedList a -> SortedList a
- dropWhile :: (a -> Bool) -> SortedList a -> SortedList a
- span :: (a -> Bool) -> SortedList a -> (SortedList a, SortedList a)
- partition :: (a -> Bool) -> SortedList a -> (SortedList a, SortedList a)
- filter :: (a -> Bool) -> SortedList a -> SortedList a
- filterLT :: Ord a => a -> SortedList a -> SortedList a
- filterGT :: Ord a => a -> SortedList a -> SortedList a
- filterLE :: Ord a => a -> SortedList a -> SortedList a
- filterGE :: Ord a => a -> SortedList a -> SortedList a
- elemOrd :: Ord a => a -> SortedList a -> Bool
- findIndices :: (a -> Bool) -> SortedList a -> SortedList Int
- map :: Ord b => (a -> b) -> SortedList a -> SortedList b
- mapDec :: Ord b => (a -> b) -> SortedList a -> SortedList b
- unfoldr :: Ord a => (b -> Maybe (a, b)) -> b -> SortedList a
- reverse :: SortedList a -> SortedList (Down a)
- reverseDown :: SortedList (Down a) -> SortedList a
- nub :: Eq a => SortedList a -> SortedList a
- intersect :: Ord a => SortedList a -> SortedList a -> SortedList a
- union :: Ord a => SortedList a -> SortedList a -> SortedList a

# Type

data SortedList a #

Type of sorted lists. Any (non-bottom) value of this type is a sorted list. Use the `Monoid` instance to merge sorted lists.

#### Instances

Instances details

Foldable SortedList # |
Instance details Defined in Data.SortedList Methods fold :: Monoid m => SortedList m -> m # foldMap :: Monoid m => (a -> m) -> SortedList a -> m # foldMap' :: Monoid m => (a -> m) -> SortedList a -> m # foldr :: (a -> b -> b) -> b -> SortedList a -> b # foldr' :: (a -> b -> b) -> b -> SortedList a -> b # foldl :: (b -> a -> b) -> b -> SortedList a -> b # foldl' :: (b -> a -> b) -> b -> SortedList a -> b # foldr1 :: (a -> a -> a) -> SortedList a -> a # foldl1 :: (a -> a -> a) -> SortedList a -> a # toList :: SortedList a -> [a] # null :: SortedList a -> Bool # length :: SortedList a -> Int # elem :: Eq a => a -> SortedList a -> Bool # maximum :: Ord a => SortedList a -> a # minimum :: Ord a => SortedList a -> a # sum :: Num a => SortedList a -> a # product :: Num a => SortedList a -> a #
NFData a => NFData (SortedList a) # |
Instance details Defined in Data.SortedList Methods rnf :: SortedList a -> () #
Ord a => Monoid (SortedList a) # |
Instance details Defined in Data.SortedList Methods mempty :: SortedList a # mappend :: SortedList a -> SortedList a -> SortedList a # mconcat :: [SortedList a] -> SortedList a #
Ord a => Semigroup (SortedList a) # |
Instance details Defined in Data.SortedList Methods (<>) :: SortedList a -> SortedList a -> SortedList a # sconcat :: NonEmpty (SortedList a) -> SortedList a # stimes :: Integral b => b -> SortedList a -> SortedList a #
Ord a => IsList (SortedList a) # |
Instance details Defined in Data.SortedList Associated Types
type Item (SortedList a) |
Instance details Defined in Data.SortedList type Item (SortedList a) = a
Methods fromList :: [Item (SortedList a)] -> SortedList a # fromListN :: Int -> [Item (SortedList a)] -> SortedList a # toList :: SortedList a -> [Item (SortedList a)] #
Show a => Show (SortedList a) # |
Instance details Defined in Data.SortedList Methods showsPrec :: Int -> SortedList a -> ShowS # show :: SortedList a -> String # showList :: [SortedList a] -> ShowS #
Eq a => Eq (SortedList a) # |
Instance details Defined in Data.SortedList Methods (==) :: SortedList a -> SortedList a -> Bool # (/=) :: SortedList a -> SortedList a -> Bool #
Ord a => Ord (SortedList a) # |
Instance details Defined in Data.SortedList Methods compare :: SortedList a -> SortedList a -> Ordering # (<) :: SortedList a -> SortedList a -> Bool # (<=) :: SortedList a -> SortedList a -> Bool # (>) :: SortedList a -> SortedList a -> Bool # (>=) :: SortedList a -> SortedList a -> Bool # max :: SortedList a -> SortedList a -> SortedList a # min :: SortedList a -> SortedList a -> SortedList a #
type Item (SortedList a) # |
Instance details Defined in Data.SortedList type Item (SortedList a) = a

# List conversions

toSortedList :: Ord a => [a] -> SortedList a #

Create a `SortedList` by sorting a regular list.

fromSortedList :: SortedList a -> [a] #

O(1). Create a list from a `SortedList`. The returned list is guaranteed to be sorted.

# Construction

singleton :: a -> SortedList a #

O(1). Create a sorted list with only one element.

repeat :: a -> SortedList a #

An infinite list with all its elements equal to the given argument.

replicate :: Int -> a -> SortedList a #

Replicate a given number of times a single element.

iterate :: Ord a => (a -> a) -> a -> SortedList a #

Create a sorted list by repeatedly applying the same function to an element, until the image by that function is stricly less than its argument. In other words:

    iterate f x = [x, f x, f (f x), ... ]

With the list ending whenever `f (f (... (f (f x)) ...)) < f (... (f (f x)) ...)`. If this never happens, the list will be infinite.

By definition:

    iterate f = unfoldr (\x -> Just (x, f x))

# Deconstruction

uncons :: SortedList a -> Maybe (a, SortedList a) #

O(1). Decompose a sorted list into its minimal element and the rest. If the list is empty, it returns `Nothing`.

# Inserting

insert :: Ord a => a -> SortedList a -> SortedList a #

O(n). Insert a new element in a sorted list.

# Deleting

delete :: Ord a => a -> SortedList a -> SortedList a #

Delete the first occurrence of the given element.

deleteAll :: Ord a => a -> SortedList a -> SortedList a #

Delete all occurrences of the given element.

# Sublists

take :: Int -> SortedList a -> SortedList a #

Extract the prefix with the given length from a sorted list.

drop :: Int -> SortedList a -> SortedList a #

Drop the given number of elements from a sorted list, starting from the smallest and following ascending order.

splitAt :: Int -> SortedList a -> (SortedList a, SortedList a) #

Split a sorted list in two sublists, with the first one having length equal to the given argument, except when the length of the list is less than that.

takeWhile :: (a -> Bool) -> SortedList a -> SortedList a #

Return the longest prefix of a sorted list of elements that satisfy the given condition.

dropWhile :: (a -> Bool) -> SortedList a -> SortedList a #

Return the suffix remaining after dropping the longest prefix of elements that satisfy the given condition.

span :: (a -> Bool) -> SortedList a -> (SortedList a, SortedList a) #

Return the longest prefix of a sorted list of elements that satisfy the given condition, and the rest of the list.

## Filtering

partition :: (a -> Bool) -> SortedList a -> (SortedList a, SortedList a) #

O(n). Divide a sorted list into two lists, one with all the elements that satisfy the given predicate, and another list with the rest of elements.

filter :: (a -> Bool) -> SortedList a -> SortedList a #

O(n). Extract the elements of a list that satisfy the predicate.

filterLT :: Ord a => a -> SortedList a -> SortedList a #

O(n). Select only elements that are strictly less than the argument.

filterGT :: Ord a => a -> SortedList a -> SortedList a #

O(n). Select only elements that are strictly greater than the argument.

filterLE :: Ord a => a -> SortedList a -> SortedList a #

O(n). Select only elements less or equal to the argument.

filterGE :: Ord a => a -> SortedList a -> SortedList a #

O(n). Select only elements greater or equal to the argument.

# Queries

elemOrd :: Ord a => a -> SortedList a -> Bool #

O(n). An efficient implementation of `elem`, using the `Ord` instance of the elements in a sorted list. It only traverses the whole list if the requested element is greater than all the elements in the sorted list.

findIndices :: (a -> Bool) -> SortedList a -> SortedList Int #

O(n). Return the indices of all elements in a sorted list that satisfy the given condition.

# `map` function

map :: Ord b => (a -> b) -> SortedList a -> SortedList b #

Map a function over all the elements of a sorted list. Note that `map` will hang if the argument is an infinite list.

Even though `SortedList` can't be made an instance of `Functor`, `map` does hold the `Functor` laws (for finite lists). We can't however write an instance because of the `Ord` instance requirement on the type of the elements of the result list. Therefore, while `SortedList` is not a functor type in general, it is when restricted to elements of orderable types (for finite lists).

The complexity range goes from O(n) (if the function is monotonically increasing) to O(n²) (if the function is monotonically decreasing). These are the best and worst case scenarios. We provide an alternative (`mapDec`) where monotonically decreasing functions are the best case scenario.

mapDec :: Ord b => (a -> b) -> SortedList a -> SortedList b #

Just like `map`, but favoring functions that are monotonically decreasing instead of those that are monotonically increasing.

# Unfolding

unfoldr :: Ord a => (b -> Maybe (a, b)) -> b -> SortedList a #

Dual (sort of) to `foldr` for sorted lists. It builds a sorted list from a generator function and an initial element. The generator function is applied to the initial element, and then it will produce either `Nothing` - meaning that the list building must stop - or `Just` applied to the value that is going to be added to the list, and a new accumulator to be fed to the generator function. The list building will stop prematurely if the generator function happens to create an element for the list that is strictly smaller than the previous value.

# Others

reverse :: SortedList a -> SortedList (Down a) #

O(n). Reverse a sorted list. The result uses `Down`, thus it is a sorted list as well. The following equality holds for any sorted list `xs`:

    map Down xs = reverse xs

Only available from `base` version 4.6.0.0.

reverseDown :: SortedList (Down a) -> SortedList a #

O(n). Reverse a sorted list with elements embedded in the `Down` type.

Only available from `base` version 4.6.0.0.

# Set operations

nub :: Eq a => SortedList a -> SortedList a #

O(n). Remove duplicate elements from a sorted list.

intersect :: Ord a => SortedList a -> SortedList a -> SortedList a #

O(n+m). Intersection of sorted lists. If the first list contains duplicates, so will the result.

union :: Ord a => SortedList a -> SortedList a -> SortedList a #

Union of sorted lists. Duplicates, and elements of the first list, are removed from the the second list, but if the first list contains duplicates, so will the result.