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