forked from svn2github/freearc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.hs
1052 lines (828 loc) · 39.7 KB
/
Utils.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
{-# OPTIONS_GHC -cpp -fallow-overlapping-instances -fallow-undecidable-instances -fno-monomorphism-restriction #-}
---------------------------------------------------------------------------------------------------
---- Âñïîìîãàòåëüíûå ôóíêöèè: ðàáîòà ñî ñòðîêàìè, ñïèñêàìè, ðåãóëÿðíûìè âûðàæåíèÿìè, ----
---- àëëîêàòîð ïàìÿòè. óïðîùåíèå ìàíèïóëÿöèé ñ IORef-ïåðåìåííûìè, ----
---- îïðåäåëåíèå óäîáíûõ îïåðàöèé è óïðàâëÿþùèõ ñòðóêòóð ïðîãðàììû. ----
---------------------------------------------------------------------------------------------------
module Utils (module Utils, module CompressionLib) where
import Prelude hiding (catch)
import Control.Concurrent
import Control.Exception
import Control.Monad
import Data.Array
import Data.Bits
import Data.Char
import Data.Either
import Data.IORef
import Data.List
import Data.Maybe
import Data.Word
import Debug.Trace
import Foreign.Marshal.Utils
import Foreign.Ptr
import CompressionLib (MemSize,b,kb,mb,gb,tb)
---------------------------------------------------------------------------------------------------
---- Ïðîâåðèì define's ----------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
#if !defined(FREEARC_WIN) && !defined(FREEARC_UNIX)
#error "You must define OS!"
#endif
#if !defined(FREEARC_INTEL_BYTE_ORDER) && !defined(FREEARC_MOTOROLA_BYTE_ORDER)
#error "You must define byte order!"
#endif
---------------------------------------------------------------------------------------------------
---- Ðàçíîå :) ------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
-- |Ñîáðàòü 4-áàéòîâîå ÷èñëî èç îòäåëüíûõ áàéòîâ
#if defined(FREEARC_INTEL_BYTE_ORDER)
make4byte b0 b1 b2 b3 = b0+256*(b1+256*(b2+256*b3)) :: Word32
#else
make4byte b0 b1 b2 b3 = b3+256*(b2+256*(b1+256*b0)) :: Word32
#endif
-- |Ðàçáîðùèê ÷èñåë, óêàçûâàåìûõ âî âñÿêèõ îïöèÿõ. Òîëêîâàíèå ÷èñëà îïðåäåëÿåòñÿ ñèìâîëàìè,
-- íàïèñàííûìè ïîñëå íåãî (b/k/f/...), åñëè èõ íåò - èñïîëüçóåòñÿ `default_specifier`.
-- Ðåçóëüòàò âîçâðàùàåòñÿ â âèäå ïàðû, âòîðîé ýëåìåíò â êîòîðîé - `b`, åñëè ðåçóëüòàò âûðàæåí â áàéòàõ,
-- èëè äðóãîé ñèìâîë, çàïèñàííûé ïîñëå ÷èñëà, èëè ïåðåäàííûé êàê `default_specifier`.
parseNumber num default_specifier =
case (span isDigit$ strLower$ num++[default_specifier]) of
(digits, 'b':_) -> (readI digits , 'b')
(digits, 'k':_) -> (readI digits * kb, 'b')
(digits, 'm':_) -> (readI digits * mb, 'b')
(digits, 'g':_) -> (readI digits * gb, 'b')
(digits, 't':_) -> (readI digits * tb, 'b')
(digits, '^':_) -> (2 ^ readI digits , 'b')
(digits, c :_) -> (readI digits , c )
-- |Ðàñøèôðîâàòü çàïèñü ðàçìåðà, ïî óìîë÷àíèþ - â áàéòàõ
parseSize memstr =
case (parseNumber memstr 'b') of
(bytes, 'b') -> bytes
_ -> error$ memstr++" - unrecognized size specifier"
-- |Ðàñøèôðîâàòü çàïèñü îáú¸ìà ïàìÿòè: "512b", "32k", "8m" è òàê äàëåå. "24" îçíà÷àåò 24mb
parseMem memstr =
case (parseNumber memstr 'm') of
(bytes, 'b') -> clipToMaxMemSize bytes
_ -> error$ memstr++" - unrecognized size specifier"
-- |Àíàëîãè÷íî parseMem, íî ñ äîáàâëåíèåì ïîääåðæêè çàïèñè â âèäå 75%/75p (îò îáùåãî îáú¸ìà ïàìÿòè)
parseMemWithPercents memory memstr =
case (parseNumber memstr 'm') of
(bytes, 'b') -> clipToMaxMemSize$ bytes
(percents, c) | c `elem` "%p"
-> clipToMaxMemSize$ (memory * percents) `div` 100
_ -> error$ memstr++" - unrecognized size specifier"
-- Äîëæíà óñåêàòü ê ìàêñèìàëüíîìó ïîçâîëåííîìó â MemSize ÷èñëó èëè ìîæåò âîîáùå âûäàâàòü îøèáêó?
clipToMaxMemSize x | x < i(maxBound::MemSize) = i x
| otherwise = i(maxBound::MemSize)
readI = foldl f 0
where f m c | isDigit c = fromIntegral (ord c - ord '0') + (m * 10)
| otherwise = error ("Non-digit "++[c]++" in readI")
readInt :: String -> Int
readInt = readI
readSignedInt ('-':xs) = - readInt xs
readSignedInt xs = readInt xs
isSignedInt = all isDigit.tryToSkip "-"
lb :: Integral a => a -> Int
lb 0 = 0
lb 1 = 0
lb n = 1 + lb (n `div` 2)
{-# NOINLINE parseNumber #-}
{-# NOINLINE parseSize #-}
{-# NOINLINE parseMem #-}
{-# NOINLINE parseMemWithPercents #-}
{-# NOINLINE readI #-}
{-# NOINLINE readInt #-}
-- |Íåêîòîðûå îïåðàöèè äëÿ áîëåå èçÿùíîé çàïèñè ïðîãðàìì
infixl 9 .$
infixl 1 >>==, ==<<, =<<., .>>=, .>>, .>>==, ==<<.
a.$b = b a -- âàðèàíò $ ñ ïåðåâ¸ðíóòûì ïîðÿäêîì àðãóìåíòîâ
a>>==b = a >>= return.b -- âàðèàíò >>=, â êîòîðîì âòîðîé àðãóìåíò íóæäàåòcÿ â ëèôòèíãå
a==<<b = return.a =<< b -- âàðèàíò =<<, â êîòîðîì ïåðâûé àðãóìåíò íóæäàåòcÿ â ëèôòèíãå
(a=<<.b) c = a =<< b c -- âàðèàíò =<< äëÿ ïðèìåíåíèÿ â mapM è òîìó ïîäîáíûõ ìåñòàõ
(a.>>=b) c = a c >>= b -- âàðèàíò >>= äëÿ ïðèìåíåíèÿ â mapM è òîìó ïîäîáíûõ ìåñòàõ
(a.>>b) c = a c >> b -- âàðèàíò >> äëÿ ïðèìåíåíèÿ â mapM è òîìó ïîäîáíûõ ìåñòàõ
(a==<<.b) c = return.a =<< b c -- âàðèàíò ==<< äëÿ ïðèìåíåíèÿ â mapM è òîìó ïîäîáíûõ ìåñòàõ
(a.>>==b) c = a c >>= return.b -- âàðèàíò >>== äëÿ ïðèìåíåíèÿ â mapM è òîìó ïîäîáíûõ ìåñòàõ
-- Òèïû äàííûõ, èìåþùèå çíà÷åíèÿ ïî óìîë÷àíèþ
class Defaults a where defaultValue :: a
instance Defaults () where defaultValue = ()
instance Defaults Bool where defaultValue = False
instance Defaults [a] where defaultValue = []
instance Defaults (a->a) where defaultValue = id
instance Defaults (Maybe a) where defaultValue = Nothing
instance Defaults (a->IO a) where defaultValue = return
instance Defaults a => Defaults (IO a) where defaultValue = return defaultValue
instance Defaults Int where defaultValue = 0
instance Defaults Integer where defaultValue = 0
instance Defaults Double where defaultValue = 0
class TestDefaultValue a where isDefaultValue :: a -> Bool
instance TestDefaultValue Bool where isDefaultValue = not
instance TestDefaultValue [a] where isDefaultValue = null
instance TestDefaultValue Int where isDefaultValue = (==0)
instance TestDefaultValue Integer where isDefaultValue = (==0)
instance TestDefaultValue Double where isDefaultValue = (==0)
infixr 3 &&&
infixr 2 |||
-- |Äàòü âåëè÷èíå çíà÷åíèå ïî óìîë÷àíèþ
a ||| b | isDefaultValue a = b
| otherwise = a
-- |Âîçâðàòèòü âòîðîå çíà÷åíèå, åñëè ïåðâîå íå ÿâëÿåòñÿ çíà÷åíèåì ïî óìîë÷àíèþ
a &&& b | isDefaultValue a = defaultValue
| otherwise = b
-- |Âûïîëíèòü b åñëè a (óñëîâíîå âûïîëíåíèå ñ óñëîâèåì â êîíöå ñòðîêè)
infixr 0 `on`
on b a | isDefaultValue a = return ()
| otherwise = b >> return ()
-- |Ïðèìåíèòü ôóíêöèþ f ê ñïèñêó òîëüêî åñëè îí íå ïóñòîé
unlessNull f xs = xs &&& f xs
-- |Ìîíàäè÷åñêèé âàðèàíò concatMap
concatMapM :: Monad io => (a -> io [b]) -> [a] -> io [b]
concatMapM f x = mapM f x >>== concat
-- |Óñëîâíîå âûïîëíåíèå
whenM cond action = do
allow <- cond
when allow
action
unlessM = whenM . liftM not
-- |Âûïîëíèòü `action` íàä çíà÷åíèåì, âîçâðàù¸ííûì `x`, åñëè îíî íå Nothing
whenJustM x action = x >>= (`whenJust` action)
whenJustM_ x action = x >>= (`whenJust_` action)
whenJust x action = x .$ maybe (return Nothing) (action .>>== Just)
whenJust_ x action = x .$ maybe (return ()) (action .>> return ())
-- |Âûïîëíèòü `action` íàä çíà÷åíèåì, âîçâðàù¸ííûì `x`, åñëè îíî "Right _"
whenRightM_ x action = x >>= either doNothing (action .>> return ())
-- |Âûïîëíèòü onLeft/onRight íàä çíà÷åíèåì, âîçâðàù¸ííûì `x`
eitherM_ x onLeft onRight = x >>= either (onLeft .>> return ())
(onRight .>> return ())
-- |Âûïîëíèòü äëÿ êàæäîãî ýëåìåíòà èç ñïèñêà è âîâçðàòèòü ðåçóëüòàò êàê ñïèñîê
foreach = flip mapM
-- |Âûïîëíèòü äëÿ êàæäîãî ýëåìåíòà èç ñïèñêà
for = flip mapM_
-- |Óäîáíûé ñïîñîá çàïèñàòü ñíà÷àëà òî, ÷òî äîëæíî îáÿçàòåëüíî áûòü âûïîëíåíî â êîíöå :)
doFinally = flip finally
-- |Âûïîëíèòü onError ïðè îøèáêå â acquire, è action â ïðîòèâíîì ñëó÷àå
handleErrors onError acquire action = do
x <- try acquire
case x of
Left err -> onError
Right res -> action res
-- |Çàïèñàòü â íà÷àëå òî, ÷òî íóæíî âûïîëíèòü â êîíöå
atExit a b = (b>>a)
-- |Âûïîëíèòü äåéñòâèå òîëüêî îäèí ðàç, ïðè var=True
once var action = do whenM (val var) action; var =: False
init_once = ref True
-- |Çàãëóøêè íà ìåñòî êîìàíä, êîòîðûå íå äîëæíû íè÷åãî âûïîëíÿòü
doNothing0 = return ()
doNothing a = return ()
doNothing2 a b = return ()
doNothing3 a b c = return ()
-- |Èãíîðèðîâàòü èñêëþ÷åíè
ignoreErrors = handle doNothing
-- |Ñîçäàòü íîâûé Channel è çàïèñàòü â íåãî íà÷àëüíûé ñïèñîê çíà÷åíèé
newChanWith xs = do c <- newChan
writeList2Chan c xs
return c
-- |Êîíñòàíòíûå ôóíêöèè
const2 x _ _ = x
const3 x _ _ _ = x
const4 x _ _ _ _ = x
-- |Íàôèãà âàì ýòîò ThreadId??
forkIO_ action = forkIO action >> return ()
-- |Ïîâòîðÿòü áåñêîíå÷íî
foreverM action = do
action
foreverM action
-- |Óïðàâëÿþùàÿ ñòðóêòóðà, àíàëîãè÷íàÿ öèêëó 'while' â îáû÷íûõ ÿçûêàõ
repeat_while inp cond out = do
x <- inp
if (cond x)
then do out x
repeat_while inp cond out
else return x
-- |Óïðàâëÿþùàÿ ñòðóêòóðà, àíàëîãè÷íàÿ repeat-until â Ïàñêàëå
repeat_until action = do
done <- action
when (not done) $ do
repeat_until action
-- |Óïðàâëÿþùàÿ ñòðóêòóðà, ðàçáèâàþùàÿ âûïîëíåíèå îïåðàöèè ðàçìåðîì size
-- íà îòäåëüíûå îïåðàöèè ðàçìåðîì íå áîëåå chunk êàæäà
doChunks size chunk action =
case size of
0 -> return ()
_ -> do let n = minI size chunk
action (fromIntegral n)
doChunks (size-n) chunk action
-- |Âûïîëíèòü `action` íàä x, çàòåì íàä êàæäûì ýëåìåíòîì ñïèñêà, âîçâðàù¸ííîãî èç `action`, è òàê äàëåå ðåêóðñèâíî
recursiveM action x = action x >>= mapM_ (recursiveM action)
-- |Âûïîëíèòü ðåêóðñèâíî, åñëè âåðíî óñëîâèå `cond`, è îäíîêðàòíî â ïðîòèâíîì ñëó÷àå
recursiveIfM cond action x = if cond then recursiveM action x else (action x >> return ())
-- |Âûïîëíÿåò äåéñòâèå `action` íàä ýëåìåíòàìè ñïèñêà `list` ïîî÷åð¸äíî, âîçâðàùàÿ
-- ñïèñîê ðåçóëüòàòîâ, âîçâðàù¸ííûõ `action` - â îáùåì, àíàëîãè÷íî mapM.
-- Íî äîïîëíèòåëüíî ê ýòîìó ïðîâåðÿåò îáðàáîòàííûå äàííûå ïî êðèòåðèþ `crit_f` è âûõîäèò èç öèêëà,
-- åñëè ýòîò êðèòåðèé óäîâëåòâîð¸í. Ïîýòîìó äîïîëíèòåëüíî âîçâðàùàåòñÿ ñïèñîê íåîáðàáîòàííûõ
-- çíà÷åíèé èç `list`
mapMConditional (init,map_f,sum_f,crit_f) action list = do
let go [] ys summary = return (reverse ys, []) -- çàâåðøåíî èç-çà èñ÷åðïàíèÿ ñïèñêà
let go (x:xs) ys summary = do
y <- action x
let summary = sum_f summary (map_f y)
if (crit_f summary)
then return (reverse$ y:ys, xs) -- çàâåðøåíî ñîãëàñíî êðèòåðèþ
else go xs (y:ys) summary
go list [] init
-- |Execute action with background computation
withThread thread = bracket (forkIO thread) killThread . const
-- |Âûïîëíèòü äåéñòâèå â äðóãîì òðåäå è âîçâðàòèòü êîíå÷íûé ðåçóëüòàò
bg action = do
resultVar <- newEmptyMVar
forkIO (action >>= putMVar resultVar)
takeMVar resultVar
#ifdef FREEARC_GUI
isGUI = True
#else
isGUI = False
#endif
{-# NOINLINE foreverM #-}
{-# NOINLINE repeat_while #-}
{-# NOINLINE repeat_until #-}
{-# NOINLINE mapMConditional #-}
{-# NOINLINE bg #-}
-- |Îòôèëüòðîâàòü ñïèñîê ñ ïîìîùüþ ìîíàäè÷åñêîãî (âûïîëíÿåìîãî) ïðåäèêàòà
filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
filterM p = go []
where go accum [] = return$ reverse accum
go accum (x:xs) = p x >>= bool (go accum xs)
(go (x:accum) xs)
-- |mapMaybe, ïåðåíåñ¸ííàÿ â êëàññ Monad
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM f = go []
where go accum [] = return$ reverse accum
go accum (x:xs) = f x >>= maybe ( go accum xs)
(\r -> go (r:accum) xs)
-- |@firstJust@ takes a list of @Maybes@ and returns the
-- first @Just@ if there is one, or @Nothing@ otherwise.
firstJust :: [Maybe a] -> Maybe a
firstJust [] = Nothing
firstJust (Just x : ms) = Just x
firstJust (Nothing : ms) = firstJust ms
-- |Âåðíóòü ïåðâûé óñïåøíûé (Just) ðåçóëüòàò ïðèìåíåíèÿ f ê ñïèñêó èëè Nothing
firstMaybe :: (a -> Maybe b) -> [a] -> Maybe b
firstMaybe f = firstJust . map f
-- |Çàìåíèòü Nothing íà çíà÷åíèå ïî óìîë÷àíèþ
defaultVal = flip fromMaybe
-- |Çàìåíèòü Nothing íà çíà÷åíèå ïî óìîë÷àíèþ - äëÿ èìïåðàòèâíîé îïåðàöèè
defaultValM = liftM2 defaultVal
-- |Âûáðàòü îäíî èç äâóõ çíà÷åíèé â çàâèñèìîñòè îò ïîñëåäíåãî àðãóìåíòà
bool onFalse onTrue False = onFalse
bool onFalse onTrue True = onTrue
-- |if áåç ñèíò. îâåðõåäà
iif True onTrue onFalse = onTrue
iif False onTrue onFalse = onFalse
-- Ïðèìåíèòü ê ñïèñêó îäíó èç äâóõ ôóíêöèé â çàâèñèìîñòè îò òîãî, ïóñò ëè îí
list onNotNull onNull [] = onNull
list onNotNull onNull xs = onNotNull xs
-- |Âîçâðàòèòü True, åñëè çíà÷åíèå - íå Nothing
maybe2bool (Just _) = True
maybe2bool Nothing = False
-- |Ïðîâåðêà íà Left
isLeft (Left _) = True
isLeft _ = False
-- |Óäàëèòü ýëåìåíòû, îòâå÷àþùèå çàäàííîìó ïðåäèêàòó
deleteIf p = filter (not.p)
-- |Óäàëèòü ýëåìåíòû, ñîîòâåòñòâóþùèå ëþáîìó èç ñïèñêà ïðåäèêàòîâ
deleteIfs = deleteIf.anyf
-- |Îáíîâëåíèå lookup-ñïèñêà
update list a@(key,value) = a : [x | x@(k,v)<-list, k/=key]
-- |Çàìåíà çíà÷åíèé ïî ñïèñêó
changeTo list value = lookup value list `defaultVal` value
-- |Íàïå÷àòàòü è âîçâðàòèòü îäíî çíà÷åíèå
trace2 s = trace (show s) s
-- |Evaluate list elements
evalList (x:xs) = x `seq` evalList xs
evalList [] = ()
{-
-- Cale Gibbard
A useful little higher order function. Some examples of use:
swing map :: forall a b. [a -> b] -> a -> [b]
swing any :: forall a. [a -> Bool] -> a -> Bool
swing foldr :: forall a b. b -> a -> [a -> b -> b] -> b
swing zipWith :: forall a b c. [a -> b -> c] -> a -> [b] -> [c]
swing find :: forall a. [a -> Bool] -> a -> Maybe (a -> Bool) -- applies each of the predicates to the given value, returning the first predicate which succeeds, if any
swing partition :: forall a. [a -> Bool] -> a -> ([a -> Bool], [a -> Bool])
-}
swing :: (((a -> b) -> b) -> c -> d) -> c -> a -> d
swing f = flip (f . flip ($))
-- |Map on functions instead of its' arguments!
map_functions [] x = []
map_functions (f:fs) x = f x : map_functions fs x
-- |Ïðîâåðèòü, ÷òî âñå ôóíêöèè èç ñïèñêà äàþò True íà (îïóùåííîì çäåñü) àðãóìåíòå. Ýôôåêòèâíåé, ÷åì swing all
allf x = all_functions x
all_functions [] = const True
all_functions [f] = f
all_functions fs = and . map_functions fs
-- |Ïðîâåðèòü, ÷òî õîòü îäíà ôóíêöèÿ èç ñïèñêà äà¸ò True íà (îïóùåííîì çäåñü) àðãóìåíòå. Ýôôåêòèâíåé, ÷åì swing any
anyf x = any_function x
any_function [] = const False
any_function [f] = f
any_function fs = or . map_functions fs
-- |Ïðèìåíèòü ê àðãóìåíòó ïîñëåäîâàòåëüíî âñå ôóíêöèè èç ñïèñêà
applyAll [] x = x
applyAll (f:fs) x = applyAll fs (f x)
(f>>>g) x = g(f x)
---------------------------------------------------------------------------------------------------
---- Îïåðàöèè íàä ñòðîêàìè ------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
-- |Ðàçáèòü ñòðîêó íà äâå ïîäñòðîêè, ðàçäåë¸ííûå çàäàííûì ñèìâîëîì
split2 :: (Eq a) => a -> [a] -> ([a],[a])
split2 c s = (chunk, drop 1 rest)
where (chunk, rest) = break (==c) s
-- |Ñîåäèíèòü èõ íàçàä êàê áûëî :)
join2 :: [a] -> ([a],[a]) -> [a]
join2 between (a,b) = a++between++b
-- |Ðàçáèòü ñòðîêó íà ïîäñòðîêè, ðàçäåë¸ííûå çàäàííûì ñèìâîëîì
split :: (Eq a) => a -> [a] -> [[a]]
split c s =
let (chunk, rest) = break (==c) s
in case rest of [] -> [chunk]
_:rest -> chunk : split c rest
-- |Ñîåäèíèòü ñïèñîê ñòðîê â åäèíûé òåêñò ñ ðàçäåëèòåëåì: "one, two, three"
joinWith :: [a] -> [[a]] -> [a]
joinWith x = concat . intersperse x
-- |Ñîåäèíèòü ñïèñîê ñòðîê â åäèíûé òåêñò, èñïîëüçóÿ äâà ðàçíûõ ðàçäåëèòåëÿ:
-- joinWith2 ", " " and " ["one","two","three","four"] --> "one, two, three and four"
joinWith2 :: [a] -> [a] -> [[a]] -> [a]
joinWith2 a b [] = []
joinWith2 a b [x] = x
joinWith2 a b list = joinWith a (init list) ++ b ++ last list
-- |Ïîñòàâèòü x ìåæäó s1 è s2, åñëè îáå ñòðîêè íåïóñòûå
between s1 x [] = s1
between [] x s2 = s2
between s1 x s2 = s1++x++s2
-- |Äîáàâèòü äâîéíûå êàâû÷êè âîêðóã ñòðîêè
quote :: String -> String
quote str = "\"" ++ str ++ "\""
-- |Óäàëèòü äâîéíûå êàâû÷êè âîêðóã ñòðîêè (åñëè îíè åñòü)
unquote :: String -> String
unquote ('"':str) | str>"" && x=='"' = xs where (x:xs) = reverse str
unquote str = str
contains = flip elem
-- |Óäàëèòü n ýëåìåíòîâ â êîíöå ñïñèêà
dropEnd n = reverse . drop n . reverse
-- |Èñòèíà, åñëè `s` ñîäåðæèò õîòÿ áû îäèí èç ýëåìåíòîâ ìíîæåñòâà `set`
s `contains_one_of` set = any (`elem` set) s
-- |Ïîñëåäíèå n ýëåìåíòîâ
n `lastElems` xs = drop (length xs - n) xs
-- |Çàìåíèòü n'é ýëåìåíò (ñ÷èòàÿ ñ 0) â ñïèñêå `xs` íà `x`
replaceAt n x xs = hd ++ x : drop 1 tl
where (hd,tl) = splitAt n xs
-- |Èçìåíèòü n'é ýëåìåíò (ñ÷èòàÿ ñ 0) â ñïèñêå `xs` ñ `x` íà `f x`
updateAt n f xs = hd ++ f x : tl
where (hd,x:tl) = splitAt n xs
-- |Çàìåíèòü â ñïèñêå âñå âõîæäåíèÿ ýëåìåíòà 'from' íà 'to'
replace from to = map (\x -> if x==from then to else x)
-- |Åñëè ïåðâàÿ ñòðîêà ÿâëÿåòñÿ ïðåôèêñîì âòîðîé - âîçâðàòèòü îñòàòîê âòîðîé ñòðîêè, èíà÷å Nothing
startFrom (x:xs) (y:ys) | x==y = startFrom xs ys
startFrom [] str = Just str
startFrom _ _ = Nothing
-- |Ïðîâåðêà, ÷òî ñòðîêà íà÷èíàåòñÿ èëè çàêàí÷èâàåòñÿ çàäàííûìè ñèìâîëàìè
beginWith s = isJust . startFrom s
endWith s = beginWith (reverse s) . reverse
-- |Ïîïûòàåìñÿ óäàëèòü ñòðîêó substr â íà÷àëå str
tryToSkip substr str = (startFrom substr str) `defaultVal` str
-- |Ïîïûòàòüñÿ óäàëèòü ñòðîêó substr â êîíöå str
tryToSkipAtEnd substr str = reverse (tryToSkip (reverse substr) (reverse str))
-- | The 'isInfixOf' function takes two lists and returns 'True'
-- if the second list is contained, wholy and intact,
-- anywhere within the first.
substr haystack needle = any (needle `isPrefixOf`) (tails haystack)
-- |Ñïèñîê ïîçèöèé ïîäñòðîêè â ñòðîêå
strPositions haystack needle = elemIndices True$ map (needle `isPrefixOf`) (tails haystack)
-- |Çàìåíèòü â ñòðîêå `s` âñå âõîæäåíèÿ `from` íà `to`
replaceAll from to = repl
where repl s | Just remainder <- startFrom from s = to ++ repl remainder
repl (c:cs) = c : repl cs
repl [] = []
-- |Çàìåíèòü %1 íà çàäàííóþ ñòðîêó
format msg s = replaceAll "%1" s msg
-- |Çàìåíèòü %1..%9 íà çàäàííûå ñòðîêè
formatn msg s = go msg
where go ('%':d:rest) | isDigit d = (s !! (digitToInt d-1)) ++ go rest
go (x:rest) = x : go rest
go "" = ""
-- |Çàìåíèòü â ñòðîêå `s` ïðåôèêñ `from` íà `to`
replaceAtStart from to s =
case startFrom from s of
Just remainder -> to ++ remainder
Nothing -> s
-- |Çàìåíèòü â ñòðîêå `s` ñóôôèêñ `from` íà `to`
replaceAtEnd from to s =
case startFrom (reverse from) (reverse s) of
Just remainder -> reverse remainder ++ to
Nothing -> s
-- |Âåðíóòü øåñòíàäöàòåðè÷íóþ çàïèñü ñòðîêè ñèìâîëîâ ñ êîäàìè <=255
encode16 (c:cs) | n<256 = [intToDigit(n `div` 16), intToDigit(n `mod` 16)] ++ encode16 cs
where n = ord c
encode16 "" = ""
-- |Äåêîäèðîâàòü øåñòíàäöàòåðè÷íóþ çàïèñü ñòðîêè ñèìâîëîâ ñ êîäàìè <=255
decode16 (c1:c2:cs) = chr(digitToInt c1 * 16 + digitToInt c2) : decode16 cs
decode16 "" = ""
-- |Âçÿòü ïåðâûõ n ýëåìåíòîâ ñïèñêà è äîáàâèòü ê íèì more äëÿ èíäèêàöèè òîãî, ÷òî ÷òî-òî áûëî îïóùåíî
takeSome n more s | (y>[]) = x ++ more
| otherwise = x
where (x,y) = splitAt n s
-- |Âûðîâíÿòü ñòðîêó âëåâî/âïðàâî, äîïîëíèâ å¸ äî çàäàííîé øèðèíû ïðîáåëàìè èëè ÷åì-íèáóäü åù¸
right_fill c n s = s ++ replicate (n-length s) c
left_fill c n s = replicate (n-length s) c ++ s
left_justify = right_fill ' '
right_justify = left_fill ' '
-- Óäàëèòü ïðîáåëû â íà÷àëå/êîíöå ñòðîêè èëè ïî îáîèì ñòîðîíàì
trimLeft = dropWhile (==' ')
trimRight = reverse.trimLeft.reverse
trim = trimLeft.trimRight
-- |Ïåðåâåñòè ñòðîêó â íèæíèé ðåãèñòð
strLower = map toLower
-- |Ñðàâíèòü äâå ñòðîêè, èãíîðèðóÿ ðåãèñòð
strLowerEq a b = strLower a == strLower b
-- |break íà÷èíàÿ ñî âòîðîãî ýëåìåíòà
break1 f (x:xs) = mapFst (x:) (break f xs)
-- |Âîçâðàòèòü çíà÷åíèå ïî óìîë÷àíèþ âìåñòî ãîëîâû ñïèñêà, åñëè îí ïóñò
head1 [] = defaultValue
head1 xs = head xs
-- Àíàëîã tail, ñïîêîéíî ðåàãèðóþùèé íà ïóñòûå ñïèñêè
tail1 [] = []
tail1 xs = tail xs
-- Àíàëîã init, ñïîêîéíî ðåàãèðóþùèé íà ïóñòûå ñïèñêè
init1 [] = []
init1 xs = init xs
-- Àíàëîã last, ñïîêîéíî ðåàãèðóþùèé íà ïóñòûå ñïèñêè
last1 [] = defaultValue
last1 xs = last xs
-- |Map various parts of list
mapHead f [] = []
mapHead f (x:xs) = f x : xs
mapTail f [] = []
mapTail f (x:xs) = x : map f xs
mapInit f [] = []
mapInit f xs = map f (init xs) : last xs
mapLast f [] = []
mapLast f xs = init xs ++ [f (last xs)]
{-# NOINLINE replaceAll #-}
{-# NOINLINE replaceAtEnd #-}
---------------------------------------------------------------------------------------------------
---- Îïåðàöèè íàä ñïèñêàìè ------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
-- |Sort list by function result (use Schwarznegian transform)
sortOn f = map snd . sortOn' fst . map (keyval f)
-- |Sort list by function result (don't use Schwarznegian transform!)
sortOn' f = sortBy (map2cmp f)
-- |Group list by function result
groupOn f = groupBy (map2eq f)
-- |Sort and Group list by function result
sort_and_groupOn f = groupOn f . sortOn f
sort_and_groupOn' f = groupOn f . sortOn' f
-- |Ñãðóïïèðîâàòü âñå ýëåìåíòû (a.b) ñ îäèíàêîâûì çíà÷åíèåì 'a'
groupFst :: (Ord a) => [(a,b)] -> [(a,[b])]
groupFst = map (\xs -> (fst (head xs), map snd xs)) . sort_and_groupOn fst
-- |Óäàëÿåò äóáëèêàòû èç ñïèñêà
removeDups = removeDupsOn id
-- |Îñòàâëÿåò òîëüêî ïî îäíîìó ýëåìåíòó èç êàæäîé ãðóïïû ñ îäèíàêîâûì çíà÷åíèåì f
removeDupsOn f = map head . sort_and_groupOn f
-- |Ïðîâåðèòü, ÷òî âñå ïîñëåäîâàòåëüíûå çíà÷åíèÿ â ñïèñêå óäîâëåòâîðÿþò çàäàííîìó ñîîòíîøåíèþ
isAll f [] = True
isAll f [x] = True
isAll f (x:y:ys) = f x y && isAll f (y:ys)
-- |Ïðîâåðèòü, ÷òî õîòÿ áû äâà êàêèõ-íèáóäü ïîñëåäîâàòåëüíûõ çíà÷åíèÿ â ñïèñêå óäîâëåòâîðÿþò çàäàííîìó ñîîòíîøåíèþ
isAny f [] = False
isAny f [x] = False
isAny f (x:y:ys) = f x y || isAny f (y:ys)
-- |Check that list is sorted by given field/critery
isSortedOn f = isAll (<=) . map f
-- |Check that all elements in list are equal by given field/critery
isEqOn f = isAll (==) . map f
-- |Find maximum element by given comparison critery
maxOn f (x:xs) = go x xs
where go x [] = x
go x (y:ys) | f x > f y = go x ys
| otherwise = go y ys
-- |Merge two lists, sorted by `cmp`, in one sorted list
merge :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
merge cmp xs [] = xs
merge cmp [] ys = ys
merge cmp (x:xs) (y:ys)
= case x `cmp` y of
GT -> y : merge cmp (x:xs) ys
_ -> x : merge cmp xs (y:ys)
-- |Ðàçáèòü ñïèñîê íà `numGroups` ïîäñïèñêîâ â ñîîòâåòñòâèè ñî çíà÷åíèåì, âîçâðàùàåìûì `crit_f`
partitionList numGroups crit_f list =
elems $ accumArray (flip (:)) [] (0, numGroups-1) (map (keyval crit_f) (reverse list))
-- partitionList numGroups crit_f list =
-- let xs = map (keyval crit_f) list
-- go 0 [] all = all
-- go n list prev = let (this, next) = partition (\(a,b) -> a==n-1) list
-- in go (n-1) next (map snd this:prev)
-- in go numGroups xs []
-- |Ðàçáèòü ñïèñîê íà ãðóïïû â ñîîòâåòñòâèè ñ ïðåäèêàòàìè èç ñïèñêà `groups`:
-- splitList [(=='a'), (=='c')] 2 "cassa" -> ["aa","c","ss"]
--
splitList groups default_group filelist =
let go [] filelist sorted = replaceAt default_group filelist (reverse sorted)
go (group:groups) filelist sorted =
let (found, notfound) = partition group filelist
in go groups notfound (found:sorted)
in go groups filelist []
-- |Íàéòè íîìåð ïåðâîãî ïðåäèêàòà èç ñïèñêà `groups`, êîòîðîìó óäîâëåòâîðÿåò çíà÷åíèå `value`
findGroup groups default_group value = (findIndex ($ value) groups) `defaultVal` default_group
-- Utility functions for list operations
keyval f x = (f x, x) -- |Return pair containing computed key and original value
map2cmp f x y = (f x) `compare` (f y) -- |Converts "key_func" to "compare_func"
map2eq f x y = (f x) == (f y) -- |Converts "key_func" to "eq_func"
-- |Ðåêóðñèâíàÿ îáðàáîòêà ñïèñêà
recursive :: ([a]->(b,[a])) -> [a] -> [b]
recursive f list = list &&& (x:recursive f xs) where (x,xs) = f list
-- |Ðàçáèòü ñïèñîê íà ïîäñïèñêè, äëèíà êîòîðûõ îïðåäåëÿåòñÿ âûçîâîì ôóíêöèè `len_f` íà îñòàòêå ñïèñêà
splitByLen :: ([a]->Int) -> [a] -> [[a]]
splitByLen len_f = recursive (\xs -> splitAt (len_f xs) xs)
-- |Ýòà ôóíêöèÿ ïîëó÷àåò ñïèñîê äëèí ïîäñïèñêîâ è ðàçáèâàåò `xs` â ñîîòâåòñòâèè ñ íèì
splitByLens (len:lens) list = (x:splitByLens lens xs) where (x,xs) = splitAt len list
splitByLens [] [] = []
-- |Âîçâðàùàåò äëèíó íà÷àëüíîãî ñåãìåíòà ñïèñêà, óäîâëåòâîðÿþùåãî êîìáèíèðîâàííîìó óñëîâèþ,
-- íàïðèìåð "groupLen (fiSize) (+) (<16*mb) files" âîçâðàùàåò äëèíó íà÷àëüíîãî ñåãìåíòà ñïèñêà,
-- ñîäåðæàùóþ ôàéëû ñóììàðíûì îáú¸ìîì íå áîëåå 16 ìåãàáàéò
groupLen mapper combinator tester = length . takeWhile tester . scanl1 combinator . map mapper
-- |Îáúåäèíèòü ðåçóëüòàòû span è break: spanBreak isDigit "100a10b2c" = ("100a", "10b2c")
spanBreak crit xs = let (s1,tail1) = span crit xs
(s2,tail2) = break crit tail1
in (s1++s2, tail2)
-- |Ðàçáèòü ñïèñîê íà ãðóïïû, çàãîëîâêè êîòîðûõ - ýëåìåíòû, îòâå÷àþùèå êðèòåðèþ 'crit'
makeGroups :: (a -> Bool) -> [a] -> [[a]]
makeGroups crit [] = []
makeGroups crit (x:xs) = (x:ys) : makeGroups crit zs
where (ys,zs) = break crit xs
-- |Ðàçáèòü ñïèñîê íà ãðóïïû, ðàçäåë¸ííûå ýëåìåíòàìè, îòâå÷àþùèìè êðèòåðèþ 'crit':
-- splitOn even [1,2,4,8,3,5,7] == [[1],[2],[4],[8],[3,5,7]]
splitOn crit [] = []
splitOn crit xs = (not(null ys) &&& (ys :))
(not(null zs) &&& ([head zs] : splitOn crit (tail zs)))
where (ys,zs) = break crit xs
-- |Óäàëèòü â ñïèñêå äóáëèêàòû ïî çàäàííîìó êðèòåðèþ. O(n^2), çàòî ñîõðàíÿåò ïîðÿäîê ýëåìåíòîâ â ñïèñêå
keepOnlyFirstOn f [] = []
keepOnlyFirstOn f (x:xs) = x : keepOnlyFirstOn f (filter (\a -> f x /= f a) xs)
-- |Îñòàâèòü â ñïèñêå òîëüêî ïîñëåäíèé èç äóáëèêàòîâ ïî çàäàííîìó êðèòåðèþ
keepOnlyLastOn f = reverse . keepOnlyFirstOn f . reverse
-- |Óäàëèòü ýëåìåíòû ñ çàäàííûìè íîìåðàìè èç ñïèñêà
deleteElems = go 0
where go n xs [] = xs -- Óäàëÿòü áîëüøå íå÷åãî
go n (x:xs) iis@(i:is) | n<i = x:go (n+1) xs iis -- ìû åù¸ íå äîøëè äî i-ãî ýëåìåíòà
| n==i = go (n+1) xs is -- äîøëè - óäàëÿåì!
-- |Ïðåâðàùàåò âîçðàñòàþùèé ñïèñîê ÷èñåë â ñïèñîê äèàïàçîíîâ:
-- 1,2,3,10,21,22 -> (1,3),(10,10),(21,22)
makeRanges (x:y:zs) | x+1==y = makeRanges1 x (y:zs)
| otherwise = (x,x) : makeRanges (y:zs)
makeRanges [x] = [(x,x)]
makeRanges [] = []
-- Âñïîìîãàòåëüíîå îïðåäåëåíèå äëÿ makeRanges
makeRanges1 start (x:y:zs) | x+1==y = makeRanges1 start (y:zs)
| otherwise = (start,x) : makeRanges (y:zs)
makeRanges1 start [x] = [(start,x)]
{-# NOINLINE partitionList #-}
{-# NOINLINE splitList #-}
---------------------------------------------------------------------------------------------------
---- Îïåðàöèè ñ ìàññèâàìè -------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
-- |Ïðåâðàòèòü ñïèñîê â 0-based array
listArray0 list = listArray (0,length(list)-1) list
-- |Íàéòè ìèíèì. è ìàêñ. èíäåêñû â ñïèñêå ïàð è ñîçäàòü èç íèõ ìàññèâ,
populateArray defaultValue castValue pairs =
accumArray (\a b -> castValue b) defaultValue (minimum indexes, maximum indexes) pairs
where indexes = map fst pairs
---------------------------------------------------------------------------------------------------
---- Îïåðàöèè ñ tuples ----------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
-- Îïåðàöèè íàä tuple/2
mapFst f (a,b) = (f a, b)
mapSnd f (a,b) = ( a, f b)
mapFstSnd f (a,b) = (f a, f b)
map2 (f,g) a = (f a, g a)
map5 (f1,f2,f3,f4,f5) a = (f1 a,f2 a,f3 a,f4 a,f5 a)
mapFsts = map . mapFst
mapSnds = map . mapSnd
map2s = map . map2
-- |Ñëèòü âòîðûå ýëåìåíòû ïàð â ñïèñêå è îñòàâèòü îäèí (îáùèé) ïåðâûé ýëåìåíò
concatSnds xs = (fst (head xs), concatMap snd xs)
-- Îïåðàöèè íàä tuple/3
fst3 (a,_,_) = a
snd3 (_,a,_) = a
thd3 (_,_,a) = a
map3 (f,g,h) a = (f a, g a, h a)
---------------------------------------------------------------------------------------------------
---- Ýìóëÿöèÿ îáû÷íûõ ïåðåìåííûõ ------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
infixl 0 =:, +=, -=, ++=, =::, .=, .<-, <<=, <=>
-- Simple variables
class Variable v a | v->a where
new :: a -> IO v
val :: v -> IO a
(=:) :: v -> a -> IO ()
(.=) :: v -> (a->a) -> IO ()
(=::) :: v -> IO a -> IO ()
(.<-) :: v -> (a->IO a) -> IO ()
-- Default implementations
a.=f = do x<-val a; a=:f x
a=::b = (a=:) =<< b
a.<-f = do x<-val a>>=f; a=:x
ref = newIORef
instance Variable (IORef a) a where
new = newIORef
val = readIORef
a=:b = writeIORef a b
a.=b = modifyIORef a b
a.<-b = modifyIORefIO a b
mvar = newMVar
instance Variable (MVar a) a where
new = newMVar
val = readMVar
a=:b = swapMVar a b >> return ()
a.=b = modifyMVar_ a (return.b)
a.<-b = modifyMVar_ a b
a+=b = a.=(\a->a+b)
a-=b = a.=(\a->a-b)
a++=b = a.=(\a->a++b)
a<=>b = do x <- val a; a =: b; return x
withRef init = with' (ref init) val
-- Accumulation lists
newtype AccList a = AccList [a]
newList = ref$ AccList []
a<<=b = a .= (\(AccList x) -> AccList$ b:x)
pushList = (<<=)
listVal a = val a >>== (\(AccList x) -> reverse x)
withList = with' newList listVal
-- |Äîáàâèòü çíà÷åíèå â ñïèñîê, õðàíèìûé ïî ññûëêå IORef
addToIORef :: IORef [a] -> a -> IO ()
addToIORef var x = var .= (x:)
-- |Èñïîëüçîâàòü çíà÷åíèå, õðàíÿùååñÿ ïî ññûëêå IORef, â ïðîöåäóðå,
-- è çàïèñàòü íà åãî ìåñòî íîâîå çíà÷åíèå, âîçâðàù¸ííîå ýòîé ïðîöåäóðîé
modifyIORefIO :: IORef a -> (a -> IO a) -> IO ()
modifyIORefIO var action = do
readIORef var >>= action >>= writeIORef var
-- |Åù¸ îäíà ïîëåçíàÿ óïðàâëÿþùàÿ ñòðóêòóðà
with' init finish action = do a <- init; action a; finish a
-- |Âûïîëíèòü îïåðàöèþ è âîçâðàòèòü å¸ ðåçóëüòàò âíóòðè îáðàìëåíèÿ init/finish îïåðàöèé
inside init finish action = do init; x <- action; finish; return x
-- |Âûïîëíèòü "add key" c êåøèðîâàíèåì ðåçóëüòàòîâ
lookupMVarCache mvar add key = do
modifyMVar mvar $ \assocs -> do
case (lookup key assocs) of
Just value -> return (assocs, value)
Nothing -> do value <- add key
return ((key,value):assocs, value)
-- JIT-ïåðåìåííûå èíèöèàëèçèðóþòñÿ òîëüêî â ìîìåíò èõ ïåðâîãî èñïîëüçîâàíè
newJIT init = ref (Left init)
delJIT a finish = whenRightM_ (val a) finish
valJIT a = do x <- val a
case x of
Left init -> do x<-init; a=:Right x; return x
Right x -> return x
withJIT init finish action = do a <- newJIT init; action a `finally` delJIT a finish
---------------------------------------------------------------------------------------------------
---- Ññûëî÷íàÿ àðèôìåòèêà è îïåðàöèé íàä öåëûìè ---------------------------------------------------
---------------------------------------------------------------------------------------------------
infixl 6 +:, -:
ptr+:n = ptr `plusPtr` (fromIntegral n)
ptr-:buf = fromIntegral (ptr `minusPtr` buf)
copyBytesI dst src len = copyBytes dst src (i len)
minI a b = i$ min (i a) (i b)
maxI a b = i$ max (i a) (i b)
clipToMaxInt = i. min (i (maxBound::Int))
atLeast = max
i = fromIntegral
clipTo low high = min high . max low
divRoundUp x chunk = ((x-1) `div` i chunk) + 1
roundUp x chunk = divRoundUp x chunk * i chunk
divRoundDown x chunk = x `div` i chunk
roundDown x chunk = divRoundDown x chunk * i chunk
roundTo x chunk = i (((((toInteger(x)*2) `divRoundDown` chunk)+1) `divRoundDown` 2) * i chunk) `asTypeOf` x
---------------------------------------------------------------------------------------------------
---- Ðàñïðåäåëåíèå ïàìÿòè â öèêëè÷åñêîì áóôåðå ----------------------------------------------------
---------------------------------------------------------------------------------------------------
-- |Ðàñïðåäåëåíèå ïàìÿòè â öèêëè÷åñêîì áóôåðå ñ âûðàâíèâàíèåì âûäåëÿåìûõ áëîêîâ
-- heapsize - ðàçìåð áóôåðà
-- aBUFFER_SIZE - ìàêñèìàëüíûé ðàçìåð ðàñïðåäåëÿåìîãî áëîêà
-- aALIGN - âñå âûäåëÿåìûå áëîêè âûðàâíèâàþòñÿ íà ãðàíèöó, êðàòíóþ ýòîìó ÷èñëó
-- returnBlock - ïðîöåäóðà ïîëó÷åíèÿ áóôåðà, îñâîáîæä¸ííîãî ïîòðåáèòåëåì. Âûçûâàåòñÿ,
-- êîãäà ïàìÿòè ñòàíîâèòñÿ íåäîñòàòî÷íî äëÿ óäîâëåòâîðåíèÿ î÷åðåäíîãî çàïðîñà
--
allocator heapsize aBUFFER_SIZE aALIGN returnBlock = do
let aHEAP_START = 0 -- íà÷àëî áóôåðà, äîëæíî áûòü = 0
aHEAP_END = heapsize -- êîíåö áóôåðà
start <- ref aHEAP_START -- óêàçàòåëü íà÷àëà ñâîáîäíîãî ìåñòà â áóôåðå
end <- ref aHEAP_END -- óêàçàòåëü êîíöà ñâîáîäíîãî ìåñòà
-- åñëè ýòè óêàçàòåëè ðàâíû, òî ñâîáîäíîãî ìåñòà íåò
#if 0
let debug = putStr -- îòëàäî÷íàÿ ïå÷àòü
let printStats s = do -- Íàïå÷àòàòü ñîñòîÿíèå áóôåðà ïðè îòëàäêå
astart <- val start
aend <- val end
debug$ left_justify 48 s++"STATE start:"++show astart++", end:"++show aend++", avail:"++show ((aend-astart) `mod` aHEAP_END)++"\n"
debug "\n"
#else
let debug = return
printStats = return
#endif
-- Âûðàâíÿòü çíà÷åíèå íà áëèæàéøóþ âåëè÷èíó, êðàòíóþ aALIGN
let align n = (((n-1) `div` aALIGN) + 1) * aALIGN
-- Âîçâðàùàåò àäðåñ áëîêà: >=n, âûðîâíåííîãî íà aALIGN è èìåþùåãî êàê ìèíèìóì aBUFFER_SIZE áàéò äî êîíöà áóôåðà
let nextAvail n = if (aHEAP_END-aligned<aBUFFER_SIZE)
then aHEAP_END
else aligned
where aligned = align n
-- Âîçâðàòèòü êîëè÷åñòâî ñâîáîäíîé ïàìÿòè â áóôåðå
let available = do
astart <- val start
aend <- val end
if (astart<=aend) then
return (aend-astart)
else if (astart<aHEAP_END) then
return (aHEAP_END-astart)
else do
-- Ïåðåâåñòè óêàçàòåëü íà÷àëà ñâîáîäíîé ïàìÿòè íà íà÷àëî áóôåðà
start =: aHEAP_START
debug "===================================\n"
printStats ""
available
-- Äîæäàòüñÿ îñâîáîæäåíèÿ áëîêà ïàìÿòè è îòìåòèòü åãî îñâîáîæäåíèå
let waitReleasingMemory = do
(addr,size) <- returnBlock
astart <- val start
aend <- val end
unless (addr == aend || (addr==aHEAP_START && (aHEAP_END-aend<aBUFFER_SIZE)))$ fail "addToAvail!"
let new_end = nextAvail(addr+size)
if new_end == astart
then do start=:aHEAP_START; end=:aHEAP_END -- now all memory is free
else end =: new_end
printStats$ "*** returned buf:"++show addr++" size:"++show size++" "
-- Ïîëó÷èòü î÷åðåäíîé áëîê ðàçìåðà aBUFFER_SIZE. Åñëè ñâîáîäíûõ áëîêîâ íåò - äîæäàòüñ
-- âîçâðàùåíèÿ íåîáõîäèìîãî êîëè÷åñòâà âûäåëåííîé ïðåæäå ïàìÿòè
let getBlock = do
avail <- available
if (avail >= aBUFFER_SIZE) then do
block <- val start
start =: error "Block not shrinked"
return block
else do
waitReleasingMemory
getBlock
-- Óêîðîòèòü âûäåëåííûé áëîê äî ðàçìåðà `size`. Äîëæíà áûòü îáÿçàòåëüíî âûçâàíà ïîñëå getBlock
let shrinkBlock block size = do
astart <- val start
--unless (astart == block)$ fail "Tryed to shrink another block"
unless (size <= aBUFFER_SIZE)$ fail "Growing instead of shrinking :)"
start =: nextAvail(block+size)
printStats$ "getBlock buf:"++show block++", size: "++show aBUFFER_SIZE++" --> "++show size++" "
-- Âîçâðàòèòü èíòåðôåéñ ê èñïîëüçîâàíèþ öèêëè÷åñêîãî áóôåðà
return (getBlock, shrinkBlock)
-- |Öèêëè÷åñêèé àëëîêàòîð, èñïîëüçóþùèé áëîê ïàìÿòè `heap`.
-- Ïðåîáðàçóåò ôóíêöèè, ñ êîòîðûìè ðàáîòàåò ôóíêöèÿ `allocator`
memoryAllocator heap size chunksize align returnBlock = do
let returnBlock2 = do (buf,len) <- returnBlock; return (buf-:heap, len)