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
|
module FMInt (
FMI,
emptyFM,
unitFM,
listToFM,
addToFM,
addToFM_C,
addListToFM,
addListToFM_C,
delFromFM,
delListFromFM,
splitFM,
plusFM,
plusFM_C,
minusFM,
intersectFM,
intersectFM_C,
foldFM,
sizeFM,
eqFM,
isEmptyFM,
elemFM,
lookupFM,
lookupWithDefaultFM,
keyOrder,
fmToList,
keysFM,
eltsFM,
minFM,maxFM,updFM, fmToListPreOrder,
showFM, readFM
) where
import Maybe
import ReadShowTerm (readQTerm, showQTerm)
type LeKey = Int -> Int -> Bool
emptyFM :: FMI
emptyFM = FMI EmptyFMI
unitFM :: Int -> Int -> FMI
unitFM key elt = FMI (unitFM' key elt)
unitFM' :: Int -> Int -> FiniteMap
unitFM' key elt = BranchFMI key elt 1 EmptyFMI EmptyFMI
listToFM :: [(Int,Int)] -> FMI
listToFM = addListToFM (emptyFM)
addToFM :: FMI -> Int -> Int -> FMI
addToFM (FMI fm) key elt = FMI (addToFM' fm key elt)
addToFM' :: FiniteMap -> Int -> Int -> FiniteMap
addToFM' fm key elt = addToFM_C' (\ _ new -> new) fm key elt
addToFM_C' :: (Int -> Int -> Int)
-> FiniteMap -> Int -> Int -> FiniteMap
addToFM_C' _ EmptyFMI key elt = unitFM' key elt
addToFM_C' combiner (BranchFMI key elt size fm_l fm_r) new_key new_elt
= if new_key < key
then mkBalBranch key elt (addToFM_C' combiner fm_l new_key new_elt) fm_r
else
if new_key==key
then BranchFMI new_key (combiner elt new_elt) size fm_l fm_r
else mkBalBranch key elt fm_l (addToFM_C' combiner fm_r new_key new_elt)
addListToFM :: FMI -> [(Int,Int)] -> FMI
addListToFM (FMI fm) key_elt_pairs =
FMI (addListToFM' fm key_elt_pairs)
addListToFM' :: FiniteMap
-> [(Int, Int)] -> FiniteMap
addListToFM' fm key_elt_pairs =
addListToFM_C' (\ _ new -> new) fm key_elt_pairs
addListToFM_C' :: (Int -> Int -> Int)
-> FiniteMap -> [(Int, Int)] -> FiniteMap
addListToFM_C' combiner fm key_elt_pairs
= foldl add fm key_elt_pairs
where
add fmap (key,elt) = addToFM_C' combiner fmap key elt
addToFM_C :: (Int -> Int -> Int) -> FMI -> Int -> Int
-> FMI
addToFM_C combiner (FMI fm) key elt =
FMI (addToFM_C' combiner fm key elt)
addListToFM_C :: (Int -> Int -> Int) -> FMI -> [(Int,Int)]
-> FMI
addListToFM_C combiner (FMI fm) key_elt_pairs =
FMI (addListToFM_C' combiner fm key_elt_pairs)
delFromFM :: FMI -> Int -> FMI
delFromFM (FMI fm) del_key = FMI (delFromFM' fm del_key)
delFromFM' :: FiniteMap -> Int -> FiniteMap
delFromFM' EmptyFMI _ = EmptyFMI
delFromFM' (BranchFMI key elt _ fm_l fm_r) del_key
= if del_key < key
then mkBalBranch key elt (delFromFM' fm_l del_key) fm_r
else
if del_key==key
then glueBal fm_l fm_r
else mkBalBranch key elt fm_l (delFromFM' fm_r del_key)
delListFromFM :: FMI -> [Int] -> FMI
delListFromFM (FMI fm) keys = FMI (foldl (delFromFM') fm keys)
updFM :: FMI -> Int -> (Int -> Int) -> FMI
updFM (FMI fm) i f = FMI (upd fm)
where
upd EmptyFMI = EmptyFMI
upd (BranchFMI k x h l r)
| i == k = BranchFMI k (f x) h l r
| i < k = BranchFMI k x h (upd l) r
| otherwise = BranchFMI k x h l (upd r)
splitFM :: FMI -> Int -> Maybe (FMI,(Int,Int))
splitFM g v = maybe Nothing (\x->Just (delFromFM g v,(v,x))) (lookupFM g v)
plusFM :: FMI -> FMI -> FMI
plusFM (FMI fm1) (FMI fm2) = FMI (plusFM' fm1 fm2)
plusFM' :: FiniteMap -> FiniteMap -> FiniteMap
plusFM' EmptyFMI fm2 = fm2
plusFM' (BranchFMI split_key1 elt1 s1 left1 right1) EmptyFMI =
(BranchFMI split_key1 elt1 s1 left1 right1)
plusFM' (BranchFMI split_key1 elt1 s1 left1 right1)
(BranchFMI split_key elt2 _ left right)
= mkVBalBranch split_key elt2 (plusFM' lts left) (plusFM' gts right)
where
fm1 = BranchFMI split_key1 elt1 s1 left1 right1
lts = splitLT fm1 split_key
gts = splitGT fm1 split_key
plusFM_C :: (Int -> Int -> Int)
-> FMI -> FMI -> FMI
plusFM_C combiner (FMI fm1) (FMI fm2) =
FMI (plusFM_C' combiner fm1 fm2)
plusFM_C' :: (Int -> Int -> Int)
-> FiniteMap -> FiniteMap -> FiniteMap
plusFM_C' _ EmptyFMI fm2 = fm2
plusFM_C' _ (BranchFMI split_key1 elt1 s1 left1 right1) EmptyFMI =
BranchFMI split_key1 elt1 s1 left1 right1
plusFM_C' combiner (BranchFMI split_key1 elt1 s1 left1 right1)
(BranchFMI split_key elt2 _ left right)
= mkVBalBranch split_key new_elt
(plusFM_C' combiner lts left)
(plusFM_C' combiner gts right)
where
fm1 = BranchFMI split_key1 elt1 s1 left1 right1
lts = splitLT fm1 split_key
gts = splitGT fm1 split_key
new_elt = case lookupFM' fm1 split_key of
Nothing -> elt2
Just elt1' -> combiner elt1' elt2
minusFM :: FMI -> FMI -> FMI
minusFM (FMI fm1) (FMI fm2) = FMI (minusFM' fm1 fm2)
minusFM' :: FiniteMap -> FiniteMap -> FiniteMap
minusFM' EmptyFMI _ = EmptyFMI
minusFM' (BranchFMI split_key1 elt1 s1 left1 right1) EmptyFMI =
BranchFMI split_key1 elt1 s1 left1 right1
minusFM' (BranchFMI split_key1 elt1 s1 left1 right1)
(BranchFMI split_key _ _ left right)
= glueVBal (minusFM' lts left) (minusFM' gts right)
where
fm1 = BranchFMI split_key1 elt1 s1 left1 right1
lts = splitLT fm1 split_key
gts = splitGT fm1 split_key
intersectFM :: FMI-> FMI -> FMI
intersectFM (FMI fm1) (FMI fm2) = FMI (intersectFM' fm1 fm2)
intersectFM' :: FiniteMap -> FiniteMap -> FiniteMap
intersectFM' fm1 fm2 = intersectFM_C' (\ _ right -> right) fm1 fm2
intersectFM_C :: (Int -> Int -> Int) -> FMI-> FMI -> FMI
intersectFM_C combiner (FMI fm1) (FMI fm2) =
FMI (intersectFM_C' combiner fm1 fm2)
intersectFM_C' :: (Int -> Int -> Int)
-> FiniteMap -> FiniteMap -> FiniteMap
intersectFM_C' _ _ EmptyFMI = EmptyFMI
intersectFM_C' _ EmptyFMI (BranchFMI _ _ _ _ _) = EmptyFMI
intersectFM_C' combiner (BranchFMI split_key1 elt1 s1 left1 right1)
(BranchFMI split_key elt2 _ left right)
| isJust maybe_elt1
= mkVBalBranch split_key (combiner elt1' elt2)
(intersectFM_C' combiner lts left)
(intersectFM_C' combiner gts right)
| otherwise
= glueVBal (intersectFM_C' combiner lts left)
(intersectFM_C' combiner gts right)
where
fm1 = BranchFMI split_key1 elt1 s1 left1 right1
lts = splitLT fm1 split_key
gts = splitGT fm1 split_key
maybe_elt1 = lookupFM' fm1 split_key
Just elt1' = maybe_elt1
foldFM :: (Int -> Int -> a -> a) -> a -> FMI -> a
foldFM k z (FMI fm) = foldFM' k z fm
foldFM' :: (Int -> Int -> a -> a) -> a -> FiniteMap -> a
foldFM' _ z EmptyFMI = z
foldFM' k z (BranchFMI key elt _ fm_l fm_r)
= foldFM' k (k key elt (foldFM' k z fm_r)) fm_l
sizeFM :: FMI -> Int
sizeFM (FMI EmptyFMI) = 0
sizeFM (FMI (BranchFMI _ _ size _ _)) = size
sizeFM' :: FiniteMap -> Int
sizeFM' EmptyFMI = 0
sizeFM' (BranchFMI _ _ size _ _) = size
eqFM :: FMI -> FMI -> Bool
fm_1 `eqFM` fm_2 =
(sizeFM fm_1 == sizeFM fm_2) &&
(fmToList fm_1 == fmToList fm_2)
isEmptyFM :: FMI -> Bool
isEmptyFM fm = sizeFM fm == 0
elemFM :: Int -> FMI -> Bool
key `elemFM` fm = isJust (lookupFM fm key)
lookupFM :: FMI -> Int -> Maybe Int
lookupFM (FMI fm) key = lookupFM' fm key
lookupFM' :: FiniteMap -> Int -> Maybe Int
lookupFM' EmptyFMI _ = Nothing
lookupFM' (BranchFMI key elt _ fm_l fm_r) key_to_find
= if key_to_find < key
then lookupFM' fm_l key_to_find
else if key_to_find==key
then Just elt
else lookupFM' fm_r key_to_find
lookupWithDefaultFM :: FMI -> Int -> Int -> Int
lookupWithDefaultFM fm deflt key
= case lookupFM fm key of
Nothing -> deflt
Just elt -> elt
keyOrder :: FMI -> (Int->Int->Bool)
keyOrder (FMI _) = (<)
minFM :: FMI -> Maybe (Int,Int)
minFM = min . tree
where
min EmptyFMI = Nothing
min (BranchFMI k x _ l _) | l==EmptyFMI = Just (k,x)
| otherwise = min l
maxFM :: FMI -> Maybe (Int,Int)
maxFM = max . tree
where
max EmptyFMI = Nothing
max (BranchFMI k x _ _ r) | r==EmptyFMI = Just (k,x)
| otherwise = max r
fmToList :: FMI -> [(Int,Int)]
fmToList fm = foldFM (\ key elt rest -> (key,elt) : rest) [] fm
keysFM :: FMI -> [Int]
keysFM fm = foldFM (\ key _ rest -> key : rest) [] fm
eltsFM :: FMI -> [Int]
eltsFM fm = foldFM (\ _ elt rest -> elt : rest) [] fm
fmToListPreOrder :: FMI -> [(Int,Int)]
fmToListPreOrder (FMI fm) = pre fm []
where
pre EmptyFMI xs = xs
pre (BranchFMI k x _ l r) xs = (k,x):pre l (pre r xs)
showFM :: FMI -> String
showFM (FMI fm) = showQTerm fm
readFM :: String -> FMI
readFM s = FMI (readQTerm s)
data FMI = FMI (FiniteMap)
tree :: FMI -> FiniteMap
tree (FMI fm) = fm
data FiniteMap
= EmptyFMI
| BranchFMI Int Int
Int
(FiniteMap)
(FiniteMap)
isEmptyFM' :: FiniteMap -> Bool
isEmptyFM' fm = sizeFM' fm == 0
sIZE_RATIO :: Int
sIZE_RATIO = 5
mkBranch :: Int
-> Int -> Int
-> FiniteMap -> FiniteMap
-> FiniteMap
mkBranch _ key elt fm_l fm_r =
let result = BranchFMI key elt (unbox (1 + left_size + right_size)) fm_l fm_r
in
result
where
left_size = sizeFM' fm_l
right_size = sizeFM' fm_r
unbox :: Int -> Int
unbox x = x
mkBalBranch :: Int -> Int
-> FiniteMap -> FiniteMap
-> FiniteMap
mkBalBranch key elt fm_L fm_R
| size_l + size_r < 2
= mkBranch 1 key elt fm_L fm_R
| size_r > sIZE_RATIO * size_l
= case fm_R of
BranchFMI _ _ _ fm_rl fm_rr ->
if sizeFM' fm_rl < 2 * sizeFM' fm_rr
then single_L fm_L fm_R
else double_L fm_L fm_R
EmptyFMI -> error "FiniteMap.mkBalBranch"
| size_l > sIZE_RATIO * size_r
= case fm_L of
BranchFMI _ _ _ fm_ll fm_lr ->
if sizeFM' fm_lr < 2 * sizeFM' fm_ll
then single_R fm_L fm_R
else double_R fm_L fm_R
EmptyFMI -> error "FiniteMap.mkBalBranch"
| otherwise
= mkBranch 2 key elt fm_L fm_R
where
size_l = sizeFM' fm_L
size_r = sizeFM' fm_R
single_L fm_l (BranchFMI key_r elt_r _ fm_rl fm_rr)
= mkBranch 3 key_r elt_r (mkBranch 4 key elt fm_l fm_rl) fm_rr
single_L _ EmptyFMI = error "FiniteMap.single_L"
double_L fm_l (BranchFMI key_r elt_r _ (BranchFMI key_rl elt_rl _ fm_rll fm_rlr) fm_rr)
= mkBranch 5 key_rl elt_rl (mkBranch 6 key elt fm_l fm_rll)
(mkBranch 7 key_r elt_r fm_rlr fm_rr)
double_L _ EmptyFMI = error "FiniteMap.double_L"
double_L _ (BranchFMI _ _ _ EmptyFMI _) = error "FiniteMap.double_L"
single_R (BranchFMI key_l elt_l _ fm_ll fm_lr) fm_r
= mkBranch 8 key_l elt_l fm_ll (mkBranch 9 key elt fm_lr fm_r)
single_R EmptyFMI _ = error "FiniteMap.single_R"
double_R (BranchFMI key_l elt_l _ fm_ll (BranchFMI key_lr elt_lr _ fm_lrl fm_lrr)) fm_r
= mkBranch 10 key_lr elt_lr (mkBranch 11 key_l elt_l fm_ll fm_lrl)
(mkBranch 12 key elt fm_lrr fm_r)
double_R EmptyFMI _ = error "FiniteMap.double_R"
double_R (BranchFMI _ _ _ _ EmptyFMI) _ = error "FiniteMap.double_R"
mkVBalBranch :: Int -> Int
-> FiniteMap -> FiniteMap
-> FiniteMap
mkVBalBranch key elt EmptyFMI fm_r = addToFM' fm_r key elt
mkVBalBranch key elt (BranchFMI key_l elt_l s_l fm_ll fm_lr) EmptyFMI =
addToFM' (BranchFMI key_l elt_l s_l fm_ll fm_lr) key elt
mkVBalBranch key elt (BranchFMI key_l elt_l s_l fm_ll fm_lr)
(BranchFMI key_r elt_r s_r fm_rl fm_rr)
| sIZE_RATIO * size_l < size_r
= mkBalBranch key_r elt_r (mkVBalBranch key elt fm_l fm_rl) fm_rr
| sIZE_RATIO * size_r < size_l
= mkBalBranch key_l elt_l fm_ll (mkVBalBranch key elt fm_lr fm_r)
| otherwise
= mkBranch 13 key elt fm_l fm_r
where
fm_l = BranchFMI key_l elt_l s_l fm_ll fm_lr
fm_r = BranchFMI key_r elt_r s_r fm_rl fm_rr
size_l = sizeFM' fm_l
size_r = sizeFM' fm_r
glueBal :: FiniteMap -> FiniteMap
-> FiniteMap
glueBal fm1 fm2 =
if isEmptyFM' fm1
then fm2
else if isEmptyFM' fm2
then fm1
else
let (mid_key1, mid_elt1) = findMax fm1
(mid_key2, mid_elt2) = findMin fm2
in
if sizeFM' fm2 > sizeFM' fm1
then mkBalBranch mid_key2 mid_elt2 fm1 (deleteMin fm2)
else mkBalBranch mid_key1 mid_elt1 (deleteMax fm1) fm2
glueVBal :: FiniteMap -> FiniteMap
-> FiniteMap
glueVBal fm_l fm_r =
if isEmptyFM' fm_l
then fm_r
else if isEmptyFM' fm_r
then fm_l
else
let BranchFMI key_l elt_l _ fm_ll fm_lr = fm_l
BranchFMI key_r elt_r _ fm_rl fm_rr = fm_r
size_l = sizeFM' fm_l
size_r = sizeFM' fm_r
in
if sIZE_RATIO * size_l < size_r
then
mkBalBranch key_r elt_r (glueVBal fm_l fm_rl) fm_rr
else if sIZE_RATIO * size_r < size_l
then
mkBalBranch key_l elt_l fm_ll (glueVBal fm_lr fm_r)
else glueBal fm_l fm_r
splitLT, splitGT :: FiniteMap -> Int
-> FiniteMap
splitLT EmptyFMI _ = EmptyFMI
splitLT (BranchFMI key elt _ fm_l fm_r) split_key
= if split_key < key
then splitLT fm_l split_key
else if split_key == key
then fm_l
else mkVBalBranch key elt fm_l (splitLT fm_r split_key)
splitGT EmptyFMI _ = EmptyFMI
splitGT (BranchFMI key elt _ fm_l fm_r) split_key
= if split_key < key
then mkVBalBranch key elt (splitGT fm_l split_key) fm_r
else if split_key == key
then fm_r
else splitGT fm_r split_key
findMin :: FiniteMap -> (Int,Int)
findMin EmptyFMI = error "FiniteMap.findMin: empty map"
findMin (BranchFMI key elt _ EmptyFMI _) = (key,elt)
findMin (BranchFMI _ _ _ (BranchFMI key_l elt_l s_l fm_ll fm_lr)_) =
findMin (BranchFMI key_l elt_l s_l fm_ll fm_lr)
deleteMin :: FiniteMap -> FiniteMap
deleteMin EmptyFMI = error "FiniteMap.deleteMin: empty map"
deleteMin (BranchFMI _ _ _ EmptyFMI fm_r) = fm_r
deleteMin (BranchFMI key elt _ (BranchFMI key_l elt_l s_l fm_ll fm_lr) fm_r) =
mkBalBranch key elt (deleteMin (BranchFMI key_l elt_l s_l fm_ll fm_lr))
fm_r
findMax :: FiniteMap -> (Int,Int)
findMax EmptyFMI = error "FiniteMap.findMax: empty map"
findMax (BranchFMI key elt _ _ EmptyFMI) = (key,elt)
findMax (BranchFMI _ _ _ _ (BranchFMI key_r elt_r s_r fm_rl fm_rr)) =
findMax (BranchFMI key_r elt_r s_r fm_rl fm_rr)
deleteMax :: FiniteMap -> FiniteMap
deleteMax EmptyFMI = error "FiniteMap.deleteMax: empty map"
deleteMax (BranchFMI _ _ _ fm_l EmptyFMI) = fm_l
deleteMax (BranchFMI key elt _ fm_l (BranchFMI key_r elt_r s_r fm_rl fm_rr)) =
mkBalBranch key elt fm_l
(deleteMax (BranchFMI key_r elt_r s_r fm_rl fm_rr))
|