{-# LANGUAGE TupleSections #-}
module Reactive.Banana.Prim.Low.OrderedBag where
import qualified Data.HashMap.Strict as Map
import Data.Hashable
import Data.List ( foldl', sortBy )
import Data.Maybe
import Data.Ord
type Position = Integer
data OrderedBag a = OB !(Map.HashMap a Position) !Position
empty :: OrderedBag a
empty :: forall a. OrderedBag a
empty = forall a. HashMap a Position -> Position -> OrderedBag a
OB forall k v. HashMap k v
Map.empty Position
0
insert :: (Eq a, Hashable a) => OrderedBag a -> a -> OrderedBag a
insert :: forall a. (Eq a, Hashable a) => OrderedBag a -> a -> OrderedBag a
insert (OB HashMap a Position
xs Position
n) a
x = forall a. HashMap a Position -> Position -> OrderedBag a
OB (forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v
Map.insertWith (\Position
_new Position
old -> Position
old) a
x Position
n HashMap a Position
xs) (Position
nforall a. Num a => a -> a -> a
+Position
1)
inserts :: (Eq a, Hashable a) => OrderedBag a -> [a] -> OrderedBag a
inserts :: forall a. (Eq a, Hashable a) => OrderedBag a -> [a] -> OrderedBag a
inserts = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' forall a. (Eq a, Hashable a) => OrderedBag a -> a -> OrderedBag a
insert
inOrder :: (Eq a, Hashable a) => [(a,b)] -> OrderedBag a -> [(a,b)]
inOrder :: forall a b.
(Eq a, Hashable a) =>
[(a, b)] -> OrderedBag a -> [(a, b)]
inOrder [(a, b)]
xs (OB HashMap a Position
bag Position
_) = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing forall a b. (a, b) -> a
fst) forall a b. (a -> b) -> a -> b
$
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (\(a, b)
x -> (,(a, b)
x) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
Map.lookup (forall a b. (a, b) -> a
fst (a, b)
x) HashMap a Position
bag) [(a, b)]
xs