-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtable.lisp
74 lines (58 loc) · 2.03 KB
/
table.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
;;;; Simple table data structure.
;;;; Copyright (c) 2013, Francisco Soto All rights reserved (see COPYING file for details).
(in-package :cl-simple-table)
(deftype row ()
"Table row type."
`(vector t *))
(deftype table ()
"Table type."
`(vector row *))
(defun make-table ()
"Creates a table."
(make-array 1 :element-type 'row :fill-pointer 0 :adjustable t))
(defun make-row ()
"Create a row."
(make-array 1 :fill-pointer 0 :adjustable t))
(defun add-to-table (row table)
"Appends a row to the table."
(vector-push-extend row table)
table)
(defun add-to-row (value row)
"Append a column to row and set it to the given value."
(vector-push-extend value row)
row)
(defun get-row (index table)
"Returns the row in the given index inside the table."
(elt table index))
(defun get-row-column (column row)
"Gets the value in the given column inside row."
(elt row column))
(defun set-row-column (column value row)
"Sets the value of the given column inside the row."
(setf (elt row column) value)
row)
(defun num-rows (table)
"Returns the number of rows in the table."
(length table))
(defun num-cols (row)
"Returns the number of elements in this row."
(length row))
(defun rectangular-table-p (table)
"Returns true if all the rows in the table have the same number of elements."
(or (= (num-rows table) 0)
(let ((cols (num-cols (get-row 0 table))))
(every (lambda (row)
(eql (num-cols row) cols))
table))))
(defun sequence->row (elements)
"Converts a sequence of elements into a table row."
(coerce elements 'row))
(defun row-sequence->table (rows)
"Converts a sequence of rows into a table."
(coerce rows 'table))
(defmacro with-rows ((table row-var &optional return-expression) &body body)
"Iterates the rows in the given table, row-var is the current row, returning return-expression."
(let ((iterator (gensym)))
`(dotimes (,iterator (num-rows ,table) ,return-expression)
(let ((,row-var (get-row ,iterator ,table)))
,@body))))