-
Notifications
You must be signed in to change notification settings - Fork 0
/
deques.sls
203 lines (182 loc) · 5.96 KB
/
deques.sls
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
#!r6rs
;;; deques.sls --- Purely functional deques
;; Copyright (C) 2011,2012 Ian Price <[email protected]>
;; Author: Ian Price <[email protected]>
;; This program is free software, you can redistribute it and/or
;; modify it under the terms of the new-style BSD license.
;; You should have received a copy of the BSD license along with this
;; program. If not, see <http://www.debian.org/misc/bsd.license>.
;; Documentation:
;;
;; make-deque : () -> deque
;; returns a deque containing to items
;;
;; deque? : any -> boolean
;; tests if an object is a deque
;;
;; deque-length : deque -> non-negative integer
;; returns the number of items in the deque
;;
;; deque-empty? : deque -> boolean
;; returns true if there are no items in the deque, false otherwise
;;
;; enqueue-front : deque any -> deque
;; returns a new deque with the inserted item at the front
;;
;; enqueue-rear : deque any -> deque
;; returns a new deque with the inserted item at the rear
;;
;; dequeue-front : deque -> any queue
;; returns two values, the item at the front of the deque, and a new
;; deque containing all the other items
;; raises a &deque-empty condition if the deque is empty
;;
;; dequeue-rear : deque -> any queue
;; returns two values, the item at the rear of the deque, and a new
;; deque containing all the other items
;; raises a &deque-empty condition if the deque is empty
;;
;; deque-empty-condition? : object -> boolean
;; tests if an object is a &deque-empty condition
;;
;; deque->list : deque -> listof(any)
;; returns a list containing all the elements of the deque. The order
;; of the elements in the list is the same as the order they would be
;; dequeued from the front of the deque.
;;
;; list->deque : listof(any) -> deque
;; returns a deque containing all of the elements in the list. The
;; order of the elements in the deque is the same as the order of the
;; elements in the list.
;;
(library (pfds deques)
(export make-deque
deque?
deque-length
deque-empty?
enqueue-front
enqueue-rear
dequeue-front
dequeue-rear
deque-empty-condition?
deque->list
list->deque
)
(import (except (rnrs) cons*)
(pfds deques private condition)
(pfds private lazy-lists))
(define c 2)
(define (rot1 n l r)
(if (>= n c)
(cons* (head l)
(rot1 (- n c) (tail l) (drop c r)))
(rot2 l (drop n r) '())))
(define (rot2 l r a)
(if (empty? l)
(append* (rev r) a)
(cons* (head l)
(rot2 (tail l)
(drop c r)
(append* (rev (take c r)) a)))))
(define-record-type (deque %make-deque deque?)
(fields
(immutable length)
(immutable lenL)
(immutable lenR)
(immutable l)
(immutable r)
(immutable l^)
(immutable r^)))
(define (make-deque)
(%make-deque 0 0 0 '() '() '() '()))
(define (deque-empty? deque)
(zero? (deque-length deque)))
(define (enqueue-front deque item)
(let ((len (deque-length deque))
(l (deque-l deque))
(r (deque-r deque))
(lenL (deque-lenL deque))
(lenR (deque-lenR deque))
(l^ (deque-l^ deque))
(r^ (deque-r^ deque)))
(makedq (+ 1 len) (+ 1 lenL) lenR (cons* item l) r (tail l^) (tail r^))))
(define (enqueue-rear deque item)
(let ((len (deque-length deque))
(l (deque-l deque))
(r (deque-r deque))
(lenL (deque-lenL deque))
(lenR (deque-lenR deque))
(l^ (deque-l^ deque))
(r^ (deque-r^ deque)))
(makedq (+ 1 len) lenL (+ 1 lenR) l (cons* item r) (tail l^) (tail r^))))
(define (dequeue-front deque)
(when (deque-empty? deque)
(raise (condition
(make-deque-empty-condition)
(make-who-condition 'dequeue-front)
(make-message-condition "There are no elements to remove")
(make-irritants-condition (list deque)))))
(let ((len (deque-length deque))
(lenL (deque-lenL deque))
(lenR (deque-lenR deque))
(l (deque-l deque))
(r (deque-r deque))
(l^ (deque-l^ deque))
(r^ (deque-r^ deque)))
(if (empty? l)
(values (head r) (make-deque))
(values (head l)
(makedq (- len 1)
(- lenL 1)
lenR
(tail l)
r
(tail (tail l^))
(tail (tail r^)))))))
(define (dequeue-rear deque)
(when (deque-empty? deque)
(raise (condition
(make-deque-empty-condition)
(make-who-condition 'dequeue-rear)
(make-message-condition "There are no elements to remove")
(make-irritants-condition (list deque)))))
(let ((len (deque-length deque))
(lenL (deque-lenL deque))
(lenR (deque-lenR deque))
(l (deque-l deque))
(r (deque-r deque))
(l^ (deque-l^ deque))
(r^ (deque-r^ deque)))
(if (empty? r)
(values (head l) (make-deque))
(values (head r)
(makedq (- len 1)
lenL
(- lenR 1)
l
(tail r)
(tail (tail l^))
(tail (tail r^)))))))
(define (makedq len lenL lenR l r l^ r^)
(cond ((> lenL (+ 1 (* c lenR)))
(let* ((n (floor (/ (+ lenL lenR) 2)))
(l* (take n l))
(r* (rot1 n r l)))
(%make-deque len n (- len n) l* r* l* r*)))
((> lenR (+ 1 (* c lenL)))
(let* ((n (floor (/ (+ lenL lenR) 2)))
(l* (rot1 n l r))
(r* (take n r)))
(%make-deque len (- len n) n l* r* l* r*)))
(else
(%make-deque len lenL lenR l r l^ r^))))
(define (list->deque l)
(fold-left enqueue-rear (make-deque) l))
(define (deque->list deq)
(define (recur deq l)
(if (deque-empty? deq)
l
(let-values ([(last deq*) (dequeue-rear deq)])
(recur deq* (cons last l)))))
(recur deq '()))
)