Last change
on this file since 482 was
481,
checked in by Sam Hocevar, 12 years ago
|
Fuck the history, I'm renaming all .hpp files to .h for my own sanity.
|
-
Property svn:keywords set to
Id
|
File size:
1.2 KB
|
Line | |
---|
1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * |
---|
5 | * This software was released into the Public Domain. As with most public |
---|
6 | * domain software, no warranty is made or implied by Crack dot Com or |
---|
7 | * Jonathan Clark. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifndef __STACK_HPP_ |
---|
11 | #define __STACK_HPP_ |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <stdlib.h> |
---|
15 | |
---|
16 | // A fixed-size stack class |
---|
17 | template<class T> class grow_stack |
---|
18 | { |
---|
19 | public: |
---|
20 | T **sdata; |
---|
21 | long son; |
---|
22 | |
---|
23 | private: |
---|
24 | long smax; |
---|
25 | |
---|
26 | public: |
---|
27 | grow_stack(int max_size) |
---|
28 | { |
---|
29 | smax = max_size; |
---|
30 | son = 0; |
---|
31 | sdata = (T **)malloc(sizeof(T *) * smax); |
---|
32 | } |
---|
33 | |
---|
34 | void push(T *data) |
---|
35 | { |
---|
36 | if(son >= smax) |
---|
37 | { |
---|
38 | lbreak("error: stack overflow (%ld >= %ld)\n", son, smax); |
---|
39 | exit(1); |
---|
40 | } |
---|
41 | sdata[son] = data; |
---|
42 | son++; |
---|
43 | } |
---|
44 | |
---|
45 | T *pop(long total) |
---|
46 | { |
---|
47 | if (total > son) |
---|
48 | { |
---|
49 | lbreak("error: stack underflow\n"); |
---|
50 | exit(1); |
---|
51 | } |
---|
52 | son -= total; |
---|
53 | return sdata[son]; |
---|
54 | } |
---|
55 | |
---|
56 | void clean_up() |
---|
57 | { |
---|
58 | if(son != 0) |
---|
59 | fprintf(stderr, "warning: cleaning up stack and not empty\n"); |
---|
60 | free(sdata); |
---|
61 | sdata = NULL; |
---|
62 | son = 0; |
---|
63 | } |
---|
64 | }; |
---|
65 | |
---|
66 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.