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

Contents of /flexichain/flexichain.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Sat Jan 26 11:23:09 2008 UTC (6 years, 2 months ago) by rstrandh
Branch: MAIN
Changes since 1.4: +12 -3 lines
Patch from Troels Henriksen.
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 rstrandh 1.2 (defgeneric insert-vector* (chain position vector)
103     (:documentation "Inserts the elements of VECTOR before the
104     element at POSITION in the chain. If POSITION is out of
105     range (less than 0 or greater than the length of CHAIN, the
106     FLEXI-POSITION-ERROR condition will be signaled."))
107    
108 rkreuter 1.1 (defgeneric delete* (chain position)
109     (:documentation "Deletes an 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 charmon 1.4 (defgeneric delete-elements* (chain position n)
115     (:documentation "Delete N elements at POSITION of the chain. If
116     POSITION+N is out of range (less than 0 or greater than or equal
117     to the length of CHAIN, the FLEXI-POSITION-ERROR condition will
118     be signaled. N can be negative, in which case elements will be
119     deleted before POSITION."))
120    
121 rkreuter 1.1 (defgeneric element* (chain position)
122     (:documentation "Returns the element at POSITION of the chain.
123     If POSITION is out of range (less than 0 or greater than or equal
124     to the length of CHAIN, the FLEXI-POSITION-ERROR condition
125     will be signaled."))
126    
127     (defgeneric (setf element*) (object chain position)
128     (:documentation "Replaces the element at POSITION of CHAIN by OBJECT.
129     If POSITION if out of range (less than 0 or greater than or equal to
130     the length of CHAIN, the FLEXI-POSITION-ERROR condition will be signaled."))
131    
132     (defgeneric push-start (chain object)
133     (:documentation "Inserts an object at the beginning of CHAIN."))
134    
135     (defgeneric push-end (chain object)
136     (:documentation "Inserts an object at the end of CHAIN."))
137    
138     (defgeneric pop-start (chain)
139     (:documentation "Pops and returns the element at the beginning of CHAIN."))
140    
141     (defgeneric pop-end (chain)
142     (:documentation "Pops and returns the element at the end of CHAIN."))
143    
144     (defgeneric rotate (chain &optional n)
145     (:documentation "Rotates the elements of CHAIN so that the element
146     that used to be at position N is now at position 0. With a negative
147     value of N, rotates the elements so that the element that used to be
148     at position 0 is now at position N."))
149    
150     (defclass standard-flexichain (flexichain)
151     ((buffer)
152     (gap-start)
153     (gap-end)
154     (data-start))
155     (:documentation "The standard instantiable subclass of FLEXICHAIN."))
156    
157     (defun required-space (chain nb-elements)
158     (with-slots (min-size expand-factor) chain
159     (+ 2 (max (ceiling (* nb-elements expand-factor))
160     min-size))))
161    
162     (defmethod initialize-instance :after ((chain standard-flexichain)
163     &rest initargs
164 charmon 1.3 &key
165     initial-contents
166     (initial-nb-elements 0)
167     (initial-element nil))
168 rkreuter 1.1 (declare (ignore initargs))
169     ;; Check initial-contents if provided
170     (unless (null initial-contents)
171     (with-slots (element-type) chain
172     (multiple-value-bind (offending-element foundp)
173     (find-if-2 (lambda (x)
174     (not (typep x element-type)))
175     initial-contents)
176     (assert (not foundp) ()
177     'flexi-initialization-error
178     :cause (format nil "Initial element ~A not of type ~S."
179     offending-element element-type)))))
180     ;; Initialize slots
181     (with-slots (element-type fill-element buffer) chain
182 charmon 1.3 (let* ((data-length (if (> (length initial-contents) initial-nb-elements)
183     (length initial-contents)
184     initial-nb-elements))
185 rkreuter 1.1 (size (required-space chain data-length))
186     (fill-size (- size data-length 2))
187     (sentinel-list (make-list 2 :initial-element fill-element))
188     (fill-list (make-list fill-size :initial-element fill-element)))
189     (setf buffer
190 charmon 1.3 (if initial-contents
191     (make-array size
192     :element-type element-type
193     :initial-contents (concatenate 'list
194     sentinel-list
195     initial-contents
196     fill-list))
197     (let ((arr (make-array size
198     :element-type element-type
199     :initial-element initial-element)))
200     (fill arr fill-element :end (length sentinel-list))
201     (fill arr fill-element
202     :start (+ (length sentinel-list) initial-nb-elements)
203     :end size))))
204     (with-slots (gap-start gap-end data-start) chain
205     (setf gap-start (+ 2 data-length)
206     gap-end 0
207     data-start 1)))))
208 rkreuter 1.1
209     (defmacro with-virtual-gap ((bl ds gs ge) chain &body body)
210     (let ((c (gensym)))
211     `(let* ((,c ,chain)
212     (,bl (length (slot-value ,c 'buffer)))
213     (,ds (slot-value ,c 'data-start))
214     (,gs (slot-value ,c 'gap-start))
215     (,ge (slot-value ,c 'gap-end)))
216     (declare (ignorable ,bl ,ds ,gs ,ge))
217     (when (< ,gs ,ds) (incf ,gs ,bl))
218     (when (< ,ge ,ds) (incf ,ge ,bl))
219     ,@body)))
220    
221     (defmethod nb-elements ((chain standard-flexichain))
222     (with-virtual-gap (bl ds gs ge) chain
223     (- bl (- ge gs) 2)))
224    
225     (defmethod flexi-empty-p ((chain standard-flexichain))
226     (zerop (nb-elements chain)))
227    
228     (defun position-index (chain position)
229     "Returns the (0 indexed) index of the POSITION-th element
230     of the CHAIN in the buffer."
231     (with-virtual-gap (bl ds gs ge) chain
232     (let ((index (+ ds position 1)))
233     (when (>= index gs)
234     (incf index (- ge gs)))
235     (when (>= index bl)
236     (decf index bl))
237     index)))
238    
239     (defun index-position (chain index)
240     "Returns the position corresponding to the INDEX in the CHAIN.
241     Note: the result is undefined if INDEX is not the index of a valid
242     element of the CHAIN."
243     (with-virtual-gap (bl ds gs ge) chain
244     (when (< index ds)
245     (incf index bl))
246     (when (>= index ge)
247     (decf index (- ge gs)))
248     (- index ds 1)))
249    
250     (defun ensure-gap-position (chain position)
251     (move-gap chain (position-index chain position)))
252    
253     (defun ensure-room (chain nb-elements)
254     (with-slots (buffer) chain
255     (when (> nb-elements (- (length buffer) 2))
256     (increase-buffer-size chain nb-elements))))
257    
258     (defmethod insert* ((chain standard-flexichain) position object)
259     (with-slots (element-type buffer gap-start) chain
260     (assert (<= 0 position (nb-elements chain)) ()
261     'flexi-position-error :chain chain :position position)
262     (assert (typep object element-type) ()
263     'flexi-incompatible-type-error :element object :chain chain)
264     (ensure-gap-position chain position)
265     (ensure-room chain (1+ (nb-elements chain)))
266     (setf (aref buffer gap-start) object)
267     (incf gap-start)
268     (when (= gap-start (length buffer))
269     (setf gap-start 0))))
270    
271     (defmethod insert-vector* ((chain standard-flexichain) position vector)
272     (with-slots (element-type buffer gap-start) chain
273     (assert (<= 0 position (nb-elements chain)) ()
274     'flexi-position-error :chain chain :position position)
275 charmon 1.3 (assert (subtypep (array-element-type vector) element-type) ()
276 rkreuter 1.1 'flexi-incompatible-type-error :element vector :chain chain)
277     (ensure-gap-position chain position)
278     (ensure-room chain (+ (nb-elements chain) (length vector)))
279     (loop for elem across vector
280     do (setf (aref buffer gap-start) elem)
281     (incf gap-start)
282     (when (= gap-start (length buffer))
283     (setf gap-start 0)))))
284    
285     (defmethod delete* ((chain standard-flexichain) position)
286     (with-slots (buffer expand-factor min-size fill-element gap-end) chain
287     (assert (< -1 position (nb-elements chain)) ()
288     'flexi-position-error :chain chain :position position)
289     (ensure-gap-position chain position)
290     (setf (aref buffer gap-end) fill-element)
291     (incf gap-end)
292     (when (= gap-end (length buffer))
293     (setf gap-end 0))
294     (when (and (> (length buffer) (+ min-size 2))
295     (< (+ (nb-elements chain) 2) (/ (length buffer) (square expand-factor))))
296     (decrease-buffer-size chain))))
297    
298 charmon 1.4 (defmethod delete-elements* ((chain standard-flexichain) position n)
299     (unless (zerop n)
300 rstrandh 1.5 (with-slots (buffer expand-factor min-size gap-end data-start) chain
301 charmon 1.4 (when (minusp n)
302     (incf position n)
303     (setf n (* -1 n)))
304     (assert (<= 0 (+ position n) (nb-elements chain)) ()
305     'flexi-position-error :chain chain :position position)
306     (ensure-gap-position chain position)
307 rstrandh 1.5 ;; Two cases to consider - one where position+n is wholly on
308     ;; this side of the gap in buffer, and one where part of it is
309     ;; "wrapped around" to the beginning of buffer.
310     (cond ((>= (length buffer) (+ gap-end n))
311     (fill-gap chain gap-end (+ gap-end n))
312     (incf gap-end n))
313     (t (let ((surplus-elements (- n (- (length buffer) gap-end))))
314     (fill-gap chain gap-end (length buffer))
315     (fill-gap chain 0 surplus-elements)
316     (setf gap-end surplus-elements
317     data-start (1+ gap-end)))))
318 charmon 1.4 (when (= gap-end (length buffer))
319     (setf gap-end 0))
320     (when (and (> (length buffer) (+ min-size 2))
321     (< (+ (nb-elements chain) 2) (/ (length buffer) (square expand-factor))))
322     (decrease-buffer-size chain)))))
323    
324 rkreuter 1.1 (defmethod element* ((chain standard-flexichain) position)
325     (with-slots (buffer) chain
326     (assert (< -1 position (nb-elements chain)) ()
327     'flexi-position-error :chain chain :position position)
328     (aref buffer (position-index chain position))))
329    
330     (defmethod (setf element*) (object (chain standard-flexichain) position)
331     (with-slots (buffer element-type) chain
332     (assert (< -1 position (nb-elements chain)) ()
333     'flexi-position-error :chain chain :position position)
334     (assert (typep object element-type) ()
335     'flexi-incompatible-type-error :chain chain :element object)
336     (setf (aref buffer (position-index chain position)) object)))
337    
338     (defmethod push-start ((chain standard-flexichain) object)
339     (insert* chain 0 object))
340    
341     (defmethod push-end ((chain standard-flexichain) object)
342     (insert* chain (nb-elements chain) object))
343    
344     (defmethod pop-start ((chain standard-flexichain))
345     (prog1 (element* chain 0)
346     (delete* chain 0)))
347    
348     (defmethod pop-end ((chain standard-flexichain))
349     (let ((position (1- (nb-elements chain))))
350     (prog1 (element* chain position)
351     (delete* chain position))))
352    
353     (defmethod rotate ((chain standard-flexichain) &optional (n 1))
354     (when (> (nb-elements chain) 1)
355     (cond ((plusp n) (loop repeat n do (push-start chain (pop-end chain))))
356     ((minusp n) (loop repeat (- n) do (push-end chain (pop-start chain))))
357     (t nil))))
358    
359     (defun move-gap (chain hot-spot)
360     "Moves the elements and gap inside the buffer so that
361     the element currently at HOT-SPOT becomes the first element following
362     the gap, or does nothing if there are no elements."
363     (with-slots (gap-start gap-end) chain
364     (unless (= hot-spot gap-end)
365     (case (gap-location chain)
366     (:gap-empty (move-empty-gap chain hot-spot))
367     (:gap-left (move-left-gap chain hot-spot))
368     (:gap-right (move-right-gap chain hot-spot))
369     (:gap-middle (move-middle-gap chain hot-spot))
370     (:gap-non-contiguous (move-non-contiguous-gap chain hot-spot))))
371     (values gap-start gap-end)))
372    
373     (defun move-empty-gap (chain hot-spot)
374     "Moves the gap. Handles the case where the gap is empty."
375     (with-slots (gap-start gap-end) chain
376     (setf gap-start hot-spot
377     gap-end hot-spot)))
378    
379     (defun move-left-gap (chain hot-spot)
380     "Moves the gap. Handles the case where the gap is
381     on the left of the buffer."
382     (with-slots (buffer gap-start gap-end data-start) chain
383     (let ((buffer-size (length buffer)))
384     (cond ((< (- hot-spot gap-end) (- buffer-size hot-spot))
385     (push-elements-left chain (- hot-spot gap-end)))
386     ((<= (- buffer-size hot-spot) gap-end)
387     (hop-elements-left chain (- buffer-size hot-spot)))
388     (t
389     (hop-elements-left chain (- gap-end gap-start))
390     (push-elements-right chain (- gap-start hot-spot)))))))
391    
392     (defun move-right-gap (chain hot-spot)
393     "Moves the gap. Handles the case where the gap is
394     on the right of the buffer."
395     (with-slots (buffer gap-start gap-end) chain
396     (let ((buffer-size (length buffer)))
397     (cond ((< (- gap-start hot-spot) hot-spot)
398     (push-elements-right chain (- gap-start hot-spot)))
399     ((<= hot-spot (- buffer-size gap-start))
400     (hop-elements-right chain hot-spot))
401     (t
402     (hop-elements-right chain (- buffer-size gap-start))
403     (push-elements-left chain (- hot-spot gap-end)))))))
404    
405     (defun move-middle-gap (chain hot-spot)
406     "Moves the gap. Handles the case where the gap is
407     in the middle of the buffer."
408     (with-slots (buffer gap-start gap-end) chain
409     (let ((buffer-size (length buffer)))
410     (cond ((< hot-spot gap-start)
411     (cond ((<= (- gap-start hot-spot)
412     (+ (- buffer-size gap-end) hot-spot))
413     (push-elements-right chain (- gap-start hot-spot)))
414     (t
415     (push-elements-left chain (- buffer-size gap-end))
416     (move-right-gap chain hot-spot))))
417     (t
418     (cond ((< (- hot-spot gap-end)
419     (+ (- buffer-size hot-spot) gap-start))
420     (push-elements-left chain (- hot-spot gap-end)))
421     (t
422     (push-elements-right chain gap-start)
423     (move-left-gap chain hot-spot))))))))
424    
425     (defun move-non-contiguous-gap (chain hot-spot)
426     "Moves the gap. Handles the case where the gap is in 2 parts,
427     on both ends of the buffer."
428     (with-slots (buffer gap-start gap-end) chain
429     (let ((buffer-size (length buffer)))
430     (cond ((< (- hot-spot gap-end) (- gap-start hot-spot))
431     (hop-elements-right chain (min (- buffer-size gap-start)
432     (- hot-spot gap-end)))
433     (let ((nb-left (- hot-spot gap-end)))
434     (unless (zerop nb-left)
435     (push-elements-left chain nb-left))))
436     (t
437     (hop-elements-left chain (min gap-end (- gap-start hot-spot)))
438     (let ((nb-right (- gap-start hot-spot)))
439     (unless (zerop nb-right)
440     (push-elements-right chain nb-right))))))))
441    
442     (defgeneric move-elements (standard-flexichain to from start1 start2 end2)
443     (:documentation "move elements of a flexichain and adjust data-start"))
444    
445     (defmethod move-elements ((fc standard-flexichain) to from start1 start2 end2)
446     (replace to from :start1 start1 :start2 start2 :end2 end2)
447     (with-slots (data-start) fc
448     (when (and (<= start2 data-start) (< data-start end2))
449     (incf data-start (- start1 start2)))))
450    
451     (defgeneric fill-gap (standard-flexichain start end)
452     (:documentation "fill part of gap with the fill element"))
453    
454     (defmethod fill-gap ((fc standard-flexichain) start end)
455     (with-slots (buffer fill-element) fc
456     (fill buffer fill-element :start start :end end)))
457    
458     (defun push-elements-left (chain count)
459     "Pushes the COUNT elements of CHAIN at the right of the gap,
460     to the beginning of the gap. The gap must be continuous. Example:
461     PUSH-ELEMENTS-LEFT abcd-----efghijklm 2 => abcdef-----ghijklm"
462     (with-slots (buffer gap-start gap-end) chain
463     (move-elements chain buffer buffer gap-start gap-end (+ gap-end count))
464     (fill-gap chain (max gap-end (+ gap-start count)) (+ gap-end count))
465     (incf gap-start count)
466     (incf gap-end count)
467     (normalize-indices chain)))
468    
469     (defun push-elements-right (chain count)
470     "Pushes the COUNT elements of CHAIN at the left of the gap,
471     to the end of the gap. The gap must be continuous. Example:
472     PUSH-ELEMENTS-RIGHT abcd-----efghijklm 2 => ab-----cdefghijklm"
473     (with-slots (buffer gap-start gap-end) chain
474     (let* ((buffer-size (length buffer))
475     (rotated-gap-end (if (zerop gap-end) buffer-size gap-end)))
476     (move-elements chain buffer buffer
477     (- rotated-gap-end count) (- gap-start count) gap-start)
478     (fill-gap chain (- gap-start count) (min gap-start (- rotated-gap-end count)))
479     (decf gap-start count)
480     (setf gap-end (- rotated-gap-end count))
481     (normalize-indices chain))))
482    
483     (defun hop-elements-left (chain count)
484     "Moves the COUNT rightmost elements to the end of the gap,
485     on the left of the data. Example:
486     HOP-ELEMENTS-LEFT ---abcdefghijklm--- 2 => -lmabcdefghijk-----"
487     (with-slots (buffer gap-start gap-end) chain
488     (let* ((buffer-size (length buffer))
489     (rotated-gap-start (if (zerop gap-start) buffer-size gap-start)))
490     (move-elements chain buffer buffer
491     (- gap-end count) (- rotated-gap-start count) rotated-gap-start)
492     (fill-gap chain (- rotated-gap-start count) rotated-gap-start)
493     (setf gap-start (- rotated-gap-start count))
494     (decf gap-end count)
495     (normalize-indices chain))))
496    
497     (defun hop-elements-right (chain count)
498     "Moves the COUNT leftmost elements to the beginning of the gap,
499     on the right of the data. Example:
500     HOP-ELEMENTS-RIGHT ---abcdefghijklm--- 2 => -----cdefghijklmab-"
501     (with-slots (buffer gap-start gap-end) chain
502     (move-elements chain buffer buffer gap-start gap-end (+ gap-end count))
503     (fill-gap chain gap-end (+ gap-end count))
504     (incf gap-start count)
505     (incf gap-end count)
506     (normalize-indices chain)))
507    
508     (defun increase-buffer-size (chain nb-elements)
509     (resize-buffer chain (required-space chain nb-elements)))
510    
511     (defun decrease-buffer-size (chain)
512     (resize-buffer chain (required-space chain (nb-elements chain))))
513    
514     (defgeneric resize-buffer (standard-flexichain new-buffer-size)
515     (:documentation "allocate a new buffer with the size indicated"))
516    
517     (defmethod resize-buffer ((fc standard-flexichain) new-buffer-size)
518     (with-slots (buffer gap-start gap-end
519     fill-element element-type expand-factor) fc
520     (let ((buffer-size (length buffer))
521     (buffer-after (make-array new-buffer-size
522     :element-type element-type
523     :initial-element fill-element)))
524     (case (gap-location fc)
525     ((:gap-empty :gap-middle)
526     (move-elements fc buffer-after buffer 0 0 gap-start)
527     (let ((gap-end-after (- new-buffer-size (- buffer-size gap-end))))
528     (move-elements fc buffer-after buffer gap-end-after gap-end buffer-size)
529     (setf gap-end gap-end-after)))
530     (:gap-right
531     (move-elements fc buffer-after buffer 0 0 gap-start))
532     (:gap-left
533     (let ((gap-end-after (- new-buffer-size (+ 2 (nb-elements fc)))))
534     (move-elements fc buffer-after buffer gap-end-after gap-end buffer-size)
535     (setf gap-end gap-end-after)))
536     (:gap-non-contiguous
537     (move-elements fc buffer-after buffer 0 gap-end gap-start)
538     (decf gap-start gap-end)
539     (setf gap-end 0)))
540     (setf buffer buffer-after)))
541     (normalize-indices fc))
542    
543     (defun normalize-indices (chain)
544     "Sets gap limits to 0 if they are at the end of the buffer."
545     (with-slots (buffer gap-start gap-end data-start) chain
546     (let ((buffer-size (length buffer)))
547     (when (>= data-start buffer-size)
548     (setf data-start 0))
549     (when (>= gap-start buffer-size)
550     (setf gap-start 0))
551     (when (>= gap-end buffer-size)
552     (setf gap-end 0)))))
553    
554     (defun gap-location (chain)
555     "Returns a keyword indicating the general location of the gap."
556     (with-slots (buffer gap-start gap-end) chain
557     (cond ((= gap-start gap-end) :gap-empty)
558     ((and (zerop gap-start) (>= gap-end 0)) :gap-left)
559     ((and (zerop gap-end) (> gap-start 0)) :gap-right)
560     ((> gap-end gap-start) :gap-middle)
561     (t :gap-non-contiguous))))
562    

  ViewVC Help
Powered by ViewVC 1.1.5