1 ;;;; $Id: stack-m.el,v 1.1.1.1 1998-10-07 11:10:58 jareth Exp $
2 ;;;; This file implements a simple LIFO stack using macros.
4 ;; Copyright (C) 1991-1995 Free Software Foundation
6 ;; Author: Inge Wallin <inge@lysator.liu.se>
7 ;; Maintainer: elib-maintainers@lysator.liu.se
8 ;; Created: 12 May 1991
9 ;; Keywords: extensions, lisp
11 ;;;; This file is part of the GNU Emacs lisp library, Elib.
13 ;;;; GNU Elib is free software; you can redistribute it and/or modify
14 ;;;; it under the terms of the GNU General Public License as published by
15 ;;;; the Free Software Foundation; either version 2, or (at your option)
16 ;;;; any later version.
18 ;;;; GNU Elib is distributed in the hope that it will be useful,
19 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;;; GNU General Public License for more details.
23 ;;;; You should have received a copy of the GNU General Public License
24 ;;;; along with GNU Elib; see the file COPYING. If not, write to
25 ;;;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;;;; Boston, MA 02111-1307, USA
28 ;;;; Author: Inge Wallin
33 ;;; The stack is implemented as a linked list with a tag 'STACK
34 ;;; as the first element. All entries and removals are done using
35 ;;; destructive functions.
37 ;;; This file implements the functions as macros for speed in compiled
44 ;; Provide the function version and remove the macro version
46 (setq features (delq 'stack-f features))
49 ;;; ================================================================
52 (defmacro stack-create ()
53 "Create an empty lifo stack."
54 (` (cons 'STACK nil)))
57 (defmacro stack-p (stack)
58 "Return t if STACK is a stack, otherwise return nil."
59 (` (eq (car-safe (, stack)) 'STACK)))
62 (defmacro stack-push (stack element)
63 "Push an element onto the stack.
65 (` (setcdr (, stack) (cons (, element) (cdr (, stack))))))
68 (defmacro stack-pop (stack)
69 "Remove the topmost element from STACK and return it.
70 If the stack is empty, return nil."
72 (car-safe (cdr (, stack)))
73 (setcdr (, stack) (cdr-safe (cdr (, stack)))))))
76 (defmacro stack-empty (stack)
77 "Return t if STACK is empty, otherwise return nil."
78 (` (null (cdr (, stack)))))
81 (defmacro stack-top (stack)
82 "Return the topmost element of STACK or nil if it is empty."
83 (` (car-safe (cdr (, stack)))))
86 (defmacro stack-nth (stack n)
87 "Return nth element of a stack, but don't remove it.
89 If the length of the stack is less than N, return nil.
91 The top stack element has number 0."
92 (` (nth (, n) (cdr (, stack)))))
95 (defmacro stack-all (stack)
96 "Return a list of all entries in STACK.
97 The element last pushed is first in the list."
101 (defmacro stack-copy (stack)
102 "Return a copy of STACK.
103 All entries in STACK are also copied."
104 (` (cons 'STACK (copy-sequence (cdr (, stack))))))
107 (defmacro stack-length (stack)
108 "Return the number of elements on STACK."
109 (` (length (cdr (, stack)))))
112 (defmacro stack-clear (stack)
113 "Remove all elements from STACK."
114 (` (setcdr (, stack) nil)))
116 ;;; stack-m.el ends here