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

Contents of /flexichain/flexichain.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations)
Thu Jan 31 17:10:58 2008 UTC (6 years, 2 months ago) by charmon
Branch: MAIN
Changes since 1.6: +1 -2 lines
flexichain 1.4.1

 * remove unnecessary hack that would set data-start to (1+ gap-end)
   in delete-elements*
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 charmon 1.6 min-size))))
161 rkreuter 1.1
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 charmon 1.6 (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 rkreuter 1.1 (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 charmon 1.6 (,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 rkreuter 1.1 (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 charmon 1.6 (incf index (- ge gs)))
235 rkreuter 1.1 (when (>= index bl)
236 charmon 1.6 (decf index bl))
237 rkreuter 1.1 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 charmon 1.6 'flexi-position-error :chain chain :position position)
262 rkreuter 1.1 (assert (typep object element-type) ()
263 charmon 1.6 'flexi-incompatible-type-error :element object :chain chain)
264 rkreuter 1.1 (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 charmon 1.6 'flexi-position-error :chain chain :position position)
275     (assert (subtypep (array-element-type vector) element-type) ()
276     'flexi-incompatible-type-error :element vector :chain chain)
277 rkreuter 1.1 (ensure-gap-position chain position)
278     (ensure-room chain (+ (nb-elements chain) (length vector)))
279     (loop for elem across vector
280 charmon 1.6 do (setf (aref buffer gap-start) elem)
281     (incf gap-start)
282     (when (= gap-start (length buffer))
283     (setf gap-start 0)))))
284 rkreuter 1.1
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 charmon 1.6 (< (+ (nb-elements chain) 2) (/ (length buffer) (square expand-factor))))
296 rkreuter 1.1 (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 charmon 1.7 (setf gap-end surplus-elements))))
317 charmon 1.4 (when (= gap-end (length buffer))
318     (setf gap-end 0))
319     (when (and (> (length buffer) (+ min-size 2))
320     (< (+ (nb-elements chain) 2) (/ (length buffer) (square expand-factor))))
321     (decrease-buffer-size chain)))))
322    
323 rkreuter 1.1 (defmethod element* ((chain standard-flexichain) position)
324     (with-slots (buffer) chain
325     (assert (< -1 position (nb-elements chain)) ()
326 charmon 1.6 'flexi-position-error :chain chain :position position)
327 rkreuter 1.1 (aref buffer (position-index chain position))))
328    
329     (defmethod (setf element*) (object (chain standard-flexichain) position)
330     (with-slots (buffer element-type) chain
331     (assert (< -1 position (nb-elements chain)) ()
332 charmon 1.6 'flexi-position-error :chain chain :position position)
333 rkreuter 1.1 (assert (typep object element-type) ()
334 charmon 1.6 'flexi-incompatible-type-error :chain chain :element object)
335 rkreuter 1.1 (setf (aref buffer (position-index chain position)) object)))
336    
337     (defmethod push-start ((chain standard-flexichain) object)
338     (insert* chain 0 object))
339    
340     (defmethod push-end ((chain standard-flexichain) object)
341     (insert* chain (nb-elements chain) object))
342    
343     (defmethod pop-start ((chain standard-flexichain))
344 charmon 1.6 (prog1
345     (element* chain 0)
346     (delete* chain 0)))
347 rkreuter 1.1
348     (defmethod pop-end ((chain standard-flexichain))
349     (let ((position (1- (nb-elements chain))))
350 charmon 1.6 (prog1
351     (element* chain position)
352     (delete* chain position))))
353 rkreuter 1.1
354     (defmethod rotate ((chain standard-flexichain) &optional (n 1))
355     (when (> (nb-elements chain) 1)
356     (cond ((plusp n) (loop repeat n do (push-start chain (pop-end chain))))
357 charmon 1.6 ((minusp n) (loop repeat (- n) do (push-end chain (pop-start chain))))
358     (t nil))))
359 rkreuter 1.1
360     (defun move-gap (chain hot-spot)
361     "Moves the elements and gap inside the buffer so that
362     the element currently at HOT-SPOT becomes the first element following
363     the gap, or does nothing if there are no elements."
364     (with-slots (gap-start gap-end) chain
365     (unless (= hot-spot gap-end)
366     (case (gap-location chain)
367     (:gap-empty (move-empty-gap chain hot-spot))
368     (:gap-left (move-left-gap chain hot-spot))
369     (:gap-right (move-right-gap chain hot-spot))
370     (:gap-middle (move-middle-gap chain hot-spot))
371     (:gap-non-contiguous (move-non-contiguous-gap chain hot-spot))))
372     (values gap-start gap-end)))
373    
374     (defun move-empty-gap (chain hot-spot)
375     "Moves the gap. Handles the case where the gap is empty."
376     (with-slots (gap-start gap-end) chain
377     (setf gap-start hot-spot
378     gap-end hot-spot)))
379    
380     (defun move-left-gap (chain hot-spot)
381     "Moves the gap. Handles the case where the gap is
382     on the left of the buffer."
383     (with-slots (buffer gap-start gap-end data-start) chain
384     (let ((buffer-size (length buffer)))
385     (cond ((< (- hot-spot gap-end) (- buffer-size hot-spot))
386     (push-elements-left chain (- hot-spot gap-end)))
387     ((<= (- buffer-size hot-spot) gap-end)
388     (hop-elements-left chain (- buffer-size hot-spot)))
389     (t
390     (hop-elements-left chain (- gap-end gap-start))
391     (push-elements-right chain (- gap-start hot-spot)))))))
392    
393     (defun move-right-gap (chain hot-spot)
394     "Moves the gap. Handles the case where the gap is
395     on the right of the buffer."
396     (with-slots (buffer gap-start gap-end) chain
397     (let ((buffer-size (length buffer)))
398     (cond ((< (- gap-start hot-spot) hot-spot)
399     (push-elements-right chain (- gap-start hot-spot)))
400     ((<= hot-spot (- buffer-size gap-start))
401     (hop-elements-right chain hot-spot))
402     (t
403     (hop-elements-right chain (- buffer-size gap-start))
404     (push-elements-left chain (- hot-spot gap-end)))))))
405    
406     (defun move-middle-gap (chain hot-spot)
407     "Moves the gap. Handles the case where the gap is
408     in the middle of the buffer."
409     (with-slots (buffer gap-start gap-end) chain
410     (let ((buffer-size (length buffer)))
411     (cond ((< hot-spot gap-start)
412     (cond ((<= (- gap-start hot-spot)
413     (+ (- buffer-size gap-end) hot-spot))
414     (push-elements-right chain (- gap-start hot-spot)))
415     (t
416     (push-elements-left chain (- buffer-size gap-end))
417     (move-right-gap chain hot-spot))))
418     (t
419     (cond ((< (- hot-spot gap-end)
420     (+ (- buffer-size hot-spot) gap-start))
421     (push-elements-left chain (- hot-spot gap-end)))
422     (t
423     (push-elements-right chain gap-start)
424     (move-left-gap chain hot-spot))))))))
425    
426     (defun move-non-contiguous-gap (chain hot-spot)
427     "Moves the gap. Handles the case where the gap is in 2 parts,
428     on both ends of the buffer."
429     (with-slots (buffer gap-start gap-end) chain
430     (let ((buffer-size (length buffer)))
431     (cond ((< (- hot-spot gap-end) (- gap-start hot-spot))
432     (hop-elements-right chain (min (- buffer-size gap-start)
433     (- hot-spot gap-end)))
434     (let ((nb-left (- hot-spot gap-end)))
435     (unless (zerop nb-left)
436     (push-elements-left chain nb-left))))
437     (t
438     (hop-elements-left chain (min gap-end (- gap-start hot-spot)))
439     (let ((nb-right (- gap-start hot-spot)))
440     (unless (zerop nb-right)
441     (push-elements-right chain nb-right))))))))
442    
443     (defgeneric move-elements (standard-flexichain to from start1 start2 end2)
444     (:documentation "move elements of a flexichain and adjust data-start"))
445    
446     (defmethod move-elements ((fc standard-flexichain) to from start1 start2 end2)
447     (replace to from :start1 start1 :start2 start2 :end2 end2)
448     (with-slots (data-start) fc
449     (when (and (<= start2 data-start) (< data-start end2))
450     (incf data-start (- start1 start2)))))
451    
452     (defgeneric fill-gap (standard-flexichain start end)
453     (:documentation "fill part of gap with the fill element"))
454    
455     (defmethod fill-gap ((fc standard-flexichain) start end)
456     (with-slots (buffer fill-element) fc
457     (fill buffer fill-element :start start :end end)))
458    
459     (defun push-elements-left (chain count)
460     "Pushes the COUNT elements of CHAIN at the right of the gap,
461     to the beginning of the gap. The gap must be continuous. Example:
462     PUSH-ELEMENTS-LEFT abcd-----efghijklm 2 => abcdef-----ghijklm"
463     (with-slots (buffer gap-start gap-end) chain
464     (move-elements chain buffer buffer gap-start gap-end (+ gap-end count))
465     (fill-gap chain (max gap-end (+ gap-start count)) (+ gap-end count))
466     (incf gap-start count)
467     (incf gap-end count)
468     (normalize-indices chain)))
469    
470     (defun push-elements-right (chain count)
471     "Pushes the COUNT elements of CHAIN at the left of the gap,
472     to the end of the gap. The gap must be continuous. Example:
473     PUSH-ELEMENTS-RIGHT abcd-----efghijklm 2 => ab-----cdefghijklm"
474     (with-slots (buffer gap-start gap-end) chain
475     (let* ((buffer-size (length buffer))
476     (rotated-gap-end (if (zerop gap-end) buffer-size gap-end)))
477     (move-elements chain buffer buffer
478 charmon 1.6 (- rotated-gap-end count) (- gap-start count) gap-start)
479 rkreuter 1.1 (fill-gap chain (- gap-start count) (min gap-start (- rotated-gap-end count)))
480     (decf gap-start count)
481     (setf gap-end (- rotated-gap-end count))
482     (normalize-indices chain))))
483    
484     (defun hop-elements-left (chain count)
485     "Moves the COUNT rightmost elements to the end of the gap,
486     on the left of the data. Example:
487     HOP-ELEMENTS-LEFT ---abcdefghijklm--- 2 => -lmabcdefghijk-----"
488     (with-slots (buffer gap-start gap-end) chain
489     (let* ((buffer-size (length buffer))
490     (rotated-gap-start (if (zerop gap-start) buffer-size gap-start)))
491     (move-elements chain buffer buffer
492 charmon 1.6 (- gap-end count) (- rotated-gap-start count) rotated-gap-start)
493 rkreuter 1.1 (fill-gap chain (- rotated-gap-start count) rotated-gap-start)
494     (setf gap-start (- rotated-gap-start count))
495     (decf gap-end count)
496     (normalize-indices chain))))
497    
498     (defun hop-elements-right (chain count)
499     "Moves the COUNT leftmost elements to the beginning of the gap,
500     on the right of the data. Example:
501     HOP-ELEMENTS-RIGHT ---abcdefghijklm--- 2 => -----cdefghijklmab-"
502     (with-slots (buffer gap-start gap-end) chain
503     (move-elements chain buffer buffer gap-start gap-end (+ gap-end count))
504     (fill-gap chain gap-end (+ gap-end count))
505     (incf gap-start count)
506     (incf gap-end count)
507     (normalize-indices chain)))
508    
509     (defun increase-buffer-size (chain nb-elements)
510     (resize-buffer chain (required-space chain nb-elements)))
511    
512     (defun decrease-buffer-size (chain)
513     (resize-buffer chain (required-space chain (nb-elements chain))))
514    
515     (defgeneric resize-buffer (standard-flexichain new-buffer-size)
516     (:documentation "allocate a new buffer with the size indicated"))
517    
518     (defmethod resize-buffer ((fc standard-flexichain) new-buffer-size)
519     (with-slots (buffer gap-start gap-end
520     fill-element element-type expand-factor) fc
521     (let ((buffer-size (length buffer))
522     (buffer-after (make-array new-buffer-size
523     :element-type element-type
524     :initial-element fill-element)))
525     (case (gap-location fc)
526     ((:gap-empty :gap-middle)
527     (move-elements fc buffer-after buffer 0 0 gap-start)
528     (let ((gap-end-after (- new-buffer-size (- buffer-size gap-end))))
529     (move-elements fc buffer-after buffer gap-end-after gap-end buffer-size)
530     (setf gap-end gap-end-after)))
531     (:gap-right
532     (move-elements fc buffer-after buffer 0 0 gap-start))
533     (:gap-left
534     (let ((gap-end-after (- new-buffer-size (+ 2 (nb-elements fc)))))
535     (move-elements fc buffer-after buffer gap-end-after gap-end buffer-size)
536     (setf gap-end gap-end-after)))
537     (:gap-non-contiguous
538     (move-elements fc buffer-after buffer 0 gap-end gap-start)
539     (decf gap-start gap-end)
540     (setf gap-end 0)))
541     (setf buffer buffer-after)))
542     (normalize-indices fc))
543    
544     (defun normalize-indices (chain)
545     "Sets gap limits to 0 if they are at the end of the buffer."
546     (with-slots (buffer gap-start gap-end data-start) chain
547     (let ((buffer-size (length buffer)))
548     (when (>= data-start buffer-size)
549     (setf data-start 0))
550     (when (>= gap-start buffer-size)
551     (setf gap-start 0))
552     (when (>= gap-end buffer-size)
553     (setf gap-end 0)))))
554    
555     (defun gap-location (chain)
556     "Returns a keyword indicating the general location of the gap."
557     (with-slots (buffer gap-start gap-end) chain
558     (cond ((= gap-start gap-end) :gap-empty)
559     ((and (zerop gap-start) (>= gap-end 0)) :gap-left)
560     ((and (zerop gap-end) (> gap-start 0)) :gap-right)
561     ((> gap-end gap-start) :gap-middle)
562     (t :gap-non-contiguous))))
563    

  ViewVC Help
Powered by ViewVC 1.1.5