Skip to content

Commit d8c9008

Browse files
committed
Revert the fromFunction shallowing
I don't actually know whether we want it shallower or "safer". Make `fromFunction` easier to read.
1 parent 58f3597 commit d8c9008

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

Data/Sequence.hs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,25 +1374,29 @@ fromFunction len f | len < 0 = error "Data.Sequence.fromFunction called with neg
13741374
#else
13751375
| otherwise = Seq $ create (Elem . f) 1 0 len
13761376
#endif
1377-
where
1378-
create :: (Int -> a) -> Int -> Int -> Int -> FingerTree a
1379-
create b{-tree_builder-} s{-tree_size-} i{-start_index-} trees = i `seq` s `seq` case trees of
1380-
1 -> Single $ b i
1381-
2 -> Deep (2*s) (One (b i)) Empty (One (b (i+s)))
1382-
3 -> Deep (3*s) (Two (b i) (b (i+s))) Empty (One (b (i+2*s)))
1383-
4 -> Deep (4*s) (Two (b i) (b (i+s))) Empty (Two (b (i+2*s)) (b (i+3*s)))
1384-
5 -> Deep (5*s) (Three (b i) (b (i+s)) (b (i+2*s))) Empty (Two (b (i+3*s)) (b (i+4*s)))
1385-
6 -> Deep (6*s) (Three (b i) (b (i+s)) (b (i+2*s))) Empty (Three (b (i+3*s)) (b (i+4*s)) (b (i+5*s)))
1386-
_ -> case trees `quotRem` 3 of
1387-
(trees',1) -> Deep (trees*s) (Two (b i) (b (i+s)))
1388-
(create (\j -> Node3 (3*s) (b j) (b (j+s)) (b (j+2*s))) (3*s) (i+2*s) (trees'-1))
1389-
(Two (b (i+(2+3*(trees'-1))*s)) (b (i+(3+3*(trees'-1))*s)))
1390-
(trees',2) -> Deep (trees*s) (Three (b i) (b (i+s)) (b (i+2*s)))
1391-
(create (\j -> Node3 (3*s) (b j) (b (j+s)) (b (j+2*s))) (3*s) (i+3*s) (trees'-1))
1392-
(Two (b (i+(3+3*(trees'-1))*s)) (b (i+(4+3*(trees'-1))*s)))
1393-
(trees',0) -> Deep (trees*s) (Three (b i) (b (i+s)) (b (i+2*s)))
1394-
(create (\j -> Node3 (3*s) (b j) (b (j+s)) (b (j+2*s))) (3*s) (i+3*s) (trees'-2))
1395-
(Three (b (i+(3+3*(trees'-2))*s)) (b (i+(4+3*(trees'-2))*s)) (b (i+(5+3*(trees'-2))*s)))
1377+
where
1378+
create :: (Int -> a) -> Int -> Int -> Int -> FingerTree a
1379+
create b{-tree_builder-} s{-tree_size-} i{-start_index-} trees = i `seq` s `seq` case trees of
1380+
1 -> Single $ b i
1381+
2 -> Deep (2*s) (One (b i)) Empty (One (b (i+s)))
1382+
3 -> Deep (3*s) (createTwo b s i) Empty (One (b (i+2*s)))
1383+
4 -> Deep (4*s) (createTwo b s i) Empty (createTwo b s (i+2*s))
1384+
5 -> Deep (5*s) (createThree b s i) Empty (createTwo b s (i+3*s))
1385+
6 -> Deep (6*s) (createThree b s i) Empty (createThree b s (i+3*s))
1386+
_ -> case trees `quotRem` 3 of
1387+
(trees', 1) -> Deep (trees*s) (createTwo b s i)
1388+
(create mb (3*s) (i+2*s) (trees'-1))
1389+
(createTwo b s (i+(2+3*(trees'-1))*s))
1390+
(trees', 2) -> Deep (trees*s) (createThree b s i)
1391+
(create mb (3*s) (i+3*s) (trees'-1))
1392+
(createTwo b s (i+(3+3*(trees'-1))*s))
1393+
(trees', 0) -> Deep (trees*s) (createThree b s i)
1394+
(create mb (3*s) (i+3*s) (trees'-2))
1395+
(createThree b s (i+(3+3*(trees'-2))*s))
1396+
where
1397+
createTwo b s i = Two (b i) (b (i + s))
1398+
createThree b s i = Three (b i) (b (i + s)) (b (i + s + s))
1399+
mb j = Node3 (3*s) (b j) (b (j + s)) (b (j + 2*s))
13961400

13971401
-- Splitting
13981402

0 commit comments

Comments
 (0)