From 3985455fc28e326f36acb0aa3e771ce6280ce760 Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Sun, 16 Apr 2017 21:33:26 +1000 Subject: [PATCH] Add dynamic array pop, i32 to str returns len --- dqn.h | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/dqn.h b/dqn.h index 0be88bc..676b671 100644 --- a/dqn.h +++ b/dqn.h @@ -26,6 +26,7 @@ #define DQN_WIN32_ERROR_BOX(text, title) MessageBoxA(NULL, text, title, MB_OK); #define DQN_WIN32 + // TODO(doyle): Our own windows.h? #define WIN32_LEAN_AND_MEAN #include #endif @@ -68,22 +69,18 @@ struct DqnArray T *data; }; +// REMINDER: This can be used as a stack. So don't need to create a stack data +// structure. #if 0 template bool dqn_darray_init (DqnArray *array, size_t capacity); -template bool dqn_darray_grow (DqnArray *array); -template -bool dqn_darray_push (DqnArray *array, T item); -template +T *dqn_darray_push (DqnArray *array, T item); +T *dqn_darray_pop (DqnArray *array) T *dqn_darray_get (DqnArray *array, u64 index); -template bool dqn_darray_clear (DqnArray *array); -template bool dqn_darray_free (DqnArray *array); -template bool dqn_darray_remove (DqnArray *array, u64 index); -template bool dqn_darray_remove_stable(DqnArray *array, u64 index); #endif @@ -94,6 +91,12 @@ bool dqn_darray_init(DqnArray *array, size_t capacity) { if (!array) return false; + if (array->data) + { + // TODO(doyle): Logging? The array already exists + if (!dqn_darray_free(array)) return false; + } + array->data = (T *)calloc((size_t)capacity, sizeof(T)); if (!array->data) return false; @@ -125,19 +128,29 @@ bool dqn_darray_grow(DqnArray *array) } template -bool dqn_darray_push(DqnArray *array, T item) +T *dqn_darray_push(DqnArray *array, T item) { - if (!array) return false; + if (!array) return NULL; if (array->count >= array->capacity) { - if (!dqn_darray_grow(array)) return false; + if (!dqn_darray_grow(array)) return NULL; } DQN_ASSERT(array->count < array->capacity); array->data[array->count++] = item; - return true; + return &array->data[array->count-1]; +} + +template +T *dqn_darray_pop(DqnArray *array) +{ + if (!array) return NULL; + if (array->count == 0) return NULL; + + T *result = &array->data[--array->count]; + return result; } template @@ -224,7 +237,7 @@ bool dqn_darray_remove_stable(DqnArray *array, u64 index) //////////////////////////////////////////////////////////////////////////////// // Math //////////////////////////////////////////////////////////////////////////////// -DQN_FILE_SCOPE f32 dqn_math_lerp(f32 a, f32 t, f32 b); +DQN_FILE_SCOPE f32 dqn_math_lerp (f32 a, f32 t, f32 b); DQN_FILE_SCOPE f32 dqn_math_sqrtf(f32 a); //////////////////////////////////////////////////////////////////////////////// @@ -351,7 +364,8 @@ DQN_FILE_SCOPE char *dqn_strncpy(char *dest, const char *src, i32 numChars); #define DQN_I32_TO_STR_MAX_BUF_SIZE 11 DQN_FILE_SCOPE bool dqn_str_reverse(char *buf, const i32 bufSize); DQN_FILE_SCOPE i32 dqn_str_to_i32 (const char *const buf, const i32 bufSize); -DQN_FILE_SCOPE void dqn_i32_to_str (i32 value, char *buf, i32 bufSize); +// Return the len of the derived string +DQN_FILE_SCOPE i32 dqn_i32_to_str (i32 value, char *buf, i32 bufSize); // Both return the number of bytes read, return 0 if invalid codepoint or UTF8 DQN_FILE_SCOPE u32 dqn_ucs_to_utf8(u32 *dest, u32 character); @@ -1592,14 +1606,14 @@ DQN_FILE_SCOPE i32 dqn_str_to_i32(const char *const buf, const i32 bufSize) return result; } -DQN_FILE_SCOPE void dqn_i32_to_str(i32 value, char *buf, i32 bufSize) +DQN_FILE_SCOPE i32 dqn_i32_to_str(i32 value, char *buf, i32 bufSize) { - if (!buf || bufSize == 0) return; + if (!buf || bufSize == 0) return 0; if (value == 0) { buf[0] = '0'; - return; + return 0; } // NOTE(doyle): Max 32bit integer (+-)2147483647 @@ -1627,6 +1641,8 @@ DQN_FILE_SCOPE void dqn_i32_to_str(i32 value, char *buf, i32 bufSize) { dqn_str_reverse(buf, charIndex); } + + return charIndex; } /*