/
Base.hs
1068 lines (942 loc) · 38.1 KB
/
Base.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
-----------------------------------------------------------------------------
-- |
-- Module : Data.EnumMapMap.Base
-- Copyright : (c) Daan Leijen 2002
-- (c) Andriy Palamarchuk 2008
-- (c) Matthew West 2012
-- License : BSD-style
-- Stability : experimental
-- Portability : Uses GHC extensions
--
-- Based on Data.IntMap.Base.
--
-- This defines the @'EnumMapMap' (k ':&' t) v@ instance, and the Key data types. The
-- terminating key type is K, and the @'EnumMapMap' (K k) v@ instances are defined
-- in EnumMapMap.Lazy and EnumMapMap.Strict.
-----------------------------------------------------------------------------
module Data.EnumMapMap.Base(
-- * Key types
(:&)(..), N(..), Z(..),
d1, d2, d3, d4, d5, d6, d7, d8, d9, d10,
-- * Split/Join Keys
IsSplit(..),
Plus,
SubKey(..),
-- * Internal
MkNestedPair(..),
-- ** IsEMM
EMM(..),
IsKey(..),
EnumMapMap(..),
-- ** SKey
HasSKey(..),
SubKeyS(..),
-- ** EMM internals
mergeWithKey',
mapWithKey_,
mapMaybeWithKey_,
traverseWithKey_,
foldrWithKey_,
foldlStrict,
-- ** IntMap internals
Prefix,
Mask,
Nat,
Key,
intFromNat,
natFromInt,
shiftRL,
shiftLL,
branchMask,
mask,
bin,
tip,
shorter,
nomatch,
match,
join,
zero
) where
import Prelude hiding (lookup,
map,
filter,
foldr, foldl,
null, init,
head, tail)
import Control.Applicative (Applicative(pure,(<*>)), (<$>))
import Control.DeepSeq (NFData(rnf))
import Data.Bits
import Data.Default
import qualified Data.Foldable as Fold
import Control.Lens.At (At, Index, Ixed, IxValue,
at, ix)
import Control.Lens.Lens ((<&>))
import Control.Lens.Each (Each)
import qualified Control.Lens.Fold as Lens
import qualified Control.Lens.Indexed as Lens
import qualified Control.Lens.Setter as Lens
import Data.Maybe (fromMaybe)
import Data.SafeCopy
import Data.Semigroup
import Data.Traversable (Traversable(traverse))
import Data.Typeable
import GHC.Exts (Word(..), Int(..),
uncheckedShiftRL#, uncheckedShiftL#)
data EMM k v = Bin {-# UNPACK #-} !Prefix {-# UNPACK #-} !Mask
!(EMM k v) !(EMM k v)
| Tip {-# UNPACK #-} !Int v
| Nil
deriving (Show)
type Nat = Word
type Key = Int
type Prefix = Int
type Mask = Int
infixr 3 :&
-- | Multiple keys are joined by the (':&') constructor.
--
-- > multiKey :: Int :& Int :& K Int
-- > multiKey = 5 :& 6 :& K 5
--
data k :& t = !k :& !t
deriving (Show, Eq)
data Z = Z
data N n = N !n
-- | Split after 1 key.
--
-- > emm :: EnumMapMap (T1 :& T2 :& K T3) v
-- > splitKey d1 emm :: EnumMapMap (T1 :& K T2) (EnumMapMap (K T3) v)
d1 :: Z
d1 = Z
-- | Split after 2 keys.
--
-- > emm :: EnumMapMap (T1 :& T2 :& K T3) v
-- > splitKey d1 emm :: EnumMapMap (K T1) (EnumMapMap (T2 :& K T3) v)
d2 :: N Z
d2 = N d1
d3 :: N(N Z)
d3 = N d2
d4 :: N(N(N Z))
d4 = N d3
d5 :: N(N(N(N Z)))
d5 = N d4
d6 :: N(N(N(N(N Z))))
d6 = N d5
d7 :: N(N(N(N(N(N Z)))))
d7 = N d6
d8 :: N(N(N(N(N(N(N Z))))))
d8 = N d7
d9 :: N(N(N(N(N(N(N(N Z)))))))
d9 = N d8
d10 :: N(N(N(N(N(N(N(N(N Z))))))))
d10 = N d9
class IsSplit k z where
type Head k z :: *
type Tail k z :: *
-- | Split a key so that an 'EnumMapMap' becomes an 'EnumMapMap' of
-- 'EnumMapMap's.
--
-- > newtype ID = ID Int deriving Enum
-- > emm = empty :: EnumMapMap (Int :& K ID) Bool
-- > res :: EnumMapMap (K ID) Bool
-- > res = lookup (K 5) $ splitKey d1 emm
--
-- If the level is too high then the compilation will fail with an error
--
-- > emm = empty :: EnumMapMap (Int :& Int :& K Int) Bool -- 3 levels
-- > res1 = splitKey d4 emm -- ERROR! Instance not found...
-- > res2 = splitKey d3 emm -- ERROR! Instance not found...
-- > res3 = splitKey d2 emm -- Good
--
splitKey :: z -> EnumMapMap k v
-> EnumMapMap (Head k z) (EnumMapMap (Tail k z) v)
instance (IsSplit t n, Enum k) => IsSplit (k :& t) (N n) where
type Head (k :& t) (N n) = k :& Head t n
type Tail (k :& t) (N n) = Tail t n
splitKey (N n) (KCC emm) = KCC $ mapWithKey_ (\_ -> splitKey n) emm
type family Plus k1 k2 :: *
type instance Plus (k1 :& t) k2 = k1 :& Plus t k2
-- | This is used by the SafeCopy instance. It strips the constructors and Enum
-- from the key so that
-- > NestedPair (T1 :& T2 :& K T3) v ~ (Int, (Int, (Int, v)))
class MkNestedPair k v where
type NestedPair k v :: *
nestedPair :: k -> v -> NestedPair k v
unNestedPair :: NestedPair k v -> (k ,v)
instance (MkNestedPair t v, Enum k) => MkNestedPair (k :& t) v where
type NestedPair (k :& t) v = (Int, NestedPair t v)
nestedPair (k :& t) v = (fromEnum k, nestedPair t v)
unNestedPair (k, x) = (toEnum k :& t, v)
where
(t, v) = unNestedPair x
class SubKey k1 k2 v where
-- | k1 should be a prefix of k2. If @k1 ~ k2@ then the 'Result' will be
-- @v@.
--
-- > Result (K ID1) (ID1 :& K ID2) v ~ EnumMapMap (K ID2) v
-- > Result (ID1 :& K ID2) (ID1 :& K ID2) v ~ v
-- > Result (ID1 :& K ID2) (K ID1) v -- ERROR
-- > Result (ID2 :& K ID1) (ID1 :& K ID2) -- ERROR
type Result k1 k2 v :: *
-- | Is the key present in the 'EnumMapMap'?
member :: k1 -> EnumMapMap k2 v -> Bool
-- | An 'EnumMapMap' with one element
--
-- > singleton (5 :& K 3) "a" == fromList [(5 :& K 3, "a")]
-- > singleton (K 5) $ singleton (K 2) "a" == fromList [(5 :& K 3, "a")]
singleton :: k1 -> Result k1 k2 v -> EnumMapMap k2 v
-- | Lookup up the value at a key in the 'EnumMapMap'.
--
-- > emm = fromList [(3 :& K 1, "a")]
-- > lookup (3 :& K 1) emm == Just "a"
-- > lookup (2 :& K 1) emm == Nothing
--
-- If the given key has less dimensions then the 'EnumMapMap' then a submap
-- is returned.
--
-- > emm2 = fromList [(3 :& 2 :& K 1, "a"), (3 :& 2 :& K 4, "a")]
-- > lookup (3 :& K 2) emm2 == Just $ fromList [(K 1, "a"), (K 4, "a")]
--
lookup :: (IsKey k1, IsKey k2) =>
k1 -> EnumMapMap k2 v -> Maybe (Result k1 k2 v)
-- | Insert a new key\/value pair into the 'EnumMapMap'. Can also insert submaps.
insert :: (IsKey k1, IsKey k2) =>
k1 -> Result k1 k2 v -> EnumMapMap k2 v -> EnumMapMap k2 v
-- | Insert with a combining function. Can also insert submaps.
insertWith :: (IsKey k1, IsKey k2) =>
(Result k1 k2 v -> Result k1 k2 v -> Result k1 k2 v)
-> k1 -> Result k1 k2 v -> EnumMapMap k2 v -> EnumMapMap k2 v
insertWith f = insertWithKey (const f)
-- | Insert with a combining function. Can also insert submaps.
insertWithKey :: (IsKey k1, IsKey k2) =>
(k1 -> Result k1 k2 v -> Result k1 k2 v -> Result k1 k2 v)
-> k1 -> Result k1 k2 v -> EnumMapMap k2 v -> EnumMapMap k2 v
-- | Remove a key and it's value from the 'EnumMapMap'. If the key is not
-- present the original 'EnumMapMap' is returned.
delete :: (IsKey k1, IsKey k2) =>
k1 -> EnumMapMap k2 v -> EnumMapMap k2 v
class SubKeyS k s where
-- | The intersection of an 'EnumMapMap' and an 'EnumMapSet'. If a key is
-- present in the EnumMapSet then it will be present in the resulting
-- 'EnumMapMap'. Works with 'EnumMapSet's that are submaps of the
-- 'EnumMapMap'.
intersectSet :: (IsKey k, IsKey s) =>
EnumMapMap k v -> EnumMapMap s () -> EnumMapMap k v
-- | The difference between an 'EnumMapMap' and an 'EnumMapSet'. If a key
-- is present in the 'EnumMapSet' it will not be present in the result.
differenceSet :: (IsKey k, IsKey s) =>
EnumMapMap k v -> EnumMapMap s () -> EnumMapMap k v
class HasSKey k where
type Skey k :: *
-- | Convert a key terminated with 'K' into one terminated with 'S'.
--
-- > k = 1 :& 2 :& 'K' 3
-- > toS k == 1 :& 2 :& 'S' 3
--
toS :: k -> Skey k
-- | Convert a key terminated with 'S' into one terminated with 'K'.
--
-- > s = 1 :& 2 :& S 3
-- > toK s == 1 :& 2 :& K 3
toK :: Skey k -> k
instance (HasSKey t) => HasSKey (k :& t) where
type Skey (k :& t) = k :& Skey t
toS (k :& t) = (:&) k $! toS t
toK (k :& t) = (:&) k $! toK t
class (Eq k) => IsKey k where
-- | A map of keys to values. The keys are 'Enum' types but are stored as 'Int's
-- so any keys with the same 'Int' value are treated as the same. The aim is to
-- provide typesafe indexing.
data EnumMapMap k :: * -> *
-- | No subtrees should be empty. Returns 'True' if one is.
emptySubTrees :: EnumMapMap k v -> Bool
emptySubTrees_ :: EnumMapMap k v -> Bool
-- | Remove empty subtrees.
removeEmpties :: EnumMapMap k v -> EnumMapMap k v
-- | Join a key so that an 'EnumMapMap' of 'EnumMapMap's becomes an
-- 'EnumMapMap'.
--
-- > newtype ID = ID Int deriving Enum
-- > emm :: EnumMapMap (K Int) (EnumMapMap (K ID) Bool)
-- > res :: EnumMapMap (Int :& K ID) Bool
-- > res = joinKey emm
--
-- 'joinKey' is the opposite of 'splitKey'.
--
-- > emm = empty :: EnumMapMap (Int :& Int :& K ID) Bool)
-- > emm == joinKey $ splitKey d2 emm
--
joinKey :: (IsKey (Plus k k2)) =>
EnumMapMap k (EnumMapMap k2 v)
-> EnumMapMap (Plus k k2) v
joinKey = removeEmpties . unsafeJoinKey
-- | Join a key so that an 'EnumMapMap' of 'EnumMapMap's becomes an
-- 'EnumMapMap'. The unsafe version does not check for empty subtrees, so
-- it is faster.
--
-- > newtype ID = ID Int deriving Enum
-- > emm :: EnumMapMap (K Int) (EnumMapMap (K ID) Bool)
-- > res :: EnumMapMap (Int :& K ID) Bool
-- > res = unsafeJoinKey emm
--
unsafeJoinKey :: EnumMapMap k (EnumMapMap k2 v)
-> EnumMapMap (Plus k k2) v
-- | The empty 'EnumMapMap'.
empty :: EnumMapMap k v
-- | Is the 'EnumMapMap' empty?
--
-- Submaps can never be empty, so the following should always hold true:
--
-- > emm :: EnumMapMap (Int :& Int :& K ID) Bool)
-- > null $ splitKey x emm == False
null :: EnumMapMap k v -> Bool
-- | Number of elements in the 'EnumMapMap'.
size :: EnumMapMap k v -> Int
-- | The expression (@'alter' f k emm@) alters the value at @k@, or absence thereof.
-- 'alter' can be used to insert, delete, or update a value in an 'EnumMapMap'.
alter :: (Maybe v -> Maybe v) -> k -> EnumMapMap k v -> EnumMapMap k v
-- | Map a function over all values in the 'EnumMapMap'.
map :: (v -> t) -> EnumMapMap k v -> EnumMapMap k t
map f = mapWithKey (const f)
-- | Map values and collect the 'Just' results.
mapMaybe :: (v -> Maybe t) -> EnumMapMap k v -> EnumMapMap k t
mapMaybe f = mapMaybeWithKey (\_ x -> f x)
-- | Map keys\/values and collect the 'Just' results.
mapMaybeWithKey :: (k -> v -> Maybe t) -> EnumMapMap k v -> EnumMapMap k t
-- | Map a function over all key\/value pairs in the 'EnumMapMap'.
mapWithKey :: (k -> v -> t) -> EnumMapMap k v -> EnumMapMap k t
-- | @TraverseWithKey@ behaves exactly like a regular 'traverse' except that
-- the traversing function also has access to the key associated with a
-- value.
traverseWithKey :: (Applicative t) =>
(k -> a -> t b) -> EnumMapMap k a -> t (EnumMapMap k b)
-- | Fold the values in the 'EnumMapMap' using the given
-- right-associative binary operator
foldr :: (v -> t -> t) -> t -> EnumMapMap k v -> t
-- | Fold the keys and values in the 'EnumMapMap' using the given right-associative
-- binary operator.
foldrWithKey :: (k -> v -> t -> t) -> t -> EnumMapMap k v -> t
-- | Convert the 'EnumMapMap' to a list of key\/value pairs.
toList :: SubKey k k v =>
EnumMapMap k v -> [(k, v)]
toList = foldrWithKey (\k x xs -> (k, x):xs) []
-- | Convert the 'EnumMapMap' to a list of nested tuples
toNestedPairList :: (SubKey k k v, MkNestedPair k v) =>
EnumMapMap k v -> [NestedPair k v]
toNestedPairList = foldrWithKey (\k x xs -> (nestedPair k x):xs) []
-- | Create an 'EnumMapMap' from a list of key\/value pairs.
fromList :: (SubKey k k v, Result k k v ~ v) => [(k, v)] -> EnumMapMap k v
fromList = foldlStrict (\t (k, x) -> insert k x t) empty
-- | Create an 'EnumMapMap' from a list of nested tuples
fromNestedPairList :: (SubKey k k v, Result k k v ~ v, MkNestedPair k v) =>
[NestedPair k v] -> EnumMapMap k v
fromNestedPairList = foldlStrict f empty
where
f :: (IsKey k, SubKey k k v, Result k k v ~ v, MkNestedPair k v) =>
EnumMapMap k v -> NestedPair k v -> EnumMapMap k v
f emm = g emm . unNestedPair
g :: (IsKey k, SubKey k k v, Result k k v ~ v) =>
EnumMapMap k v -> (k, v) -> EnumMapMap k v
g emm (k, v) = insert k v emm
-- | List of elements in ascending order of keys
elems :: EnumMapMap k v -> [v]
elems = foldr (:) []
-- | List of keys
keys :: EnumMapMap k v -> [k]
keys = foldrWithKey (\k _ ks -> k:ks) []
-- | The 'Data.EnumMapSet' of the keys. 'EnumMapMap' keys can be converted into
-- 'Data.EnumMapSet' keys using 'toS', and back again using 'toK'.
keysSet :: (HasSKey k) => EnumMapMap k v -> EnumMapMap (Skey k) ()
-- | Build an 'EnumMapMap' from an 'EnumMapSet' and a function which for each
-- key computes it's value
fromSet :: HasSKey k => (k -> v) -> EnumMapMap (Skey k) () -> EnumMapMap k v
-- | The minimal key and value of the 'EnumMapMap'.
--
-- > findMin empty -- ERROR, no minimal key
-- > findMin $ fromList [(K 1, "a", K 3, "b")] == (K 1, a)
findMin :: EnumMapMap k v -> (k, v)
-- | Retrieves the minimal (key,value) pair of the EnumMapMap, and the
-- EnumMapMap stripped of that element, or 'Nothing' if passed an empty map.
minViewWithKey :: EnumMapMap k v -> Maybe ((k, v), EnumMapMap k v)
deleteFindMin :: EnumMapMap k v -> ((k, v), EnumMapMap k v)
deleteFindMin =
fromMaybe(error "deleteFindMin: empty EnumMapMap has no minimal\
\ element") . minViewWithKey
-- | The (left-biased) union of two 'EnumMapMap's.
-- It prefers the first 'EnumMapMap' when duplicate keys are encountered.
union :: EnumMapMap k v -> EnumMapMap k v -> EnumMapMap k v
-- | The union of a list of maps.
unions :: [EnumMapMap k v] -> EnumMapMap k v
unions = foldlStrict union empty
-- | The union of a list of maps with a combining function
unionsWith :: (v -> v -> v) -> [EnumMapMap k v] -> EnumMapMap k v
unionsWith f = foldlStrict (unionWith f) empty
-- | The union with a combining function.
unionWith :: (v -> v -> v)
-> EnumMapMap k v -> EnumMapMap k v -> EnumMapMap k v
unionWith f = unionWithKey (const f)
-- | The union with a combining function.
unionWithKey :: (k -> v -> v -> v)
-> EnumMapMap k v -> EnumMapMap k v -> EnumMapMap k v
-- | Difference between two 'EnumMapMap's (based on keys).
difference :: EnumMapMap k v1 -> EnumMapMap k v2 -> EnumMapMap k v1
-- | Difference with a combining function.
differenceWith :: (v1 -> v2 -> Maybe v1)
-> EnumMapMap k v1
-> EnumMapMap k v2
-> EnumMapMap k v1
differenceWith f = differenceWithKey (const f)
-- | Difference with a combining function.
differenceWithKey :: (k -> v1 -> v2 -> Maybe v1)
-> EnumMapMap k v1
-> EnumMapMap k v2
-> EnumMapMap k v1
-- | The (left-biased) intersection of two 'EnumMapMap' (based on keys).
intersection :: EnumMapMap k v1
-> EnumMapMap k v2
-> EnumMapMap k v1
-- | The intersection with a combining function.
intersectionWith :: (v1 -> v2 -> v3)
-> EnumMapMap k v1
-> EnumMapMap k v2
-> EnumMapMap k v3
intersectionWith f = intersectionWithKey (const f)
-- | The intersection with a combining function.
intersectionWithKey :: (k -> v1 -> v2 -> v3)
-> EnumMapMap k v1
-> EnumMapMap k v2
-> EnumMapMap k v3
equal :: Eq v => EnumMapMap k v -> EnumMapMap k v -> Bool
nequal :: Eq v => EnumMapMap k v -> EnumMapMap k v -> Bool
instance (Enum k, IsKey t1, IsKey t2, SubKey t1 t2 v) =>
SubKey (k :& t1) (k :& t2) v where
type Result (k :& t1) (k :& t2) v = Result t1 t2 v
member (key' :& nxt) (KCC emm) = key `seq` go emm
where
go t = case t of
Bin _ m l r -> case zero key m of
True -> go l
False -> go r
Tip kx x -> case key == kx of
True -> member nxt x
False -> False
Nil -> False
key = fromEnum key'
singleton (key :& nxt) = KCC . Tip (fromEnum key) . singleton nxt
lookup (key' :& nxt) (KCC emm) = key `seq` go emm
where
go (Bin _ m l r)
| zero key m = go l
| otherwise = go r
go (Tip kx x)
= case kx == key of
True -> lookup nxt x
False -> Nothing
go Nil = Nothing
key = fromEnum key'
insert (key :& nxt) val (KCC emm) =
KCC $ insertWith_ (insert nxt val) key (singleton nxt val) emm
insertWithKey f k@(key :& nxt) val (KCC emm) =
KCC $ insertWith_ go key (singleton nxt val) emm
where
go = insertWithKey (\_ -> f k) nxt val
delete (key :& nxt) (KCC emm) =
KCC $ alter_ (delete nxt) (fromEnum key) emm
instance (Enum k, IsKey t1, IsKey t2, SubKeyS t1 t2) =>
SubKeyS (k :& t1) (k :& t2) where
intersectSet (KCC emm) (KCC ems) =
KCC $ mergeWithKey' binD go (const Nil) (const Nil) emm ems
where
go = \(Tip k1 x1) (Tip _ x2) ->
tip k1 $ intersectSet x1 x2
differenceSet (KCC emm) (KCC ems) =
KCC $ mergeWithKey' binD go id (const Nil) emm ems
where
go = \(Tip k1 x1) (Tip _ x2) ->
tip k1 $ differenceSet x1 x2
instance (Eq k, Enum k, IsKey t, HasSKey t) => IsKey (k :& t) where
newtype EnumMapMap (k :& t) v = KCC (EMM k (EnumMapMap t v))
emptySubTrees e@(KCC emm) =
case emm of
Nil -> False
_ -> emptySubTrees_ e
emptySubTrees_ (KCC emm) = go emm
where
go t = case t of
Bin _ _ l r -> go l || go r
Tip _ v -> emptySubTrees_ v
Nil -> True
removeEmpties (KCC emm) = KCC $ go emm
where
go t = case t of
Bin p m l r -> bin p m (go l) (go r)
Tip k v -> tip k (removeEmpties v)
Nil -> Nil
unsafeJoinKey (KCC emm) = KCC $ mapWithKey_ (const unsafeJoinKey) emm
empty = KCC Nil
null (KCC t) =
case t of
Nil -> True
_ -> False
size (KCC t) = go t
where
go (Bin _ _ l r) = go l + go r
go (Tip _ y) = size y
go Nil = 0
alter f !(key :& nxt) (KCC emm) =
KCC $ alter_ (alter f nxt) (fromEnum key) emm
mapWithKey f (KCC emm) = KCC $ mapWithKey_ go emm
where
go k = mapWithKey (\nxt -> f $! k :& nxt)
mapMaybeWithKey f (KCC emm) = KCC $ mapMaybeWithKey_ go emm
where
go k = mapMaybeWithKey (\nxt -> f $! k :& nxt)
traverseWithKey f (KCC emm) = KCC <$> traverseWithKey_ go emm
where
go k = traverseWithKey (\nxt -> f $! k :& nxt)
foldr f init (KCC emm) = foldrWithKey_ (\_ val z -> foldr f z val) init emm
foldrWithKey f init (KCC emm) = foldrWithKey_ go init emm
where
go k val z = foldrWithKey (\nxt -> f $! k :& nxt) z val
keysSet (KCC emm) = KCC $ mapWithKey_ (const keysSet) emm
fromSet f (KCC ems) = KCC $ mapWithKey_ go ems
where
go k = fromSet (\nxt -> f $! k :& nxt)
findMin (KCC emm) =
case emm of
Nil -> error "findMin: no minimal element"
Tip k v -> (toEnum k :& t, v')
where (t, v') = findMin v
Bin _ m l r
| m < 0 -> go r
| otherwise -> go l
where go (Tip k v) = (toEnum k :& t, v')
where (t, v') = findMin v
go (Bin _ _ l' _) = go l'
go Nil = error "findMin: Nil"
minViewWithKey (KCC emm) =
goat emm >>= \(r, emm') -> return (r, KCC emm')
where
goat t =
case t of
Nil -> Nothing
Bin p m l r | m < 0 ->
case go r of
(result, r') ->
Just (result, binD p m l r')
_ -> Just (go t)
go (Bin p m l r) = case go l of
(result, l') -> (result, binD p m l' r)
go (Tip k y) = case minViewWithKey y of
Just ((t, v), y') ->
((toEnum k :& t, v), tip k y')
Nothing -> error "minViewWithKey: Nothing"
go Nil = error "minViewWithKey Nil"
union (KCC emm1) (KCC emm2) = KCC $ mergeWithKey' binD go id id emm1 emm2
where
go = \(Tip k1 x1) (Tip _ x2) -> tip k1 $ union x1 x2
unionWithKey f (KCC emm1) (KCC emm2) =
KCC $ mergeWithKey' binD go id id emm1 emm2
where
go = \(Tip k1 x1) (Tip _ x2) ->
Tip k1 $ unionWithKey (g k1) x1 x2
g k1 nxt = f $! toEnum k1 :& nxt
difference (KCC emm1) (KCC emm2) =
KCC $ mergeWithKey' binD go id (const Nil) emm1 emm2
where
go = \(Tip k1 x1) (Tip _ x2) ->
tip k1 (difference x1 x2)
differenceWithKey f (KCC emm1) (KCC emm2) =
KCC $ mergeWithKey' binD go id (const Nil) emm1 emm2
where
go = \(Tip k1 x1) (Tip _ x2) ->
tip k1 $ differenceWithKey (\nxt ->
f $! toEnum k1 :& nxt) x1 x2
intersection (KCC emm1) (KCC emm2) =
KCC $ mergeWithKey' binD go (const Nil) (const Nil) emm1 emm2
where
go = \(Tip k1 x1) (Tip _ x2) ->
tip k1 $ intersection x1 x2
intersectionWithKey f (KCC emm1) (KCC emm2) =
KCC $ mergeWithKey' binD go (const Nil) (const Nil) emm1 emm2
where
go = \(Tip k1 x1) (Tip _ x2) ->
tip k1 $ intersectionWithKey (\nxt ->
f $! toEnum k1 :& nxt) x1 x2
equal (KCC emm1) (KCC emm2) = emm1 == emm2
nequal (KCC emm1) (KCC emm2) = emm1 /= emm2
{--------------------------------------------------------------------
Helpers
--------------------------------------------------------------------}
insertWith_ :: Enum k => (v -> v) -> k -> v -> EMM k v -> EMM k v
insertWith_ f !key' val emm = key `seq` go emm
where
go t =
case t of
Bin p m l r
| nomatch key p m -> join key (Tip key val) p t
| zero key m -> Bin p m (go l) r
| otherwise -> Bin p m l (go r)
Tip ky y
| key == ky -> Tip key (f y)
| otherwise -> join key (Tip key val) ky t
Nil -> Tip key val
key = fromEnum key'
{-# INLINE insertWith_ #-}
-- | 'alter_' is used to walk down the tree to find the 'EnumMapMap' to actually
-- change. If the new 'EnumMapMap' is null then it's removed from the containing
-- 'EMM'.
alter_ :: (IsKey b) =>
(EnumMapMap b v -> EnumMapMap b v)
-> Key
-> EMM a (EnumMapMap b v)
-> EMM a (EnumMapMap b v)
alter_ f k = go
where
go t =
case t of
Bin p m l r | nomatch k p m -> joinD k (tip k $ f empty) p t
| zero k m -> binD p m (go l) r
| otherwise -> binD p m l (go r)
Tip ky y | k == ky -> tip k $ f y
| otherwise -> joinD k (tip k $ f empty) ky t
Nil -> tip k $ f empty
{-# INLINE alter_ #-}
mapWithKey_ :: Enum k => (k -> v -> t) -> EMM k v -> EMM k t
mapWithKey_ f = go
where
go (Bin p m l r) = Bin p m (go l) (go r)
go (Tip k x) = Tip k (f (toEnum k) x)
go Nil = Nil
{-# INLINE mapWithKey_ #-}
mapMaybeWithKey_ :: (IsKey b, Enum key) =>
(key -> EnumMapMap b v -> EnumMapMap b t) ->
EMM a (EnumMapMap b v) ->
EMM a (EnumMapMap b t)
mapMaybeWithKey_ f = go
where
go (Bin p m l r) = binD p m (go l) (go r)
go (Tip k x) = tip k $ f (toEnum k) x
go Nil = Nil
{-# INLINE mapMaybeWithKey_ #-}
traverseWithKey_ :: (Enum k, Applicative t) =>
(k -> a -> t b) -> EMM k a -> t (EMM k b)
traverseWithKey_ f = go
where
go Nil = pure Nil
go (Tip k v) = Tip k <$> f (toEnum k) v
go (Bin p m l r) = Bin p m <$> go l <*> go r
foldrWithKey_ :: (Enum k) => (k -> v -> t -> t) -> t -> EMM k v -> t
foldrWithKey_ f z = \emm ->
case emm of Bin _ m l r | m < 0 -> go (go z l) r
| otherwise -> go (go z r) l
_ -> go z emm
where
go z' Nil = z'
go z' (Tip kx tx) = f (toEnum kx) tx z'
go z' (Bin _ _ l r) = go (go z' r) l
{-# INLINE foldrWithKey_ #-}
-- | See 'IntMap' documentation for an explanation of 'mergeWithKey''.
mergeWithKey' :: (Prefix -> Mask -> EMM a v3 -> EMM a v3 -> EMM a v3)
-> (EMM a v1 -> EMM a v2 -> EMM a v3)
-> (EMM a v1 -> EMM a v3)
-> (EMM a v2 -> EMM a v3)
-> EMM a v1 -> EMM a v2 -> EMM a v3
mergeWithKey' bin' f g1 g2 = go
where
go t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
| shorter m1 m2 = merge1
| shorter m2 m1 = merge2
| p1 == p2 = bin' p1 m1 (go l1 l2) (go r1 r2)
| otherwise = maybe_join p1 (g1 t1) p2 (g2 t2)
where
merge1 | nomatch p2 p1 m1 = maybe_join p1 (g1 t1) p2 (g2 t2)
| zero p2 m1 = bin' p1 m1 (go l1 t2) (g1 r1)
| otherwise = bin' p1 m1 (g1 l1) (go r1 t2)
merge2 | nomatch p1 p2 m2 = maybe_join p1 (g1 t1) p2 (g2 t2)
| zero p1 m2 = bin' p2 m2 (go t1 l2) (g2 r2)
| otherwise = bin' p2 m2 (g2 l2) (go t1 r2)
go t1'@(Bin {}) t2'@(Tip k2' _) = merge t2' k2' t1'
where merge t2 k2 t1@(Bin p1 m1 l1 r1)
| nomatch k2 p1 m1 = maybe_join p1 (g1 t1) k2 (g2 t2)
| zero k2 m1 = bin' p1 m1 (merge t2 k2 l1) (g1 r1)
| otherwise = bin' p1 m1 (g1 l1) (merge t2 k2 r1)
merge t2 k2 t1@(Tip k1 _)
| k1 == k2 = f t1 t2
| otherwise = maybe_join k1 (g1 t1) k2 (g2 t2)
merge t2 _ Nil = g2 t2
go t1@(Bin {}) Nil = g1 t1
go t1'@(Tip k1' _) t2' = merge t1' k1' t2'
where merge t1 k1 t2@(Bin p2 m2 l2 r2)
| nomatch k1 p2 m2 = maybe_join k1 (g1 t1) p2 (g2 t2)
| zero k1 m2 = bin' p2 m2 (merge t1 k1 l2) (g2 r2)
| otherwise = bin' p2 m2 (g2 l2) (merge t1 k1 r2)
merge t1 k1 t2@(Tip k2 _)
| k1 == k2 = f t1 t2
| otherwise = maybe_join k1 (g1 t1) k2 (g2 t2)
merge t1 _ Nil = g1 t1
go Nil t2 = g2 t2
maybe_join _ Nil _ t2 = t2
maybe_join _ t1 _ Nil = t1
maybe_join p1 t1 p2 t2 = join p1 t1 p2 t2
{-# INLINE maybe_join #-}
{-# INLINE mergeWithKey' #-}
{---------------------------------------------------------------------
Instances
---------------------------------------------------------------------}
-- Eq
instance (Eq v, IsKey k) => Eq (EnumMapMap k v) where
t1 == t2 = equal t1 t2
t1 /= t2 = nequal t1 t2
instance Eq v => Eq (EMM k v) where
t1 == t2 = equalE t1 t2
t1 /= t2 = nequalE t1 t2
equalE :: Eq v => EMM k v -> EMM k v -> Bool
equalE (Bin p1 m1 l1 r1) (Bin p2 m2 l2 r2)
= (m1 == m2) && (p1 == p2) && equalE l1 l2 && equalE r1 r2
equalE (Tip kx x) (Tip ky y)
= (kx == ky) && (x==y)
equalE Nil Nil = True
equalE _ _ = False
nequalE :: Eq v => EMM k v -> EMM k v -> Bool
nequalE (Bin p1 m1 l1 r1) (Bin p2 m2 l2 r2)
= (m1 /= m2) || (p1 /= p2) || nequalE l1 l2 || nequalE r1 r2
nequalE (Tip kx x) (Tip ky y)
= (kx /= ky) || (x/=y)
nequalE Nil Nil = False
nequalE _ _ = True
instance (IsKey k) => Functor (EnumMapMap k)
where
fmap = map
-- | This instance differs from the 'Monoid' instance in 'IntMap'. Where the keys
-- are the same the values are combined using 'mappend'.
instance (IsKey k, Semigroup v) => Monoid (EnumMapMap k v) where
mempty = empty
mappend = unionWith (<>)
mconcat = unionsWith (<>)
instance (IsKey k, Semigroup v) =>
Semigroup (EnumMapMap k v) where
(<>) = unionWith (<>)
times1p _ a = a
instance (Show v, Show (EnumMapMap t v)) => Show (EnumMapMap (k :& t) v) where
show (KCC emm) = show emm
instance (NFData v, NFData (EnumMapMap t v)) => NFData (EnumMapMap (k :& t) v)
where
rnf (KCC emm) = go emm
where
go Nil = ()
go (Tip _ v) = rnf v
go (Bin _ _ l r) = go l `seq` go r
instance (NFData k, NFData t) => NFData (k :& t)
where
rnf (k :& t) = rnf k `seq` rnf t
-- Foldable
instance (Fold.Foldable (EnumMapMap t), Enum k, Eq k, IsKey t, HasSKey t) =>
Fold.Foldable (EnumMapMap (k :& t)) where
fold (KCC emm) = go emm
where
go Nil = mempty
go (Tip _ v) = Fold.fold v
go (Bin _ _ l r) = go l `mappend` go r
foldr = foldr
foldMap f (KCC emm) = go emm
where
go Nil = mempty
go (Tip _ v) = Fold.foldMap f v
go (Bin _ _ l r) = go l `mappend` go r
instance (IsKey k, Fold.Foldable (EnumMapMap k)) =>
Traversable (EnumMapMap k) where
traverse f = traverseWithKey (\_ -> f)
-- Default
instance (IsKey k) => Default (EnumMapMap k v) where
def = empty
-- Typeable
deriving instance Typeable2 (:&)
deriving instance Typeable2 EnumMapMap
-- SafeCopy
instance (Enum a, SafeCopy b) => SafeCopy (a :& b) where
getCopy = contain $ do
a <- safeGet
b <- safeGet
return (toEnum a :& b)
putCopy (a :& b) = contain $ do
safePut $ fromEnum a
safePut b
errorTypeName _ = "(:&)"
instance (SafeCopy k, SafeCopy (NestedPair k v), IsKey k,
Result k k v ~ v, SubKey k k v,
MkNestedPair k v) =>
SafeCopy (EnumMapMap k v) where
getCopy = contain $ fmap fromNestedPairList safeGet
putCopy = contain . safePut . toNestedPairList
errorTypeName _ = "EnumMapMap"
-- Control.Lens
type instance Index (EnumMapMap k v) = k
instance (Fold.Foldable (EnumMapMap k), IsKey k, SubKey k k a, SubKey k k b) =>
Each (EnumMapMap k a) (EnumMapMap k b) a b
type instance IxValue (EnumMapMap k v) = Result k k v
instance (IsKey k, SubKey k k v) =>
Ixed (EnumMapMap k v) where
ix k f m = case lookup k m of
Just v -> f v <&> \v' -> insert k v' m
Nothing -> pure m
{-# INLINE ix #-}
instance (IsKey k, SubKey k k v) =>
At (EnumMapMap k v) where
at k f m = f mv <&>
\r -> case r of
Nothing -> maybe m (const (delete k m)) mv
Just v' -> insert k v' m
where mv = lookup k m
{-# INLINE at #-}
instance (IsKey k, Fold.Foldable (EnumMapMap k)) =>
Lens.FunctorWithIndex k (EnumMapMap k) where
imap = Lens.iover Lens.itraversed
{-# INLINE imap #-}
instance (IsKey k, Fold.Foldable (EnumMapMap k)) =>
Lens.FoldableWithIndex k (EnumMapMap k) where
ifoldMap = Lens.ifoldMapOf Lens.itraversed
{-# INLINE ifoldMap #-}
instance (IsKey k, Fold.Foldable (EnumMapMap k)) =>
Lens.TraversableWithIndex k (EnumMapMap k) where
itraverse = traverseWithKey
{-# INLINE itraverse #-}
{--------------------------------------------------------------------
Nat conversion
--------------------------------------------------------------------}
natFromInt :: Int -> Nat
natFromInt = fromIntegral
{-# INLINE natFromInt #-}
intFromNat :: Nat -> Int
intFromNat = fromIntegral
{-# INLINE intFromNat #-}
shiftRL, shiftLL :: Nat -> Int -> Nat
shiftRL (W# x) (I# i) = W# (uncheckedShiftRL# x i)
shiftLL (W# x) (I# i) = W# (uncheckedShiftL# x i)
{-# INLINE shiftRL #-}
{-# INLINE shiftLL #-}
{--------------------------------------------------------------------
Join
--------------------------------------------------------------------}
join :: Prefix -> EMM a v -> Prefix -> EMM a v -> EMM a v
join p1 t1 p2 t2
| zero p1 m = Bin p m t1 t2
| otherwise = Bin p m t2 t1
where
m = branchMask p1 p2
p = mask p1 m
{-# INLINE join #-}
joinD :: (IsKey b) =>
Prefix -> EMM a (EnumMapMap b v)
-> Prefix -> EMM a (EnumMapMap b v)
-> EMM a (EnumMapMap b v)
joinD p1 t1 p2 t2
| zero p1 m = binD p m t1 t2
| otherwise = binD p m t2 t1
where
m = branchMask p1 p2
p = mask p1 m
{-# INLINE joinD #-}
{--------------------------------------------------------------------
@bin@ assures that we never have empty trees within a tree.
--------------------------------------------------------------------}
bin :: Prefix -> Mask -> EMM k v -> EMM k v -> EMM k v
bin _ _ l Nil = l
bin _ _ Nil r = r
bin p m l r = Bin p m l r
{-# INLINE bin #-}
{--------------------------------------------------------------------
@binD@ assures that we never have empty trees in the next level
--------------------------------------------------------------------}
binD :: (IsKey b) =>
Prefix -> Mask
-> EMM a (EnumMapMap b v)
-> EMM a (EnumMapMap b v)
-> EMM a (EnumMapMap b v)
binD _ _ l Nil = l
binD _ _ Nil r = r
binD p m l r@(Tip _ y)
| null y = l
| otherwise = Bin p m l r
binD p m l@(Tip _ y) r
| null y = r
| otherwise = Bin p m l r
binD p m l r = Bin p m l r
{-# INLINE binD #-}
tip :: (IsKey b) => Key -> EnumMapMap b v -> EMM a (EnumMapMap b v)
tip k val
| null val = Nil
| otherwise = Tip k val
{-# INLINE tip #-}
{--------------------------------------------------------------------
Endian independent bit twiddling