-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intern.hs
80 lines (65 loc) · 2.22 KB
/
Intern.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
{-# LANGUAGE BangPatterns #-}
module Intern
( disintern2trie1
, disintern2trie2
, disintern2map1
, DCT
, DCM ) where
-- disintern[1..3] are by Daniel Fischer, @dafis
-- he also hacked IntMap into a stricter one:
import IntBS
import qualified IntMap as IM
import IntMap ((!))
import qualified Data.Trie as T
import Data.Trie (Trie)
import qualified Data.Map as M
import Data.Map (Map)
import SocRun (DCaps)
import Data.ByteString.Char8 (ByteString)
import SLP
type Vals = SLP
type DCT = Trie Vals
type DCM = Map ByteString Vals
-- TODO: Cale suggested using builder for toAscList
-- on #haskell circa 2010-06-22 -- see Utils.hs
-- here M is Data.Map.Map ByteString ...
-- disintern1 dic =
-- let !ib = backIB dic
-- step !k !v !res = {-# SCC "disintern.step" #-} case ib ! k of
-- !name -> M.insert name v res
-- in
-- IM.foldWithKey step M.empty
-- to disintern to a Trie:
disintern2trie2 :: IntBS -> DCaps -> DCT
disintern2trie2 dic dcaps =
let !tr = trieIB dic
in fmap (dcaps !) tr
-- (if there is even a faint possibility that the dictionary knows users
-- without dcaps, instead of (dcaps !), you'd use
-- (flip (findWithDefault []) dcaps), possibly doing
-- Data.Map.filter (not . null) afterwards)
-- A related way to try to disintern to a Data.Map,
-- disintern3 dic dcaps =
-- let !mdic = M.fromDistinctAscList . T.toList $ trieIB dic
-- in M.map (dcaps !) mdic
-- this is not strict enough, the thing explodes
-- how can we ensure M.insert stays strict?
-- might as well disintern into a Trie instead
-- current disinterning into a trie without fmap
-- TODO generalize to both Trie Map ByteString
-- by parameterizing empty and insert,
-- curry and benchmark
disintern2trie1 :: IntBS -> DCaps -> DCT
disintern2trie1 dic =
let !ib = backIB dic
step !k !v !res = {-# SCC "disintern.step" #-} case ib ! k of
!name -> T.insert name v res
in
IM.foldWithKey step T.empty
disintern2map1 :: IntBS -> DCaps -> DCM
disintern2map1 dic =
let !ib = backIB dic
step !k !v !res = {-# SCC "disintern.step" #-} case ib ! k of
!name -> M.insert name v res
in
IM.foldWithKey step M.empty