;;; ----------------------------------------------------------------------
;;; 
;;; ----------------------------------------------------------------------

(define-slotted-class 'rope '((length 0)) '(slotted))

(define-method rope new args
  (apply init self args))

(define-method rope init (init-size)
  (oos!simple-clone self (+ 1 init-size)))

(define-method rope size ()
  (sysoos!get-slot self 0))

(define-method rope add (element)
  (let ((l (sysoos!get-slot self 0)))
    (sysoos!set-slot self 0 (+ 1 l))
    (sysoos!set-slot self (+ 1 l) element)
    self))

(define-method rope nth (n)
  (let ((l (sysoos!get-slot self 0)))
    (if (<= 0 n (- l 1))
	(sysoos!get-slot self (+ 1 n))
	(error "Out of bounds " n self))))

(define-method rope empty ()
  (sysoos!set-slot self 0 0))

(define-method rope show-on (a-port)
  (display (class-name self) a-port)
  (display "[0-" a-port)
  (display (size self))
  (display "]" a-port))

(define-method rope for-elements (do-proc)
  (let ((l (size self))
	(i 0))
    (while (< i l)
      (do-proc (nth self i))
      (set! i (+ 1 i)))))

(define-method rope for-elements-reverse (do-proc)
  (let* ((l (size self))
	 (i (- l 1)))
    (while (>= i 0)
	   (do-proc (nth self i))
	   (set! i (- i 1)))))

;; example
;; (define rr ())
;; (set! rr (new (class-rope) 10))
;; (add rr 7)
;; (add rr 8)
;; (add rr 9)

;;;eof
