vector.h revision 2cb03d0d9990356717482846beac17c74939dc13
/** @file
* STL-like vector implementation in C
* @note functions in this file are inline to prevent warnings about
* unused static functions. I assume that in this day and age a
* compiler makes its own decisions about whether to actually
* inline a function.
*/
/*
* Copyright (C) 2008-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#define VECTOR_CONCAT(a, b) a ## b
#define VECTOR_XCONCAT(a, b) VECTOR_CONCAT(a, b)
/** A publicly visible (method, type, etc) name relating to the vector type */
/** An implementation-private (method, type, etc) name */
/** The size of a vector element */
#define VECTOR_ELEMENT_SIZE sizeof(VECTOR_TYPE)
/** The unit by which the vector capacity is increased */
#define VECTOR_ALLOC_UNIT 16
#ifndef VECTOR_TYPE
/** VECTOR_TYPE must be defined to the type that the vector will contain. The
* macro will be undef-ed again by this header. */
#endif
#ifndef VECTOR_TYPENAME
/** VECTOR_TYPENAME must be defined to the typename for the vector. The
* macro will be undef-ed again by this header. */
#endif
#ifndef VECTOR_ALLOCATOR
/** VECTOR_ALLOCATOR can be defined to an alternative allocator for the
* vector's memory. The allocator must be a function with realloc semantics.
* The macro will be undef-ed again by this header. */
# define VECTOR_ALLOCATOR RTMemRealloc
#endif
#ifndef VECTOR_DESTRUCTOR
/** VECTOR_DESTRUCTOR can be defined to be a routine to clean up vector
* elements before they are freed. It must return void and take a pointer to
* an element as a parameter. The macro will be undef-ed again by this header.
*/
#endif
/** Structure describing the vector itself */
typedef struct VECTOR_TYPENAME
{
/** The beginning of the allocated memory */
/** Pointer to just after the end of the last element */
/** Pointer to just after the end of the allocated memory */
/** Non-constant iterator over the vector */
/*** Private methods ***/
/** Destructor that does nothing. */
{
(void) pSelf;
}
/** Expand an existing vector */
{
if (!pRealloc)
return 0;
return 1;
}
/** Expand an existing vector */
{
}
/*** Public methods ***/
/** Initialise a newly allocated vector. The vector will be zeroed on failure.
*/
{
}
/** Clean up a vector so that the memory can be returned. For convenience,
* this may be used safely on a zeroed vector structure. */
{
{
}
}
/** Add a value onto the end of a vector */
{
return 0;
if (pElement)
return 1;
}
/** Reset the vector */
{
}
/** Number of elements in the vector */
{
}
/** Get the special "begin" iterator for a vector */
{
}
/** Get the special "end" iterator for a vector */
{
}
/** A structure with pointers to class operations to save too much use of long
* identifiers in source code. Use like:
* <vector_type>_op_table *pOps = &<vector_type>_ops;
* and then
* pOps->init(&my_vector);
*/
typedef const struct VECTOR_INTERNAL(op_table)
{
const VECTOR_PUBLIC(iterator) *
const VECTOR_PUBLIC(iterator) *
{
};
/*** Iterator methods ***/
/** Initialise a newly allocated iterator */
{
}
/** Dereference an iterator */
{
return *pSelf;
}
/** Increment an iterator */
{
++*pSelf;
}
/** Test whether an iterator is less than another. */
{
}
/** Test whether an iterator is equal to another. The special values
* ITERATOR_BEGIN and ITERATOR_END are recognised. */
{
}
/** A structure with pointers to iterator operations to save too much use of
* long identifiers in source code. Use like:
* <vector_type>_iter_op_table *pOps = &<vector_type>_iter_ops;
* and then
* pOps->init(&my_iter, &other_iter);
*/
typedef const struct VECTOR_INTERNAL(iter_op_table)
{
{
};
/* We need to undefine anything we have defined (and for convenience we also
* undefine our "parameter" macros) as this header may be included multiple
* times in one source file with different parameters. */