Просмотр исходного кода

add 2-3 fingertree implementation.

"very, very slow", says daniel spiewak. we'll find out why later.
Lucas Stadler лет назад: 11
Родитель
Сommit
344ee7b723
1 измененных файлов с 80 добавлено и 0 удалено
  1. 80 0
      hs/DataStructures.hs

+ 80 - 0
hs/DataStructures.hs

162
    isEmpty (BankersQueue _ Nil _ Nil) = True
162
    isEmpty (BankersQueue _ Nil _ Nil) = True
163
    isEmpty _ = False
163
    isEmpty _ = False
164
164
165
data FingerTree a =
166
      Empty
167
    | Single a
168
    | Deep {
169
          ftPrefix :: Digit a,
170
          ftTree :: FingerTree (Node a),
171
          ftSuffix :: Digit a
172
      } deriving (Show)
173
174
data Digit a = One a | Two a a | Three a a a | Four a a a a deriving Show
175
data Node a = Node2 a a | Node3 a a a deriving Show
176
177
instance Seq Digit where
178
    first (One x) = Just x
179
    first (Two x _) = Just x
180
    first (Three x _ _) = Just x
181
    first (Four x _ _ _) = Just x
182
183
    last (One x) = Just x
184
    last (Two _ x) = Just x
185
    last (Three _ _ x) = Just x
186
    last (Four _ _ _ x) = Just x
187
188
    cons x (One a) = Two x a
189
    cons x (Two a b) = Three x a b
190
    cons x (Three a b c) = Four x a b c
191
192
    nil = error "can't be empty"
193
194
    rest (Two _ a) = One a
195
    rest (Three _ a b) = Two a b
196
    rest (Four _ a b c) = Three a b c
197
198
    isEmpty _ = False
199
200
    append x (One a) = Two a x
201
    append x (Two a b) = Three a b x
202
    append x (Three a b c) = Four a b c x
203
204
instance Seq FingerTree where
205
    first Empty = Nothing
206
    first (Single x) = Just x
207
    first (Deep p _ _) = first p
208
209
    last Empty = Nothing
210
    last (Single x) = Just x
211
    last (Deep _ _ s) = last s
212
213
    cons x Empty = Single x
214
    cons x (Single y) = Deep (One x) Empty (One y)
215
    cons x (Deep (Four a b c d) t s) =
216
        Deep (Two x a) (cons (Node3 b c d) t) s
217
    cons x (Deep p t s) = Deep (cons x p) t s
218
219
    nil = Empty
220
221
    rest Empty = Empty
222
    rest (Single _) = Empty
223
    rest (Deep (One _) t s) =
224
        case first t of
225
            Nothing ->
226
                case s of
227
                    One x -> Single x
228
                    Two x y -> Deep (One x) Empty (One y)
229
                    Three x y z -> Deep (Two x y) Empty (One z)
230
                    Four x y z w -> Deep (Three x y z) Empty (One w)
231
            Just (Node2 x y) -> Deep (Two x y) (rest t) s
232
            Just (Node3 x y z) -> Deep (Three x y z) (rest t) s
233
    rest (Deep p t s) = Deep (rest p) t s
234
235
    isEmpty Empty = True
236
    isEmpty _ = False
237
238
    append x Empty = Single x
239
    append x (Single y) = Deep (One x) Empty (One y)
240
    append x (Deep p t (Four a b c d)) =
241
        Deep p (append (Node3 a b c) t) (Two d x)
242
    append x (Deep p t s) = Deep p t (append x s)
243
165
-- examples
244
-- examples
166
sl = fromList [1..10] :: List Integer
245
sl = fromList [1..10] :: List Integer
167
bq = fromList [1..10] :: BankersQueue Integer
246
bq = fromList [1..10] :: BankersQueue Integer
247
ft = fromList [1..10] :: FingerTree Integer
168
248
169
-- properties
249
-- properties
170
250