Mercurial > ~dholland > hg > ag > index.cgi
comparison anagram/support/agstack-imp.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_IMP_H | |
12 #define AGSTACK_IMP_H | |
13 | |
14 | |
15 template <class T> | |
16 AgClassWrapper<T>::AgClassWrapper(const T &t) | |
17 : data(t) | |
18 {} | |
19 | |
20 template <class T> | |
21 AgClassWrapper<T>::operator T () const { | |
22 return data; | |
23 } | |
24 | |
25 template <class T> | |
26 AgStack<T>::AgStack(unsigned logSize, unsigned bsLogSize) | |
27 : AgIndexedContainer<T>() | |
28 , store(new AgStackStorage(sizeof(AgClassWrapper<T>), logSize, bsLogSize)) | |
29 { | |
30 //LOGSECTION("AgStack constructor"); | |
31 //constructorCalls++; | |
32 //LOGV(constructorCalls); | |
33 } | |
34 | |
35 template <class T> | |
36 AgStack<T>::AgStack(const AgStack<T> &s) | |
37 : AgIndexedContainer<T>() | |
38 , store(s.store) | |
39 { | |
40 //LOGSECTION("AgStack copy constructor"); | |
41 //copyConstructorCalls++; | |
42 //LOGV(copyConstructorCalls); | |
43 store->lock(); | |
44 } | |
45 | |
46 template <class T> | |
47 AgStack<T>::~AgStack() { | |
48 //LOGSECTION("~AgStack"); | |
49 //destructorCalls++; | |
50 //LOGV(destructorCalls); | |
51 if (store->unlock()) { | |
52 discardData(); | |
53 delete store; | |
54 } | |
55 } | |
56 | |
57 template <class T> | |
58 AgStack<T> &AgStack<T>::operator = (const AgStack<T> &s) { | |
59 //LOGSECTION("AgStack::operator ="); | |
60 if (store->unlock()) { | |
61 delete store; | |
62 } | |
63 store = s.store; | |
64 store->lock(); | |
65 return *this; | |
66 } | |
67 | |
68 template <class T> | |
69 int AgStack<T>::operator < (const AgStack<T> &s) const { | |
70 int n = min(size(), s.size()); | |
71 int i = 0; | |
72 while (i < n) { | |
73 if ((*this)[i] < s[i]) { | |
74 return 1; | |
75 } | |
76 if (s[i] < (*this)[i]) { | |
77 return 0; | |
78 } | |
79 i++; | |
80 } | |
81 return size() < s.size(); | |
82 } | |
83 | |
84 template <class T> | |
85 AgStack<T> &AgStack<T>::push(const T &t) { | |
86 new(store->push()) AgClassWrapper<T>(t); | |
87 return *this; | |
88 } | |
89 | |
90 //template <class T> | |
91 //AgStack<T> &AgStack<T>::operator << (const T &t) { | |
92 // new(store->push()) AgClassWrapper<T>(t); | |
93 // return *this; | |
94 //} | |
95 | |
96 template <class T> | |
97 AgStack<T> &AgStack<T>::pop(T &t) { | |
98 AgClassWrapper<T> *pointer = (AgClassWrapper<T> *) store->pop(); | |
99 t = (T) *pointer; | |
100 delete pointer; | |
101 return *this; | |
102 } | |
103 | |
104 template <class T> | |
105 T AgStack<T>::pop() { | |
106 AgClassWrapper<T> *pointer = (AgClassWrapper<T> *) store->pop(); | |
107 T value((T) *pointer); | |
108 delete pointer; | |
109 return value; | |
110 } | |
111 | |
112 //template <class T> | |
113 //AgArray<T> AgStack<T>::popArray() { | |
114 // unsigned n = size(); | |
115 // AgArray<T> array(n); | |
116 // while (n--) pop(array[n]); | |
117 // //for (int i = 0; i < n; i++) array[i] = (*this)[i]; | |
118 // //discardData(n); | |
119 // return array; | |
120 //} | |
121 | |
122 template <class T> | |
123 AgStack<T> &AgStack<T>::discardData(unsigned n) { | |
124 while (n--) { | |
125 AgClassWrapper<T> *pointer = (AgClassWrapper<T> *) store->pop(); | |
126 delete pointer; | |
127 } | |
128 //store->discardData(n); | |
129 return *this; | |
130 } | |
131 | |
132 //template <class T> | |
133 //AgStack<T> &operator << (AgStack<T> &stack, const T &t) { | |
134 // return stack.push(t); | |
135 //} | |
136 | |
137 //template <class T> | |
138 //AgStack<T> &operator >> (AgStack<T> &stack, T &t) { | |
139 // return stack.pop(t); | |
140 //} | |
141 | |
142 //template <class T> | |
143 //AgStack<T> &operator << (AgStack<T> &stack, const AgIndexedContainer<T> &a) { | |
144 // for (int i = 0; i < a.size(); i++) stack.push(a[i]); | |
145 // return stack; | |
146 //} | |
147 | |
148 | |
149 #endif /* AGSTACK_IMP_H */ |