Skip to content

Commit 3b469c7

Browse files
committed
fix: use uint64_t for indices
1 parent b8d55e2 commit 3b469c7

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

program/c/src/oracle/model/price_model.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,34 @@
1111
* thinking of a as 1-based array. Fortunately, BPF compiler optimizes that for us.
1212
*/
1313
void heapsort(int64_t * a, uint64_t n) {
14+
if (n <= 1) return;
15+
1416
/*
1517
* This is a bottom-up heapify which is linear in time.
1618
*/
17-
for (int i = n / 2 - 1; i >= 0; i--) {
19+
for (uint64_t i = n / 2 - 1;; --i) {
1820
int64_t root = a[i];
19-
int j = i * 2 + 1;
21+
uint64_t j = i * 2 + 1;
2022
while (j < n) {
21-
if (j + 1 < n && a[j] < a[j + 1]) j++;
23+
if (j + 1 < n && a[j] < a[j + 1]) ++j;
2224
if (root >= a[j]) break;
2325
a[(j - 1) / 2] = a[j];
2426
j = j * 2 + 1;
2527
}
2628
a[(j - 1) / 2] = root;
29+
30+
if (i == 0) break;
2731
}
2832

29-
for (int i = n - 1; i > 0; i--) {
33+
for (uint64_t i = n - 1; i > 0; --i) {
3034
int64_t tmp = a[0];
3135
a[0] = a[i];
3236
a[i] = tmp;
3337

3438
int64_t root = a[0];
35-
int j = 1;
39+
uint64_t j = 1;
3640
while (j < i) {
37-
if (j + 1 < i && a[j] < a[j + 1]) j++;
41+
if (j + 1 < i && a[j] < a[j + 1]) ++j;
3842
if (root >= a[j]) break;
3943
a[(j - 1) / 2] = a[j];
4044
j = j * 2 + 1;

0 commit comments

Comments
 (0)