Mercurial > ~dholland > hg > ag > index.cgi
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/anagram/support/agstack-imp.h Sat Dec 22 17:52:45 2007 -0500 @@ -0,0 +1,149 @@ +/********************************************************** + +The AnaGram Class Library + +The AgStackStorage Class +Copyright 1997 Parsifal Software. All Rights Reserved. +See the file COPYING for license and usage terms. + +***********************************************************/ + +#ifndef AGSTACK_IMP_H +#define AGSTACK_IMP_H + + +template <class T> +AgClassWrapper<T>::AgClassWrapper(const T &t) + : data(t) +{} + +template <class T> +AgClassWrapper<T>::operator T () const { + return data; +} + +template <class T> +AgStack<T>::AgStack(unsigned logSize, unsigned bsLogSize) + : AgIndexedContainer<T>() + , store(new AgStackStorage(sizeof(AgClassWrapper<T>), logSize, bsLogSize)) +{ + //LOGSECTION("AgStack constructor"); + //constructorCalls++; + //LOGV(constructorCalls); +} + +template <class T> +AgStack<T>::AgStack(const AgStack<T> &s) + : AgIndexedContainer<T>() + , store(s.store) +{ + //LOGSECTION("AgStack copy constructor"); + //copyConstructorCalls++; + //LOGV(copyConstructorCalls); + store->lock(); +} + +template <class T> +AgStack<T>::~AgStack() { + //LOGSECTION("~AgStack"); + //destructorCalls++; + //LOGV(destructorCalls); + if (store->unlock()) { + discardData(); + delete store; + } +} + +template <class T> +AgStack<T> &AgStack<T>::operator = (const AgStack<T> &s) { + //LOGSECTION("AgStack::operator ="); + if (store->unlock()) { + delete store; + } + store = s.store; + store->lock(); + return *this; +} + +template <class T> +int AgStack<T>::operator < (const AgStack<T> &s) const { + int n = min(size(), s.size()); + int i = 0; + while (i < n) { + if ((*this)[i] < s[i]) { + return 1; + } + if (s[i] < (*this)[i]) { + return 0; + } + i++; + } + return size() < s.size(); +} + +template <class T> +AgStack<T> &AgStack<T>::push(const T &t) { + new(store->push()) AgClassWrapper<T>(t); + return *this; +} + +//template <class T> +//AgStack<T> &AgStack<T>::operator << (const T &t) { +// new(store->push()) AgClassWrapper<T>(t); +// return *this; +//} + +template <class T> +AgStack<T> &AgStack<T>::pop(T &t) { + AgClassWrapper<T> *pointer = (AgClassWrapper<T> *) store->pop(); + t = (T) *pointer; + delete pointer; + return *this; +} + +template <class T> +T AgStack<T>::pop() { + AgClassWrapper<T> *pointer = (AgClassWrapper<T> *) store->pop(); + T value((T) *pointer); + delete pointer; + return value; +} + +//template <class T> +//AgArray<T> AgStack<T>::popArray() { +// unsigned n = size(); +// AgArray<T> array(n); +// while (n--) pop(array[n]); +// //for (int i = 0; i < n; i++) array[i] = (*this)[i]; +// //discardData(n); +// return array; +//} + +template <class T> +AgStack<T> &AgStack<T>::discardData(unsigned n) { + while (n--) { + AgClassWrapper<T> *pointer = (AgClassWrapper<T> *) store->pop(); + delete pointer; + } + //store->discardData(n); + return *this; +} + +//template <class T> +//AgStack<T> &operator << (AgStack<T> &stack, const T &t) { +// return stack.push(t); +//} + +//template <class T> +//AgStack<T> &operator >> (AgStack<T> &stack, T &t) { +// return stack.pop(t); +//} + +//template <class T> +//AgStack<T> &operator << (AgStack<T> &stack, const AgIndexedContainer<T> &a) { +// for (int i = 0; i < a.size(); i++) stack.push(a[i]); +// return stack; +//} + + +#endif /* AGSTACK_IMP_H */