-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdlist.rkt
429 lines (378 loc) · 12.3 KB
/
dlist.rkt
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
#lang racket
(provide dcons ; (dcons element prev next) like cons
dempty ; empty dlist
dempty? ; empty predicate
dfirst ; first element (car)
dnext ; next dcons (cdr)
dprev ; previous dcons (cdr the other way)
last-dcons? ; is next dempty?
first-dcons? ; is prev dempty?
dlist? ; is input part of a dlist?
first-dcons ; find first dcons in the dlist
last-dcons ; find last dcons in the dlist
list->dlist ; create dlist with elements from list
right-dlist->list ; create list with elements from the dcons and dconses to the right
left-dlist->list ; create list with elements from the dcons and dconses to the left
dlist->list ; convert dlist to list
dappend! ; destructively append two lists
connect-dconses! ; link two dconses together
dinsert-before! ; insert element in a newly allocated dcons before input dcons
dinsert-after! ; insert element in a newly allocated dcons after input dcons
dlist ; create dlist, like list
in-right-dlist ; iterate to the right
in-left-dlist ; iterate to the left
in-dlist ; iterate through all elements
dlist ; match pattern
right-dlist ; match pattern
left-dlist ; match pattern
dlist-ref ; like list-ref, negative indices to the left
dlist-move ; find dcons relative to input
dlength ; find total length
right-dlength ; find total length of right sublist
left-dlength ; find total length of left sublist
for/dlist ; dlist comprehension
dcons-remove! ; remove dcons from dlist, return previous
dlength ; length
(struct-out dcons))
;;;
;;; DOUBLE LINKED LIST
;;;
; A doubly linked list is either
; (dcons a p n)
; or ()
; here a is a list element,
; p is the previous dcons
; n is the next dcons.
(define dempty '())
(struct dcons (a p n) #:mutable #:transparent
#:property prop:sequence
(λ (xs) (in-right-dlist/proc xs)))
; dempty? : any -> boolean
; does xs represent the empty doubly linked list
(define (dempty? xs)
(null? xs))
; dfirst : dlist -> any
; return first element
(define (dfirst xs)
(dcons-a xs))
; dnext : dcons -> dlist
(define (dnext xs)
(dcons-n xs))
; dprev : dcons -> dlist
(define (dprev xs)
(dcons-p xs))
; last-dcons? : any -> boolean
; is xs the last dcons in a doubly linked list?
(define (last-dcons? xs)
(and (dcons? xs)
(dempty? (dcons-n xs))))
; first-dcons? : any -> boolean
; is xs the first dcons in a doubly linked list?
(define (first-dcons? xs)
(and (dcons? xs)
(dempty? (dcons-p xs))))
; dlist? xs -> boolean
; is xs a part of a dlist?
(define (dlist? xs)
(or (dempty? xs)
(and (dcons? xs)
(dlist-left? xs)
(dlist-right? xs))))
(define (dlist-left? xs)
(or (null? xs)
(and (dcons? xs)
(dlist-left? (dcons-p xs)))))
(define (dlist-right? xs)
(or (null? xs)
(and (dcons? xs)
(dlist-right? (dcons-n xs)))))
; last-dcons : dcons -> dcons
; find the last dcons to the right
(define (last-dcons xs)
; loop : dcons -> dcons
(define (loop xs)
(define n (dcons-n xs))
(if (dempty? n) xs (loop n)))
(if (dcons? xs)
(loop xs)
(error 'last-dcons "non-empty doubly linked list (dcons) expected, got ~a" xs)))
; first-dcons : dcons -> dcons
; find the first dcons to the left
(define (first-dcons xs)
; loop : dcons -> dcons
(define (loop xs)
(define p (dcons-p xs))
(if (dempty? p) xs (loop p)))
(if (dcons? xs)
(loop xs)
(error 'first-dcons "non-empty doubly linked list (dcons) expected, got ~a" xs)))
(define (list->dlist xs)
(define (recur p xs)
(cond
[(null? xs) dempty]
[else (define d (dcons (car xs) p #f))
(define n (recur d (cdr xs)))
(set-dcons-n! d n)
d]))
(cond
[(null? xs) dempty]
[else (define d (dcons (car xs) dempty #f))
(define n (recur d (cdr xs)))
(set-dcons-n! d n)
d]))
; right-dlist->list : dlist -> list
; return a list of the elements in the dconses starting with xs and going right
(define (right-dlist->list xs)
(if (dcons? xs)
(cons (dcons-a xs) (right-dlist->list (dcons-n xs)))
'()))
; left-dlist->list : dlist -> list
; return a list of the elements in the dconses starting with xs and going left
(define (left-dlist->list xs)
(if (dcons? xs)
(cons (dcons-a xs) (left-dlist->list (dcons-p xs)))
'()))
; dlist->list : dlist -> list
; return a list of all elements in the dlist in which is a part of of
(define (dlist->list xs)
(if (dempty? xs)
'()
(right-dlist->list (first-dcons xs))))
; connect-dconses! : dcons dcons -> void
(define (connect-dconses! d1 d2)
(set-dcons-n! d1 d2)
(set-dcons-p! d2 d1))
; dpappend! : dlist dlist -> dlist
; Return a dlist containing all elements of xs and ys.
; If non-empty then the last doncs of xs is returned.
(define (dappend! xs ys)
(cond
[(dempty? xs) ys]
[(dempty? ys) xs]
[else (define l (last-dcons xs))
(define f (first-dcons ys))
(connect-dconses! l f)
l]))
; dinsert-before! dlist any -> dcons
; insert the element a before the dcons xs
; the doncs allocated for a is returned
(define (dinsert-before! xs a)
(cond
[(dempty? xs)
(dcons a dempty dempty)]
[(first-dcons? xs)
(define d (dcons a dempty xs))
(set-dcons-p! xs d)
d]
[else
(define p (dcons-p xs))
(define d (dcons a p xs))
(set-dcons-n! p d)
(set-dcons-p! xs d)
d]))
; dinsert-after! dlist any -> dcons
; insert the element a after the dcons xs
; the doncs allocated for a is returned
(define (dinsert-after! xs a [Dcons dcons])
(cond
[(dempty? xs)
(Dcons a dempty dempty)]
[(last-dcons? xs)
(define d (Dcons a xs dempty))
(set-dcons-n! xs d)
d]
[else
(define n (dcons-n xs))
(define d (Dcons a xs n))
(set-dcons-p! n d)
(set-dcons-n! xs d)
d]))
(define (dcons-remove! xs)
(cond
[(and (first-dcons? xs) (last-dcons? xs))
dempty]
[(first-dcons? xs)
(define n (dcons-n xs))
(set-dcons-p! n dempty)
n]
[(last-dcons? xs)
(define p (dcons-p xs))
(set-dcons-n! p dempty)
p]
[else
(define p (dcons-p xs))
(define n (dcons-n xs))
(set-dcons-n! p n)
(set-dcons-p! n p)
p]))
(define (dlist-split-before! xs)
(cond
[(first-dcons? xs) (values dempty xs)]
[else (define p (dcons-p xs))
(set-dcons-n! p dempty)
(set-dcons-p! xs dempty)
(values p xs)]))
(define (dlist-split-after! xs)
(cond
[(last-dcons? xs) (values xs dempty)]
[else (define n (dcons-n xs))
(set-dcons-n! xs dempty)
(set-dcons-p! n dempty)
(values xs n)]))
; dlist-move : dcons integer -> dcons
; return dcons n to the right (if n positive)
; return dcons -n to the left (if n negative)
(define (dlist-move xs n)
(cond
[(= n 0) xs]
[(> n 0) (right-dlist-move xs n)]
[(< n 0) (left-dlist-move xs (- n))]
[else (error 'dlist-move "expected integer, got ~a" n)]))
(define (right-dlist-move xs n)
(if (= n 0)
xs
(right-dlist-move (dcons-n xs) (- n 1))))
(define (left-dlist-move xs n)
(if (= n 0)
xs
(left-dlist-move (dcons-p xs) (- n 1))))
; dlist-ref : dlist n -> any
; like list-ref but for dlists
(define (dlist-ref xs n)
(dcons-a (dlist-move xs n)))
(require (for-syntax syntax/parse))
(define-match-expander right-dlist
(λ (stx)
(syntax-parse stx
[(_) #'(== dempty)]
[(_ a) #'(dcons a _ _)]
[(_ a . more) #'(dcons a _ (right-dlist . more))])))
(define-match-expander left-dlist
(λ (stx)
(syntax-parse stx
[(_) #'(== dempty)]
[(_ a) #'(dcons a _ _)]
[(_ a . more) #'(dcons a (dlist . more) _)])))
(define-match-expander dlist
(λ (stx)
(syntax-parse stx
[(_ . more) #'(app (λ (xs) (dlist->list (first-dcons xs))) (list . more))]))
(λ (stx)
(syntax-parse stx
[(_) #'dempty]
[(_ x ...) #'(list->dlist (list x ...))]
[dl #'(λ xs (list->dlist xs))])))
(define-sequence-syntax in-right-dlist
(λ () #'in-right-dlist/proc)
(λ (stx)
(syntax-parse stx
[[(a) (_ xs-expr)]
#'[(a)
(:do-in
([(xs0) xs-expr])
(unless (dcons? xs0)
(raise-type-error 'in-right-dlist "doubly linked list" xs0))
([xs xs0])
(not (dempty? xs))
([(ys a) (values (dcons-n xs) (dcons-a xs))])
#true
#true
[ys])]])))
(define-sequence-syntax in-left-dlist
(λ () #'in-left-dlist/proc)
(λ (stx)
(syntax-parse stx
[[(a) (_ xs-expr)]
#'[(a)
(:do-in
([(xs0) xs-expr])
(unless (dcons? xs0)
(raise-type-error 'in-left-dlist "doubly linked list" xs0))
([xs xs0])
(not (dempty? xs))
([(ys a) (values (dcons-p xs) (dcons-a xs))])
#true
#true
[ys])]])))
(define-sequence-syntax in-dlist
(λ () #'in-dlist/proc)
(λ (stx)
(syntax-parse stx
[[(a) (_ xs-expr)]
#'[(a) (in-right-dlist (first-dcons xs-expr))]])))
(define (in-right-dlist/proc xs)
(make-do-sequence
(λ ()
(values
dcons-a ; pos->element
dcons-n ; next pos
xs ; initial pos
dcons? ; continue-with-pos?
#f ; use continue-with-pos?
#f ; ditto
))))
(define (in-left-dlist/proc xs)
(make-do-sequence
(λ ()
(values
dcons-a ; pos->element
dcons-p ; next pos
xs ; initial pos
dcons? ; continue-with-pos?
#f ; use continue-with-pos?
#f ; ditto
))))
(define (in-dlist/proc xs)
(in-right-dlist/proc (first-dcons xs)))
(define (dlength xs)
(if (dempty? xs)
0
(+ (right-dlength xs)
(left-dlength xs)
-1)))
(define (right-dlength xs)
(define (loop n xs)
(if (dempty? xs)
n
(loop (+ n 1) (dcons-n xs))))
(loop 0 xs))
(define (left-dlength xs)
(define (loop n xs)
(if (dempty? xs)
n
(loop (+ n 1) (dcons-p xs))))
(loop 0 xs))
(define-syntax (for/dlist stx)
(syntax-parse stx
[(_ #:dcons Dcons clauses . defs+exprs)
#'(let ()
(define xs (for/fold/derived stx ([xs dempty]) clauses
(define a (let () . defs+exprs))
(dinsert-after! xs a Dcons)))
(if (dempty? xs) xs (first-dcons xs)))]
#;[(_ clauses . defs+exprs)
#'(let ()
(define xs (for/fold/derived stx ([xs dempty]) clauses
(define a (let () . defs+exprs))
(dinsert-after! xs a)))
(if (dempty? xs) xs (first-dcons xs)))]))
(module+ test (require rackunit)
(define xs (list->dlist '(1 2 3)))
(check-true (dempty? (list->dlist '())))
(check-equal? (dlist->list xs) '(1 2 3))
(check-equal? (right-dlist->list xs) '(1 2 3))
(check-equal? (dcons-a (last-dcons xs)) 3)
(check-equal? (left-dlist->list (last-dcons xs)) '(3 2 1))
(check-equal? (right-dlist->list (first-dcons (last-dcons xs))) '(1 2 3))
(check-equal? (let ()
(define xs (list->dlist '(1 2 3)))
(define ys (list->dlist '(4 5 6)))
(dlist->list (dappend! xs ys)))
'(1 2 3 4 5 6))
(check-equal? (map (λ (n) (dlist-ref xs n)) '(0 1 2)) '(1 2 3))
(check-equal? (map (λ (n) (dlist-ref (last-dcons xs) n)) '(0 -1 -2)) '(3 2 1))
(check-equal? (let ()
(define xs (list->dlist '(1 2 3 4)))
(define-values (ys zs) (dlist-split-after! (dlist-move xs 1)))
(list (dlist->list ys) (dlist->list zs)))
'((1 2) (3 4))))