/*
 * This file is part of libdyn++.a, the C++ Dynamic Object library.
 * It contains the public header file.
 *
 * There are no restrictions on this code; however, if you make any
 * changes, I request that you document them so that I do not get
 * credit or blame for your modifications.
 *
 * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
 * and MIT-Project Athena, 1990.
 */

/*
 * DynObject.H -- header file to be included by programs linking against
 * libDynObject.a.
 */

/*
 * Rep invariant:
 * 1) el_size is the number of bytes per element in the object
 * 2) num_el is the number of elements currently in the object.  It is
 * one higher than the highest index at which an element lives.
 * 3) size is the number of elements the object can hold without
 * resizing.  num_el <= index.
 * 4) inc is a multiple of the number of elements the object grows by
 * each time it is reallocated.
 */

#ifndef _DynObject_
#define _DynObject_

/* Return status codes */
const int DYN_OK = -1000;
const int DYN_NOMEM = -1001;
const int DYN_BADINDEX = -1002;
const int DYN_BADVALUE = -1003;

#include <stdio.h>
#include <stdlib.h>

typedef char *DynPtr;
     
class DynObject {
public:
     DynObject(int, int = 100);
     DynObject(const DynObject&);
     ~DynObject();
     inline int High() const;
     inline int Low () const;
     inline int Size() const;
     inline int operator+=(DynPtr);
     inline DynPtr operator[](int) const;
     int Add(DynPtr);
     int Delete(int);
     int Debug(int);
     int Paranoid(int);
     int Insert(int, DynPtr, int);
     int Append(DynPtr, int);
     DynPtr Get(int) const;

     DynObject& operator=(const DynObject&);

protected:
     char *array;
     int el_size, num_el, size, inc;
     char debug, paranoid;
     inline int Resize(int);
     int Realloc(int);
     int Put(DynPtr, int);
};

inline int DynObject::operator+=(DynPtr el)
{
     return Add(el);
}

inline DynPtr DynObject::operator[](int index) const
{
     return Get(index);
}

inline int DynObject::Size() const
{
     if (debug)
	  fprintf(stderr, "dyn: size: returning size %d.\n", num_el);

     return num_el;
}

inline int DynObject::Resize(int req)
{
     return (size > req) ? DYN_OK :
	  Realloc((req - size) / inc + 1);
}

inline int DynObject::High() const
{
     return Size() - 1;
}

inline int DynObject::Low() const
{
     return 0;
}

/* DON'T ADD ANYTHING AFTER THIS #endif */
#endif /* _DynObject */
