/
conversions.lisp
225 lines (173 loc) · 5.67 KB
/
conversions.lisp
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
(in-package #:com.clearly-useful.generic-collection-interface)
;;;;;;; note that the internals of this package are subject to change.
;;;an integer range
(defstruct %range
"an immutable integer range from
low to high, exclusive."
low high)
(defun %range-size (range)
"the number of elements in a range"
(- (%range-high range)
(%range-low range)))
(defun %next-range (range)
"return the next range by incrementing
the lower bound of range, or nil"
(if (<= (%range-size range) 1)
nil
(make-%range :low (1+ (%range-low range))
:high (%range-high range))))
(extend-type %range
collection
(empty (range) (make-%range :low 0 :high 0))
(empty-p (range) (not (plusp (%range-size range))))
seqable
(seq (range) (when (plusp (%range-size range))
range))
seq
(head (range) (%range-low range))
(tail (range) (%next-range range))
countable
(counted-p (range) t)
(count-elements (range) (%range-size range))
indexable
(element-at (range index)
(if (and
(integerp index)
(<= (%range-low range) index)
(< index (%range-high range)))
(+ (%range-low range) index)
(error "~S is an invalid index for ~S" index range))))
;;;;convert a countable to a seq:
;;;;;; NOTE: the following needs must get converted from
;;;;;; 'countable' to 'indexable'
(defstruct %countable-sequence
length index countable)
(defun %countable-sequence-size (c)
(- (%countable-sequence-length c)
(%countable-sequence-index c)))
(defun %next-countable-sequence (c)
(let ((length (%countable-sequence-length c))
(new-index (1+ (%countable-sequence-index c))))
(unless (zerop (- length new-index))
(make-%countable-sequence
:length length
:index new-index
:countable (%countable-sequence-countable c)))))
(defun %countable-to-seq (countable)
(when (plusp (count-elements countable))
(make-%countable-sequence
:length (count-elements countable)
:index 0
:countable countable)))
(extend-type %countable-sequence
collection
(empty (c) (%countable-to-seq (empty (%countable-sequence-countable c))))
(empty-p (c) (empty-p (%countable-sequence-countable c)))
seqable
(seq (c) (when (plusp (%countable-sequence-size c))
c))
seq
(head (c) (element-at (%countable-sequence-countable c)
(%countable-sequence-index c)))
(tail (c) (%next-countable-sequence c)))
;;;;convert a countable to an associative:
(defstruct %countable-associative
length countable)
(defun %countable-to-associative (countable)
(make-%countable-associative
:length (count-elements countable)
:countable countable))
(extend-type %countable-associative
collection
(empty (o)
(%countable-to-associative
(empty (%countable-associative-countable o))))
(empty-p (o)
(empty-p (%countable-associative-countable o)))
associative
(all-keys (o) (make-%range :low 0
:high (%countable-associative-length o)))
(all-values (o) (%countable-to-seq (%countable-associative-countable o)))
(contains-key-p (o key) (and (integerp key)
(< -1 key (%countable-associative-length o))))
(value-for-key (o key)
(if (contains-key-p o key)
(element-at (%countable-associative-countable o) key)
(error "~S does not contain key ~S" o key))))
;;;;; seq functions
(defun %count-seq (seq)
(loop for i from 0
for tail = seq then (tail tail)
while tail
finally (return i)))
(defun %seq-indexable-by (seq n)
(loop for i below (1+ n)
for tail = seq then (tail tail)
unless tail do (return nil)
finally (return t)))
(defun %seq-nth-or-error (seq n)
(flet ((lose ()
(error "~S is an invalid index for ~S" n seq)))
(unless (and (integerp n)
(< -1 n))
(lose))
(loop for i below (1+ n)
for tail = seq then (tail tail)
unless tail do (lose)
finally (return (head tail)))))
(defun %seq-nth-or-nil-with-values (seq n)
(let ((foundp nil))
(values
(loop for i below (1+ n)
for tail = seq then (tail tail)
unless tail do (return nil)
finally (progn
(setf foundp t)
(return (head tail))))
foundp)))
(defun %seq-to-list (seq)
(let ((result (list)))
(doseq (o seq (nreverse result))
(push o result))))
;;;;; convert a seq to a countable:
(defstruct %seq-countable
seq
ubound ;;highest index known to be valid
fully-counted ;;when fully counted
;;ubound == count - 1
)
(defun %seq-countable-contains-index (sc index)
(if (%seq-countable-fully-counted sc)
(<= index (%seq-countable-ubound sc))
(if (<= index (%seq-countable-ubound sc))
t
(if (%seq-indexable-by (%seq-countable-seq sc) index)
(prog1 t (setf (%seq-countable-ubound sc) index))))))
(defun %seq-countable-count (sc)
(if (%seq-countable-fully-counted sc)
(1+ (%seq-countable-ubound sc))
(let ((c (%count-seq (%seq-countable-seq sc))))
(prog1 c
(setf (%seq-countable-ubound sc) (1- c)
(%seq-countable-fully-counted sc) t)))))
(defun %seq-to-countable (seq)
(when seq
(make-%seq-countable
:seq seq
:ubound -1
:fully-counted nil)))
(extend-type %seq-countable
collection
(empty (o) (%seq-to-countable (empty (%seq-countable-seq o))))
(empty-p (o) (empty-p (%seq-countable-seq o)))
countable
(counted-p (o) (declare (ignore o)) nil)
(count-elements (o) (%seq-countable-count o))
indexable
(element-at (o index) (%seq-nth-or-error (%seq-countable-seq o) index)))
;;;;; convert a seq to an associative:
(defun %seq-to-associative (seq)
(%countable-to-associative (%seq-to-countable seq)))
;;;; convert associative to indexable:
(defun %associative-to-indexable (assoc)
(%seq-to-countable (all-keys-and-values assoc)))