-
Notifications
You must be signed in to change notification settings - Fork 0
/
defseries.lisp
545 lines (488 loc) · 24.6 KB
/
defseries.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
(cl:in-package :cl-maxlib)
(eval-when (:compile-toplevel :load-toplevel :execute)
(import '(demacs::struct-options-of
demacs::name-of
demacs::slot-descs-of
demacs::documentation-of)))
(def (struct ea) (series (:constructor nil))
(length 0 :type fixnum)
(capacity 0 :type fixnum))
(def (print-object :print-identity t :print-type t) series (self)
(format t "len=~d capacity=~d" (series-length self) (series-capacity self)))
(def (generic e) make-series (name))
(def (generic e) series-shallow-copy (series))
(def (generic e) series-deep-copy (series))
(def (generic e) series-ensure-capacity (series size))
(def (generic e) series-allocate-capacity (series size))
(def (function e) series-trim-capacity (series)
"Trim series capacity to series length"
(series-allocate-capacity series (series-length series)))
;; all the helper structures and function have to be defined
;; doing compile time also, so wrap everything into eval-when
(defc (struct) (series-desc (:type list))
(columns nil)
(increase-capacity nil)
(check-capacity nil)
(series-add nil)
(series-add-from nil)
(series-add-values nil)
(with-series nil)
(series-values nil)
(series-values-as-list nil)
(make-series-name nil)
(shallow-copy-series nil)
(deep-copy-series nil)
(series-ensure-capacity nil)
(series-allocate-capacity nil))
(def (struct) (series-column (:type list))
(name nil)
(series nil)
(type nil)
(default nil)
;; below are various symbols
(column-type nil)
(array-type nil)
(array-accessor nil))
(defc special-variable *series* (make-hash-table))
(defc function format-series-symbol (format &rest args)
(intern (string-upcase (apply #'format nil format args))))
(defc struct (col-binding (:type list)) name var type accessor)
(defc struct (col-desc (:type list)) column as at binding type)
(defc function maybe-add-col-binding (list column)
"Adds the col-binding to the list if its not already there. Needed because
both symbols-macrolets and macrolets in with-series-expander share teh same
binding for the series column arrays. Returns the new list"
(let ((found (find (series-column-name column) list :key #'col-binding-name)))
(unless found
(push
(setq found (make-col-binding
:name (series-column-name column)
:var (gensym)
:type (series-column-array-type column)
:accessor (series-column-array-accessor column)))
list))
(values list found)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun get-col-binding (binds col)
"Find a col-binding for the column in the list of binds. Throws
error if not found"
(or (find (col-desc-column col) binds
:key #'col-binding-name)
(error "Unable to find column ~s in binding list ~s"
(col-desc-column col) binds))))
(defc function series-values-expander (series-name series-expr global-at)
"Expands into the definition of (series-name-values series-expr)
macro"
(let* ((desc (gethash series-name *series*))
(columns (series-desc-columns desc)))
(with-gensyms (series index)
`(let* ((,series ,series-expr)
(,index ,(if (member global-at '(nil :default))
`(1- (series-length ,series))
global-at)))
(declare (type ,series-name ,series)
(type fixnum ,index))
(values ,@(iter
(for col in columns)
(collect
`(the ,(series-column-column-type col)
(aref (the ,(series-column-array-type col)
(,(series-column-array-accessor col)
,series)) ,index)))))))))
(defc function with-series-expander (series-name series-expr global-at columns
accessors
var for iter from below to
body)
"Expands into the definition of (with-series-name series-expr
columns) macro"
(with-gensyms (index indexes tmp)
(let* ((series (or var (gensym "SERIES")))
default-at-expr
(desc (gethash series-name *series*))
(all-columns (series-desc-columns desc))
iteration-p
(cl-package (find-package :cl))
limit-expr
binds bind
(columns
(iter (for col in columns)
(destructuring-bind (col &key as at)
(ensure-list col)
(let ((column (find col all-columns :key #'series-column-name)))
(or column (error "Column ~S not found" col))
(multiple-value-setq (binds bind)
(maybe-add-col-binding binds column))
(collect (make-col-desc
:column col
:as (or as col)
:at (or at global-at)
:binding bind
:type (series-column-column-type column)))))))
(accessors
(iter (for col in accessors)
(destructuring-bind (col &key as)
(ensure-list col)
(let ((column (find col all-columns :key #'series-column-name)))
(or column (error "Column ~S not found" col))
(multiple-value-setq (binds bind)
(maybe-add-col-binding binds column))
;; do not add column accessor with the name
;; of symbol in CL package, as SBCL gives
;; package lock error when one tries to macrolet
;; OPEN or such
(unless (and (null as)
(eq (symbol-package col) cl-package))
(collect (make-col-desc
:column col
:as (or as col)
:binding bind
:type (series-column-column-type column)))))))))
(cond ((or for from iter)
(and for iter (error "Only one of :iter or :for can be specified"))
(setq iteration-p t)
(setq for (or for iter (gensym "I")))
(setq default-at-expr for)
(setq from (or from 0))
(cond ((and below to)
(error "Only one of :below or :to can be specified"))
(to
(setq limit-expr (if iter `(to (the fixnum ,to))
`(to ,to))))
(below
(setq limit-expr (if iter `(below (the fixnum ,below))
`(below ,below))))
(t
(setq limit-expr (if iter `(below (the fixnum (series-length ,series)))
`(below (series-length ,series)))))))
(t
(setq default-at-expr `(1- (series-length ,series)))
(when (or to below)
(error ":to/:below cannot be used without :for/:from"))))
`(let ((,series ,series-expr))
(declare (type ,series-name ,series))
(let (,@(iter (for bind in binds)
(collect `(,(col-binding-var bind)
(,(col-binding-accessor bind) ,series)))))
(declare
,@(iter (for bind in binds)
(collect `(type ,(col-binding-type bind)
,(col-binding-var bind)))
(collect `(ignorable ,(col-binding-var bind)))))
(symbol-macrolet
(,@(iter (for col in columns)
(for bind = (get-col-binding binds col))
(collect
`(,(col-desc-as col)
(the ,(col-desc-type col)
(aref ,(col-binding-var bind)
,(if (member (col-desc-at col)
'(nil :default))
default-at-expr
(col-desc-at col))))))))
(macrolet
(,@(iter (for col in accessors)
(for bind = (get-col-binding binds col))
(collect
`(,(col-desc-as col) (,index &rest ,indexes)
(if (null ,indexes)
`(the ,',(col-desc-type col)
(aref ,',(col-binding-var bind)
,,index))
`(values
,@(iter
(for ,tmp in (cons ,index ,indexes))
(collect
`(the ,',(col-desc-type col)
(aref ,',(col-binding-var bind)
,,tmp))))))))))
,@(if iteration-p
(if iter
`((iterate
(declare (iterate:declare-variables))
(for (the fixnum ,for) from ,from ,@limit-expr)
,@body))
`((loop for ,for fixnum from ,from ,@limit-expr
do (progn
,@body))))
body))))))))
(defc function series-add-from-expander (series-name series1-expr series2-expr at)
"Expands the (series-name-add-from) macro"
`(multiple-value-call
(function ,(series-desc-series-add-values (gethash series-name *series*)))
,series1-expr
,(series-values-expander series-name series2-expr at)))
(defc function series-add-expander (series-name series-expr &rest vals)
"Expands into the definition of (series-name-add series-expr &key columns) macro"
(with-gensyms (series index)
(let ((desc (gethash series-name *series*)))
`(let* ((,series ,series-expr)
(,index (series-length ,series)))
(declare (type ,series-name ,series)
(type fixnum ,index))
(,(series-desc-check-capacity desc)
,series (incf (series-length ,series)))
,@(iter (for c in (series-desc-columns desc))
(for v in vals)
(collect
`(setf (the ,(series-column-column-type c)
(aref
(the ,(series-column-array-type c)
(,(series-column-array-accessor c) ,series))
,index))
,v)))
,series))))
(defc function %defseries (series-name-and-options column-specs)
(let* ((have-options-p (listp series-name-and-options))
(series-name (if have-options-p (first series-name-and-options)
series-name-and-options))
(inherit-from (or (and have-options-p
(second (assoc :include (rest series-name-and-options))))
'series))
(parent-columns (unless (eq inherit-from 'series)
(series-desc-columns
(gethash inherit-from *series*))))
(all-column-names)
(desc (make-series-desc
:increase-capacity (format-series-symbol "increase-capacity-~s" series-name)
:check-capacity (format-series-symbol "check-capacity-~s" series-name)
:series-add (format-series-symbol "~s-add" series-name)
:series-add-from (format-series-symbol "~s-add-from" series-name)
:series-add-values (format-series-symbol "~s-add-values" series-name)
:with-series (format-series-symbol "with-~s" series-name)
:series-values (format-series-symbol "~s-values" series-name)
:series-values-as-list (format-series-symbol "~s-values-as-list" series-name)
:make-series-name (format-series-symbol "make-~s" series-name)
:shallow-copy-series (format-series-symbol "~s-shallow-copy" series-name)
:deep-copy-series (format-series-symbol "~s-deep-copy" series-name)
:series-ensure-capacity (format-series-symbol "~s-ensure-capacity" series-name)
:series-allocate-capacity (format-series-symbol "~s-allocate-capacity" series-name))))
(iterate
(for column in column-specs)
;; types
(let* ((column-name (first column))
(type (getf (cddr column) :type t))
(column (make-series-column
:name column-name
:series series-name
:default (second column)
:type type
:column-type (format-series-symbol "~s-~s-column-type"
series-name
column-name)
:array-type (format-series-symbol "~s-~s-type"
series-name
column-name)
:array-accessor (format-series-symbol "~s-~s"
series-name
column-name))))
(when (find column-name parent-columns :key #'series-column-name)
(error "Duplicate column name ~S" column-name))
(collect `(deftype ,(series-column-column-type column) () ',type)
into types1)
(collect `(deftype ,(series-column-array-type column) ()
'(simple-array ,(series-column-column-type column) *))
into types2)
(collect `(,column-name nil :type (or ,(series-column-array-type column) null))
into defs)
(collect column into columns))
(finally
(setf columns (append parent-columns columns))
(setf (series-desc-columns desc) columns)
(setf (gethash series-name *series*) desc)
(setf all-column-names (mapcar #'series-column-name columns))
(return
`(progn
(eval-when (:load-toplevel :compile-toplevel :execute)
(setf (gethash ',series-name *series*) ',desc))
,@types1
,@types2
(defstruct (,series-name
(:include ,inherit-from))
,@defs)
(defmethod make-series ((name (eql ',series-name)))
(,(series-desc-make-series-name desc)))
(defmethod series-shallow-copy ((series ,series-name))
(,(series-desc-shallow-copy-series desc) series))
(defmethod series-deep-copy ((series ,series-name))
(,(series-desc-deep-copy-series desc) series))
(defmethod series-ensure-capacity ((series ,series-name) size)
(,(series-desc-series-ensure-capacity desc) series size))
(defmethod series-allocate-capacity ((series ,series-name) size)
(,(series-desc-series-allocate-capacity desc) series size))
(defun ,(series-desc-series-ensure-capacity desc) (series size)
(declare (type ,series-name series)
(type fixnum size))
(,(series-desc-increase-capacity desc) series size))
(defun ,(series-desc-series-allocate-capacity desc) (series size)
(declare (type ,series-name series)
(type fixnum size))
(when (< size (series-length series))
(error "Can not allocate series capacity below its length"))
(setf (series-capacity series) size)
(,(series-desc-increase-capacity desc) series size))
(defun ,(series-desc-shallow-copy-series desc) (series)
(declare (type ,series-name series))
(,(series-desc-make-series-name desc)
:length (series-length series)
:capacity (series-length series)
,@(iter (for column in columns)
(collect (make-keyword (series-column-name column)))
(collect `(,(series-column-array-accessor column) series)))))
(defun ,(series-desc-deep-copy-series desc) (series)
(declare (type ,series-name series))
(,(series-desc-make-series-name desc)
:length (series-length series)
:capacity (series-capacity series)
,@(iter (for column in columns)
(collect (make-keyword (series-column-name column)))
(collect `(make-array
(length (the ,(series-column-array-type column)
(,(series-column-array-accessor column) series)))
:element-type ',(series-column-column-type column)
:initial-contents (the ,(series-column-array-type column)
(,(series-column-array-accessor column) series)))))))
(defmacro ,(series-desc-series-values desc)
(series &key (at :default))
"Return all columns of a series at the specified index
as multiple-values."
(series-values-expander ',series-name series at))
(defmacro ,(series-desc-series-values-as-list desc)
(series &key (at :default))
"Return all columns of a series at the specified index
as list"
`(multiple-value-list
,(series-values-expander ',series-name series at)))
(defmacro ,(series-desc-with-series desc)
((series &key
(at :default)
(columns :all)
(accessors :all)
var
for
iter
from
below
to)
&body body)
"Evaluate forms in the BODY with series columns bound to
symbols and/or accessor functions.
AT is an index at which series is accessed. A special value of :default
means the last record in the series.
VAR is a variable to which SERIES is bound. If not specified then it will
be gensym'ed.
Iteration:
If :FOR VAR or :ITER VAR or :FROM INDEX keyword is specified, then
BODY will be iterated over all elements of the series, with index
variable bound to :FOR or :ITER variable. If :FROM is specified
but :FOR/:ITER is not, index variable will be gensymed. If :FOR was
used then iteration is done using (LOOP) and if :ITER was specified
then iteration is done using (ITERATE).
When doing iteration the :default value of AT keyword will be the
iteration index variable instead of last element of the series
Columns/Accessors:
COLUMNS is a list of columns to bind to symbols. Each list element is
either a symbol of one of the columns in teh series, or it can also
have a form of (COL [:as SYMBOL] [:at INDEX]), to use different
binding symbol from the column name, or bind it at different position
then the entire series AT parameter.
A special value of :ALL for the COLUMNS (which is a default) causes
all columns to be bound under their own column names.
ACCESORS is a similar list of columns to bind, except these are bound
to macrolets that accept index parameter.. Eath element of ACCESSORS
can be a symbol name, or a (COL [:as symbol])
The corresponding macrolets for each column will have the form
of: (COL index &rest indexes). If only one index is specified, then
macrolets will expand into SETF'able AREF form. If multiple indexes
are specified, they will expand into (VALUES) form returning elements
at each specified index.
"
(with-series-expander
',series-name
series at
(if (eq columns :all) ',all-column-names columns)
(if (eq accessors :all) ',all-column-names accessors)
var
for
iter
from
below
to
body))
(defmacro ,(series-desc-series-add desc)
(series &key ,@(mapcar (lambda (column)
(if (series-column-default column)
`(,(series-column-name column)
',(series-column-default column))
(series-column-name column)))
columns))
(series-add-expander ',series-name series ,@all-column-names))
(defmacro ,(series-desc-series-add-from desc)
(series1 series2 &key (at :default))
"Add the record at specified index from SERIES1 to the end of SERIES1"
(series-add-from-expander ',series-name series1 series2 at))
(defun ,(series-desc-series-add-values desc)
(series ,@all-column-names)
"Add the specified values at the end of the SERIES"
,(apply #'series-add-expander series-name 'series all-column-names))
(proclaim '(inline ,(series-desc-check-capacity desc)))
(defun ,(series-desc-check-capacity desc)
(series size)
"Check if series capacacity is above the specified size,
and increase it if nessesary"
(declare (type series series) (type fixnum size))
(when (> size (series-capacity series))
(,(series-desc-increase-capacity desc) series size))
(values))
(defun ,(series-desc-increase-capacity desc)
(series size)
"Increase the series capacity to at least specified size"
(declare (type series series)
(type fixnum size))
(iter
(while (< (series-capacity series) size))
(setf (series-capacity series)
(cond ((zerop (series-capacity series))
1)
((< (series-capacity series) 512)
(the fixnum (* 8 (series-capacity series))))
(t
(the fixnum (* 2 (series-capacity series)))))))
,@(iter (for column in columns)
(let ((accessor
(series-column-array-accessor column))
(array-type (series-column-array-type column)))
(collect
`(setf
(,accessor series)
(let ((array (make-array
(series-capacity series)
:element-type ',(series-column-column-type column)))
(old (,accessor series)))
(declare (type ,array-type array)
(type (or ,array-type null array)))
(when old
(replace array (the ,array-type old)))
array)))))
(values))))))))
(defc (class nce) series-definer (struct-definer)
())
(def method expand-definer ((definer series-definer))
(let* ((form (%defseries `(,(name-of definer) ,@(struct-options-of definer))
(slot-descs-of definer)))
(series (gethash (name-of definer) *series*)))
`(progn
,form
,@(when (has-option-p definer #\e)
(export-now-and-later
(cons (name-of definer)
(rest series)))) ;; everything after columns are symbols
,@(when (has-option-p definer #\s)
(export-now-and-later
(mapcar #'series-column-name (series-desc-columns series))))
,@(when (has-option-p definer #\a)
(export-now-and-later
(let ((series (gethash (name-of definer) *series*)))
(mapcar #'series-column-array-accessor
(series-desc-columns series))))))))
(def (macro e) defseries (series-name-and-options &rest column-specs)
(%defseries series-name-and-options column-specs))