diff options
author | Max <mparisi@stevens.edu> | 2020-09-16 21:22:50 -0400 |
---|---|---|
committer | Max <mparisi@stevens.edu> | 2020-09-16 21:22:50 -0400 |
commit | ea73b61cea2b6e50e0079042eae1185a726e2273 (patch) | |
tree | b2c11e196725da12bc23e976edd8c857ae1b97c1 | |
parent | 88cdd94339cc0307dac9f2a9e30465d6f4c203d2 (diff) |
rough decomp
-rw-r--r-- | include/darray.h | 7 | ||||
-rw-r--r-- | include/hashtable.h | 12 | ||||
-rw-r--r-- | include/nonport.h | 2 | ||||
-rw-r--r-- | include/qsort.h | 4 | ||||
-rw-r--r-- | obj_files.mk | 2 | ||||
-rw-r--r-- | src/darray.c | 76 | ||||
-rw-r--r-- | src/hashtable.c | 22 |
7 files changed, 94 insertions, 31 deletions
diff --git a/include/darray.h b/include/darray.h index ec6010f..db8b264 100644 --- a/include/darray.h +++ b/include/darray.h @@ -7,7 +7,7 @@ extern "C" { #include "types.h"
-typedef int (*CompareFunction)(const void *, const void *);
+typedef s32 (*CompareFunction)(const void *, const void *);
typedef BOOL (*MapFunction)(const void *, s32);
typedef void (*DtorFunction)(void *);
@@ -20,9 +20,10 @@ typedef struct DArray { char *buf;
} DArray;
-void *ArrayNew(u32 p1, s32 p2, s32 p3);
+DArray *ArrayNew(u32 p1, s32 p2, DtorFunction dtor);
void *ArrayNth(DArray *p1, s32 p2);
-void *ArrayMapBackwards2(DArray *p1, s32 p2, s32 p3);
+void ArrayMapBackwards(DArray *p1, MapFunction p2, s32 p3);
+void *ArrayMapBackwards2(DArray *p1, MapFunction p2, s32 p3);
#ifdef __cplusplus
}
diff --git a/include/hashtable.h b/include/hashtable.h index 79acbf6..784ab35 100644 --- a/include/hashtable.h +++ b/include/hashtable.h @@ -12,20 +12,20 @@ typedef s32 (*HashFunction)(void *, s32); typedef struct HashTable {
DArray **chains;
s32 size;
- s32 unk8;
+ DtorFunction dtor;
HashFunction hashFunc;
- s32 unk10; // comparison callback?
+ CompareFunction compar;
} HashTable;
-HashTable *TableNew(u32 p1, s32 p2, HashFunction hf, s32 p4, s32 p5);
-HashTable *TableNew2(u32 p1, s32 size, s32 p3, HashFunction hf, s32 p5, s32 p6);
+HashTable *TableNew(u32 p1, s32 p2, HashFunction hf, CompareFunction cmp, DtorFunction dtor);
+HashTable *TableNew2(u32 p1, s32 size, s32 p3, HashFunction hf, CompareFunction cmp, DtorFunction dtor);
void TableFree(HashTable *table);
s32 TableCount(HashTable *table);
void TableEnter(HashTable *table, void *elem);
BOOL TableRemove(HashTable *table, void *elem);
void *TableLookup(HashTable *table, void *elem);
-void TableMapSafe(HashTable *table, s32 p2, s32 p3);
-void *TableMapSafe2(HashTable *table, s32 p2, s32 p3);
+void TableMapSafe(HashTable *table, MapFunction p2, s32 p3);
+void *TableMapSafe2(HashTable *table, MapFunction p2, s32 p3);
#ifdef __cplusplus
}
diff --git a/include/nonport.h b/include/nonport.h index 2cc5146..c1ed0e7 100644 --- a/include/nonport.h +++ b/include/nonport.h @@ -6,7 +6,7 @@ extern "C" { #endif
void *gsimalloc(u32 sz);
-
+void *gsirealloc(void *ptr, u32 sz);
void gsifree(void *ptr);
#ifdef __cplusplus
diff --git a/include/qsort.h b/include/qsort.h index c68b747..c13ae0e 100644 --- a/include/qsort.h +++ b/include/qsort.h @@ -5,8 +5,8 @@ extern "C" {
#endif
-void qsort(void *base, size_t nmemb, size_t size,
- int (*compar)(const void *, const void *));
+void qsort(void *base, u32 nmemb, u32 size,
+ s32 (*compar)(const void *, const void *));
#ifdef __cplusplus
}
diff --git a/obj_files.mk b/obj_files.mk index c8ad886..8974758 100644 --- a/obj_files.mk +++ b/obj_files.mk @@ -26,7 +26,7 @@ TEXT_O_FILES := \ $(BUILD_DIR)/asm/text_8.o \ $(BUILD_DIR)/asm/text_9.o \ $(BUILD_DIR)/asm/text_10.o \ - $(BUILD_DIR)/asm/darray.o \ + $(BUILD_DIR)/src/darray.o \ $(BUILD_DIR)/src/hashtable.o \ $(BUILD_DIR)/asm/md5c.o \ $(BUILD_DIR)/asm/text_10_2.o \ diff --git a/src/darray.c b/src/darray.c index 42ccddb..290d365 100644 --- a/src/darray.c +++ b/src/darray.c @@ -17,6 +17,7 @@ ArraySearch 8035aad8 80336430 198 #include "types.h"
#include "qsort.h"
+#include "nonport.h"
#include "darray.h"
// TODO: separate function, or embedded in ArrayInsert
@@ -29,7 +30,7 @@ static inline void ArrayCheckGrow(DArray *p1) }
}
-static inline void ArrayInsert(DArray *p1, s32 n)
+static inline void ArrayInsert(DArray *p1, void *elem, s32 n)
{
ArrayCheckGrow(p1);
if (n < p1->size++) {
@@ -39,7 +40,15 @@ static inline void ArrayInsert(DArray *p1, s32 n) memcpy(ArrayNth(p1, n), elem, p1->elemSz);
}
-DArray *ArrayNew(u32 p1, s32 p2, s32 p3)
+#if 0
+static inline void ArrayBinarySearch(....)
+{
+
+}
+#endif
+
+
+DArray *ArrayNew(u32 p1, s32 p2, DtorFunction dtor)
{
DArray *darray = gsimalloc(sizeof(DArray)); // r31
if (p2 == 0)
@@ -48,7 +57,7 @@ DArray *ArrayNew(u32 p1, s32 p2, s32 p3) darray->capacity = p2;
darray->elemSz = p1;
darray->growAmount = p2;
- darray->elemDtor = p3;
+ darray->elemDtor = dtor;
if (p2 != 0) {
darray->buf = gsimalloc(p2 * darray->elemSz);
} else {
@@ -83,13 +92,15 @@ void *ArrayNth(DArray *arr, s32 n) void ArrayAppend(DArray *p1, void *elem)
{
if (p1) {
- ArrayInsert(p1, p1->size);
+ ArrayInsert(p1, elem, p1->size);
}
}
// params r29 p1, r30 p2, compar r24
ArrayInsertSorted(DArray *p1, void *p2, CompareFunction compar)
{
+ // TODO: inlined binary search?
+
s32 high = p1->size - 1; // r26
u32 elemSz = p1->elemSz; // r28
char *buf = p1->buf; // r31
@@ -121,8 +132,10 @@ ArrayInsertSorted(DArray *p1, void *p2, CompareFunction compar) s32 cap = p1->capacity // r4
#endif
+ // TODO: maybe this is because inline binarySearch func
+ // returns a char * into the buf
s32 recomputeLow = (buf + (low * elemSz) - p1->buf) / p1->elemSz;
- ArrayInsert(p1, recomputeLow);
+ ArrayInsert(p1, p2, recomputeLow);
}
// Faster deleter for DArrays of types that do not
@@ -158,12 +171,61 @@ void ArrayReplaceAt(DArray *p1, void *elem, s32 n) void ArraySort(DArray *p1, CompareFunction compar)
{
- qsort(p1->buf, p1->size, elemSz, compar);
+ qsort(p1->buf, p1->size, p1->elemSz, compar);
}
-ArraySearch()
+s32 ArraySearch(DArray *p1, void *elem, CompareFunction cmp, s32 p4, s32 p5)
{
+ #if 0
+ p1 = r28, elem = r29, cmp = r30, p4 = r6
+ #endif
+ char *ptr; // r4
+
+ BOOL found = TRUE; // r31
+ s32 arrSize = p1->size; // r5
+ if (!p1 || arrSize == 0) {
+ return -1;
+ }
+ if (p5) {
+ char *r27 = ArrayNth(p1, p4); // r27
+ s32 high = arrSize - p4 - 1; // r24
+ u32 elemSz = p1->elemSz; // r26
+ s32 low = 0; // r23
+ found = FALSE;
+ while (low <= high) {
+ s32 mid = (low + high) / 2; // r25
+ s32 result = cmp(&r27[mid * elemSz], elem);
+ if (result == 0) {
+ found = TRUE;
+ }
+ if (result < 0) {
+ low = mid + 1;
+ }
+ if (result >= 0) {
+ high = mid - 1;
+ }
+ }
+ ptr = &r27[low * elemSz];
+ } else {
+ // 510
+ char *r23 = ArrayNth(p1, p4); // r23
+ u32 elemSz = p1->elemSz; // r25
+ s32 r24 = arrSize - p4;
+ for (s32 i = 0, j = 0; i < r24; j += elemSz, i++) { // r26 and r27
+ if (cmp(elem, &r23[j]) == 0) {
+ ptr = &r23[i * elemSz];
+ goto exit;
+ }
+ }
+ ptr = NULL;
+ }
+ // 588
+ exit:
+ if (ptr && found) {
+ return (ptr - p1->buf) / (s32)p1->elemSz;
+ }
+ return -1;
}
// params r28, r29, r30
diff --git a/src/hashtable.c b/src/hashtable.c index b717ffe..d170aa7 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -2,21 +2,21 @@ #include "nonport.h"
#include "hashtable.h"
-HashTable *TableNew(u32 p1, s32 p2, HashFunction hf, s32 p4, s32 p5)
+HashTable *TableNew(u32 p1, s32 p2, HashFunction hf, CompareFunction cmp, DtorFunction dtor)
{
- return TableNew2(p1, p2, 4, hf, p4, p5);
+ return TableNew2(p1, p2, 4, hf, cmp, dtor);
}
-HashTable *TableNew2(u32 p1, s32 size, s32 p3, HashFunction hf, s32 p5, s32 p6)
+HashTable *TableNew2(u32 p1, s32 size, s32 p3, HashFunction hf, CompareFunction cmp, DtorFunction dtor)
{
HashTable *table = gsimalloc(sizeof(HashTable));
table->chains = gsimalloc(size * sizeof(DArray *));
for (s32 i = 0; i < size; i++) {
- table->chains[i] = (DArray *)ArrayNew(p1, p3, p6);
+ table->chains[i] = ArrayNew(p1, p3, dtor);
}
table->size = size;
- table->unk8 = p6;
- table->unk10 = p5;
+ table->dtor = dtor;
+ table->compar = cmp;
table->hashFunc = hf;
return table;
}
@@ -47,7 +47,7 @@ void TableEnter(HashTable *table, void *elem) {
if (table) {
s32 i = table->hashFunc(elem, table->size);
- s32 result = ArraySearch(table->chains[i], elem, table->unk10, 0, 0);
+ s32 result = ArraySearch(table->chains[i], elem, table->compar, 0, 0);
if (result == -1) {
ArrayAppend(table->chains[i], elem);
} else {
@@ -61,7 +61,7 @@ BOOL TableRemove(HashTable *table, void *elem) if (!table)
return FALSE;
s32 i = table->hashFunc(elem, table->size);
- s32 result = ArraySearch(table->chains[i], elem, table->unk10, 0, 0);
+ s32 result = ArraySearch(table->chains[i], elem, table->compar, 0, 0);
if (result == -1) {
return FALSE;
} else {
@@ -75,7 +75,7 @@ void *TableLookup(HashTable *table, void *elem) if (!table)
return NULL;
s32 i = table->hashFunc(elem, table->size);
- s32 result = ArraySearch(table->chains[i], elem, table->unk10, 0, 0);
+ s32 result = ArraySearch(table->chains[i], elem, table->compar, 0, 0);
if (result == -1) {
return NULL;
} else {
@@ -83,14 +83,14 @@ void *TableLookup(HashTable *table, void *elem) }
}
-void TableMapSafe(HashTable *table, s32 p2, s32 p3)
+void TableMapSafe(HashTable *table, MapFunction p2, s32 p3)
{
for (s32 i = 0; i < table->size; i++) {
ArrayMapBackwards(table->chains[i], p2, p3);
}
}
-void *TableMapSafe2(HashTable *table, s32 p2, s32 p3)
+void *TableMapSafe2(HashTable *table, MapFunction p2, s32 p3)
{
s32 i;
void *result;
|