
/** @file  psArray.h
 *
 *  @brief Contains basic array definitions and operations
 *
 *  This file defines the basic type for a array struct and functions useful
 *  in manupulating arrays.
 *
 *  @ingroup Array
 *
 *  @author Robert DeSonia, MHPCC
 *  @author Ross Harman, MHPCC
 *
 *  @version $Revision: 1.31 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2005-08-30 01:14:13 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
 */

#ifndef PS_ARRAY_H
#define PS_ARRAY_H

#include "psType.h"
#include "psCompare.h"

/// @addtogroup Array
/// @{

/** An array to support primitive types.
 *
 * Struct for maintaining an array of frequently used primitive types.
 *
 */
typedef struct
{
    long n;                            ///< Number of elements in use.
    const long nalloc;                 ///< Total number of elements available.
    psPtr* data;                       ///< An Array of pointer elements
    void *lock;                        ///< Optional lock for thread safety
}
psArray;

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

/* FUNCTION PROTOTYPES                                                       */

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

/** Checks the type of a particular pointer.
 *
 *  Uses the appropriate deallocation function in psMemBlock to check the ptr datatype.
 *
 *  @return bool:       True if the pointer matches a psArray structure, false otherwise.
 */
bool psMemCheckArray(
    psPtr ptr                          ///< the pointer whose type to check
)
;


/** Allocate an array.
 *
 * Uses psLib memory allocation functions to create an array collection of
 * data
 *
 * @return psArray* : Pointer to psArray.
 *
 */
psArray* psArrayAlloc(
    long nalloc                        ///< Total number of elements to make available.
);

/** Reallocate an array.
 *
 * Uses psLib memory allocation functions to reallocate an array collection
 * of data.
 *
 * @return psArray* : Pointer to psArray.
 *
 */
psArray* psArrayRealloc(
    psArray* array,                    ///< array to reallocate.
    long nalloc                        ///< Total number of elements to make available.
);

/** Add an element to the end the array, expanding the array storage if
 *  necessary.
 *
 *  @return psArray*        The array with the element added
 */
psArray* psArrayAdd(
    psArray* array,                    ///< array to operate on
    long delta,
    ///< the amount to expand array, if necessary.  If less than one, 10 will be used.
    psPtr data                         ///< the data pointer to add to psArray
);

/** Remove an element from the array
 *
 *  Finds and removes the specified data pointer from the list.
 *
 * @return bool:  TRUE if the specified data pointer was found and removed,
 *                otherwise FALSE.
 *
 */
bool psArrayRemove(
    psArray* array,                    ///< array to operate on
    const psPtr data                   ///< the data pointer to remove from psArray
);

/** Deallocate/Dereference elements of an array.
 *
 * Uses psLib memory allocation functions to deallocate/dereference elements
 * of a array of void pointers.  The array psArr is not freed, and its elements
 * will all be set to NULL.
 *
 */
void psArrayElementFree(
    psArray* psArr                     ///< Void pointer array to destroy.
);

/** Sort the array according to an external compare function.
 *
 *  Sorts an array via the specification of a comparison function
 *  to specify how the objects on the array should be sorted.
 *
 *  The comparison function must return an integer less than, equal to, or
 *  greater than zero if the first argument is considered to be respectively
 *  less than, equal to, or greater than the second.
 *
 *  If two members compare as equal, their order in the sorted array is
 *  undefined.
 *
 *  @return psArray* The sorted array.
 */
psArray* psArraySort(
    psArray* array,                       ///< input array to sort.
    psComparePtrFunc func                 ///< the compare function
);

/** Set an element in the array.  If the current element is non-NULL, the old
 *  element is freed.
 *
 *  @return psBool  TRUE if the element was set successfully, otherwise FALSE
 */
bool psArraySet(
    psArray* array,                    ///< input array to set element in
    long position,                     ///< the element position to set
    psPtr data                         ///< the value to set it to
);

/** Get an element from the array.
 *
 *  @return void*   the element at given position.
 */
psPtr psArrayGet(
    const psArray* array,              ///< input array to get element from
    long position                      ///< the element position to get
);

/// @}

#endif // #ifndef PS_ARRAY_H
