summaryrefslogtreecommitdiff
path: root/storage/innobase/include/ut0bh.ic
blob: a604237665df551c387f0eba9e460e6c6c021a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/***************************************************************************//**

Copyright (c) 2011, Oracle and/or its affiliates. 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.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA

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

/******************************************************************//**
@file include/ut0bh.ic
Binary min-heap implementation.

Created 2011-01-15 by Sunny Bains
*******************************************************/

#include "ut0bh.h"
#include "ut0mem.h"	/* For ut_memcpy() */

/**********************************************************************//**
Get the number of elements in the binary heap.
@return number of elements */
UNIV_INLINE
ulint
ib_bh_size(
/*=======*/
	const ib_bh_t*	ib_bh)			/*!< in: instance */
{
	return(ib_bh->n_elems);
}

/**********************************************************************//**
Test if binary heap is empty.
@return TRUE if empty. */
UNIV_INLINE
ibool
ib_bh_is_empty(
/*===========*/
	const ib_bh_t*	ib_bh)			/*!< in: instance */
{
	return(ib_bh_size(ib_bh) == 0);
}

/**********************************************************************//**
Test if binary heap is full.
@return TRUE if full. */
UNIV_INLINE
ibool
ib_bh_is_full(
/*===========*/
	const ib_bh_t*	ib_bh)			/*!< in: instance */
{
	return(ib_bh_size(ib_bh) >= ib_bh->max_elems);
}

/**********************************************************************//**
Get a pointer to the element.
@return pointer to element */
UNIV_INLINE
void*
ib_bh_get(
/*=======*/
	ib_bh_t*	ib_bh,			/*!< in: instance */
	ulint		i)			/*!< in: index */
{
	byte*		ptr = (byte*) (ib_bh + 1);

	ut_a(i < ib_bh_size(ib_bh));

	return(ptr + (ib_bh->sizeof_elem * i));
}

/**********************************************************************//**
Copy an element to the binary heap.
@return pointer to copied element */
UNIV_INLINE
void*
ib_bh_set(
/*======*/
	ib_bh_t*	ib_bh,			/*!< in/out: instance */
	ulint		i,			/*!< in: index */
	const void*	elem)			/*!< in: element to add */
{
	void*		ptr = ib_bh_get(ib_bh, i);

	ut_memcpy(ptr, elem, ib_bh->sizeof_elem);

	return(ptr);
}

/**********************************************************************//**
Return the first element from the binary heap.
@return pointer to first element or NULL if empty. */
UNIV_INLINE
void*
ib_bh_first(
/*========*/
	ib_bh_t*	ib_bh)			/*!< in: instance */
{
	return(ib_bh_is_empty(ib_bh) ? NULL : ib_bh_get(ib_bh, 0));
}

/**********************************************************************//**
Return the last element from the binary heap.
@return pointer to last element or NULL if empty. */
UNIV_INLINE
void*
ib_bh_last(
/*========*/
	ib_bh_t*	ib_bh)			/*!< in/out: instance */
{
	return(ib_bh_is_empty(ib_bh)
		? NULL
		: ib_bh_get(ib_bh, ib_bh_size(ib_bh) - 1));
}