Mercurial > ~dholland > hg > ag > index.cgi
view anagram/support/agarray.h @ 17:12171da8943f
Don't refer to CVS.
author | David A. Holland |
---|---|
date | Tue, 31 May 2022 01:56:37 -0400 |
parents | 607e3be6bad8 |
children |
line wrap: on
line source
/********************************************************** 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_H #define AGARRAY_H #include <stdlib.h> #include "port.h" #include "agcontainer.h" #include "agref.h" #include "agstring.h" template <class T> class AgArray : public AgIndexedContainer<T> , public AgReferenceBase { public: class Kernel : public AgReferenceCount { public: unsigned length; T *data; Kernel(); Kernel(unsigned n); inline void *operator new (size_t n) { return malloc(n); } inline void operator delete (void *p) { free(p); } T &operator [] (const unsigned x); const T &operator [] (const unsigned x) const; ~Kernel(); }; Kernel &kernel() const; void copy(T *s, unsigned n); public: AgString asString(); AgArray(); inline AgArray(unsigned n) : AgReferenceBase(n ? new Kernel(n) : 0) {} AgArray(const AgIndexedContainer<T> &); AgArray(const AgArray<T> &); void reset() { discardData(); } inline T &operator [] (const unsigned x) { return kernel()[x]; } inline const T &operator [] (const unsigned x) const { return kernel()[x]; } inline AgArray<T> &operator = (const AgArray<T> &t) { doAssignmentLocks(t); return *this; } int operator < (const AgArray<T> &t) const; inline T *pointer() const { return object!=0? kernel().data : 0; } inline unsigned size() const { return object!=0? kernel().length : 0; } inline AgArray(T *s, unsigned n) : AgReferenceBase(n ? new Kernel(n) : 0) { copy(s, n); } inline AgArray(T *s, unsigned k, unsigned n) : AgReferenceBase(n ? new Kernel(n) : 0) { copy(s, k); } }; template <class T> AgArray<T> makeArray(const T *tp, int n) { AgArray<T> result(n); while (n--) { result[n] = tp[n]; } return result; } #endif /* AGARRAY_H */