#ifndef ARRAY_H
#define ARRAY_H
static char test_h_id[] = "$Header: /usr/tiburon/xPlatform/utils/RCS/test.h,v 1.1 1997/03/20 12:27:08 oreilly Exp pean $";

/*
$Log: test.h,v $
Revision 1.1  1997/03/20 12:27:08  oreilly
Initial revision

*/

#include <stdio.h>
#include <malloc.h>
#include <mbari/types.h>
#include <mbari/const.h>


template<class T>
class DynArray {

  public:
  // Constructor
  DynArray(int incr) {

    if (incr <= 0)    
    {
      mem_error = TRUE;
      return;
    }
    n_elems = 0;
    n_allocd = 0;
    alloc_incr = incr;
  }

  // Destructor
  ~DynArray() {
    if (n_allocd)
      free(array);
  }

  // Set specified element to specified value
  int set(int i, T& val)
  {
    if (i < 0)
    {
      mem_error = TRUE;
      return -1;
    }

    if (i >= n_allocd)
    {
      // Compute blocks needed to index specified element
      int nblocks = (i + 1) / alloc_incr;
      if ((i+1) % alloc_incr)
        nblocks++;

      if (!n_allocd)
      {
        n_allocd = nblocks * alloc_incr;
        if ((array = (T *)malloc(n_allocd*sizeof(T))) == NULL)
        {
          mem_error = TRUE;
          return -1;
        }
      }
      else
      {
        n_allocd += (nblocks * alloc_incr);
        if ((array = (T *)realloc(array, n_allocd*sizeof(T))) == NULL)
        {
          mem_error = TRUE;
          return -1;
        }
      }
    }
    array[i] = val;
    mem_error = FALSE;
    n_elems = MAX((i+1), n_elems);
    return 0;
  }

  // Get value of specified element
  int get(int i, T& val)
  {
    if (i < 0 || i >= n_elems)
    {
      mem_error = TRUE;
      return -1;
    }
    val = array[i];
    mem_error = FALSE;
    return 0;
  }

  int get_n_elems() { return n_elems; }
  int get_n_allocd() { return n_allocd; }
  T *get_array() { return array; }

  void clear()
  {
    n_elems = 0;
  }
  
  // Check for memory allocation error
  MBool mem_error;
 
  private:
  T *array;
  int n_elems;
  int n_allocd;

  // Size of malloc'ed and realloc'ed blocks (in # elems)
  int alloc_incr;

};


#endif
