-
Notifications
You must be signed in to change notification settings - Fork 8
/
mmap-mcons.lisp
186 lines (142 loc) · 4.34 KB
/
mmap-mcons.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
;;;;; -*- mode: common-lisp; common-lisp-style: modern; coding: utf-8; -*-
;;;;;
(in-package :mm)
(defmmclass mcons (marray)
((mcar :reader mcar :persistent nil)
(mcdr :reader mcdr :persistent nil)))
(defmethod slot-unbound (class (instance mcons) (slot-name (eql 'mcar)))
(setf (slot-value instance slot-name) (marray-ref instance 0)))
(defmethod slot-unbound (class (instance mcons) (slot-name (eql 'mcdr)))
(setf (slot-value instance slot-name) (marray-ref instance 1)))
(defgeneric mcar (cell))
(defgeneric mcdr (cell))
(defgeneric mcadr (cell))
(defgeneric mcddr (cell))
(defgeneric (setf mcar) (value cell))
(defgeneric (setf mcdr) (value cell))
(defgeneric mconsp (thing))
(defgeneric emptyp (thing))
(defgeneric as-list (thing))
(defgeneric mpush (element place))
(defgeneric mpop (place))
(defmethod mcar ((cell null))
nil)
(defmethod mcdr ((cell null))
nil)
(defmethod mcadr ((cell mcons))
(mcar (mcdr cell)))
(defmethod mcddr ((cell mcons))
(mcdr (mcdr cell)))
(defmethod (setf mcar) (value (mcons mcons))
(setf (marray-ref mcons 0) value)
(setf (slot-value mcons 'mcar) value))
(defmethod (setf mcdr) (value (mcons mcons))
(setf (marray-ref mcons 1) value)
(setf (slot-value mcons 'mcdr) value))
(defun mcons (mcar mcdr)
(make-marray 2 :marray-class 'mcons :initial-contents (list mcar mcdr)))
(defmethod mconsp (thing)
nil)
(defmethod mconsp ((thing mcons))
t)
(defmethod mconsp ((thing null))
t)
(defmethod print-object ((self mcons) stream)
(print-unreadable-object (self stream)
(loop
initially (format stream "(")
for first = self then (mcdr first)
while first do (typecase first
(mcons (if (mcar first)
(format stream " ~S" (mcar first))
(loop-finish)))
(atom (format stream " . ~S" first) (loop-finish))
(null (loop-finish)))
finally (format stream " )"))))
(defun empty ()
(mcons nil nil))
(defmethod emptyp ((cell null))
t)
(defmethod emptyp ((cell mcons))
(null (mcar cell)))
(defun mlist (&rest args)
(loop with new = (mcons nil nil)
for first = (nreverse args) then (cdr first)
while first do (setf new (mcons (car first) new))
finally (return new)))
(defmethod as-list ((mlist mcons))
(loop
for first = mlist then (mcdr first)
when (not (mconsp first)) collect first
when (and first (mconsp first) (not (emptyp first))) collect (mcar first)
until (or (not (mconsp first)) (emptyp first))))
;; (as-list (mlist :a :b :c :D)) => (:A :B :C :D)
;; (as-list (mcons :a (mcons :b :c))) => (:A :B :C)
(defmethod mpush (element (mlist null))
(mcons element (empty)))
(defmethod mpush (element (mlist mcons))
(prog1 mlist
(let ((new (mcons (mcar mlist) (mcdr mlist))))
(setf (mcar mlist) element)
(setf (mcdr mlist) new))))
(defmethod mpop ((mlist null))
nil)
(defmethod mpop ((mlist mcons))
(prog1 (mcar mlist)
(let* ((next-cell (mcdr mlist))
(next-val (mcar next-cell))
(rest (mcdr next-cell)))
(if (not (emptyp next-cell))
(setf
(mcar mlist) next-val
(mcdr mlist) rest)
(setf
(mcar mlist) nil
(mcdr mlist) nil
mlist nil)
))))
#|
(let((m (mlist 1 2 3 4)))
(print (eq m (mpush 0 m)))
(describe m)
(terpri)
(print (mpop m))
(describe m)
(let ((m0 m))
(print (mpop m))
(print (eq m m0)))
(print (mpop m))
(print (mpop m))
(print (mpop m))
(print (mpop m))
(describe m)
(terpri))
(let ((m (apply #'mlist *features*)))
(loop
until (emptyp m)
with z = (empty)
with y = (length *features*)
for i from 1 to (+ 10 y)
do (mpush (print (mpop m)) z)
finally (print (list i y m z))))
(let ((x (mcons :a :b)))
(prog1 x
(setf (mcar x) :c)
(print x)
(setf (mcdr x) :d)
(print x)
(setf (mcar x) nil)
(print x)
))
(mapcar (lambda (x)
(describe x)
(mcar x)
(mcdr x)
(describe x))
(list (mcons 1 (mcons :a (mcons :b (mcons 'mcar nil))))))
(defmmclass mlink (marray)
((prev :reader prev :persistent nil)
(next :reader next :persistent nil)
(lock :accessor lock :persistent nil :initform (bt:make-lock))
(segment :reader segment :persistent nil)))
|#