Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MathJax format for complexity annotations #411

Merged
merged 2 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 57 additions & 57 deletions Data/HashMap/Internal.hs

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Data/HashMap/Internal/Array.hs
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,13 @@ trim :: MArray s a -> Int -> ST s (Array a)
trim mary n = cloneM mary 0 n >>= unsafeFreeze
{-# INLINE trim #-}

-- | /O(n)/ Insert an element at the given position in this array,
-- | \(O(n)\) Insert an element at the given position in this array,
-- increasing its size by one.
insert :: Array e -> Int -> e -> Array e
insert ary idx b = runST (insertM ary idx b)
{-# INLINE insert #-}

-- | /O(n)/ Insert an element at the given position in this array,
-- | \(O(n)\) Insert an element at the given position in this array,
-- increasing its size by one.
insertM :: Array e -> Int -> e -> ST s (Array e)
insertM ary idx b =
Expand All @@ -325,12 +325,12 @@ insertM ary idx b =
where !count = length ary
{-# INLINE insertM #-}

-- | /O(n)/ Update the element at the given position in this array.
-- | \(O(n)\) Update the element at the given position in this array.
update :: Array e -> Int -> e -> Array e
update ary idx b = runST (updateM ary idx b)
{-# INLINE update #-}

-- | /O(n)/ Update the element at the given position in this array.
-- | \(O(n)\) Update the element at the given position in this array.
updateM :: Array e -> Int -> e -> ST s (Array e)
updateM ary idx b =
CHECK_BOUNDS("updateM", count, idx)
Expand All @@ -340,7 +340,7 @@ updateM ary idx b =
where !count = length ary
{-# INLINE updateM #-}

-- | /O(n)/ Update the element at the given positio in this array, by
-- | \(O(n)\) Update the element at the given positio in this array, by
-- applying a function to it. Evaluates the element to WHNF before
-- inserting it into the array.
updateWith' :: Array e -> Int -> (e -> e) -> Array e
Expand All @@ -349,7 +349,7 @@ updateWith' ary idx f
= update ary idx $! f x
{-# INLINE updateWith' #-}

-- | /O(1)/ Update the element at the given position in this array,
-- | \(O(1)\) Update the element at the given position in this array,
-- without copying.
unsafeUpdateM :: Array e -> Int -> e -> ST s ()
unsafeUpdateM ary idx b =
Expand Down Expand Up @@ -428,13 +428,13 @@ thaw !ary !_o@(I# o#) _n@(I# n#) =
(# s2, mary# #) -> (# s2, MArray mary# #)
{-# INLINE thaw #-}

-- | /O(n)/ Delete an element at the given position in this array,
-- | \(O(n)\) Delete an element at the given position in this array,
-- decreasing its size by one.
delete :: Array e -> Int -> Array e
delete ary idx = runST (deleteM ary idx)
{-# INLINE delete #-}

-- | /O(n)/ Delete an element at the given position in this array,
-- | \(O(n)\) Delete an element at the given position in this array,
-- decreasing its size by one.
deleteM :: Array e -> Int -> ST s (Array e)
deleteM ary idx = do
Expand Down
42 changes: 21 additions & 21 deletions Data/HashMap/Internal/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
-- especially when key comparison is expensive, as in the case of
-- strings.
--
-- Many operations have a average-case complexity of /O(log n)/. The
-- Many operations have a average-case complexity of \(O(\log n)\). The
-- implementation uses a large base (i.e. 32) so in practice these
-- operations are constant time.
module Data.HashMap.Internal.Strict
Expand Down Expand Up @@ -164,21 +164,21 @@ values are exempted.
------------------------------------------------------------------------
-- * Construction

-- | /O(1)/ Construct a map with a single element.
-- | \(O(1)\) Construct a map with a single element.
singleton :: (Hashable k) => k -> v -> HashMap k v
singleton k !v = HM.singleton k v

------------------------------------------------------------------------
-- * Basic interface

-- | /O(log n)/ Associate the specified value with the specified
-- | \(O(\log n)\) Associate the specified value with the specified
-- key in this map. If this map previously contained a mapping for
-- the key, the old value is replaced.
insert :: (Eq k, Hashable k) => k -> v -> HashMap k v -> HashMap k v
insert k !v = HM.insert k v
{-# INLINABLE insert #-}

-- | /O(log n)/ Associate the value with the key in this map. If
-- | \(O(\log n)\) Associate the value with the key in this map. If
-- this map previously contained a mapping for the key, the old value
-- is replaced by the result of applying the given function to the new
-- and old value. Example:
Expand Down Expand Up @@ -259,7 +259,7 @@ unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
{-# INLINABLE unsafeInsertWithKey #-}

-- | /O(log n)/ Adjust the value tied to a given key in this map only
-- | \(O(\log n)\) Adjust the value tied to a given key in this map only
-- if it is present. Otherwise, leave the map alone.
adjust :: (Eq k, Hashable k) => (v -> v) -> k -> HashMap k v -> HashMap k v
adjust f k0 m0 = go h0 k0 0 m0
Expand Down Expand Up @@ -288,14 +288,14 @@ adjust f k0 m0 = go h0 k0 0 m0
| otherwise = t
{-# INLINABLE adjust #-}

-- | /O(log n)/ The expression @('update' f k map)@ updates the value @x@ at @k@
-- | \(O(\log n)\) The expression @('update' f k map)@ updates the value @x@ at @k@
-- (if it is in the map). If @(f x)@ is 'Nothing', the element is deleted.
-- If it is @('Just' y)@, the key @k@ is bound to the new value @y@.
update :: (Eq k, Hashable k) => (a -> Maybe a) -> k -> HashMap k a -> HashMap k a
update f = alter (>>= f)
{-# INLINABLE update #-}

-- | /O(log n)/ The expression @('alter' f k map)@ alters the value @x@ at @k@, or
-- | \(O(\log n)\) The expression @('alter' f k map)@ alters the value @x@ at @k@, or
-- absence thereof.
--
-- 'alter' can be used to insert, delete, or update a value in a map. In short:
Expand All @@ -310,7 +310,7 @@ alter f k m =
Just v -> insert k v m
{-# INLINABLE alter #-}

-- | /O(log n)/ The expression (@'alterF' f k map@) alters the value @x@ at
-- | \(O(\log n)\) The expression (@'alterF' f k map@) alters the value @x@ at
-- @k@, or absence thereof.
--
-- 'alterF' can be used to insert, delete, or update a value in a map.
Expand Down Expand Up @@ -436,14 +436,14 @@ alterFEager f !k !m = (<$> f mv) $ \fres ->
------------------------------------------------------------------------
-- * Combine

-- | /O(n+m)/ The union of two maps. If a key occurs in both maps,
-- | \(O(n+m)\) The union of two maps. If a key occurs in both maps,
-- the provided function (first argument) will be used to compute the result.
unionWith :: (Eq k, Hashable k) => (v -> v -> v) -> HashMap k v -> HashMap k v
-> HashMap k v
unionWith f = unionWithKey (const f)
{-# INLINE unionWith #-}

-- | /O(n+m)/ The union of two maps. If a key occurs in both maps,
-- | \(O(n+m)\) The union of two maps. If a key occurs in both maps,
-- the provided function (first argument) will be used to compute the result.
unionWithKey :: (Eq k, Hashable k) => (k -> v -> v -> v) -> HashMap k v -> HashMap k v
-> HashMap k v
Expand Down Expand Up @@ -532,7 +532,7 @@ unionWithKey f = go 0
------------------------------------------------------------------------
-- * Transformations

-- | /O(n)/ Transform this map by applying a function to every value.
-- | \(O(n)\) Transform this map by applying a function to every value.
mapWithKey :: (k -> v1 -> v2) -> HashMap k v1 -> HashMap k v2
mapWithKey f = go
where
Expand All @@ -544,7 +544,7 @@ mapWithKey f = go
Collision h $ A.map' (\ (L k v) -> let !v' = f k v in L k v') ary
{-# INLINE mapWithKey #-}

-- | /O(n)/ Transform this map by applying a function to every value.
-- | \(O(n)\) Transform this map by applying a function to every value.
map :: (v1 -> v2) -> HashMap k v1 -> HashMap k v2
map f = mapWithKey (const f)
{-# INLINE map #-}
Expand All @@ -553,7 +553,7 @@ map f = mapWithKey (const f)
------------------------------------------------------------------------
-- * Filter

-- | /O(n)/ Transform this map by applying a function to every value
-- | \(O(n)\) Transform this map by applying a function to every value
-- and retaining only some of them.
mapMaybeWithKey :: (k -> v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2
mapMaybeWithKey f = HM.filterMapAux onLeaf onColl
Expand All @@ -564,13 +564,13 @@ mapMaybeWithKey f = HM.filterMapAux onLeaf onColl
| otherwise = Nothing
{-# INLINE mapMaybeWithKey #-}

-- | /O(n)/ Transform this map by applying a function to every value
-- | \(O(n)\) Transform this map by applying a function to every value
-- and retaining only some of them.
mapMaybe :: (v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2
mapMaybe f = mapMaybeWithKey (const f)
{-# INLINE mapMaybe #-}

-- | /O(n)/ Perform an 'Applicative' action for each key-value pair
-- | \(O(n)\) Perform an 'Applicative' action for each key-value pair
-- in a 'HashMap' and produce a 'HashMap' of all the results. Each 'HashMap'
-- will be strict in all its values.
--
Expand Down Expand Up @@ -599,7 +599,7 @@ traverseWithKey f = go
------------------------------------------------------------------------
-- * Difference and intersection

-- | /O(n*log m)/ Difference with a combining function. When two equal keys are
-- | \(O(n \log m)\) Difference with a combining function. When two equal keys are
-- encountered, the combining function is applied to the values of these keys.
-- If it returns 'Nothing', the element is discarded (proper set difference). If
-- it returns (@'Just' y@), the element is updated with a new value @y@.
Expand All @@ -611,7 +611,7 @@ differenceWith f a b = HM.foldlWithKey' go HM.empty a
Just w -> maybe m (\ !y -> HM.unsafeInsert k y m) (f v w)
{-# INLINABLE differenceWith #-}

-- | /O(n+m)/ Intersection of two maps. If a key occurs in both maps
-- | \(O(n+m)\) Intersection of two maps. If a key occurs in both maps
-- the provided function is used to combine the values from the two
-- maps.
intersectionWith :: (Eq k, Hashable k) => (v1 -> v2 -> v3) -> HashMap k v1
Expand All @@ -623,7 +623,7 @@ intersectionWith f a b = HM.foldlWithKey' go HM.empty a
_ -> m
{-# INLINABLE intersectionWith #-}

-- | /O(n+m)/ Intersection of two maps. If a key occurs in both maps
-- | \(O(n+m)\) Intersection of two maps. If a key occurs in both maps
-- the provided function is used to combine the values from the two
-- maps.
intersectionWithKey :: (Eq k, Hashable k) => (k -> v1 -> v2 -> v3)
Expand All @@ -638,14 +638,14 @@ intersectionWithKey f a b = HM.foldlWithKey' go HM.empty a
------------------------------------------------------------------------
-- ** Lists

-- | /O(n*log n)/ Construct a map with the supplied mappings. If the
-- | \(O(n \log n)\) Construct a map with the supplied mappings. If the
-- list contains duplicate mappings, the later mappings take
-- precedence.
fromList :: (Eq k, Hashable k) => [(k, v)] -> HashMap k v
fromList = List.foldl' (\ m (k, !v) -> HM.unsafeInsert k v m) HM.empty
{-# INLINABLE fromList #-}

-- | /O(n*log n)/ Construct a map from a list of elements. Uses
-- | \(O(n \log n)\) Construct a map from a list of elements. Uses
-- the provided function @f@ to merge duplicate entries with
-- @(f newVal oldVal)@.
--
Expand Down Expand Up @@ -679,7 +679,7 @@ fromListWith :: (Eq k, Hashable k) => (v -> v -> v) -> [(k, v)] -> HashMap k v
fromListWith f = List.foldl' (\ m (k, v) -> unsafeInsertWith f k v m) HM.empty
{-# INLINE fromListWith #-}

-- | /O(n*log n)/ Construct a map from a list of elements. Uses
-- | \(O(n \log n)\) Construct a map from a list of elements. Uses
-- the provided function to merge duplicate entries.
--
-- === Examples
Expand Down
2 changes: 1 addition & 1 deletion Data/HashMap/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-- especially when key comparison is expensive, as in the case of
-- strings.
--
-- Many operations have a average-case complexity of /O(log n)/. The
-- Many operations have a average-case complexity of \(O(\log n)\). The
-- implementation uses a large base (i.e. 32) so in practice these
-- operations are constant time.
module Data.HashMap.Lazy
Expand Down
2 changes: 1 addition & 1 deletion Data/HashMap/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-- especially when key comparison is expensive, as in the case of
-- strings.
--
-- Many operations have a average-case complexity of /O(log n)/. The
-- Many operations have a average-case complexity of \(O(\log n)\). The
-- implementation uses a large base (i.e. 16) so in practice these
-- operations are constant time.
module Data.HashMap.Strict
Expand Down
2 changes: 1 addition & 1 deletion Data/HashSet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ The implementation is based on /hash array mapped tries/. A
especially when value comparisons are expensive, as in the case of
strings.

Many operations have a average-case complexity of /O(log n)/. The
Many operations have a average-case complexity of \(O(\log n)\). The
implementation uses a large base (i.e. 16) so in practice these
operations are constant time.
-}
Expand Down
Loading