Blame include/complib/cl_vector.h

Packit 13e616
/*
Packit 13e616
 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
Packit 13e616
 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
Packit 13e616
 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
Packit 13e616
 *
Packit 13e616
 * This software is available to you under a choice of one of two
Packit 13e616
 * licenses.  You may choose to be licensed under the terms of the GNU
Packit 13e616
 * General Public License (GPL) Version 2, available from the file
Packit 13e616
 * COPYING in the main directory of this source tree, or the
Packit 13e616
 * OpenIB.org BSD license below:
Packit 13e616
 *
Packit 13e616
 *     Redistribution and use in source and binary forms, with or
Packit 13e616
 *     without modification, are permitted provided that the following
Packit 13e616
 *     conditions are met:
Packit 13e616
 *
Packit 13e616
 *      - Redistributions of source code must retain the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer.
Packit 13e616
 *
Packit 13e616
 *      - Redistributions in binary form must reproduce the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer in the documentation and/or other materials
Packit 13e616
 *        provided with the distribution.
Packit 13e616
 *
Packit 13e616
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 13e616
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 13e616
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 13e616
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit 13e616
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit 13e616
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 13e616
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit 13e616
 * SOFTWARE.
Packit 13e616
 *
Packit 13e616
 */
Packit 13e616
Packit 13e616
/*
Packit 13e616
 * Abstract:
Packit 13e616
 *	This file contains vector definitions.  Vector provides dynmically
Packit 13e616
 *	resizable array functionality.  Objects in a Vector are not relocated
Packit 13e616
 *	when the array is resized.
Packit 13e616
 */
Packit 13e616
Packit 13e616
#ifndef _CL_VECTOR_H_
Packit 13e616
#define _CL_VECTOR_H_
Packit 13e616
Packit 13e616
#include <complib/cl_qlist.h>
Packit 13e616
Packit 13e616
#ifdef __cplusplus
Packit 13e616
#  define BEGIN_C_DECLS extern "C" {
Packit 13e616
#  define END_C_DECLS   }
Packit 13e616
#else				/* !__cplusplus */
Packit 13e616
#  define BEGIN_C_DECLS
Packit 13e616
#  define END_C_DECLS
Packit 13e616
#endif				/* __cplusplus */
Packit 13e616
Packit 13e616
BEGIN_C_DECLS
Packit 13e616
/****h* Component Library/Vector
Packit 13e616
* NAME
Packit 13e616
*	Vector
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The Vector is a self-sizing array. Like a traditonal array, a vector
Packit 13e616
*	allows efficient constant time access to elements with a specified index.
Packit 13e616
*	A vector grows transparently as the user adds elements to the array.
Packit 13e616
*
Packit 13e616
*	As the vector grows in size, it does not relocate existing elements in
Packit 13e616
*	memory. This allows using pointers to elements stored in a Vector.
Packit 13e616
*
Packit 13e616
*	Users can supply an initializer functions that allow a vector to ensure
Packit 13e616
*	that new items added to the vector are properly initialized. A vector
Packit 13e616
*	calls the initializer function on a per object basis when growing the
Packit 13e616
*	array. The initializer is optional.
Packit 13e616
*
Packit 13e616
*	The initializer function can fail, and returns a cl_status_t. The vector
Packit 13e616
*	will call the destructor function, if provided, for an element that
Packit 13e616
*	failed initialization. If an initializer fails, a vector does not call
Packit 13e616
*	the initializer for objects in the remainder of the new memory allocation.
Packit 13e616
*
Packit 13e616
*	The cl_vector_t structure should be treated as opaque and should be
Packit 13e616
*	manipulated only through the provided functions.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Structures:
Packit 13e616
*		cl_vector_t
Packit 13e616
*
Packit 13e616
*	Callbacks:
Packit 13e616
*		cl_pfn_vec_init_t, cl_pfn_vec_dtor_t, cl_pfn_vec_apply_t,
Packit 13e616
*		cl_pfn_vec_find_t
Packit 13e616
*
Packit 13e616
*	Item Manipulation:
Packit 13e616
*		cl_vector_set_obj, cl_vector_obj
Packit 13e616
*
Packit 13e616
*	Initialization:
Packit 13e616
*		cl_vector_construct, cl_vector_init, cl_vector_destroy
Packit 13e616
*
Packit 13e616
*	Manipulation:
Packit 13e616
*		cl_vector_get_capacity, cl_vector_set_capacity,
Packit 13e616
*		cl_vector_get_size, cl_vector_set_size, cl_vector_set_min_size
Packit 13e616
*		cl_vector_get_ptr, cl_vector_get, cl_vector_at, cl_vector_set
Packit 13e616
*
Packit 13e616
*	Search:
Packit 13e616
*		cl_vector_find_from_start, cl_vector_find_from_end
Packit 13e616
*		cl_vector_apply_func
Packit 13e616
*********/
Packit 13e616
/****d* Component Library: Vector/cl_pfn_vec_init_t
Packit 13e616
* NAME
Packit 13e616
*	cl_pfn_vec_init_t
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_pfn_vec_init_t function type defines the prototype for functions
Packit 13e616
*	used as initializer for elements being allocated by a vector.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
typedef cl_status_t
Packit 13e616
    (*cl_pfn_vec_init_t) (IN void *const p_element, IN void *context);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_element
Packit 13e616
*		[in] Pointer to an element being added to a vector.
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		[in] Context provided in a call to cl_vector_init.
Packit 13e616
*
Packit 13e616
* RETURN VALUES
Packit 13e616
*	Return CL_SUCCESS to indicate that the element was initialized successfully.
Packit 13e616
*
Packit 13e616
*	Other cl_status_t values will be returned by the cl_vector_init,
Packit 13e616
*	cl_vector_set_size, and cl_vector_set_min_size functions.
Packit 13e616
*
Packit 13e616
*	In situations where the vector's size needs to grows in order to satisfy
Packit 13e616
*	a call to cl_vector_set, a non-successful status returned by the
Packit 13e616
*	initializer callback causes the growth to stop.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	This function type is provided as function prototype reference for
Packit 13e616
*	the initializer function provided by users as an optional parameter to
Packit 13e616
*	the cl_vector_init function.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_init
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****d* Component Library: Vector/cl_pfn_vec_dtor_t
Packit 13e616
* NAME
Packit 13e616
*	cl_pfn_vec_dtor_t
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_pfn_vec_dtor_t function type defines the prototype for functions
Packit 13e616
*	used as destructor for elements being deallocated from a vector.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
typedef void
Packit 13e616
 (*cl_pfn_vec_dtor_t) (IN void *const p_element, IN void *context);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_element
Packit 13e616
*		[in] Pointer to an element being deallocated from a vector.
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		[in] Context provided in a call to cl_vector_init.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function does not return a value.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	This function type is provided as function prototype reference for
Packit 13e616
*	the destructor function provided by users as an optional parameter to
Packit 13e616
*	the cl_vector_init function.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_init
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****d* Component Library: Vector/cl_pfn_vec_apply_t
Packit 13e616
* NAME
Packit 13e616
*	cl_pfn_vec_apply_t
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_pfn_vec_apply_t function type defines the prototype for functions
Packit 13e616
*	used to iterate elements in a vector.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
typedef void
Packit 13e616
 (*cl_pfn_vec_apply_t) (IN const size_t index,
Packit 13e616
			IN void *const p_element, IN void *context);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	index
Packit 13e616
*		[in] Index of the element.
Packit 13e616
*
Packit 13e616
*	p_element
Packit 13e616
*		[in] Pointer to an element at the specified index in the vector.
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		[in] Context provided in a call to cl_vector_apply_func.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function does not return a value.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	This function type is provided as function prototype reference for
Packit 13e616
*	the function passed by users as a parameter to the cl_vector_apply_func
Packit 13e616
*	function.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_apply_func
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****d* Component Library: Vector/cl_pfn_vec_find_t
Packit 13e616
* NAME
Packit 13e616
*	cl_pfn_vec_find_t
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_pfn_vec_find_t function type defines the prototype for functions
Packit 13e616
*	used to find elements in a vector.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
typedef cl_status_t
Packit 13e616
    (*cl_pfn_vec_find_t) (IN const size_t index,
Packit 13e616
			  IN const void *const p_element, IN void *context);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	index
Packit 13e616
*		[in] Index of the element.
Packit 13e616
*
Packit 13e616
*	p_element
Packit 13e616
*		[in] Pointer to an element at the specified index in the vector.
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		[in] Context provided in a call to cl_vector_find_from_start or
Packit 13e616
*		cl_vector_find_from_end.
Packit 13e616
*
Packit 13e616
* RETURN VALUES
Packit 13e616
*	Return CL_SUCCESS if the element was found. This stops vector iteration.
Packit 13e616
*
Packit 13e616
*	CL_NOT_FOUND to continue the vector iteration.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	This function type is provided as function prototype reference for the
Packit 13e616
*	function provided by users as a parameter to the cl_vector_find_from_start
Packit 13e616
*	and cl_vector_find_from_end functions.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_find_from_start, cl_vector_find_from_end
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****i* Component Library: Vector/cl_pfn_vec_copy_t
Packit 13e616
* NAME
Packit 13e616
*	cl_pfn_vec_copy_t
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_pfn_vec_copy_t function type defines the prototype for functions
Packit 13e616
*	used to copy elements in a vector.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
typedef void
Packit 13e616
 (*cl_pfn_vec_copy_t) (IN void *const p_dest,
Packit 13e616
		       IN const void *const p_src, IN const size_t size);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_dest
Packit 13e616
*		[in] Pointer to the destination buffer into which to copy p_src.
Packit 13e616
*
Packit 13e616
*	p_src
Packit 13e616
*		[in] Pointer to the destination buffer from which to copy.
Packit 13e616
*
Packit 13e616
*	size
Packit 13e616
*		[in] Number of bytes to copy.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function does not return a value.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****s* Component Library: Vector/cl_vector_t
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_t
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	Vector structure.
Packit 13e616
*
Packit 13e616
*	The cl_vector_t structure should be treated as opaque and should be
Packit 13e616
*	manipulated only through the provided functions.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
typedef struct _cl_vector {
Packit 13e616
	size_t size;
Packit 13e616
	size_t grow_size;
Packit 13e616
	size_t capacity;
Packit 13e616
	size_t element_size;
Packit 13e616
	cl_pfn_vec_init_t pfn_init;
Packit 13e616
	cl_pfn_vec_dtor_t pfn_dtor;
Packit 13e616
	cl_pfn_vec_copy_t pfn_copy;
Packit 13e616
	const void *context;
Packit 13e616
	cl_qlist_t alloc_list;
Packit 13e616
	void **p_ptr_array;
Packit 13e616
	cl_state_t state;
Packit 13e616
} cl_vector_t;
Packit 13e616
/*
Packit 13e616
* FIELDS
Packit 13e616
*	size
Packit 13e616
*		 Number of elements successfully initialized in the vector.
Packit 13e616
*
Packit 13e616
*	grow_size
Packit 13e616
*		 Number of elements to allocate when growing.
Packit 13e616
*
Packit 13e616
*	capacity
Packit 13e616
*		 total # of elements allocated.
Packit 13e616
*
Packit 13e616
*	element_size
Packit 13e616
*		 Size of each element.
Packit 13e616
*
Packit 13e616
*	pfn_init
Packit 13e616
*		 User supplied element initializer.
Packit 13e616
*
Packit 13e616
*	pfn_dtor
Packit 13e616
*		 User supplied element destructor.
Packit 13e616
*
Packit 13e616
*	pfn_copy
Packit 13e616
*		 Copy operator.
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		 User context for callbacks.
Packit 13e616
*
Packit 13e616
*	alloc_list
Packit 13e616
*		 List of allocations.
Packit 13e616
*
Packit 13e616
*	p_ptr_array
Packit 13e616
*		 Internal array of pointers to elements.
Packit 13e616
*
Packit 13e616
*	state
Packit 13e616
*		State of the vector.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_construct
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_construct
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_construct function constructs a vector.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
void cl_vector_construct(IN cl_vector_t * const p_vector);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure to construct.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function does not return a value.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	Allows calling cl_vector_destroy without first calling cl_vector_init.
Packit 13e616
*
Packit 13e616
*	Calling cl_vector_construct is a prerequisite to calling any other
Packit 13e616
*	vector function except cl_vector_init.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_init, cl_vector_destroy
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_init
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_init
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_init function initializes a vector for use.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
cl_status_t
Packit 13e616
cl_vector_init(IN cl_vector_t * const p_vector,
Packit 13e616
	       IN const size_t min_size,
Packit 13e616
	       IN const size_t grow_size,
Packit 13e616
	       IN const size_t element_size,
Packit 13e616
	       IN cl_pfn_vec_init_t pfn_init OPTIONAL,
Packit 13e616
	       IN cl_pfn_vec_dtor_t pfn_dtor OPTIONAL,
Packit 13e616
	       IN const void *const context);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure to inititalize.
Packit 13e616
*
Packit 13e616
*	min_size
Packit 13e616
*		[in] Initial number of elements.
Packit 13e616
*
Packit 13e616
*	grow_size
Packit 13e616
*		[in] Number of elements to allocate when incrementally growing
Packit 13e616
*		the vector.  A value of zero disables automatic growth.
Packit 13e616
*
Packit 13e616
*	element_size
Packit 13e616
*		[in] Size of each element.
Packit 13e616
*
Packit 13e616
*	pfn_init
Packit 13e616
*		[in] Initializer callback to invoke for every new element.
Packit 13e616
*		See the cl_pfn_vec_init_t function type declaration for details about
Packit 13e616
*		the callback function.
Packit 13e616
*
Packit 13e616
*	pfn_dtor
Packit 13e616
*		[in] Destructor callback to invoke for elements being deallocated.
Packit 13e616
*		See the cl_pfn_vec_dtor_t function type declaration for details about
Packit 13e616
*		the callback function.
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		[in] Value to pass to the callback functions to provide context.
Packit 13e616
*
Packit 13e616
* RETURN VALUES
Packit 13e616
*	CL_SUCCESS if the vector was initialized successfully.
Packit 13e616
*
Packit 13e616
*	CL_INSUFFICIENT_MEMORY if the initialization failed.
Packit 13e616
*
Packit 13e616
*	cl_status_t value returned by optional initializer function specified by
Packit 13e616
*	the pfn_init parameter.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	The constructor and initializer functions, if any, are invoked for every
Packit 13e616
*	new element in the array.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_construct, cl_vector_destroy, cl_vector_set,
Packit 13e616
*	cl_vector_get, cl_vector_get_ptr, cl_vector_at
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_destroy
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_destroy
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_destroy function destroys a vector.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
void cl_vector_destroy(IN cl_vector_t * const p_vector);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure to destroy.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function does not return a value.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	cl_vector_destroy frees all memory allocated for the vector. The vector
Packit 13e616
*	is left initialized to a zero capacity and size.
Packit 13e616
*
Packit 13e616
*	This function should only be called after a call to cl_vector_construct
Packit 13e616
*	or cl_vector_init.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_construct, cl_vector_init
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_get_capacity
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_get_capacity
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_get_capacity function returns the capacity of a vector.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
static inline size_t
Packit 13e616
cl_vector_get_capacity(IN const cl_vector_t * const p_vector)
Packit 13e616
{
Packit 13e616
	CL_ASSERT(p_vector);
Packit 13e616
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit 13e616
Packit 13e616
	return (p_vector->capacity);
Packit 13e616
}
Packit 13e616
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure whose capacity to return.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	Capacity, in elements, of the vector.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	The capacity is the number of elements that the vector can store, and
Packit 13e616
*	can be greater than the number of elements stored. To get the number of
Packit 13e616
*	elements stored in the vector, use cl_vector_get_size.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_set_capacity, cl_vector_get_size
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_get_size
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_get_size
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_get_size function returns the size of a vector.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
static inline size_t cl_vector_get_size(IN const cl_vector_t * const p_vector)
Packit 13e616
{
Packit 13e616
	CL_ASSERT(p_vector);
Packit 13e616
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit 13e616
Packit 13e616
	return (p_vector->size);
Packit 13e616
}
Packit 13e616
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure whose size to return.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	Size, in elements, of the vector.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_set_size, cl_vector_get_capacity
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_get_ptr
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_get_ptr
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_get_ptr function returns a pointer to an element
Packit 13e616
*	stored in a vector at a specified index.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
static inline void *cl_vector_get_ptr(IN const cl_vector_t * const p_vector,
Packit 13e616
				      IN const size_t index)
Packit 13e616
{
Packit 13e616
	CL_ASSERT(p_vector);
Packit 13e616
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit 13e616
Packit 13e616
	return (p_vector->p_ptr_array[index]);
Packit 13e616
}
Packit 13e616
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure from which to get a
Packit 13e616
*		pointer to an element.
Packit 13e616
*
Packit 13e616
*	index
Packit 13e616
*		[in] Index of the element.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	Pointer to the element stored at specified index.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	cl_vector_get_ptr provides constant access times regardless of the index.
Packit 13e616
*
Packit 13e616
*	cl_vector_get_ptr does not perform boundary checking. Callers are
Packit 13e616
*	responsible for providing an index that is within the range of the vector.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_get, cl_vector_at, cl_vector_set, cl_vector_get_size
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_get
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_get
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_get function copies an element stored in a vector at a
Packit 13e616
*	specified index.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
static inline void
Packit 13e616
cl_vector_get(IN const cl_vector_t * const p_vector,
Packit 13e616
	      IN const size_t index, OUT void *const p_element)
Packit 13e616
{
Packit 13e616
	void *p_src;
Packit 13e616
Packit 13e616
	CL_ASSERT(p_vector);
Packit 13e616
	CL_ASSERT(p_vector->state == CL_INITIALIZED);
Packit 13e616
	CL_ASSERT(p_element);
Packit 13e616
Packit 13e616
	/* Get a pointer to the element. */
Packit 13e616
	p_src = cl_vector_get_ptr(p_vector, index);
Packit 13e616
	p_vector->pfn_copy(p_src, p_element, p_vector->element_size);
Packit 13e616
}
Packit 13e616
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure from which to get a copy of
Packit 13e616
*		an element.
Packit 13e616
*
Packit 13e616
*	index
Packit 13e616
*		[in] Index of the element.
Packit 13e616
*
Packit 13e616
*	p_element
Packit 13e616
*		[out] Pointer to storage for the element. Contains a copy of the
Packit 13e616
*		desired element upon successful completion of the call.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function does not return a value.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	cl_vector_get provides constant time access regardless of the index.
Packit 13e616
*
Packit 13e616
*	cl_vector_get does not perform boundary checking on the vector, and
Packit 13e616
*	callers are responsible for providing an index that is within the range
Packit 13e616
*	of the vector. To access elements after performing boundary checks,
Packit 13e616
*	use cl_vector_at.
Packit 13e616
*
Packit 13e616
*	The p_element parameter contains a copy of the desired element upon
Packit 13e616
*	return from this function.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_get_ptr, cl_vector_at
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_at
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_at
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_at function copies an element stored in a vector at a
Packit 13e616
*	specified index, performing boundary checks.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
cl_status_t
Packit 13e616
cl_vector_at(IN const cl_vector_t * const p_vector,
Packit 13e616
	     IN const size_t index, OUT void *const p_element);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure from which to get a copy of
Packit 13e616
*		an element.
Packit 13e616
*
Packit 13e616
*	index
Packit 13e616
*		[in] Index of the element.
Packit 13e616
*
Packit 13e616
*	p_element
Packit 13e616
*		[out] Pointer to storage for the element. Contains a copy of the
Packit 13e616
*		desired element upon successful completion of the call.
Packit 13e616
*
Packit 13e616
* RETURN VALUES
Packit 13e616
*	CL_SUCCESS if an element was found at the specified index.
Packit 13e616
*
Packit 13e616
*	CL_INVALID_SETTING if the index was out of range.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	cl_vector_at provides constant time access regardless of the index, and
Packit 13e616
*	performs boundary checking on the vector.
Packit 13e616
*
Packit 13e616
*	Upon success, the p_element parameter contains a copy of the desired element.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_get, cl_vector_get_ptr
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_set
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_set
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_set function sets the element at the specified index.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
cl_status_t
Packit 13e616
cl_vector_set(IN cl_vector_t * const p_vector,
Packit 13e616
	      IN const size_t index, IN void *const p_element);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure into which to store
Packit 13e616
*		an element.
Packit 13e616
*
Packit 13e616
*	index
Packit 13e616
*		[in] Index of the element.
Packit 13e616
*
Packit 13e616
*	p_element
Packit 13e616
*		[in] Pointer to an element to store in the vector.
Packit 13e616
*
Packit 13e616
* RETURN VALUES
Packit 13e616
*	CL_SUCCESS if the element was successfully set.
Packit 13e616
*
Packit 13e616
*	CL_INSUFFICIENT_MEMORY if the vector could not be resized to accommodate
Packit 13e616
*	the new element.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	cl_vector_set grows the vector as needed to accommodate the new element,
Packit 13e616
*	unless the grow_size parameter passed into the cl_vector_init function
Packit 13e616
*	was zero.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_get
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_set_capacity
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_set_capacity
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_set_capacity function reserves memory in a vector for a
Packit 13e616
*	specified number of elements.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
cl_status_t
Packit 13e616
cl_vector_set_capacity(IN cl_vector_t * const p_vector,
Packit 13e616
		       IN const size_t new_capacity);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure whose capacity to set.
Packit 13e616
*
Packit 13e616
*	new_capacity
Packit 13e616
*		[in] Total number of elements for which the vector should
Packit 13e616
*		allocate memory.
Packit 13e616
*
Packit 13e616
* RETURN VALUES
Packit 13e616
*	CL_SUCCESS if the capacity was successfully set.
Packit 13e616
*
Packit 13e616
*	CL_INSUFFICIENT_MEMORY if there was not enough memory to satisfy the
Packit 13e616
*	operation. The vector is left unchanged.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	cl_vector_set_capacity increases the capacity of the vector. It does
Packit 13e616
*	not change the size of the vector. If the requested capacity is less
Packit 13e616
*	than the current capacity, the vector is left unchanged.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_get_capacity, cl_vector_set_size,
Packit 13e616
*	cl_vector_set_min_size
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_set_size
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_set_size
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_set_size function resizes a vector, either increasing or
Packit 13e616
*	decreasing its size.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
cl_status_t
Packit 13e616
cl_vector_set_size(IN cl_vector_t * const p_vector, IN const size_t size);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure whose size to set.
Packit 13e616
*
Packit 13e616
*	size
Packit 13e616
*		[in] Number of elements desired in the vector.
Packit 13e616
*
Packit 13e616
* RETURN VALUES
Packit 13e616
*	CL_SUCCESS if the size of the vector was set successfully.
Packit 13e616
*
Packit 13e616
*	CL_INSUFFICIENT_MEMORY if there was not enough memory to complete the
Packit 13e616
*	operation. The vector is left unchanged.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	cl_vector_set_size sets the vector to the specified size. If size is
Packit 13e616
*	smaller than the current size of the vector, the size is reduced.
Packit 13e616
*	The destructor function, if any, will be invoked for all elements that
Packit 13e616
*	are above size. Likewise, the constructor and initializer, if any, will
Packit 13e616
*	be invoked for all new elements.
Packit 13e616
*
Packit 13e616
*	This function can only fail if size is larger than the current capacity.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_get_size, cl_vector_set_min_size,
Packit 13e616
*	cl_vector_set_capacity
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_set_min_size
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_set_min_size
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_set_min_size function resizes a vector to a specified size
Packit 13e616
*	if the vector is smaller than the specified size.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
cl_status_t
Packit 13e616
cl_vector_set_min_size(IN cl_vector_t * const p_vector,
Packit 13e616
		       IN const size_t min_size);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure whose minimum size to set.
Packit 13e616
*
Packit 13e616
*	min_size
Packit 13e616
*		[in] Minimum number of elements that the vector should contain.
Packit 13e616
*
Packit 13e616
* RETURN VALUES
Packit 13e616
*	CL_SUCCESS if the vector size is greater than or equal to min_size.  This
Packit 13e616
*	could indicate that the vector's capacity was increased to min_size or
Packit 13e616
*	that the vector was already of sufficient size.
Packit 13e616
*
Packit 13e616
*	CL_INSUFFICIENT_MEMORY if there was not enough memory to resize the vector.
Packit 13e616
*	The vector is left unchanged.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	If min_size is smaller than the current size of the vector, the vector is
Packit 13e616
*	unchanged. The vector is unchanged if the size could not be changed due
Packit 13e616
*	to insufficient memory being available to perform the operation.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_get_size, cl_vector_set_size, cl_vector_set_capacity
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_apply_func
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_apply_func
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_apply_func function invokes a specified function for every
Packit 13e616
*	element in a vector.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
void
Packit 13e616
cl_vector_apply_func(IN const cl_vector_t * const p_vector,
Packit 13e616
		     IN cl_pfn_vec_apply_t pfn_callback,
Packit 13e616
		     IN const void *const context);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure whose elements to iterate.
Packit 13e616
*
Packit 13e616
*	pfn_callback
Packit 13e616
*		[in] Function invoked for every element in the array.
Packit 13e616
*		See the cl_pfn_vec_apply_t function type declaration for details
Packit 13e616
*		about the callback function.
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		[in] Value to pass to the callback function.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function does not return a value.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	cl_vector_apply_func invokes the specified function for every element
Packit 13e616
*	in the vector, starting from the beginning of the vector.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_find_from_start, cl_vector_find_from_end,
Packit 13e616
*	cl_pfn_vec_apply_t
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_find_from_start
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_find_from_start
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_find_from_start function uses a specified function to
Packit 13e616
*	search for elements in a vector starting from the lowest index.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
size_t
Packit 13e616
cl_vector_find_from_start(IN const cl_vector_t * const p_vector,
Packit 13e616
			  IN cl_pfn_vec_find_t pfn_callback,
Packit 13e616
			  IN const void *const context);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure to inititalize.
Packit 13e616
*
Packit 13e616
*	pfn_callback
Packit 13e616
*		[in] Function invoked to determine if a match was found.
Packit 13e616
*		See the cl_pfn_vec_find_t function type declaration for details
Packit 13e616
*		about the callback function.
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		[in] Value to pass to the callback function.
Packit 13e616
*
Packit 13e616
* RETURN VALUES
Packit 13e616
*	Index of the element, if found.
Packit 13e616
*
Packit 13e616
*	Size of the vector if the element was not found.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	cl_vector_find_from_start does not remove the found element from
Packit 13e616
*	the vector. The index of the element is returned when the function
Packit 13e616
*	provided by the pfn_callback parameter returns CL_SUCCESS.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_find_from_end, cl_vector_apply_func, cl_pfn_vec_find_t
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Vector/cl_vector_find_from_end
Packit 13e616
* NAME
Packit 13e616
*	cl_vector_find_from_end
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The cl_vector_find_from_end function uses a specified function to search
Packit 13e616
*	for elements in a vector starting from the highest index.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
size_t
Packit 13e616
cl_vector_find_from_end(IN const cl_vector_t * const p_vector,
Packit 13e616
			IN cl_pfn_vec_find_t pfn_callback,
Packit 13e616
			IN const void *const context);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_vector
Packit 13e616
*		[in] Pointer to a cl_vector_t structure to inititalize.
Packit 13e616
*
Packit 13e616
*	pfn_callback
Packit 13e616
*		[in] Function invoked to determine if a match was found.
Packit 13e616
*		See the cl_pfn_vec_find_t function type declaration for details
Packit 13e616
*		about the callback function.
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		[in] Value to pass to the callback function.
Packit 13e616
*
Packit 13e616
* RETURN VALUES
Packit 13e616
*	Index of the element, if found.
Packit 13e616
*
Packit 13e616
*	Size of the vector if the element was not found.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	cl_vector_find_from_end does not remove the found element from
Packit 13e616
*	the vector. The index of the element is returned when the function
Packit 13e616
*	provided by the pfn_callback parameter returns CL_SUCCESS.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Vector, cl_vector_find_from_start, cl_vector_apply_func,
Packit 13e616
*	cl_pfn_vec_find_t
Packit 13e616
*********/
Packit 13e616
Packit 13e616
END_C_DECLS
Packit 13e616
#endif				/* _CL_VECTOR_H_ */