ソースを参照

make the key customizable/restrictable.

Lucas Stadler 11 年 前
コミット
0624c6bf14
共有1 個のファイルを変更した4 個の追加3 個の削除を含む
  1. 4 3
      hs/DataStructures.hs

+ 4 - 3
hs/DataStructures.hs

1
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
1
{-|
2
{-|
2
    Module: DataStructures
3
    Module: DataStructures
3
    Description: Having fun with (functional) data structures
4
    Description: Having fun with (functional) data structures
268
        case last ft of
269
        case last ft of
269
            Just x -> Just (x, rest ft) -- broken, we'd need a different version of rest
270
            Just x -> Just (x, rest ft) -- broken, we'd need a different version of rest
270
271
271
class Associative as where
272
class Associative as k where
272
    get :: (Ord k) => k -> as k v -> Maybe v
273
    get :: (Ord k) => k -> as k v -> Maybe v
273
274
274
    insert :: (Ord k) => k -> v -> as k v -> as k v
275
    insert :: (Ord k) => k -> v -> as k v -> as k v
283
-- examples
284
-- examples
284
rb = fromPairs $ zip [1..10] [2..11] :: RBTree Int Int
285
rb = fromPairs $ zip [1..10] [2..11] :: RBTree Int Int
285
286
286
fromPairs :: (Associative as, Ord k) => [(k, v)] -> as k v
287
fromPairs :: (Associative as k, Ord k) => [(k, v)] -> as k v
287
fromPairs [] = empty
288
fromPairs [] = empty
288
fromPairs ((k, v):kvs) = insert k v $ fromPairs kvs
289
fromPairs ((k, v):kvs) = insert k v $ fromPairs kvs
289
290
309
    RBNode Black (RBNode Red a xk xv b) yk yv (RBNode Black c zk zv d)
310
    RBNode Black (RBNode Red a xk xv b) yk yv (RBNode Black c zk zv d)
310
balance c l k v r = RBNode c l k v r
311
balance c l k v r = RBNode c l k v r
311
312
312
instance Associative RBTree where
313
instance Associative RBTree k where
313
    get ik (RBLeaf _) = Nothing
314
    get ik (RBLeaf _) = Nothing
314
    get ik (RBNode c l k v r) | k == ik = Just v
315
    get ik (RBNode c l k v r) | k == ik = Just v
315
    get ik (RBNode c l k v r) =
316
    get ik (RBNode c l k v r) =