/*	XGArray.cpp
 *
 *		This implements the common code that is used by the XGDynArray
 *	template and other dynamic array objects.
 */

/**********************************************************************

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <XError.h>
#include <XDynArray.h>

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGArray::XGArray
 *
 *		Construct an empty array. This initializes the various state
 *	variables used by this class, such as the number of items allocated
 *	and the length of this object.
 */

XGArray::XGArray()
{
	fLength = 0;
	fAlloc = 0;
}

/*$	XGArray::~XGArray
 *
 *		Destroy this object. This does nothing; the destruction of
 *	the contents of this thing is dependant on the template declared
 *	from this thing.
 */

XGArray::~XGArray()
{
}

/************************************************************************/
/*																		*/
/*	External Interface													*/
/*																		*/
/************************************************************************/

/*$	XGArray::Delete
 *
 *		Delete one element. This defers to the child class object
 *	for it's actual operation.
 */

void XGArray::Delete(long index)
{
	long j;
	
	/*
	 *	Range check
	 */
	
	Assert((index >= 0) && (index < fLength));
	
	/*
	 *	Copy elements over
	 */
	
	for (j = index+1; j < fLength; j++) {
		CopyArray(j-1,j);
	}
	fLength--;
	
	/*
	 *	Shrink allocation (if needed)
	 */
	
	j = fLength;
	if (j % 16) j += 16 - (j % 16);
	if (j != fAlloc) {
		ChangeArray(j,fLength);
		fAlloc = j;
	}
}

/*$	XGArray::DeleteAll
 *
 *		Delete all elements. This essentially reallocates this array
 *	as a zero-length array.
 */

void XGArray::DeleteAll(void)
{
	fLength = 0;
	fAlloc = 0;
	ChangeArray(0,0);
}

/*$	XGArray::SetLength
 *
 *		This forces the length to be set to the particular size, clipping
 *	or adding elements to the end of the array.
 */

void XGArray::SetLength(long length)
{
	long j;
	
	if (length == fLength) return;
	
	j = length;
	if (j % 16) j += 16 - (j % 16);
	if (j != fAlloc) {
		if (fLength < length) ChangeArray(j,fLength);
		else ChangeArray(j,length);
		fAlloc = j;
	}
	fLength = length;
}

/************************************************************************/
/*																		*/
/*	Hidden stuff														*/
/*																		*/
/************************************************************************/

/*$	XGArray::Copy
 *
 *		Copy arrays. This defers to the various internal methods for the
 *	actual copy operations.
 */

void XGArray::Copy(const XGArray &a)
{
	long i;
	
	if (fAlloc != a.fAlloc) {
		ChangeArray(a.fAlloc,0);
		fAlloc = a.fAlloc;
	}
	fLength = a.fLength;
	for (i = 0; i < fLength; i++) {
		CopyElement(i,a.GetElement(i));
	}
}

/*$	XGArray::InsertElement
 *
 *		Insert this element. It is assumed that the thing overriding
 *	the ChangeArray/CopyArray/etc stuff know the size of this
 */

void XGArray::InsertElement(long index, void *ptr)
{
	long j;
	
	/*
	 *	Range check
	 */
	
	Assert((index >= 0) && (index <= fLength));
	
	/*
	 *	Change the allocation if needed
	 */
	
	j = fLength + 1;
	if (j % 16) j += 16 - (j % 16);
	if (j != fAlloc) {
		ChangeArray(j,fLength);
		fAlloc = j;
	}
	
	/*
	 *	Insert the element
	 */
	
	for (j = fLength; j > index; j--) {
		CopyArray(j,j-1);
	}
	CopyElement(index,ptr);
	fLength++;
}

