ut0vec.ic 3.19 KB
Newer Older
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*****************************************************************************

Copyright (c) 2006, 2009, Innobase Oy. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

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

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
19 20 21 22 23 24 25 26 27 28
/*******************************************************************//**
@file include/ut0vec.ic
A vector of pointers to data items

Created 4/6/2006 Osku Salerma
************************************************************************/

/****************************************************************//**
Get number of elements in vector.
@return	number of elements in vector */
29 30 31 32
UNIV_INLINE
ulint
ib_vector_size(
/*===========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
33
	const ib_vector_t*	vec)	/*!< in: vector */
34 35 36 37
{
	return(vec->used);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
38 39 40
/****************************************************************//**
Get n'th element.
@return	n'th element */
41 42 43 44
UNIV_INLINE
void*
ib_vector_get(
/*==========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
45 46
	ib_vector_t*	vec,	/*!< in: vector */
	ulint		n)	/*!< in: element index to get */
47 48 49 50 51
{
	ut_a(n < vec->used);

	return(vec->data[n]);
}
Vadim Tkachenko's avatar
Vadim Tkachenko committed
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/****************************************************************//**
Get last element. The vector must not be empty.
@return	last element */
UNIV_INLINE
void*
ib_vector_get_last(
/*===============*/
	ib_vector_t*	vec)	/*!< in: vector */
{
	ut_a(vec->used > 0);

	return(vec->data[vec->used - 1]);
}

/****************************************************************//**
Set the n'th element. */
UNIV_INLINE
void
ib_vector_set(
/*==========*/
	ib_vector_t*	vec,	/*!< in/out: vector */
	ulint		n,	/*!< in: element index to set */
	void*		elem)	/*!< in: data element */
{
	ut_a(n < vec->used);

	vec->data[n] = elem;
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
82 83 84
/****************************************************************//**
Remove the last element from the vector.
@return	last vector element */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
85 86 87 88
UNIV_INLINE
void*
ib_vector_pop(
/*==========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
89
	ib_vector_t*    vec)    /*!< in/out: vector */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
90 91 92 93 94 95 96 97 98 99 100 101 102
{
	void*           elem;

	ut_a(vec->used > 0);
	--vec->used;
	elem = vec->data[vec->used];

	ut_d(vec->data[vec->used] = NULL);
	UNIV_MEM_INVALID(&vec->data[vec->used], sizeof(*vec->data));

	return(elem);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
103
/****************************************************************//**
Vadim Tkachenko's avatar
Vadim Tkachenko committed
104 105 106 107 108 109
Free the underlying heap of the vector. Note that vec is invalid
after this call. */
UNIV_INLINE
void
ib_vector_free(
/*===========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
110
	ib_vector_t*    vec)    /*!< in, own: vector */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
111 112 113 114
{
	mem_heap_free(vec->heap);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
115 116 117
/****************************************************************//**
Test whether a vector is empty or not.
@return	TRUE if empty */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
118 119 120
UNIV_INLINE
ibool
ib_vector_is_empty(
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
121 122
/*===============*/
	const ib_vector_t*	vec)	/*!< in: vector */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
123 124 125
{
	return(ib_vector_size(vec) == 0);
}