Skip to content

Commit bb51ece

Browse files
committed
fixed?
1 parent cba0527 commit bb51ece

File tree

2 files changed

+78
-39
lines changed

2 files changed

+78
-39
lines changed

dev/graphviz.lisp

+4-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@
7878

7979
#+example
8080
(let ((rope (rope::make-rope "hello world")))
81-
(dotimes (i 10)
81+
(dotimes (i 15)
8282
(setf rope (rope:append-rope rope "!")))
83-
(graph-ropes (list (rope::balance-rope rope)
83+
;; (dotimes (i 15)
84+
;; (setf rope (rope::balance-rope rope)))
85+
(graph-ropes (list rope
8486
)))

rope.lisp

+74-37
Original file line numberDiff line numberDiff line change
@@ -121,52 +121,89 @@
121121
;; / \
122122
;; f g
123123

124-
(defgeneric rotate (rope direction)
125-
(:method ((rope branch) (direction (eql :left)))
126-
(with-slots (left right) rope
127-
(concat-rope*
128-
(concat-rope* left (branch-left right))
129-
(branch-right right))))
130-
(:method ((rope branch) (direction (eql :right)))
131-
(with-slots (left right) rope
132-
(concat-rope*
133-
(branch-left left)
134-
(concat-rope* (branch-right left) right)))))
135-
136-
(defmethod rotate :around (rope direction)
137-
(format t "rope: ~a, rotating: ~a~%" (write-rope rope nil) direction)
138-
(call-next-method))
139-
140-
(defgeneric balance-direction (rope)
124+
(defgeneric balance-factor (rope)
141125
(:method ((rope leaf))
142-
nil)
126+
0)
143127
(:method ((rope branch))
144-
(with-slots (left right) rope
145-
(let ((balance (- (rope-depth left) (rope-depth right))))
146-
(cond ((< 1 balance) :right)
147-
((> -1 balance) :left)
148-
(t nil))))))
149-
150-
;; (defmethod balance :around (rope)
151-
;; (let ((bal (call-next-method)))
152-
;; (format t "rope: ~a, balance: ~a~%" (write-rope rope nil) bal)
153-
;; bal))
128+
(- (rope-depth (branch-left rope))
129+
(rope-depth (branch-right rope)))))
130+
131+
(defun rotate-left (rope)
132+
(with-slots (left right) rope
133+
(concat-rope*
134+
(concat-rope left (branch-left right))
135+
(branch-right right))))
136+
137+
(defun rotate-right (rope)
138+
(with-slots (left right) rope
139+
(concat-rope*
140+
(branch-left left)
141+
(concat-rope (branch-right left) right))))
142+
143+
(defun rotate-left-right (rope)
144+
(with-slots (left right) rope
145+
(rotate-right (concat-rope* (rotate-left left) right))))
146+
147+
(defun rotate-right-left (rope)
148+
(with-slots (left right) rope
149+
(rotate-left (concat-rope* left (rotate-right right)))))
150+
151+
(defun balance-children (rope)
152+
(with-slots (left right) rope
153+
(concat-rope* (balance-rope left)
154+
(balance-rope right))))
154155

155156
(defgeneric balance-rope (rope)
156157
(:method ((rope leaf))
157158
rope)
158159
(:method ((rope branch))
159160
(with-slots (left right) rope
160-
(let* ((left (if (balance-direction left) (balance-rope left) left))
161-
(right (if (balance-direction right) (balance-rope right) right))
162-
(rope (concat-rope* left right))
163-
(balance (balance rope)))
164-
(cond ((< 1 balance)
165-
(rotate rope :right))
166-
((> -1 balance)
167-
(rotate rope :left))
161+
(let ((bf (balance-factor rope)))
162+
(cond ((< 0 bf)
163+
;; (balance-children)
164+
(if (minusp (balance-factor left))
165+
(rotate-left-right rope)
166+
(rotate-right rope)))
167+
((> 0 bf)
168+
;; (balance-children)
169+
(if (plusp (balance-factor right))
170+
(rotate-right-left rope)
171+
(rotate-left rope)))
168172
(t
169-
rope))))))
173+
rope))))
174+
;; (with-slots (left right) rope
175+
;; (let ((left (if (zerop (balance-factor left))
176+
;; left
177+
;; (balance-rope left)))
178+
;; (right (if (zerop (balance-factor right))
179+
;; right
180+
;; (balance-rope right)))
181+
;; (rope (concat-rope* left right))
182+
;; (bf (balance-factor rope)))
183+
;; (format t "root: ~a, left: ~a, right: ~a~%"
184+
;; bf
185+
;; (balance-factor left)
186+
;; (balance-factor right)
187+
;; )
188+
;; (cond ((< 0 bf)
189+
;; (if (minusp (balance-factor left))
190+
;; (rotate-left-right rope)
191+
;; (rotate-right rope)))
192+
;; ((> 0 bf)
193+
;; (if (plusp (balance-factor right))
194+
;; (rotate-right-left rope)
195+
;; (rotate-left rope)))
196+
;; (t
197+
;; rope))))
198+
;; (with-slots (left right) rope
199+
;; (let* ((left (if (balance-direction left) (balance-rope left) left))
200+
;; (right (if (balance-direction right) (balance-rope right) right))
201+
;; (rope (concat-rope* left right))
202+
;; (balance (balance rope)))
203+
;; (cond ((< 1 balance) (rotate rope :right))
204+
;; ((> -1 balance) (rotate rope :left))
205+
;; (t rope))))
206+
))
170207

171208
;;---------;;
172209
;; Rebuild ;;

0 commit comments

Comments
 (0)