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

Contents of /flexichain/flexirank.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Thu Feb 9 02:51:06 2006 UTC (8 years, 2 months ago) by rkreuter
Branch: clnet
CVS Tags: initial
Changes since 1.1: +0 -0 lines
Initial checkin.
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 (loop for old from start2 below end2
62 for new from start1
63 do (let ((element (aref from old)))
64 (when (typep element 'element-rank-mixin)
65 (setf (index element) new)))))
66
67 (defmethod insert* :after ((chain flexirank-mixin) position (object element-rank-mixin))
68 (setf (index object) (position-index chain position)
69 (chain object) chain))
70
71 (defmethod (setf element*) :after ((object element-rank-mixin) (chain flexirank-mixin) position)
72 (setf (index object) (position-index chain position)
73 (chain object) chain))
74
75 (defmethod insert-vector* :after ((chain flexirank-mixin) position vector)
76 (loop for elem across vector
77 for pos from position
78 do (setf (index elem) (position-index pos)
79 (chain elem) chain)))

  ViewVC Help
Powered by ViewVC 1.1.5