Only perform the comparisons once
This commit is contained in:
parent
01f4423465
commit
d45ab327b0
|
@ -273,10 +273,10 @@ getPrefixSum = f 0
|
||||||
where
|
where
|
||||||
f sum _ PSLeaf = sum
|
f sum _ PSLeaf = sum
|
||||||
f sum target (PSBranch pivot left right cumulative) =
|
f sum target (PSBranch pivot left right cumulative) =
|
||||||
case () of
|
case target `compare` pivot of
|
||||||
_ | target < pivot -> f sum target left
|
LT -> f sum target left
|
||||||
_ | target > pivot -> f (sum+cumulative) target right
|
GT -> f (sum+cumulative) target right
|
||||||
_ -> sum+cumulative
|
EQ -> sum+cumulative
|
||||||
|
|
||||||
-- Add a value to the Prefix Sum tree at the given index.
|
-- Add a value to the Prefix Sum tree at the given index.
|
||||||
-- Values accumulate: addPSValue 42 2 . addPSValue 42 3 == addPSValue 42 5
|
-- Values accumulate: addPSValue 42 2 . addPSValue 42 3 == addPSValue 42 5
|
||||||
|
@ -285,10 +285,10 @@ addPSValue key value tree = if value == 0 then tree else f tree
|
||||||
where
|
where
|
||||||
f PSLeaf = PSBranch key PSLeaf PSLeaf value
|
f PSLeaf = PSBranch key PSLeaf PSLeaf value
|
||||||
f (PSBranch pivot left right sum) =
|
f (PSBranch pivot left right sum) =
|
||||||
case () of
|
case key `compare` pivot of
|
||||||
_ | key < pivot -> PSBranch pivot (f left) right (sum + value)
|
LT -> PSBranch pivot (f left) right (sum + value)
|
||||||
_ | key > pivot -> PSBranch pivot left (f right) sum
|
GT -> PSBranch pivot left (f right) sum
|
||||||
_ -> PSBranch pivot left right (sum + value)
|
EQ -> PSBranch pivot left right (sum + value)
|
||||||
|
|
||||||
prop_pstreeSumsCorrectly kvs targets =
|
prop_pstreeSumsCorrectly kvs targets =
|
||||||
let
|
let
|
||||||
|
|
Loading…
Reference in New Issue