Pārlūkot izejas kodu

add incomplete version of patricia trie.

e.g. we can lookup, but not insert because merge & matchPrefix weren't
explained. laters?
Lucas Stadler 11 gadi atpakaļ
vecāks
revīzija
18fc640a2e
1 mainītis faili ar 23 papildinājumiem un 0 dzēšanām
  1. 23 0
      hs/DataStructures.hs

+ 23 - 0
hs/DataStructures.hs

@ -11,6 +11,7 @@
11 11
module DataStructures where
12 12
13 13
import Prelude hiding (concat, drop, last, length, reverse, take)
14
import Data.Bits (Bits, (.&.), zeroBits)
14 15
15 16
-- examples
16 17
sl = fromList [1..10] :: List Integer
@ -326,3 +327,25 @@ instance Associative RBTree k where
326 327
        else balance c l k v (insert ik iv r)
327 328
328 329
    empty = RBLeaf Black
330
331
data PatriciaTrie k v =
332
      PTEmpty
333
    | PTLeaf k v
334
    | PTBranch { ptPrefix :: k, ptMask :: k, ptLeft :: PatriciaTrie k v, ptRight :: PatriciaTrie k v } deriving Show
335
336
instance Associative PatriciaTrie Int where
337
    get ik PTEmpty = Nothing
338
    get ik (PTLeaf k v) = if ik == k then Just v else Nothing
339
    get ik (PTBranch p m l r) = if (ik .&. m) == zeroBits then get ik l else get ik r
340
341
    insert ik iv b@(PTBranch p m l r) =
342
        if ptMatchPrefix ik p m
343
        then if (ik .&. m) == zeroBits
344
             then PTBranch p m (insert ik iv l) r
345
             else PTBranch p m l (insert ik iv r)
346
        else ptMerge ik (PTLeaf ik iv) p b
347
348
    empty = PTEmpty
349
350
ptMatchPrefix = undefined
351
ptMerge = undefined