Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- class Ord k ⇒ HasOKey k v | v → k where
- data OMap k v where
- null ∷ OMap k v → Bool
- size ∷ OMap k v → Int
- empty ∷ OMap k v
- singleton ∷ HasOKey k v ⇒ v → OMap k v
- lookup ∷ Ord k ⇒ k → OMap k v → Maybe v
- member ∷ Ord k ⇒ k → OMap k v → Bool
- (!?) ∷ Ord k ⇒ OMap k v → k → Maybe v
- mapUnsafe ∷ (v1 → v2) → OMap k v1 → OMap k v2
- fromSet ∷ HasOKey k v ⇒ Set v → OMap k v
- fromFoldable ∷ (Foldable f, HasOKey k v) ⇒ f v → OMap k v
- fromFoldableDuplicates ∷ (Foldable f, HasOKey k v, Ord v) ⇒ f v → (Set v, OMap k v)
- toMap ∷ OMap k v → Map k v
- assocList ∷ Ord k ⇒ OMap k v → [(k, v)]
- elems ∷ Ord k ⇒ OMap k v → [v]
- toStrictSeq ∷ Ord k ⇒ OMap k v → StrictSeq v
- toStrictSeqOKeys ∷ OMap k v → StrictSeq k
- toStrictSeqOfPairs ∷ Ord k ⇒ OMap k v → StrictSeq (k, v)
- invariantHolds ∷ OMap k v → Bool
- invariantHolds' ∷ Ord k ⇒ OMap k v → Bool
- (|>) ∷ HasOKey k v ⇒ OMap k v → v → OMap k v
- (<|) ∷ HasOKey k v ⇒ v → OMap k v → OMap k v
- (<||) ∷ HasOKey k v ⇒ v → OMap k v → OMap k v
- (||>) ∷ HasOKey k v ⇒ OMap k v → v → OMap k v
- (|><) ∷ HasOKey k v ⇒ OMap k v → OMap k v → OMap k v
- (><|) ∷ HasOKey k v ⇒ OMap k v → OMap k v → OMap k v
- elem ∷ (HasOKey k v, Eq v) ⇒ v → OMap k v → Bool
- extractKeys ∷ Ord k ⇒ Set k → OMap k v → (OMap k v, Map k v)
- adjust ∷ HasOKey k v ⇒ (v → v) → k → OMap k v → OMap k v
- filter ∷ Ord k ⇒ (v → Bool) → OMap k v → OMap k v
Documentation
class Ord k ⇒ HasOKey k v | v → k where Source #
Class of types that can be mapped by a lens or a projection to an Ord type.
For a type V
, defines a lens from V
to and Ord type K
.
A general-purpose finite, insert-ordered, map that is strict in its keys and values.
The strictness is enforced by the underlying strict Map
that can
be looked-up by a projection or lens. and the ordering is maintained
by the constructing functions, leveraging StrictSeq
to hold the
insert-order of the keys.
TODO: DecShareCBOR instance
pattern Empty ∷ OMap k v | \(O(1)\) |
pattern (:<|:) ∷ (HasOKey k v, Ord k) ⇒ v → OMap k v → OMap k v infixr 5 | \(O(\log n)\). |
pattern (:|>:) ∷ (HasOKey k v, Ord k) ⇒ OMap k v → v → OMap k v infixl 5 | \(O(\log n)\). |
Instances
mapUnsafe ∷ (v1 → v2) → OMap k v1 → OMap k v2 Source #
This mapping function is only safe when the key stored in the new value matches the key stored in the new value. This invariant is not checked for performance reasons
fromFoldable ∷ (Foldable f, HasOKey k v) ⇒ f v → OMap k v Source #
\(O(n \log n)\). Checks membership before snoc'ing.
De-duplicates the StrictSeq without overwriting.
Starts from the left or head, using foldl
`
fromFoldableDuplicates ∷ (Foldable f, HasOKey k v, Ord v) ⇒ f v → (Set v, OMap k v) Source #
\(O(n \log n)\). Checks membership before snoc'ing.
De-duplicates the StrictSeq and collects and returns the duplicates found.
Starts from the left or head, using foldl
`
toStrictSeqOKeys ∷ OMap k v → StrictSeq k Source #
\(O(1)\).
invariantHolds' ∷ Ord k ⇒ OMap k v → Bool Source #
\(O(n \log n)\). Deep, costly invariant using membership check for each value. By the pigeon-hole principle, this check is exhaustive.
(|>) ∷ HasOKey k v ⇒ OMap k v → v → OMap k v infixl 5 Source #
\(O(\log n)\). Checks membership before snoc'ing.
(<|) ∷ HasOKey k v ⇒ v → OMap k v → OMap k v infixr 5 Source #
\(O(\log n)\). Checks membership before cons'ing.
(<||) ∷ HasOKey k v ⇒ v → OMap k v → OMap k v infixr 5 Source #
\(O(\log n)\). Checks membership before cons'ing. Overwrites a duplicate.
(||>) ∷ HasOKey k v ⇒ OMap k v → v → OMap k v infixl 5 Source #
\(O(\log n)\). Checks membership before snoc'ing. Overwrites a duplicate.
(|><) ∷ HasOKey k v ⇒ OMap k v → OMap k v → OMap k v infixl 5 Source #
\( O(n \log m) \). For every uncons-ed element from the sequence on the right, check its membership in the sequence on the left, before snoc'ing it. Preserve order. Remove duplicates from sequence on the right.
(><|) ∷ HasOKey k v ⇒ OMap k v → OMap k v → OMap k v infixr 5 Source #
\( O(m \log n) \). For every unsnoc-ed element from the sequence on the left, check its membership in the sequence on the right, before cons'ing it. Preserve order. Remove duplicates from sequence on the left.
adjust ∷ HasOKey k v ⇒ (v → v) → k → OMap k v → OMap k v Source #
\(O(n)\). Like adjust
.
Returns the original OMap
unaltered when the key does not exist.
If the key exists, then the function is applied to the value, but we need to consider three possible cases:
- The modified value's
okeyL
is unaltered - we return omap with the adjusted value, - The modified value's
okeyL
is altered, but not a duplicate - we return the omap with adjusted key (in place) and value - The modified value's
okeyL
is altered and is a duplicate - we return the omap with the old key deleted from the sequence but without inserting the new key since it is a duplicate, and deleting old value and inserting the new value in place of its duplicate.
Examples:
>>>
import Data.OMap.Strict
>>>
import Lens.Micro
>>>
instance HasOKey Int (Int, Char) where okeyL = _1
>>>
let m = fromFoldable $ zip [1,2] ['a','b'] :: OMap Int (Int, Char)
>>>
m
StrictSeq {fromStrict = fromList [(1,(1,'a')),(2,(2,'b'))]}>>>
let adjustingFn (k, v) = (k, succ v) -- Changes the value
>>>
let overwritingAdjustingFn (k,v) = (succ k, v) -- Changes the `okeyL`.
>>>
adjust adjustingFn 1 m
StrictSeq {fromStrict = fromList [(1,(1,'b')),(2,(2,'b'))]}>>>
adjust overwritingAdjustingFn 1 m
StrictSeq {fromStrict = fromList [(2,(2,'a'))]}