Skip to content

Commit

Permalink
demo
Browse files Browse the repository at this point in the history
  • Loading branch information
bangoc committed Jan 14, 2024
1 parent c6cb65f commit 01550da
Show file tree
Hide file tree
Showing 6 changed files with 1,116 additions and 22 deletions.
35 changes: 26 additions & 9 deletions algo/algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,38 @@ static void q2sort(void *a, int n, int sz, compare_fnt cmp) {
q2sort(right, (a - right)/sz + n, sz, cmp);
}

static int binsearch(const void *a, int n, int sz, const void *v, compare_fnt cmp) {
int l = 0, r = n - 1;
static void *binsearch(const void *k, void *a, int n, int sz, compare_fnt cmp) {
int l = 0, r = n - 1, m, o;
while (l <= r) {
int m = (l + r) / 2;
int x = cmp(a + m * sz, v);
if (x == 0) {
return m;
m = (l + r) / 2;
o = cmp(k, a + m * sz);
if (o == 0) {
return a + m * sz;
}
if (x < 0) {
if (o < 0) {
r = m - 1;
} else {
l = m + 1;
} else if (x > 0) {
}
}
return NULL;
}

static void *bnear_search(const void *k, void *a, int n, int sz, compare_fnt cmp) {
int l = 0, r = n - 1, m = 0, o;
while (l <= r) {
m = (l + r) / 2;
o = cmp(k, a + m * sz);
if (o == 0) {
break;
}
if (o < 0) {
r = m - 1;
} else {
l = m + 1;
}
}
return -1;
return a + m * sz;
}

#define HTOP(i) (((i) - 1) >> 1)
Expand Down
34 changes: 25 additions & 9 deletions all/cgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,37 @@ static void q2sort(void *a, int n, int sz, compare_fnt cmp) {
q2sort(a, (left - a)/sz + 1, sz, cmp);
q2sort(right, (a - right)/sz + n, sz, cmp);
}
static int binsearch(const void *a, int n, int sz, const void *v, compare_fnt cmp) {
int l = 0, r = n - 1;
static void *binsearch(const void *k, void *a, int n, int sz, compare_fnt cmp) {
int l = 0, r = n - 1, m, o;
while (l <= r) {
int m = (l + r) / 2;
int x = cmp(a + m * sz, v);
if (x == 0) {
return m;
m = (l + r) / 2;
o = cmp(k, a + m * sz);
if (o == 0) {
return a + m * sz;
}
if (x < 0) {
if (o < 0) {
r = m - 1;
} else {
l = m + 1;
} else if (x > 0) {
}
}
return NULL;
}
static void *bnear_search(const void *k, void *a, int n, int sz, compare_fnt cmp) {
int l = 0, r = n - 1, m = 0, o;
while (l <= r) {
m = (l + r) / 2;
o = cmp(k, a + m * sz);
if (o == 0) {
break;
}
if (o < 0) {
r = m - 1;
} else {
l = m + 1;
}
}
return -1;
return a + m * sz;
}
#define HTOP(i) (((i) - 1) >> 1)
#define HLEFT(i) (((i) << 1) + 1)
Expand Down
4 changes: 3 additions & 1 deletion tests/cgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ add_executable(max_heap_int_demo max_heap_int_demo.c)
add_executable(perm_demo perm_demo.c)
add_executable(comb_demo comb_demo.c)
add_executable(map_of_vector map_of_vector.c)
add_executable(tmap_kv_demo tmap_kv_demo.c)
add_executable(tmap_kv_demo tmap_kv_demo.c)
add_executable(bnear_search_demo bnear_search_demo.c)
add_executable(topk_demo topk_demo.c)
24 changes: 24 additions & 0 deletions tests/cgen/bnear_search_demo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "cgen.h"
#include <stdio.h>
int icmp(const void *p1, const void *p2) {
return *(const int*)p1 - *(const int *)p2;
}
int main(int argc, char *argv[]) {
int a[] = {1, 3, 5, 7, 9, 11, 20, 22, 24, 26, 28, 30};
int n = sizeof(a)/sizeof(a[0]);
int k;
if (argc == 1) {
printf("Usage: binsearch xyz\n");
return 0;
}
for (int i = 1; i < argc; ++i) {
sscanf(argv[i], "%d", &k);
int *pos = bnear_search(&k, a, n, sizeof(int), icmp);
if (*pos == k) {
printf("Found %d at position %ld\n", k, pos - a);
} else {
printf("Not found %d.", k);
printf(" Near %ld - %d\n", pos - a, *pos);
}
}
}
11 changes: 8 additions & 3 deletions tests/cgen/bsearch_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ int icmp(const void *p1, const void *p2) {
int main(int argc, char *argv[]) {
int a[] = {1, 3, 5, 7, 9, 11, 20, 22, 24, 26, 28, 30};
int n = sizeof(a)/sizeof(a[0]);
int v;
int k;
if (argc == 1) {
printf("Usage: binsearch xyz\n");
return 0;
}
for (int i = 1; i < argc; ++i) {
sscanf(argv[i], "%d", &v);
printf("%d\n", binsearch(a, n, sizeof(int), &v, icmp));
sscanf(argv[i], "%d", &k);
int *pos = binsearch(&k, a, n, sizeof(int), icmp);
if (pos) {
printf("Found %d at position %ld\n", k, pos - a);
} else {
printf("Not found %d\n", k);
}
}
}
Loading

0 comments on commit 01550da

Please sign in to comment.