//
//  CDynamicArray.h
//  iDevelop
//
//  Created by Ben Reeves on 02/12/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

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

typedef struct
{
	unsigned int capacity;
	unsigned int count;
	void ** data;
} CArray;


/**
 * Init a new Array with capacity
 */
CArray* CArrayInit(unsigned int capacity);

/**
 * Returns a new copy of an array and elements (does not create empty capacity)
 */
CArray* CArrayCopy(CArray* array);

/**
 * Destroy an array
 */
void CArrayDestroy(CArray * array);

/**
 * Returns the object at the specified index
 */
static inline void * CArrayObjectAtIndex(CArray * array, unsigned int index)
{
	if (index >= array->count)
		return NULL;
	
	return array->data[index];
}

/**
 * Sets the capacity of an array, 
 * the array will resize dynamically if the capacity is exceeded however some cost maybe incurred
 */
void CArraySetCapacity(CArray * array, unsigned int capacity);

/**
 * Adds an object to the end of the array
 */
void CArrayAdd(CArray* array, void * element);

/**
 * Empties the array, does not compact extra capacity
 */
void CArrayEmpty(CArray* array);

/**
 * Removes and object matching the address of the provided element
 */
void CArrayRemove(CArray* array, void * element);

/**
 * Removes the object at the porvided index
 */
void CArrayRemoveObjectAtIndex(CArray* array, unsigned int element);

/**
 * Returns the number of elements in an array
 */
unsigned int CArrayCount(CArray * array);

/**
 * Removes excess capacity
 */
void CArrayCompact(CArray* array);

//Internal Test Routine
void CArrayTestRoutine();
