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

Contents of /flexichain/flexirank.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Sun Jan 27 06:05:37 2008 UTC (6 years, 2 months ago) by charmon
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +9 -9 lines
flexichain 1.4
 * replaced tabs with spaces
 * minor indentation and spacing whitespace fixes
1 ;;; Ranked flexichain
2 ;;;
3 ;;; Copyright (C) 2005 Robert Strandh (strandh@labri.fr)
4 ;;;
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 2.1 of the License, or (at your option) any later version.
9 ;;;
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
14 ;;;
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 (in-package :flexichain)
20
21 ;;; A ranked flexichain is a flexichain (or a cursorchain) in which
22 ;;; the elements know their position. To make that work, client code
23 ;;; must use mix in the flexirank-mixin class into whatever flexichain
24 ;;; class they are using, and mix in the element-rank-mixin class into
25 ;;; elements of that chain.
26
27 ;;; The element-rank-mixin supplies a method on the client-visible
28 ;;; generic function rank.
29
30 (defgeneric rank (element))
31 (defgeneric flexi-first-p (element))
32 (defgeneric flexi-last-p (element))
33 (defgeneric flexi-next (element))
34 (defgeneric flexi-prev (element))
35
36 (defclass element-rank-mixin ()
37 ((index :accessor index)
38 (chain :accessor chain)))
39
40 (defmethod rank ((element element-rank-mixin))
41 (index-position (chain element) (index element)))
42
43 (defmethod flexi-first-p ((element element-rank-mixin))
44 (zerop (rank element)))
45
46 (defmethod flexi-last-p ((element element-rank-mixin))
47 (= (rank element) (1- (nb-elements (chain element)))))
48
49 (defmethod flexi-next ((element element-rank-mixin))
50 (assert (not (flexi-last-p element)))
51 (element* (chain element) (1+ (rank element))))
52
53 (defmethod flexi-prev ((element element-rank-mixin))
54 (assert (not (flexi-first-p element)))
55 (element* (chain element) (1- (rank element))))
56
57 ;;; this class must be mixed into a flexichain that contains ranked elements
58 (defclass flexirank-mixin () ())
59
60 (defmethod move-elements :before ((chain flexirank-mixin) to from start1 start2 end2)
61 (declare (ignore to))
62 (loop for old from start2 below end2
63 for new from start1
64 do (let ((element (aref from old)))
65 (when (typep element 'element-rank-mixin)
66 (setf (index element) new)))))
67
68 (defmethod insert* :after ((chain flexirank-mixin) position (object element-rank-mixin))
69 (setf (index object) (position-index chain position)
70 (chain object) chain))
71
72 (defmethod (setf element*) :after ((object element-rank-mixin) (chain flexirank-mixin) position)
73 (setf (index object) (position-index chain position)
74 (chain object) chain))
75
76 (defmethod insert-vector* :after ((chain flexirank-mixin) position vector)
77 (loop for elem across vector
78 for pos from position
79 do (setf (index elem) (position-index chain pos)
80 (chain elem) chain)))

  ViewVC Help
Powered by ViewVC 1.1.5