/[flexichain]/flexichain/flexichain.lisp
ViewVC logotype

Contents of /flexichain/flexichain.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (hide annotations) (vendor branch)
Thu Feb 9 02:51:06 2006 UTC (8 years, 2 months ago) by rkreuter
Branch: clnet
CVS Tags: release-1-1, initial
Changes since 1.1: +0 -0 lines
Initial checkin.
1 rkreuter 1.1 ;;; Flexichain
2     ;;; Flexichain data structure definition
3     ;;;
4     ;;; Copyright (C) 2003-2004 Robert Strandh (strandh@labri.fr)
5     ;;; Copyright (C) 2003-2004 Matthieu Villeneuve (matthieu.villeneuve@free.fr)
6     ;;;
7     ;;; This library is free software; you can redistribute it and/or
8     ;;; modify it under the terms of the GNU Lesser General Public
9     ;;; License as published by the Free Software Foundation; either
10     ;;; version 2.1 of the License, or (at your option) any later version.
11     ;;;
12     ;;; This library is distributed in the hope that it will be useful,
13     ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14     ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15     ;;; Lesser General Public License for more details.
16     ;;;
17     ;;; You should have received a copy of the GNU Lesser General Public
18     ;;; License along with this library; if not, write to the Free Software
19     ;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20    
21    
22     (in-package :flexichain)
23    
24     (defclass flexichain ()
25     ((element-type :initarg :element-type :initform t)
26     (fill-element :initarg :fill-element)
27     (expand-factor :initarg :expand-factor :initform 1.5)
28     (min-size :initarg :min-size :initform 5))
29     (:documentation "The protocol class for flexichains."))
30    
31     (defmethod initialize-instance :after ((chain flexichain) &rest initargs
32     &key initial-contents)
33     (declare (ignore initargs initial-contents))
34     (with-slots (expand-factor min-size) chain
35     (assert (> expand-factor 1) ()
36     'flexichain-initialization-error
37     :cause "EXPAND-FACTOR should be greater than 1.")
38     (assert (> min-size 0) ()
39     'flexichain-initialization-error
40     :cause "MIN-SIZE should be greater than 0."))
41     (if (slot-boundp chain 'fill-element)
42     (with-slots (element-type fill-element) chain
43     (assert (typep fill-element element-type) ()
44     'flexichain-initialization-error
45     :cause (format nil "FILL-ELEMENT ~A not of type ~S."
46     fill-element element-type)))
47     (multiple-value-bind (element foundp)
48     (find-if-2 (lambda (x)
49     (typep x (slot-value chain 'element-type)))
50     '(nil 0 #\a))
51     (if foundp
52     (setf (slot-value chain 'fill-element) element)
53     (error 'flexichain-initialization-error
54     :cause
55     "FILL-ELEMENT not provided, no default applicable.")))))
56    
57     (define-condition flexi-error (simple-error)
58     ())
59    
60     (define-condition flexi-initialization-error (flexi-error)
61     ((cause :reader flexi-initialization-error-cause
62     :initarg :cause :initform ""))
63     (:report (lambda (condition stream)
64     (format stream "Error initializing FLEXICHAIN (~S)"
65     (flexi-initialization-error-cause condition)))))
66    
67     (define-condition flexi-position-error (flexi-error)
68     ((chain :reader flexi-position-error-chain
69     :initarg :chain :initform nil)
70     (position :reader flexi-position-error-position
71     :initarg :position :initform nil))
72     (:report (lambda (condition stream)
73     (format stream "Position ~D out of bounds in ~A"
74     (flexi-position-error-position condition)
75     (flexi-position-error-chain condition)))))
76    
77     (define-condition flexi-incompatible-type-error (flexi-error)
78     ((chain :reader flexi-incompatible-type-error-chain
79     :initarg :chain :initform nil)
80     (element :reader flexi-incompatible-type-error-element
81     :initarg :element :initform nil))
82     (:report (lambda (condition stream)
83     (let ((element (flexi-incompatible-type-error-element
84     condition)))
85     (format stream "Element ~A of type ~A cannot be inserted in ~A"
86     element
87     (type-of element)
88     (flexi-incompatible-type-error-chain condition))))))
89    
90     (defgeneric nb-elements (chain)
91     (:documentation "Returns the number of elements in the flexichain."))
92    
93     (defgeneric flexi-empty-p (chain)
94     (:documentation "Checks whether CHAIN is empty or not."))
95    
96     (defgeneric insert* (chain position object)
97     (:documentation "Inserts an object before the element at POSITION
98     in the chain. If POSITION is out of range (less than 0 or greater
99     than the length of CHAIN, the FLEXI-POSITION-ERROR condition will be
100     signaled."))
101    
102     (defgeneric delete* (chain position)
103     (:documentation "Deletes an element at POSITION of the chain.
104     If POSITION is out of range (less than 0 or greater than or equal
105     to the length of CHAIN, the FLEXI-POSITION-ERROR condition
106     will be signaled."))
107    
108     (defgeneric element* (chain position)
109     (:documentation "Returns the element at POSITION of the chain.
110     If POSITION is out of range (less than 0 or greater than or equal
111     to the length of CHAIN, the FLEXI-POSITION-ERROR condition
112     will be signaled."))
113    
114     (defgeneric (setf element*) (object chain position)
115     (:documentation "Replaces the element at POSITION of CHAIN by OBJECT.
116     If POSITION if out of range (less than 0 or greater than or equal to
117     the length of CHAIN, the FLEXI-POSITION-ERROR condition will be signaled."))
118    
119     (defgeneric push-start (chain object)
120     (:documentation "Inserts an object at the beginning of CHAIN."))
121    
122     (defgeneric push-end (chain object)
123     (:documentation "Inserts an object at the end of CHAIN."))
124    
125     (defgeneric pop-start (chain)
126     (:documentation "Pops and returns the element at the beginning of CHAIN."))
127    
128     (defgeneric pop-end (chain)
129     (:documentation "Pops and returns the element at the end of CHAIN."))
130    
131     (defgeneric rotate (chain &optional n)
132     (:documentation "Rotates the elements of CHAIN so that the element
133     that used to be at position N is now at position 0. With a negative
134     value of N, rotates the elements so that the element that used to be
135     at position 0 is now at position N."))
136    
137     (defclass standard-flexichain (flexichain)
138     ((buffer)
139     (gap-start)
140     (gap-end)
141     (data-start))
142     (:documentation "The standard instantiable subclass of FLEXICHAIN."))
143    
144     (defun required-space (chain nb-elements)
145     (with-slots (min-size expand-factor) chain
146     (+ 2 (max (ceiling (* nb-elements expand-factor))
147     min-size))))
148    
149     (defmethod initialize-instance :after ((chain standard-flexichain)
150     &rest initargs
151     &key initial-contents)
152     (declare (ignore initargs))
153     ;; Check initial-contents if provided
154     (unless (null initial-contents)
155     (with-slots (element-type) chain
156     (multiple-value-bind (offending-element foundp)
157     (find-if-2 (lambda (x)
158     (not (typep x element-type)))
159     initial-contents)
160     (assert (not foundp) ()
161     'flexi-initialization-error
162     :cause (format nil "Initial element ~A not of type ~S."
163     offending-element element-type)))))
164     ;; Initialize slots
165     (with-slots (element-type fill-element buffer) chain
166     (let* ((data-length (length initial-contents))
167     (size (required-space chain data-length))
168     (fill-size (- size data-length 2))
169     (sentinel-list (make-list 2 :initial-element fill-element))
170     (fill-list (make-list fill-size :initial-element fill-element)))
171     (setf buffer
172     (make-array size
173     :element-type element-type
174     :initial-contents (concatenate 'list
175     sentinel-list
176     initial-contents
177     fill-list)))))
178     (with-slots (gap-start gap-end data-start) chain
179     (setf gap-start (+ 2 (length initial-contents))
180     gap-end 0
181     data-start 1)))
182    
183     (defmacro with-virtual-gap ((bl ds gs ge) chain &body body)
184     (let ((c (gensym)))
185     `(let* ((,c ,chain)
186     (,bl (length (slot-value ,c 'buffer)))
187     (,ds (slot-value ,c 'data-start))
188     (,gs (slot-value ,c 'gap-start))
189     (,ge (slot-value ,c 'gap-end)))
190     (declare (ignorable ,bl ,ds ,gs ,ge))
191     (when (< ,gs ,ds) (incf ,gs ,bl))
192     (when (< ,ge ,ds) (incf ,ge ,bl))
193     ,@body)))
194    
195     (defmethod nb-elements ((chain standard-flexichain))
196     (with-virtual-gap (bl ds gs ge) chain
197     (- bl (- ge gs) 2)))
198    
199     (defmethod flexi-empty-p ((chain standard-flexichain))
200     (zerop (nb-elements chain)))
201    
202     (defun position-index (chain position)
203     "Returns the (0 indexed) index of the POSITION-th element
204     of the CHAIN in the buffer."
205     (with-virtual-gap (bl ds gs ge) chain
206     (let ((index (+ ds position 1)))
207     (when (>= index gs)
208     (incf index (- ge gs)))
209     (when (>= index bl)
210     (decf index bl))
211     index)))
212    
213     (defun index-position (chain index)
214     "Returns the position corresponding to the INDEX in the CHAIN.
215     Note: the result is undefined if INDEX is not the index of a valid
216     element of the CHAIN."
217     (with-virtual-gap (bl ds gs ge) chain
218     (when (< index ds)
219     (incf index bl))
220     (when (>= index ge)
221     (decf index (- ge gs)))
222     (- index ds 1)))
223    
224     (defun ensure-gap-position (chain position)
225     (move-gap chain (position-index chain position)))
226    
227     (defun ensure-room (chain nb-elements)
228     (with-slots (buffer) chain
229     (when (> nb-elements (- (length buffer) 2))
230     (increase-buffer-size chain nb-elements))))
231    
232     (defmethod insert* ((chain standard-flexichain) position object)
233     (with-slots (element-type buffer gap-start) chain
234     (assert (<= 0 position (nb-elements chain)) ()
235     'flexi-position-error :chain chain :position position)
236     (assert (typep object element-type) ()
237     'flexi-incompatible-type-error :element object :chain chain)
238     (ensure-gap-position chain position)
239     (ensure-room chain (1+ (nb-elements chain)))
240     (setf (aref buffer gap-start) object)
241     (incf gap-start)
242     (when (= gap-start (length buffer))
243     (setf gap-start 0))))
244    
245     (defmethod insert-vector* ((chain standard-flexichain) position vector)
246     (with-slots (element-type buffer gap-start) chain
247     (assert (<= 0 position (nb-elements chain)) ()
248     'flexi-position-error :chain chain :position position)
249     (assert (typep (array-element-type vector) element-type) ()
250     'flexi-incompatible-type-error :element vector :chain chain)
251     (ensure-gap-position chain position)
252     (ensure-room chain (+ (nb-elements chain) (length vector)))
253     (loop for elem across vector
254     do (setf (aref buffer gap-start) elem)
255     (incf gap-start)
256     (when (= gap-start (length buffer))
257     (setf gap-start 0)))))
258    
259     (defmethod delete* ((chain standard-flexichain) position)
260     (with-slots (buffer expand-factor min-size fill-element gap-end) chain
261     (assert (< -1 position (nb-elements chain)) ()
262     'flexi-position-error :chain chain :position position)
263     (ensure-gap-position chain position)
264     (setf (aref buffer gap-end) fill-element)
265     (incf gap-end)
266     (when (= gap-end (length buffer))
267     (setf gap-end 0))
268     (when (and (> (length buffer) (+ min-size 2))
269     (< (+ (nb-elements chain) 2) (/ (length buffer) (square expand-factor))))
270     (decrease-buffer-size chain))))
271    
272     (defmethod element* ((chain standard-flexichain) position)
273     (with-slots (buffer) chain
274     (assert (< -1 position (nb-elements chain)) ()
275     'flexi-position-error :chain chain :position position)
276     (aref buffer (position-index chain position))))
277    
278     (defmethod (setf element*) (object (chain standard-flexichain) position)
279     (with-slots (buffer element-type) chain
280     (assert (< -1 position (nb-elements chain)) ()
281     'flexi-position-error :chain chain :position position)
282     (assert (typep object element-type) ()
283     'flexi-incompatible-type-error :chain chain :element object)
284     (setf (aref buffer (position-index chain position)) object)))
285    
286     (defmethod push-start ((chain standard-flexichain) object)
287     (insert* chain 0 object))
288    
289     (defmethod push-end ((chain standard-flexichain) object)
290     (insert* chain (nb-elements chain) object))
291    
292     (defmethod pop-start ((chain standard-flexichain))
293     (prog1 (element* chain 0)
294     (delete* chain 0)))
295    
296     (defmethod pop-end ((chain standard-flexichain))
297     (let ((position (1- (nb-elements chain))))
298     (prog1 (element* chain position)
299     (delete* chain position))))
300    
301     (defmethod rotate ((chain standard-flexichain) &optional (n 1))
302     (when (> (nb-elements chain) 1)
303     (cond ((plusp n) (loop repeat n do (push-start chain (pop-end chain))))
304     ((minusp n) (loop repeat (- n) do (push-end chain (pop-start chain))))
305     (t nil))))
306    
307     (defun move-gap (chain hot-spot)
308     "Moves the elements and gap inside the buffer so that
309     the element currently at HOT-SPOT becomes the first element following
310     the gap, or does nothing if there are no elements."
311     (with-slots (gap-start gap-end) chain
312     (unless (= hot-spot gap-end)
313     (case (gap-location chain)
314     (:gap-empty (move-empty-gap chain hot-spot))
315     (:gap-left (move-left-gap chain hot-spot))
316     (:gap-right (move-right-gap chain hot-spot))
317     (:gap-middle (move-middle-gap chain hot-spot))
318     (:gap-non-contiguous (move-non-contiguous-gap chain hot-spot))))
319     (values gap-start gap-end)))
320    
321     (defun move-empty-gap (chain hot-spot)
322     "Moves the gap. Handles the case where the gap is empty."
323     (with-slots (gap-start gap-end) chain
324     (setf gap-start hot-spot
325     gap-end hot-spot)))
326    
327     (defun move-left-gap (chain hot-spot)
328     "Moves the gap. Handles the case where the gap is
329     on the left of the buffer."
330     (with-slots (buffer gap-start gap-end data-start) chain
331     (let ((buffer-size (length buffer)))
332     (cond ((< (- hot-spot gap-end) (- buffer-size hot-spot))
333     (push-elements-left chain (- hot-spot gap-end)))
334     ((<= (- buffer-size hot-spot) gap-end)
335     (hop-elements-left chain (- buffer-size hot-spot)))
336     (t
337     (hop-elements-left chain (- gap-end gap-start))
338     (push-elements-right chain (- gap-start hot-spot)))))))
339    
340     (defun move-right-gap (chain hot-spot)
341     "Moves the gap. Handles the case where the gap is
342     on the right of the buffer."
343     (with-slots (buffer gap-start gap-end) chain
344     (let ((buffer-size (length buffer)))
345     (cond ((< (- gap-start hot-spot) hot-spot)
346     (push-elements-right chain (- gap-start hot-spot)))
347     ((<= hot-spot (- buffer-size gap-start))
348     (hop-elements-right chain hot-spot))
349     (t
350     (hop-elements-right chain (- buffer-size gap-start))
351     (push-elements-left chain (- hot-spot gap-end)))))))
352    
353     (defun move-middle-gap (chain hot-spot)
354     "Moves the gap. Handles the case where the gap is
355     in the middle of the buffer."
356     (with-slots (buffer gap-start gap-end) chain
357     (let ((buffer-size (length buffer)))
358     (cond ((< hot-spot gap-start)
359     (cond ((<= (- gap-start hot-spot)
360     (+ (- buffer-size gap-end) hot-spot))
361     (push-elements-right chain (- gap-start hot-spot)))
362     (t
363     (push-elements-left chain (- buffer-size gap-end))
364     (move-right-gap chain hot-spot))))
365     (t
366     (cond ((< (- hot-spot gap-end)
367     (+ (- buffer-size hot-spot) gap-start))
368     (push-elements-left chain (- hot-spot gap-end)))
369     (t
370     (push-elements-right chain gap-start)
371     (move-left-gap chain hot-spot))))))))
372    
373     (defun move-non-contiguous-gap (chain hot-spot)
374     "Moves the gap. Handles the case where the gap is in 2 parts,
375     on both ends of the buffer."
376     (with-slots (buffer gap-start gap-end) chain
377     (let ((buffer-size (length buffer)))
378     (cond ((< (- hot-spot gap-end) (- gap-start hot-spot))
379     (hop-elements-right chain (min (- buffer-size gap-start)
380     (- hot-spot gap-end)))
381     (let ((nb-left (- hot-spot gap-end)))
382     (unless (zerop nb-left)
383     (push-elements-left chain nb-left))))
384     (t
385     (hop-elements-left chain (min gap-end (- gap-start hot-spot)))
386     (let ((nb-right (- gap-start hot-spot)))
387     (unless (zerop nb-right)
388     (push-elements-right chain nb-right))))))))
389    
390     (defgeneric move-elements (standard-flexichain to from start1 start2 end2)
391     (:documentation "move elements of a flexichain and adjust data-start"))
392    
393     (defmethod move-elements ((fc standard-flexichain) to from start1 start2 end2)
394     (replace to from :start1 start1 :start2 start2 :end2 end2)
395     (with-slots (data-start) fc
396     (when (and (<= start2 data-start) (< data-start end2))
397     (incf data-start (- start1 start2)))))
398    
399     (defgeneric fill-gap (standard-flexichain start end)
400     (:documentation "fill part of gap with the fill element"))
401    
402     (defmethod fill-gap ((fc standard-flexichain) start end)
403     (with-slots (buffer fill-element) fc
404     (fill buffer fill-element :start start :end end)))
405    
406     (defun push-elements-left (chain count)
407     "Pushes the COUNT elements of CHAIN at the right of the gap,
408     to the beginning of the gap. The gap must be continuous. Example:
409     PUSH-ELEMENTS-LEFT abcd-----efghijklm 2 => abcdef-----ghijklm"
410     (with-slots (buffer gap-start gap-end) chain
411     (move-elements chain buffer buffer gap-start gap-end (+ gap-end count))
412     (fill-gap chain (max gap-end (+ gap-start count)) (+ gap-end count))
413     (incf gap-start count)
414     (incf gap-end count)
415     (normalize-indices chain)))
416    
417     (defun push-elements-right (chain count)
418     "Pushes the COUNT elements of CHAIN at the left of the gap,
419     to the end of the gap. The gap must be continuous. Example:
420     PUSH-ELEMENTS-RIGHT abcd-----efghijklm 2 => ab-----cdefghijklm"
421     (with-slots (buffer gap-start gap-end) chain
422     (let* ((buffer-size (length buffer))
423     (rotated-gap-end (if (zerop gap-end) buffer-size gap-end)))
424     (move-elements chain buffer buffer
425     (- rotated-gap-end count) (- gap-start count) gap-start)
426     (fill-gap chain (- gap-start count) (min gap-start (- rotated-gap-end count)))
427     (decf gap-start count)
428     (setf gap-end (- rotated-gap-end count))
429     (normalize-indices chain))))
430    
431     (defun hop-elements-left (chain count)
432     "Moves the COUNT rightmost elements to the end of the gap,
433     on the left of the data. Example:
434     HOP-ELEMENTS-LEFT ---abcdefghijklm--- 2 => -lmabcdefghijk-----"
435     (with-slots (buffer gap-start gap-end) chain
436     (let* ((buffer-size (length buffer))
437     (rotated-gap-start (if (zerop gap-start) buffer-size gap-start)))
438     (move-elements chain buffer buffer
439     (- gap-end count) (- rotated-gap-start count) rotated-gap-start)
440     (fill-gap chain (- rotated-gap-start count) rotated-gap-start)
441     (setf gap-start (- rotated-gap-start count))
442     (decf gap-end count)
443     (normalize-indices chain))))
444    
445     (defun hop-elements-right (chain count)
446     "Moves the COUNT leftmost elements to the beginning of the gap,
447     on the right of the data. Example:
448     HOP-ELEMENTS-RIGHT ---abcdefghijklm--- 2 => -----cdefghijklmab-"
449     (with-slots (buffer gap-start gap-end) chain
450     (move-elements chain buffer buffer gap-start gap-end (+ gap-end count))
451     (fill-gap chain gap-end (+ gap-end count))
452     (incf gap-start count)
453     (incf gap-end count)
454     (normalize-indices chain)))
455    
456     (defun increase-buffer-size (chain nb-elements)
457     (resize-buffer chain (required-space chain nb-elements)))
458    
459     (defun decrease-buffer-size (chain)
460     (resize-buffer chain (required-space chain (nb-elements chain))))
461    
462     (defgeneric resize-buffer (standard-flexichain new-buffer-size)
463     (:documentation "allocate a new buffer with the size indicated"))
464    
465     (defmethod resize-buffer ((fc standard-flexichain) new-buffer-size)
466     (with-slots (buffer gap-start gap-end
467     fill-element element-type expand-factor) fc
468     (let ((buffer-size (length buffer))
469     (buffer-after (make-array new-buffer-size
470     :element-type element-type
471     :initial-element fill-element)))
472     (case (gap-location fc)
473     ((:gap-empty :gap-middle)
474     (move-elements fc buffer-after buffer 0 0 gap-start)
475     (let ((gap-end-after (- new-buffer-size (- buffer-size gap-end))))
476     (move-elements fc buffer-after buffer gap-end-after gap-end buffer-size)
477     (setf gap-end gap-end-after)))
478     (:gap-right
479     (move-elements fc buffer-after buffer 0 0 gap-start))
480     (:gap-left
481     (let ((gap-end-after (- new-buffer-size (+ 2 (nb-elements fc)))))
482     (move-elements fc buffer-after buffer gap-end-after gap-end buffer-size)
483     (setf gap-end gap-end-after)))
484     (:gap-non-contiguous
485     (move-elements fc buffer-after buffer 0 gap-end gap-start)
486     (decf gap-start gap-end)
487     (setf gap-end 0)))
488     (setf buffer buffer-after)))
489     (normalize-indices fc))
490    
491     (defun normalize-indices (chain)
492     "Sets gap limits to 0 if they are at the end of the buffer."
493     (with-slots (buffer gap-start gap-end data-start) chain
494     (let ((buffer-size (length buffer)))
495     (when (>= data-start buffer-size)
496     (setf data-start 0))
497     (when (>= gap-start buffer-size)
498     (setf gap-start 0))
499     (when (>= gap-end buffer-size)
500     (setf gap-end 0)))))
501    
502     (defun gap-location (chain)
503     "Returns a keyword indicating the general location of the gap."
504     (with-slots (buffer gap-start gap-end) chain
505     (cond ((= gap-start gap-end) :gap-empty)
506     ((and (zerop gap-start) (>= gap-end 0)) :gap-left)
507     ((and (zerop gap-end) (> gap-start 0)) :gap-right)
508     ((> gap-end gap-start) :gap-middle)
509     (t :gap-non-contiguous))))
510    

  ViewVC Help
Powered by ViewVC 1.1.5