aboutsummaryrefslogtreecommitdiff
path: root/example.c
blob: 7d7ee945943573a3b5e69de16a989223493446e9 (plain) (blame)
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
#include "HashTable.h"
#include "Types.h"
#include "Vector.h"
#include "String.h"
#include "BumpAlloc.h"
#include "BigInt.h"
#include <stdio.h>

int
main() {
    MemoryManager g = M_std_tracked(),
                  t = M_bump(KiB(4));

    String *hexes = V_new(&g, sizeof(String), 8);

    Result status = R_Ok;
    for (usize i = 7; i <= 4830; i += 69) {
        PRINT_NOTOK(V_pushp((Vector**)&hexes, S_printf(&t, "%x", i)));
    }

    String all = S_printf(&g, "Total: %ld\n", V_count(hexes));
    for (usize i = 0; i + 4 < V_count(hexes); i += 4) {
        PRINT_NOTOK(
            S_append(&all,
                S_concat(&t,
                    hexes[0+i], " ", hexes[1+i], " ", hexes[2+i], " ", hexes[3+i], "\n",
                    NULL)));
    }

    printf("%s|%s|\n", all, S_substr(&t, all, 23, 4));

    MemoryManager T = M_std();

    HashTable tet = H_new(&T, 16);
    PRINT_NOTOK(H_insertstr(tet, "Hello", "World"));
    PRINT_NOTOK(H_insertf(tet, "a", 1.414));
    PRINT_NOTOK(H_inserti(tet, "+", 4829));

    printf("Table: %s [%p] %f %d\n",
           H_getstr(tet, "Hello"), H_getstr(tet, "Hello"),
           H_getf(tet, "a") == NULL ? 0.0 :  *H_getf(tet, "a"),
           H_geti(tet, "+") == NULL ? 0    : *H_geti(tet, "+"));
    PRINT_NOTOK(H_freevals(&tet));
    PRINT_NOTOK(H_free(&tet));

    BigInt a = B_newstr(&g, "0"),
           b = B_newstr(&g, "1");
    printf("a takes up %lu bits\n", B_bits(a));

    B_sum(&a, b);
    char *astr = B_tostr(&g, a);
    printf("a + b = %s\n", astr);

    B_sub(&a, b);
    B_sub(&a, b);
    astr = B_tostr(&g, a);
    printf("a - b = %s\t(%lu bits)\n", astr, B_bits(a));

    B_lshift(&a, 65);
    astr = B_tostr(&g, a);
    printf("a << 65 = %s  (%d)\n", astr, B_gt(a, b));

    B_rshift(&a, 65);
    astr = B_tostr(&g, a);
    printf("a >> 65 = %s\n", astr);

    BigInt c = B_newstr(&g, "12345678901234567890123");
    astr = B_tostr(&g, c);
    printf("c = %s\n", astr);

    PRINT_NOTOK(g.destroy(&g));
    PRINT_NOTOK(t.destroy(&t));
    PRINT_NOTOK(T.destroy(&T));
    return 0;
}