module Text.Regex.TDFA.IntArrTrieSet where
import Data.Array.IArray(Array,(!),listArray)
data TrieSet v = TrieSet { forall v. TrieSet v -> v
value :: v
, forall v. TrieSet v -> Array Int (TrieSet v)
next :: Array Int (TrieSet v) }
lookupAsc :: TrieSet v -> [Int] -> v
lookupAsc :: forall v. TrieSet v -> [Int] -> v
lookupAsc (TrieSet {value :: forall v. TrieSet v -> v
value=v
v,next :: forall v. TrieSet v -> Array Int (TrieSet v)
next=Array Int (TrieSet v)
n}) =
(\[Int]
keys -> case [Int]
keys of [] -> v
v
(Int
key:[Int]
keys') -> TrieSet v -> [Int] -> v
forall v. TrieSet v -> [Int] -> v
lookupAsc (Array Int (TrieSet v)
nArray Int (TrieSet v) -> Int -> TrieSet v
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
!Int
key) [Int]
keys')
fromBounds :: (Int,Int)
-> ([Int] -> v)
-> TrieSet v
fromBounds :: forall v. (Int, Int) -> ([Int] -> v) -> TrieSet v
fromBounds (Int
start,Int
stop) [Int] -> v
keysToValue = ([Int] -> [Int]) -> Int -> TrieSet v
build [Int] -> [Int]
forall a. a -> a
id Int
start where
build :: ([Int] -> [Int]) -> Int -> TrieSet v
build [Int] -> [Int]
keys Int
low = TrieSet :: forall v. v -> Array Int (TrieSet v) -> TrieSet v
TrieSet { value :: v
value = [Int] -> v
keysToValue ([Int] -> [Int]
keys [])
, next :: Array Int (TrieSet v)
next = (Int, Int) -> [TrieSet v] -> Array Int (TrieSet v)
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [e] -> a i e
listArray (Int
low,Int
stop)
[([Int] -> [Int]) -> Int -> TrieSet v
build ([Int] -> [Int]
keys([Int] -> [Int]) -> ([Int] -> [Int]) -> [Int] -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Int
xInt -> [Int] -> [Int]
forall a. a -> [a] -> [a]
:)) (Int -> Int
forall a. Enum a => a -> a
succ Int
x) | Int
x <- [Int
low..Int
stop] ] }
fromSinglesMerge :: v
-> (v->v->v)
-> (Int,Int)
-> (Int->v)
-> TrieSet v
fromSinglesMerge :: forall v.
v -> (v -> v -> v) -> (Int, Int) -> (Int -> v) -> TrieSet v
fromSinglesMerge v
emptyValue v -> v -> v
mergeValues (Int, Int)
bound Int -> v
keyToValue = TrieSet v
trieSet where
trieSet :: TrieSet v
trieSet = (Int, Int) -> ([Int] -> v) -> TrieSet v
forall v. (Int, Int) -> ([Int] -> v) -> TrieSet v
fromBounds (Int, Int)
bound [Int] -> v
keysToValue'
keysToValue' :: [Int] -> v
keysToValue' [Int]
keys =
case [Int]
keys of
[] -> v
emptyValue
[Int
key] -> Int -> v
keyToValue Int
key
[Int]
_ -> v -> v -> v
mergeValues ([Int] -> v
keysToValue ([Int] -> [Int]
forall a. [a] -> [a]
init [Int]
keys)) ([Int] -> v
keysToValue [[Int] -> Int
forall a. [a] -> a
last [Int]
keys])
keysToValue :: [Int] -> v
keysToValue = TrieSet v -> [Int] -> v
forall v. TrieSet v -> [Int] -> v
lookupAsc TrieSet v
trieSet
fromSinglesSum :: ([v]->v)
-> (Int,Int)
-> (Int->v)
-> TrieSet v
fromSinglesSum :: forall v. ([v] -> v) -> (Int, Int) -> (Int -> v) -> TrieSet v
fromSinglesSum [v] -> v
mergeValues (Int, Int)
bound Int -> v
keyToValue = TrieSet v
trieSet where
trieSet :: TrieSet v
trieSet = (Int, Int) -> ([Int] -> v) -> TrieSet v
forall v. (Int, Int) -> ([Int] -> v) -> TrieSet v
fromBounds (Int, Int)
bound [Int] -> v
keysToValue'
keysToValue' :: [Int] -> v
keysToValue' = [v] -> v
mergeValues ([v] -> v) -> ([Int] -> [v]) -> [Int] -> v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> v) -> [Int] -> [v]
forall a b. (a -> b) -> [a] -> [b]
map Int -> v
keyToValue