@@ -338,15 +338,18 @@ aptyMiddle firstf
338
338
(Deep s (squashL pr prm) mm (squashR sfm sf)))
339
339
(fmap (fmap lastf) sfm)
340
340
341
- -- At the bottom
341
+ -- At the bottom. Note that these appendTree0 calls are very cheap, because in
342
+ -- each case, one of the arguments is guaranteed to be Empty or Single.
342
343
aptyMiddle firstf
343
344
lastf
344
345
map23
345
346
fs
346
347
(Deep s pr m sf)
347
- = (fmap (fmap firstf) m `snocTree` fmap firstf (digitToNode sf))
348
- `appendTree0` middle `appendTree0`
349
- (fmap lastf (digitToNode pr) `consTree` fmap (fmap lastf) m)
348
+ = fmap (fmap firstf) m `appendTree0`
349
+ ((fmap firstf (digitToNode sf)
350
+ `consTree` middle)
351
+ `snocTree` fmap lastf (digitToNode pr))
352
+ `appendTree0` fmap (fmap lastf) m
350
353
where middle = case trimTree $ mapMulFT s (\ (Elem f) -> fmap (fmap (map23 f)) converted) fs of
351
354
(firstMapped, restMapped, lastMapped) ->
352
355
Deep (size firstMapped + size restMapped + size lastMapped)
@@ -469,17 +472,16 @@ rigidify Single{} = error "rigidify: singleton"
469
472
-- | /O(log n)/ (incremental) Rejigger a finger tree so the digits are all ones
470
473
-- and twos.
471
474
thin :: Sized a => FingerTree a -> FingerTree a
472
- -- Note that 'thin' may call itself at most once before passing the job on to
473
- -- 'thin12'. 'thin12' will produce a 'Deep' constructor immediately before
474
- -- calling 'thin'.
475
+ -- Note that 'thin12' will produce a 'Deep' constructor immediately before
476
+ -- recursively calling 'thin'.
475
477
thin Empty = Empty
476
478
thin (Single a) = Single a
477
479
thin t@ (Deep s pr m sf) =
478
480
case pr of
479
481
One {} -> thin12 t
480
482
Two {} -> thin12 t
481
- Three a b c -> thin $ Deep s (One a) (node2 b c `consTree` m) sf
482
- Four a b c d -> thin $ Deep s (Two a b) (node2 c d `consTree` m) sf
483
+ Three a b c -> thin12 $ Deep s (One a) (node2 b c `consTree` m) sf
484
+ Four a b c d -> thin12 $ Deep s (Two a b) (node2 c d `consTree` m) sf
483
485
484
486
thin12 :: Sized a => FingerTree a -> FingerTree a
485
487
thin12 (Deep s pr m sf@ One {}) = Deep s pr (thin m) sf
0 commit comments