comparison anagram/support/agstack.h @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /**********************************************************
2
3 The AnaGram Class Library
4
5 The AgStackStorage Class
6 Copyright 1997 Parsifal Software. All Rights Reserved.
7 See the file COPYING for license and usage terms.
8
9 ***********************************************************/
10
11 #ifndef AGSTACK_H
12 #define AGSTACK_H
13
14 #include <stdlib.h>
15 #include "port.h"
16
17 #include "agcontainer.h"
18
19
20 template <class T>
21 class AgStack;
22
23 class AgStackStorage {
24 private:
25 void *buffer; // active buffer
26 AgStack<void *> *bufferStack; // buffer stack
27 unsigned count; // number of items stacked
28 unsigned usage; // usage count
29 unsigned short quantum; // size of item
30 unsigned short bufferSize; // buffer size
31 unsigned short mask; // bufferSize - 1
32 unsigned short index; // number of items in current buffer
33 unsigned char logSize; // log2 of bufferSize
34 unsigned char bsLogSize; // log2 bufferStack->bufferSize
35
36 public:
37
38 // Constructor
39 AgStackStorage(const unsigned quantum_,
40 const unsigned logSize_,
41 const unsigned bsLogSize_);
42 // Destructor
43 ~AgStackStorage();
44
45 inline void *operator new(size_t n) { return malloc(n); }
46 inline void operator delete(void *p) { free(p); }
47
48 AgStackStorage &discardData();
49 AgStackStorage &discardData(unsigned n);
50
51 unsigned size() const {
52 return count; // number of items stacked
53 }
54
55 // Push operator
56 void *push();
57
58 // Pop operator
59 void *pop();
60
61 void *locate(const unsigned n) const;
62
63 void lock() {
64 usage++;
65 }
66 int unlock() {
67 return --usage == 0;
68 }
69 };
70
71 template <class T>
72 class AgClassWrapper {
73 private:
74 T data;
75
76 public:
77 AgClassWrapper(const T &t);
78 void *operator new(size_t, void *where) {
79 return where;
80 }
81 void operator delete(void *) {}
82 operator T () const;
83 };
84
85 template <class T>
86 class AgStack : public AgIndexedContainer<T>
87 {
88 private:
89 AgStackStorage *store;
90
91 public:
92 AgStack(unsigned logSize = 8, unsigned bsLogSize = 8);
93 AgStack(const AgStack<T> &s);
94
95 void *operator new(size_t n) { return malloc(n); }
96 void operator delete(void *p) { free(p); }
97 AgStack<T> &operator =(const AgStack<T> &s);
98 ~AgStack();
99 unsigned size() const {
100 return store->size();
101 }
102
103 AgStack<T> &push(const T &t);
104 // AgStack<T> &operator << (const T &t);
105 AgStack<T> &pop(T &t);
106 T pop();
107
108 T &operator[] (const unsigned n) {
109 return *(T *)store->locate(n);
110 }
111 const T &operator[] (const unsigned n) const {
112 return *(T *)store->locate(n);
113 }
114 T &top() {
115 return (*this)[size() - 1];
116 }
117
118 AgStack<T> &discardData(unsigned n);
119 AgStack<T> &discardData() {
120 discardData(size());
121 return *this;
122 }
123 AgStack<T> &reset() {
124 discardData();
125 return *this;
126 }
127 int operator < (const AgStack<T> &s) const;
128 };
129
130
131 #endif /* AGSTACK_H */