diff anagram/support/agarray.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.h	Sat Dec 22 17:52:45 2007 -0500
@@ -0,0 +1,95 @@
+/**********************************************************
+
+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> &);
+
+  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 */