CUBRID Engine  latest
binaryheap.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * binary heap header
22  */
23 
24 #ifndef _BINARYHEAP_H_
25 #define _BINARYHEAP_H_
26 
27 #if !defined (SERVER_MODE) && !defined (SA_MODE)
28 #error Belongs to server module
29 #endif /* !defined (SERVER_MODE) && !defined (SA_MODE) */
30 
31 #include "config.h"
32 #include "thread_compat.hpp"
33 
34 // TODO: which c-code compiled file reaches this header?
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39 
40  typedef void *BH_CMP_ARG;
41 
42  typedef enum
43  {
45  BH_LT = -1,
46  BH_EQ = 0,
47  BH_GT = 1
48  } BH_CMP_RESULT;
49 
50  typedef enum
51  {
55  } BH_HEAP_STATE;
56 
57  typedef enum
58  {
63 
64  typedef BH_CMP_RESULT (*bh_key_comparator) (const void *left, const void *right, BH_CMP_ARG arg);
65 
66  typedef struct binary_heap BINARY_HEAP;
67  struct binary_heap
68  {
70  int elem_size;
72  BH_HEAP_STATE state;
74  BH_CMP_ARG cmp_arg;
75  void *members;
76  void *swap_buf;
77  };
78 
79 #define BH_ELEMENT(heap, i) (((char *) (heap)->members) + (heap)->elem_size * (i))
80 #define BH_ROOT(heap) ((heap)->members)
81 
83  BH_CMP_ARG cmp_arg);
84  extern void bh_destroy (THREAD_ENTRY * thread_p, BINARY_HEAP * heap);
85 
86  extern int bh_add (BINARY_HEAP * heap, void *elem);
87  extern void bh_build_heap (BINARY_HEAP * heap);
88 
89  extern int bh_insert (BINARY_HEAP * heap, void *elem);
90  extern BH_TRY_INSERT_RESULT bh_try_insert (BINARY_HEAP * heap, void *elem, void *replaced);
91 
92  extern void bh_down_heap (BINARY_HEAP * heap, int index);
93  extern bool bh_extract_max (BINARY_HEAP * heap, void *extract_elem);
94  extern bool bh_peek_max (BINARY_HEAP * heap, void *peek_elem);
95 
96  extern bool bh_is_consistent (BINARY_HEAP * heap);
97 
98  extern void bh_to_sorted_array (BINARY_HEAP * heap);
99 
100  extern void bh_element_at (BINARY_HEAP * heap, int index, void *elem);
101  extern bool bh_is_full (BINARY_HEAP * heap);
102 #if defined(CUBRID_DEBUG)
103  extern bool bh_tests_consistent (BINARY_HEAP * heap);
104 #endif /* CUBRID_DEBUG */
105 #ifdef __cplusplus
106 }
107 #endif
108 
109 #endif /* _BINARYHEAP_H_ */
void bh_destroy(THREAD_ENTRY *thread_p, BINARY_HEAP *heap)
Definition: binaryheap.c:157
int element_count
Definition: binaryheap.h:71
void * BH_CMP_ARG
Definition: binaryheap.h:40
void THREAD_ENTRY
void * members
Definition: binaryheap.h:75
bool bh_peek_max(BINARY_HEAP *heap, void *peek_elem)
Definition: binaryheap.c:357
BH_HEAP_STATE
Definition: binaryheap.h:50
int bh_insert(BINARY_HEAP *heap, void *elem)
Definition: binaryheap.c:209
BH_CMP_ARG cmp_arg
Definition: binaryheap.h:74
BH_TRY_INSERT_RESULT bh_try_insert(BINARY_HEAP *heap, void *elem, void *replaced)
Definition: binaryheap.c:249
int max_capacity
Definition: binaryheap.h:69
bool bh_is_full(BINARY_HEAP *heap)
Definition: binaryheap.c:469
void bh_down_heap(BINARY_HEAP *heap, int index)
Definition: binaryheap.c:79
bh_key_comparator cmp_func
Definition: binaryheap.h:73
bool bh_extract_max(BINARY_HEAP *heap, void *extract_elem)
Definition: binaryheap.c:304
void * swap_buf
Definition: binaryheap.h:76
BH_HEAP_STATE state
Definition: binaryheap.h:72
BH_TRY_INSERT_RESULT
Definition: binaryheap.h:57
bool bh_is_consistent(BINARY_HEAP *heap)
Definition: binaryheap.c:457
BH_CMP_RESULT(* bh_key_comparator)(const void *left, const void *right, BH_CMP_ARG arg)
Definition: binaryheap.h:64
int elem_size
Definition: binaryheap.h:70
void bh_element_at(BINARY_HEAP *heap, int index, void *elem)
Definition: binaryheap.c:442
void bh_to_sorted_array(BINARY_HEAP *heap)
Definition: binaryheap.c:378
void bh_build_heap(BINARY_HEAP *heap)
Definition: binaryheap.c:278
int bh_add(BINARY_HEAP *heap, void *elem)
Definition: binaryheap.c:184
BINARY_HEAP * bh_create(THREAD_ENTRY *thread_p, int max_capacity, int elem_size, bh_key_comparator cmp_func, BH_CMP_ARG cmp_arg)
Definition: binaryheap.c:114
BH_CMP_RESULT
Definition: binaryheap.h:42