From 8f129e78932c4273e766e8fc206d3bc66e27d8c1 Mon Sep 17 00:00:00 2001 From: doyle Date: Fri, 25 Aug 2023 20:35:04 +1000 Subject: [PATCH] dqn: Upgrade to latest lib --- Misc/dqn_unit_tests.cpp | 10 +++++----- dqn_math.cpp | 12 ++++++++++++ dqn_math.h | 2 ++ dqn_memory.h | 6 ++++-- dqn_platform.cpp | 2 +- dqn_strings.cpp | 7 +++++++ dqn_strings.h | 10 ++++++---- 7 files changed, 37 insertions(+), 12 deletions(-) diff --git a/Misc/dqn_unit_tests.cpp b/Misc/dqn_unit_tests.cpp index 75a5d06..b4a45bb 100644 --- a/Misc/dqn_unit_tests.cpp +++ b/Misc/dqn_unit_tests.cpp @@ -479,7 +479,7 @@ Dqn_UTest TestDSMap() uint64_t value = 0; uint64_t grow_threshold = map_start_size * 3 / 4; for (; map.occupied != grow_threshold; value++) { - uint64_t *val_copy = Dqn_Arena_NewCopy(scratch.arena, uint64_t, &value, 1); + uint64_t *val_copy = Dqn_Arena_NewCopy(scratch.arena, uint64_t, &value); Dqn_DSMapKey key = Dqn_DSMap_KeyBuffer(&map, (char *)val_copy, sizeof(*val_copy)); DQN_UTEST_ASSERT(&test, !Dqn_DSMap_Find(&map, key)); DQN_UTEST_ASSERT(&test, !Dqn_DSMap_FindSlot(&map, key)); @@ -498,7 +498,7 @@ Dqn_UTest TestDSMap() DQN_UTEST_ASSERT(&test, map.occupied == 1 /*Sentinel*/ + value); { // NOTE: One more item should cause the table to grow by 2x - uint64_t *val_copy = Dqn_Arena_NewCopy(scratch.arena, uint64_t, &value, 1); + uint64_t *val_copy = Dqn_Arena_NewCopy(scratch.arena, uint64_t, &value); Dqn_DSMapKey key = Dqn_DSMap_KeyBuffer(&map, (char *)val_copy, sizeof(*val_copy)); bool found = false; if (test_type == DSMapTestType_Set) { @@ -548,7 +548,7 @@ Dqn_UTest TestDSMap() uint64_t value = 0; uint64_t shrink_threshold = map.size * 1 / 4; for (; map.occupied != shrink_threshold; value++) { - uint64_t *val_copy = Dqn_Arena_NewCopy(scratch.arena, uint64_t, &value, 1); + uint64_t *val_copy = Dqn_Arena_NewCopy(scratch.arena, uint64_t, &value); Dqn_DSMapKey key = Dqn_DSMap_KeyBuffer(&map, (char *)val_copy, sizeof(*val_copy)); DQN_UTEST_ASSERT(&test, Dqn_DSMap_Find(&map, key)); @@ -561,7 +561,7 @@ Dqn_UTest TestDSMap() DQN_UTEST_ASSERT(&test, map.occupied == start_map_occupied - value); { // NOTE: One more item should cause the table to grow by 2x - uint64_t *val_copy = Dqn_Arena_NewCopy(scratch.arena, uint64_t, &value, 1); + uint64_t *val_copy = Dqn_Arena_NewCopy(scratch.arena, uint64_t, &value); Dqn_DSMapKey key = Dqn_DSMap_KeyBuffer(&map, (char *)val_copy, sizeof(*val_copy)); Dqn_DSMap_Erase(&map, key); value++; @@ -600,7 +600,7 @@ Dqn_UTest TestDSMap() } for (; map.occupied != 1; value++) { // NOTE: Remove all items from the table - uint64_t *val_copy = Dqn_Arena_NewCopy(scratch.arena, uint64_t, &value, 1); + uint64_t *val_copy = Dqn_Arena_NewCopy(scratch.arena, uint64_t, &value); Dqn_DSMapKey key = Dqn_DSMap_KeyBuffer(&map, (char *)val_copy, sizeof(*val_copy)); DQN_UTEST_ASSERT(&test, Dqn_DSMap_Find(&map, key)); Dqn_DSMap_Erase(&map, key); diff --git a/dqn_math.cpp b/dqn_math.cpp index 62d9d0d..4d5d2eb 100644 --- a/dqn_math.cpp +++ b/dqn_math.cpp @@ -309,12 +309,24 @@ DQN_API Dqn_V2 operator-(Dqn_V2 lhs, Dqn_V2 rhs) return result; } +DQN_API Dqn_V2 operator-(Dqn_V2 lhs, Dqn_f32 rhs) +{ + Dqn_V2 result = Dqn_V2_InitNx2(lhs.x - rhs, lhs.y - rhs); + return result; +} + DQN_API Dqn_V2 operator+(Dqn_V2 lhs, Dqn_V2 rhs) { Dqn_V2 result = Dqn_V2_InitNx2(lhs.x + rhs.x, lhs.y + rhs.y); return result; } +DQN_API Dqn_V2 operator+(Dqn_V2 lhs, Dqn_f32 rhs) +{ + Dqn_V2 result = Dqn_V2_InitNx2(lhs.x + rhs, lhs.y + rhs); + return result; +} + DQN_API Dqn_V2 operator*(Dqn_V2 lhs, Dqn_V2 rhs) { Dqn_V2 result = Dqn_V2_InitNx2(lhs.x * rhs.x, lhs.y * rhs.y); diff --git a/dqn_math.h b/dqn_math.h index 95cab24..8a693e7 100644 --- a/dqn_math.h +++ b/dqn_math.h @@ -88,7 +88,9 @@ DQN_API bool operator<=(Dqn_V2 lhs, Dqn_V2 rhs); DQN_API bool operator< (Dqn_V2 lhs, Dqn_V2 rhs); DQN_API bool operator> (Dqn_V2 lhs, Dqn_V2 rhs); DQN_API Dqn_V2 operator- (Dqn_V2 lhs, Dqn_V2 rhs); +DQN_API Dqn_V2 operator- (Dqn_V2 lhs, Dqn_f32 rhs); DQN_API Dqn_V2 operator+ (Dqn_V2 lhs, Dqn_V2 rhs); +DQN_API Dqn_V2 operator+ (Dqn_V2 lhs, Dqn_f32 rhs); DQN_API Dqn_V2 operator* (Dqn_V2 lhs, Dqn_V2 rhs); DQN_API Dqn_V2 operator* (Dqn_V2 lhs, Dqn_f32 rhs); DQN_API Dqn_V2 operator* (Dqn_V2 lhs, int32_t rhs); diff --git a/dqn_memory.h b/dqn_memory.h index 57e7e31..25605be 100644 --- a/dqn_memory.h +++ b/dqn_memory.h @@ -242,9 +242,11 @@ enum Dqn_ArenaCommit // NOTE: Allocation ================================================================================ #define Dqn_Arena_New(arena, Type, zero_mem) (Type *)Dqn_Arena_Alloc(arena, sizeof(Type), alignof(Type), zero_mem) +#define Dqn_Arena_NewCopy(arena, Type, src) (Type *)Dqn_Arena_Copy(arena, src, sizeof(*src), alignof(Type)) +#define Dqn_Arena_NewCopyZ(arena, Type, src) (Type *)Dqn_Arena_Copy(arena, src, sizeof(*src), alignof(Type)) #define Dqn_Arena_NewArray(arena, Type, count, zero_mem) (Type *)Dqn_Arena_Alloc(arena, sizeof(Type) * count, alignof(Type), zero_mem) -#define Dqn_Arena_NewCopy(arena, Type, src, count) (Type *)Dqn_Arena_Copy(arena, src, sizeof(*src) * count, alignof(Type)) -#define Dqn_Arena_NewCopyZ(arena, Type, src, count) (Type *)Dqn_Arena_CopyZ(arena, src, sizeof(*src) * count, alignof(Type)) +#define Dqn_Arena_NewArrayCopy(arena, Type, src, count) (Type *)Dqn_Arena_Copy(arena, src, sizeof(*src) * count, alignof(Type)) +#define Dqn_Arena_NewArrayCopyZ(arena, Type, src, count) (Type *)Dqn_Arena_CopyZ(arena, src, sizeof(*src) * count, alignof(Type)) DQN_API Dqn_Allocator Dqn_Arena_Allocator (Dqn_Arena *arena); DQN_API Dqn_MemBlock * Dqn_Arena_Grow (Dqn_Arena *arena, Dqn_usize size, Dqn_usize commit, uint8_t flags); diff --git a/dqn_platform.cpp b/dqn_platform.cpp index f892005..a414bd4 100644 --- a/dqn_platform.cpp +++ b/dqn_platform.cpp @@ -1275,7 +1275,7 @@ DQN_API Dqn_String16 Dqn_Win_EXEDirWArena(Dqn_Arena *arena) Dqn_String16 result = {}; if (dir_size > 0) { - result.data = Dqn_Arena_NewCopyZ(arena, wchar_t, dir, dir_size); + result.data = Dqn_Arena_NewArrayCopyZ(arena, wchar_t, dir, dir_size); if (result.data) { result.size = dir_size; } diff --git a/dqn_strings.cpp b/dqn_strings.cpp index 7939d59..8487bb8 100644 --- a/dqn_strings.cpp +++ b/dqn_strings.cpp @@ -90,6 +90,12 @@ DQN_API Dqn_String8 Dqn_String8_Slice(Dqn_String8 string, Dqn_usize offset, Dqn_ return result; } +DQN_API Dqn_String8 Dqn_String8_Advance(Dqn_String8 string, Dqn_usize amount) +{ + Dqn_String8 result = Dqn_String8_Slice(string, amount, UINT64_MAX); + return result; +} + DQN_API Dqn_String8BinarySplitResult Dqn_String8_BinarySplitArray(Dqn_String8 string, Dqn_String8 const *find, Dqn_usize find_size) { Dqn_String8BinarySplitResult result = {}; @@ -176,6 +182,7 @@ DQN_API Dqn_String8FindResult Dqn_String8_FindFirstStringArray(Dqn_String8 strin if (Dqn_String8_Eq(string_slice, find_item)) { result.found = true; result.index = index; + result.start_to_before_match = Dqn_String8_Init(string.data, index); result.match = Dqn_String8_Init(string.data + index, find_item.size); result.match_to_end_of_buffer = Dqn_String8_Init(result.match.data, string.size - index); break; diff --git a/dqn_strings.h b/dqn_strings.h index 3b0993c..fbb54fc 100644 --- a/dqn_strings.h +++ b/dqn_strings.h @@ -248,10 +248,11 @@ struct Dqn_String8BinarySplitResult struct Dqn_String8FindResult { - bool found; - Dqn_usize index; - Dqn_String8 match; - Dqn_String8 match_to_end_of_buffer; + bool found; // True if string was found. If false, the subsequent fields below are not set. + Dqn_usize index; // The index in the buffer where the found string starts + Dqn_String8 match; // The matching string in the buffer that was searched + Dqn_String8 match_to_end_of_buffer; // The substring containing the found string to the end of the buffer + Dqn_String8 start_to_before_match; // The substring from the start of the buffer up until the found string, not including it }; // NOTE: Macros ==================================================================================== @@ -317,6 +318,7 @@ DQN_API Dqn_String8 Dqn_String8_CopyCString (Dqn_Allo DQN_API Dqn_String8 Dqn_String8_Copy (Dqn_Allocator allocator, Dqn_String8 string); DQN_API Dqn_String8 Dqn_String8_Slice (Dqn_String8 string, Dqn_usize offset, Dqn_usize size); +DQN_API Dqn_String8 Dqn_String8_Advance (Dqn_String8 string, Dqn_usize amount); DQN_API Dqn_String8BinarySplitResult Dqn_String8_BinarySplitArray (Dqn_String8 string, Dqn_String8 const *find, Dqn_usize find_size); DQN_API Dqn_String8BinarySplitResult Dqn_String8_BinarySplit (Dqn_String8 string, Dqn_String8 find); DQN_API Dqn_usize Dqn_String8_Split (Dqn_String8 string, Dqn_String8 delimiter, Dqn_String8 *splits, Dqn_usize splits_count);