view anagram/support/agarray-imp.h @ 21:1c9dac05d040

Add lint-style FALLTHROUGH annotations to fallthrough cases. (in the parse engine and thus the output code) Document this, because the old output causes warnings with gcc10.
author David A. Holland
date Mon, 13 Jun 2022 00:04:38 -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_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>
AgArray<T>::AgArray(const AgArray<T> &c)
  : AgReferenceBase(c.size() ? new Kernel(c.size()) : 0)
{
  //LOGSECTION("AgArray::AgArray(AgArray)");
  //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 */