Mercurial > ~dholland > hg > ag > index.cgi
diff anagram/support/agarray-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 | 607e3be6bad8 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/anagram/support/agarray-imp.h Sat Dec 22 17:52:45 2007 -0500 @@ -0,0 +1,120 @@ +/********************************************************** + +The AnaGram Class Library + +The AgArray Class +Copyright 1997-1999 Parsifal Software. All Rights Reserved. +See the file COPYING for license and usage terms. + +***********************************************************/ + +#ifndef AGARRAY_IMP_H +#define AGARRAY_IMP_H + +template <class T> +AgArray<T>::AgArray() + : AgReferenceBase() +{ + //LOGSECTION("AgArray<T>::AgArray"); +} + + +template <class T> +T &AgArray<T>::Kernel::operator [] (const unsigned x) { + //LOGSECTION("AgArray::Kernel::operator []"); + //LOGV(x) LCV(length); + assert(x < length); + return data[x]; +} + +template <class T> +const T &AgArray<T>::Kernel::operator [] (const unsigned x) const { + //LOGSECTION("AgArray::Kernel::operator [] const"); + //LOGV(x) LCV(length); + assert(x < length); + return data[x]; +} + +template <class T> +AgArray<T>::Kernel::Kernel() : length(0), data(0) { + // nothing +} + +template <class T> +AgArray<T>::Kernel::Kernel(unsigned n) : length(n), data(new T[n]) { + //arrayBytesAllocated += n*sizeof(T); + //arraysAllocated++; + //LOGV(arrayBytesAllocated); +} + +template <class T> +AgArray<T>::Kernel::~Kernel() { + //LOGSECTION("AgArray::Kernel::~Kernel"); + if (data == 0) { + return; + } + delete [] data; + //arrayBytesFreed += length*sizeof(T); + //arraysFreed++; +} + +template <class T> +typename AgArray<T>::Kernel &AgArray<T>::kernel() const { + //LOGSECTION("AgArray::kernel"); + assert(object != 0); + return (Kernel &) (*object); +} + +template <class T> +AgString AgArray<T>::asString() { + //LOGSECTION("AgArray::asString"); + if (object) { + Kernel &k = kernel(); + return AgString::format("%d @ %p", k.length, k.data); + } + else { + return AgString("Nothing here"); + } +} + + +template <class T> +AgArray<T>::AgArray(const AgIndexedContainer<T> &c) + : AgReferenceBase(c.size() ? new Kernel(c.size()) : 0) +{ + //LOGSECTION("AgArray::AgArray(AgIndexedArray)"); + //LOGV(c.size()); + int n = c.size(); + for (int i = 0; i < n; i++) { + (*this)[i] = c[i]; + } +} + +template <class T> +void AgArray<T>::copy(T *s, unsigned n) { + T *data = kernel().data; + assert(n <= kernel().length); + while (n--) { + data[n] = s[n]; + } +} + +template <class T> +int AgArray<T>::operator < (const AgArray<T> &t) const { + //LOGSECTION("AgArray<T>::operator <"); + int n = min(size(), t.size()); + int i = 0; + while (i < n) { + if ((*this)[i] < t[i]) { + return 1; + } + if (t[i] < (*this)[i]) { + return 0; + } + i++; + } + return size() < t.size(); +} + + +#endif /* AGARRAY_IMP_H */