summaryrefslogtreecommitdiff
path: root/libs/dolphin/src/support/HTable.c
blob: 6cee989f97e1d73fc11c1ad981c577f8632b5f65 (plain)
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
#include <charPipeline/structures/HTable.h>
#include <charPipeline/structures/List.h>

void DSInitHTable(DSHashTable* hTable, u16 size, DSList* listArray, DSHashFunc* hashFunc, Ptr obj, DSLinkPtr link) {
    u16 i;

    hTable->table = listArray;
    hTable->tableSize = size;
    hTable->hash = hashFunc;
    for (i = 0; i < size; i++) {
        DSInitList(&listArray[i], obj, link);
    }
}

void DSInsertHTableObj(DSHashTable* hTable, Ptr obj) {
    DSList* list = &hTable->table[hTable->hash(obj)];
    DSInsertListObject(list, 0, obj);
}

void DSHTableToList(DSHashTable* hTable, DSList* list) {
    DSLink* link = NULL;
    u16 i = 0;

    list->Offset = hTable->table[i].Offset;
    for (i = 0; i < hTable->tableSize; i++) {
        DSAttachList(list, &hTable->table[i]);
    }
}

void* DSNextHTableObj(DSHashTable* hTable, Ptr obj) {
    s32 currentIndex;
    void* cursor;

    if (!hTable) {
        return NULL;
    }

    if (!obj) {
        currentIndex = 0;
        cursor = DSNextListObj(&hTable->table[currentIndex], NULL);
    } else {
        currentIndex = DSHTableIndex(hTable, obj);
        if (currentIndex == -1) {
            return NULL;
        }
        cursor = DSNextListObj(&hTable->table[currentIndex], obj);
    }

    while (cursor == NULL && currentIndex < hTable->tableSize - 1) {
        currentIndex++;
        cursor = DSNextListObj(&hTable->table[currentIndex], NULL);
    }
    return cursor;
}

s32 DSHTableIndex(DSHashTable* hTable, Ptr obj) {
    if (!hTable || !obj) {
        return -1;
    }
    return hTable->hash(obj);
}

void* DSHTableHead(DSHashTable* hTable, s32 index) {
    if (index < 0 || index >= hTable->tableSize) {
        return NULL;
    }
    return DSNextListObj(&hTable->table[index], NULL);
}