-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminHeap.cpp
163 lines (147 loc) · 4.75 KB
/
minHeap.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// minHeap.cpp
// topk
//
// Created by Wang Yi on 9/8/17.
// Copyright © 2017 Wang Lei. All rights reserved.
//
// if you are interested in generic C, I recommend to read source python.
// c `obj` implementation will be defined in Object directory; their corresponding declearation reside in `include`
#include "minHeap.hpp"
#include "logging.hpp"
void
swap(objref* left, objref* right)
{
objref temp = *left;
*left = *right;
*right = temp;
}
heap*
heap_new(size_t size) {
LOG(INFO) << "Allocating memory for heap ...";
heap* head = (heap*)Malloc(min_heap, 1);
CHECK(head != nullptr) << "Alloc proc for <heap> falied. exit.";
if (head == nullptr) return nullptr;
LOG(INFO) << "alloc at address: " << head;
TREE(head) = Malloc(objref, size);
CHECK(TREE(head) != nullptr) << "Alloc proc for <heap:list> failed. exit.";
if (TREE(head) == nullptr) return nullptr;
head->capacity = size;
head->last = 0;
return head;
}
void
heap_del(heap* h){
free(TREE(h));
free(h);
}
void
heap_heapify(heap* self, size_t p, cmpfunc cmp){
size_t smaller, larger;
size_t left = LEFT(p), right = RIGHT(p);
if (left >= self->capacity || (right < self->capacity && cmp(TREE_AT(self, left),TREE_AT(self, right)) > 0)) {
// when left is empyt or right is smaller than left
smaller = right;
larger = left;
} else {
smaller = left;
right = larger;
}
if (smaller < self->capacity && cmp(TREE_AT(self, 0), TREE_AT(self, smaller)) > 0) {
// the smaller is less than the curr root
swap(&TREE_AT(self, 0), &TREE_AT(self, smaller));
heap_heapify(self, smaller, cmp);
}
}
void*
heap_insert(heap* self, const void* data, cmpfunc cmp) {
size_t curr, root=0;
heap* head = self;
list l = TREE(head);
objref ob = Malloc(Object, 1);
CHECK(ob != nullptr) << "Alloc proc for <objref> falied. exit.";
SHALLOW_CPY(data, ob->data, (ob->offset))
if (head->last < head->capacity)
{
TREE_AT(head, head->last) = ob;
// percolated up
curr = head->last;
head->last++;
// for min_heap, cmp(curr, parent) == -1, percolate up
// for max_heap, cmp(curr, parent) == 1, percolate up
while(curr != 0 && cmp(ob, l[PARENT(curr)]) < 0) {
l[curr] = l[PARENT(curr)];
curr = PARENT(curr);
}
l[curr] = ob;
} else {
// espicially useful in topk operation
if (cmp(ob, l[root]) < 0) {
return nullptr; // reject
} else {
// the root must be the k+1 th largest el
TREE_AT(head, root) = ob;
// heapify op
heap_heapify(self, 0, cmp);
}
}
return (void*)ob;
}
// heap subclass init
min_heap*
min_heap_new(size_t size, cmpfunc cmp, const void* data[]) {
min_heap* head = (min_heap*) heap_new(size);
CHECK(head != nullptr) << "Alloc proc for <minheap> falied. exit.";
if (head == nullptr) return nullptr;
// this will define
// binding method for objref dynamically
head->cmp = cmp;
head->insert = heap_insert;
head->heapify = heap_heapify;
if (data != nullptr) {
int i = 0;
while (i < size) {
head->insert((heap*)head, data[i], cmp);
i++;
}
log_heap((heap*)head, size);
}
return head;
}
// mini heap of size k, topk method for largest k elments
// for topk problem, the root elment will be kth smallest element; hence we have the following strategy:
// 1) if rereshly coming element is bigger than the root, discard it directly else got 2)
// 2) we must find a new postion for the elment; we wrap it in objref; and discard the current (k+1)th smallest elemnt
void
max_topk(void const** data, void** ret, size_t l, size_t k, cmpfunc cmp){
size_t i;
min_heap* head = nullptr;
if (k < l) {
head = (min_heap*)min_heap_new(k, cmp, data);
CHECK(head != nullptr) << "Alloc proc for <topk> falied. exit.";
if (head == nullptr) exit(1);
*ret = (void*)head;
} else {
head = (min_heap*)min_heap_new(l, cmp, data);
CHECK(head != nullptr) << "Alloc proc for <topk> falied. exit.";
if (head == nullptr) exit(1);
*ret = (void*)head;
return;
}
// once allocated, we loop through the rest of elements using provided cmp
for (i=k; i<l; i++) {
LOG(INFO) << "loop through, the current el is " << *(int*)data[i];
heap_insert((heap*)head, data[i], cmp);
log_heap((heap*)head, k);
}
}
void
log_heap(heap* h, size_t size){
list l = TREE(h);
size_t i;
LOG(INFO) << "the heap is: ";
for(i=0; i<size; i++) {
std::cout << *(int*)l[i]->data << " ";
}
std::cout << std::endl;
}