view anagram/support/agarray.h @ 4:bebb2ba69e1d

maybe help with getting tex to fail properly on error
author David A. Holland
date Sat, 18 Apr 2020 17:12:17 -0400
parents 13d2b8934445
children 607e3be6bad8
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> &);

  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 */