File: stacks.lisp

package info (click to toggle)
cl-containers 20170403-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,072 kB
  • ctags: 1,387
  • sloc: lisp: 8,341; makefile: 14
file content (65 lines) | stat: -rw-r--r-- 1,874 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(in-package #:containers)

;;; Abstract Stack interface
;;;
;;; Basic and much loved first-in-last-out container.
;;;
;;; Supports:
;;; insert-item, size, empty-p, empty!, first-element,
;;; pop-item, push-item (= insert-item)

(defclass* abstract-stack (initial-contents-mixin
                           iteratable-container-mixin
                           ordered-container-mixin)
  ())

(defmethod (setf first-element) (value (stack abstract-stack))
  ;;?? should this fail when stack is empty
  (pop-item stack)
  (insert-item stack value))

(defmethod push-item ((stack abstract-stack) item)
  (insert-item stack item))


;;; Stack

(defclass* stack-container (uses-contents-mixin abstract-stack concrete-container)
  ((contents :unbound r)
   (container-type nil ir))
  (:default-initargs
    :container-type 'list-container))


(defmethod make-container-for-contents ((container stack-container)
                                        &rest args)
  (apply #'make-container (container-type container) args))


(defmethod first-element ((container stack-container))
  (first-element (contents container)))


(defmethod (setf first-element) (value (container stack-container))
  (setf (first-element (contents container)) value))


(defmethod pop-item ((container stack-container))
  (delete-first (contents container)))

(defmethod insert-item ((container stack-container) item)
  (insert-item (contents container) item))

(defmethod size ((container stack-container))
  (size (contents container)))

(defmethod empty-p ((container stack-container))
  (empty-p (contents container)))

(defmethod empty! ((container stack-container))
  (empty! (contents container))
  (values))

(defmethod search-for-item ((container stack-container) item &key 
                            (test 'eq) (key 'identity))
  (search-for-item (contents container) item :test test :key key))