CUBRID Engine  latest
broker_max_heap.c
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  * broker_max_heap.c -
22  */
23 
24 #ident "$Id$"
25 
26 #include "broker_max_heap.h"
27 
28 int
29 max_heap_insert (T_MAX_HEAP_NODE * max_heap, int max_heap_size, T_MAX_HEAP_NODE * item)
30 {
31  int i;
32 
33  if (max_heap->id >= max_heap_size)
34  {
35  return -1;
36  }
37 
38  i = ++(max_heap->id);
39  while ((i != 1) && (item->priority > max_heap[i / 2].priority))
40  {
41  max_heap[i] = max_heap[i / 2];
42  i /= 2;
43  }
44  max_heap[i] = *item;
45  return 0;
46 }
47 
48 int
49 max_heap_change_priority (T_MAX_HEAP_NODE * max_heap, int id, int new_priority)
50 {
51  T_MAX_HEAP_NODE item;
52  int i, k;
53 
54  k = -1;
55  for (i = 1; i <= max_heap[0].id; i++)
56  {
57  if (max_heap[i].id == id)
58  {
59  item = max_heap[i];
60  item.priority = new_priority;
61  k = i;
62  break;
63  }
64  }
65  if (k < 0)
66  return -1;
67 
68  while ((k != 1) && (item.priority > max_heap[k / 2].priority))
69  {
70  max_heap[k] = max_heap[k / 2];
71  k /= 2;
72  }
73  max_heap[k] = item;
74  return 0;
75 }
76 
77 int
79 {
80  T_MAX_HEAP_NODE temp;
81  int parent, child;
82 
83  if (max_heap[0].id <= 0)
84  return -1;
85 
86  *ret = max_heap[1];
87 
88  temp = max_heap[(max_heap[0].id)--];
89  parent = 1;
90  child = 2;
91 
92  while (child <= max_heap[0].id)
93  {
94  if ((child < max_heap[0].id) && (max_heap[child].priority < max_heap[child + 1].priority))
95  child++;
96  if (temp.priority > max_heap[child].priority)
97  break;
98  max_heap[parent] = max_heap[child];
99  parent = child;
100  child *= 2;
101  }
102  max_heap[parent] = temp;
103  return 0;
104 }
105 
106 void
108 {
109  int i;
110 
111  for (i = 1; i <= max_heap[0].id; i++)
112  {
113  (max_heap[i].priority)++;
114  }
115 }
int max_heap_delete(T_MAX_HEAP_NODE *max_heap, T_MAX_HEAP_NODE *ret)
int max_heap_change_priority(T_MAX_HEAP_NODE *max_heap, int id, int new_priority)
int i
Definition: dynamic_load.c:954
void max_heap_incr_priority(T_MAX_HEAP_NODE *max_heap)
int max_heap_insert(T_MAX_HEAP_NODE *max_heap, int max_heap_size, T_MAX_HEAP_NODE *item)