-
Notifications
You must be signed in to change notification settings - Fork 6
/
queue-racket.rkt
192 lines (139 loc) · 5.61 KB
/
queue-racket.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
#lang racket
(require "special.rkt" "ops-racket.rkt")
(provide queue-in-racket% queue-out-racket%)
(define queue-in-racket%
(class* special% (equal<%> printable<%>)
(super-new)
(init-field [get-fresh-val #f]
[size 4]
[queue (make-vector size #f)]
[index 0]
[ref #f])
(public pop pop-inverse
clone correctness-cost get-live-mask)
(define (filter-queue)
(filter (lambda (x) (not (equal? x #f))) (vector->list queue)))
(define/public (relevant-queue) (vector-copy queue 0 index))
(define/public (custom-print port depth)
(print `(queue-in% content: ,(filter-queue) index: ,index) port depth))
(define/public (custom-write port)
(write `(queue-in% content: ,(filter-queue) index: ,index) port))
(define/public (custom-display port)
(display `(queue-in% content: ,(filter-queue) index: ,index) port))
(define/public (equal-to? other recur)
(and (is-a? other queue-in-racket%)
(equal? (relevant-queue) (send other relevant-queue))
))
(define/public (equal-hash-code-of hash-code)
(hash-code (relevant-queue)))
(define/public (equal-secondary-hash-code-of hash-code)
(hash-code (relevant-queue)))
;; (define/public (equal-to? other recur)
;; (and (is-a? other queue-in-racket%)
;; (equal? index (get-field index other))
;; ))
;; (define/public (equal-hash-code-of hash-code)
;; (hash-code index))
;; (define/public (equal-secondary-hash-code-of hash-code)
;; (hash-code index))
(define (clone [ref #f])
(new queue-in-racket% [ref ref] [queue queue] [index index] [get-fresh-val get-fresh-val]))
;; (define (clone-all [ref #f])
;; (new queue-in-racket% [ref ref] [queue queue] [index index]))
(define (pop-spec)
(define val (vector-ref queue index))
(unless val
(set! val (get-fresh-val))
(vector-set! queue index val))
(set! index (add1 index))
val)
(define (pop-cand)
(define val (vector-ref queue index))
(assert (not (equal? val #f)) "pop more than number of elements")
(set! index (add1 index))
val)
(define (pop)
(if ref (pop-cand) (pop-spec)))
(define (pop-inverse val)
(set! index (sub1 index))
(define val-ref (vector-ref queue index))
(equal? val val-ref))
(define (correctness-cost other diff-cost bit)
(* bit (abs (- (get-field index other) index))))
(define (get-live-mask) #t)
))
(define queue-out-racket%
(class* special% (equal<%> printable<%>)
(super-new)
(init-field [get-fresh-val #f]
[size 4]
[queue (make-vector size)]
[index 0]
[ref #f])
(public push push-inverse
clone correctness-cost get-live-mask)
(define/public (get-at index) (vector-ref queue index))
(define (filter-queue)
(filter (lambda (x) (not (equal? x #f))) (vector->list queue)))
(define/public (relevant-queue) (vector-copy queue 0 index))
(define/public (custom-print port depth)
(print `(queue-out% content: ,(filter-queue) index: ,index) port depth))
(define/public (custom-write port)
(write `(queue-out% content: ,(filter-queue) index: ,index) port))
(define/public (custom-display port)
(display `(queue-out% content: ,(filter-queue) index: ,index) port))
(define/public (equal-to? other recur)
(and (is-a? other queue-out-racket%)
(equal? (relevant-queue) (send other relevant-queue))
))
(define/public (equal-hash-code-of hash-code)
(hash-code (relevant-queue)))
(define/public (equal-secondary-hash-code-of hash-code)
(hash-code (relevant-queue)))
;; (define/public (equal-to? other recur)
;; (and (is-a? other queue-in-racket%)
;; (equal? index (get-field index other))
;; ))
;; (define/public (equal-hash-code-of hash-code)
;; (hash-code index))
;; (define/public (equal-secondary-hash-code-of hash-code)
;; (hash-code index))
(define (clone [ref #f])
(new queue-out-racket% [ref ref] [queue (vector-copy queue)] [index index]
[get-fresh-val get-fresh-val]))
(define (push-spec val)
(vector-set! queue index val)
(set! index (add1 index)))
(define (push-cand val)
(define val-ref (send ref get-at index))
(assert (equal? val val-ref) "push-cand: push different val")
(vector-set! queue index val)
(set! index (add1 index)))
(define (push val)
(if ref (push-cand val) (push-spec val)))
(define (push-inverse)
(set! index (sub1 index))
(vector-ref queue index))
(define (correctness-cost other diff-cost bit)
(* bit (abs (- (get-field index other) index))))
(define (get-live-mask) #t)
))
(define (test1)
(define q-init (new queue-in-racket% [queue (vector 1 2 #f #f)] [index 0]))
(define q1 (send q-init clone))
(pretty-display `(pop ,(send q1 pop)))
(pretty-display `(pop ,(send q1 pop)))
(pretty-display `(queue-init ,q1))
(pretty-display `(pop ,(send q1 pop))) ;; assert: pop-cand: pop more than spec does
)
(define (test2)
(define q-init (new queue-out-racket% [queue (vector 1 2 #f #f)] [index 0]))
(define q2 (send q-init clone))
(send q2 push 1)
(pretty-display `(queue ,q2))
(send q2 push 22) ;; assert: push-cand: push a value different from spec does
)
(define (test-performance)
(define q (new queue-out-racket%))
(for/list ([i 1000000])
(send q clone)))