Initial Commit
[packages] / xemacs-packages / elib / stack-m.el
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.
3
4 ;; Copyright (C) 1991-1995 Free Software Foundation
5
6 ;; Author: Inge Wallin <inge@lysator.liu.se>
7 ;; Maintainer: elib-maintainers@lysator.liu.se
8 ;; Created: 12 May 1991
9 ;; Keywords: extensions, lisp
10
11 ;;;; This file is part of the GNU Emacs lisp library, Elib.
12 ;;;;
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.
17 ;;;;
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.
22 ;;;;
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
27 ;;;; 
28 ;;;; Author: Inge Wallin
29 ;;;; 
30
31 ;;; Commentary:
32
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.
36 ;;;
37 ;;; This file implements the functions as macros for speed in compiled 
38 ;;; code.
39 ;;;
40
41
42 ;;; Code:
43
44 ;; Provide the function version and remove the macro version
45 (provide 'stack-m)
46 (setq features (delq 'stack-f features))
47
48
49 ;;; ================================================================
50
51
52 (defmacro stack-create ()
53   "Create an empty lifo stack."
54   (` (cons 'STACK nil)))
55
56
57 (defmacro stack-p (stack)
58   "Return t if STACK is a stack, otherwise return nil."
59   (` (eq (car-safe (, stack)) 'STACK)))
60
61
62 (defmacro stack-push (stack element)
63   "Push an element onto the stack.
64 Args: STACK ELEMENT"
65   (` (setcdr (, stack) (cons (, element) (cdr (, stack))))))
66
67
68 (defmacro stack-pop (stack)
69   "Remove the topmost element from STACK and return it. 
70 If the stack is empty, return nil."
71   (` (prog1
72          (car-safe (cdr (, stack)))
73        (setcdr (, stack) (cdr-safe (cdr (, stack)))))))
74
75
76 (defmacro stack-empty (stack)
77   "Return t if STACK is empty, otherwise return nil."
78   (` (null (cdr (, stack)))))
79
80
81 (defmacro stack-top (stack)
82   "Return the topmost element of STACK or nil if it is empty."
83   (` (car-safe (cdr (, stack)))))
84
85
86 (defmacro stack-nth (stack n)
87   "Return nth element of a stack, but don't remove it.
88 Args: STACK N
89 If the length of the stack is less than N, return nil.
90
91 The top stack element has number 0."
92   (` (nth (, n) (cdr (, stack)))))
93
94
95 (defmacro stack-all (stack)
96   "Return a list of all entries in STACK.
97 The element last pushed is first in the list."
98   (` (cdr (, stack))))
99
100
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))))))
105
106
107 (defmacro stack-length (stack)
108   "Return the number of elements on STACK."
109   (` (length (cdr (, stack)))))
110
111
112 (defmacro stack-clear (stack)
113   "Remove all elements from STACK."
114   (` (setcdr (, stack) nil)))
115
116 ;;; stack-m.el ends here