Add some helper functions
This commit is contained in:
parent
ea8e29dc9f
commit
f743c230e7
34
Code/Dqn.h
34
Code/Dqn.h
@ -985,6 +985,16 @@ struct Dqn_Slice
|
|||||||
T *operator+ (Dqn_isize i) { DQN_ASSERT_MSG(i >= 0 && i < len, "%d >= 0 && %d < %d", i, len); return data + i; }
|
T *operator+ (Dqn_isize i) { DQN_ASSERT_MSG(i >= 0 && i < len, "%d >= 0 && %d < %d", i, len); return data + i; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T, isize N>
|
||||||
|
DQN_HEADER_COPY_PROTOTYPE(inline Dqn_Slice<T>, Dqn_Slice_InitWithArray(T (&array)[N]))
|
||||||
|
{
|
||||||
|
Dqn_Slice<T> result = {};
|
||||||
|
result.len = N;
|
||||||
|
result.data = array;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
DQN_HEADER_COPY_PROTOTYPE(template <typename T> inline Dqn_Slice<T>, Dqn_Slice_Allocate(Dqn_Allocator *allocator, Dqn_isize len))
|
DQN_HEADER_COPY_PROTOTYPE(template <typename T> inline Dqn_Slice<T>, Dqn_Slice_Allocate(Dqn_Allocator *allocator, Dqn_isize len))
|
||||||
{
|
{
|
||||||
Dqn_Slice<T> result = {};
|
Dqn_Slice<T> result = {};
|
||||||
@ -1203,7 +1213,7 @@ template <typename T> struct Dqn_Array
|
|||||||
};
|
};
|
||||||
DQN_HEADER_COPY_END
|
DQN_HEADER_COPY_END
|
||||||
|
|
||||||
DQN_HEADER_COPY_PROTOTYPE(template <typename T> Dqn_Array<T>, Dqn_Array_InitMemory(T *memory, Dqn_isize max, Dqn_isize len = 0))
|
DQN_HEADER_COPY_PROTOTYPE(template <typename T> Dqn_Array<T>, Dqn_Array_InitWithMemory(T *memory, Dqn_isize max, Dqn_isize len = 0))
|
||||||
{
|
{
|
||||||
Dqn_Array<T> result = {};
|
Dqn_Array<T> result = {};
|
||||||
result.allocator = Dqn_Allocator_Null();
|
result.allocator = Dqn_Allocator_Null();
|
||||||
@ -1213,6 +1223,14 @@ DQN_HEADER_COPY_PROTOTYPE(template <typename T> Dqn_Array<T>, Dqn_Array_InitMemo
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DQN_HEADER_COPY_PROTOTYPE(template <typename T> Dqn_Array<T>, Dqn_Array_InitWithAllocatorNoGrow(Dqn_Allocator *allocator, Dqn_isize max, Dqn_isize len = 0))
|
||||||
|
{
|
||||||
|
T *memory = DQN_CAST(T *)Dqn_Allocator_Allocate(allocator, sizeof(T) * max, alignof(T));
|
||||||
|
Dqn_Array<T> result = Dqn_Array_InitWithMemory(memory, max, len);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
DQN_HEADER_COPY_PROTOTYPE(template <typename T> bool, Dqn_Array_Reserve(Dqn_Array<T> *a, Dqn_isize size))
|
DQN_HEADER_COPY_PROTOTYPE(template <typename T> bool, Dqn_Array_Reserve(Dqn_Array<T> *a, Dqn_isize size))
|
||||||
{
|
{
|
||||||
if (size <= a->len) return true;
|
if (size <= a->len) return true;
|
||||||
@ -2560,6 +2578,20 @@ DQN_HEADER_COPY_PROTOTYPE(Dqn_String, Dqn_String_Copy(Dqn_Allocator *allocator,
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DQN_HEADER_COPY_PROTOTYPE(Dqn_String, Dqn_String_TrimWhitespaceAround(Dqn_String src))
|
||||||
|
{
|
||||||
|
Dqn_String result = src;
|
||||||
|
if (src.len <= 0) return result;
|
||||||
|
|
||||||
|
char *start = src.str;
|
||||||
|
char *end = start + (src.len - 1);
|
||||||
|
while (Dqn_Char_IsWhitespace(start[0])) start++;
|
||||||
|
while (end != start && Dqn_Char_IsWhitespace(end[0])) end--;
|
||||||
|
|
||||||
|
result.str = start;
|
||||||
|
result.len = (end - start) + 1;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
// @ -------------------------------------------------------------------------------------------------
|
// @ -------------------------------------------------------------------------------------------------
|
||||||
// @
|
// @
|
||||||
|
@ -371,6 +371,8 @@ template <Dqn_usize N> void Dqn_StringBuilder_Free(Dqn_StringBuild
|
|||||||
// NOTE: Dqn_Slices
|
// NOTE: Dqn_Slices
|
||||||
//
|
//
|
||||||
// -------------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------------
|
||||||
|
inline Dqn_Slice<T> Dqn_Slice_InitWithArray(T (&array)[N]);
|
||||||
|
template <typename T> inline Dqn_Slice<T> Dqn_Slice_Allocate(Dqn_Allocator *allocator, Dqn_isize len);
|
||||||
template <typename T> inline Dqn_Slice<T> Dqn_Slice_CopyNullTerminated(Dqn_Allocator *allocator, T const *src, Dqn_isize len);
|
template <typename T> inline Dqn_Slice<T> Dqn_Slice_CopyNullTerminated(Dqn_Allocator *allocator, T const *src, Dqn_isize len);
|
||||||
template <typename T> inline Dqn_Slice<T> Dqn_Slice_CopyNullTerminated(Dqn_Allocator *allocator, Dqn_Slice<T> const src);
|
template <typename T> inline Dqn_Slice<T> Dqn_Slice_CopyNullTerminated(Dqn_Allocator *allocator, Dqn_Slice<T> const src);
|
||||||
template <typename T> inline Dqn_Slice<T> Dqn_Slice_Copy(Dqn_Allocator *allocator, T const *src, Dqn_isize len);
|
template <typename T> inline Dqn_Slice<T> Dqn_Slice_Copy(Dqn_Allocator *allocator, T const *src, Dqn_isize len);
|
||||||
@ -412,6 +414,7 @@ T * Dqn_FixedArray_Peek(DQN_FIXED_ARRAY_TE
|
|||||||
Dqn_isize Dqn_FixedArray_GetIndex(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, T const *entry);
|
Dqn_isize Dqn_FixedArray_GetIndex(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, T const *entry);
|
||||||
T * Dqn_FixedArray_Find(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, EqualityProc IsEqual);
|
T * Dqn_FixedArray_Find(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, EqualityProc IsEqual);
|
||||||
T * Dqn_FixedArray_Find(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, T *find);
|
T * Dqn_FixedArray_Find(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, T *find);
|
||||||
|
Dqn_Slice<T> Dqn_FixedArray_Slice(DQN_FIXED_ARRAY_TEMPLATE_DECL *a);
|
||||||
// -------------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// NOTE: Dqn_Array
|
// NOTE: Dqn_Array
|
||||||
@ -434,7 +437,8 @@ template <typename T> struct Dqn_Array
|
|||||||
T *operator+(Dqn_isize i) { DQN_ASSERT_MSG(i >= 0 && i < len, "%d >= 0 && %d < %d", i, len); return data + i; }
|
T *operator+(Dqn_isize i) { DQN_ASSERT_MSG(i >= 0 && i < len, "%d >= 0 && %d < %d", i, len); return data + i; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T> Dqn_Array<T> Dqn_Array_InitMemory(T *memory, Dqn_isize max, Dqn_isize len = 0);
|
template <typename T> Dqn_Array<T> Dqn_Array_InitWithMemory(T *memory, Dqn_isize max, Dqn_isize len = 0);
|
||||||
|
template <typename T> Dqn_Array<T> Dqn_Array_InitWithAllocatorNoGrow(Dqn_Allocator *allocator, Dqn_isize max, Dqn_isize len = 0);
|
||||||
template <typename T> bool Dqn_Array_Reserve(Dqn_Array<T> *a, Dqn_isize size);
|
template <typename T> bool Dqn_Array_Reserve(Dqn_Array<T> *a, Dqn_isize size);
|
||||||
template <typename T> void Dqn_Array_Free(Dqn_Array<T> *a);
|
template <typename T> void Dqn_Array_Free(Dqn_Array<T> *a);
|
||||||
template <typename T> T * Dqn_Array_Add(Dqn_Array<T> *a, T const *items, Dqn_isize num);
|
template <typename T> T * Dqn_Array_Add(Dqn_Array<T> *a, T const *items, Dqn_isize num);
|
||||||
@ -582,6 +586,7 @@ Dqn_V2I Dqn_RectI32_Size(Dqn_RectI32 rect);
|
|||||||
//
|
//
|
||||||
// -------------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------------
|
||||||
Dqn_V2 Dqn_LerpV2(Dqn_V2 a, Dqn_f32 t, Dqn_V2 b);
|
Dqn_V2 Dqn_LerpV2(Dqn_V2 a, Dqn_f32 t, Dqn_V2 b);
|
||||||
|
f32 Dqn_LerpF32(f32 a, f32 t, f32 b);
|
||||||
// -------------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// NOTE: Dqn_Mat4
|
// NOTE: Dqn_Mat4
|
||||||
@ -661,6 +666,7 @@ Dqn_i64 Dqn_Str_ToI64(char const *buf, int len
|
|||||||
Dqn_b32 Dqn_String_Compare(Dqn_String const lhs, Dqn_String const rhs);
|
Dqn_b32 Dqn_String_Compare(Dqn_String const lhs, Dqn_String const rhs);
|
||||||
Dqn_b32 Dqn_String_CompareCaseInsensitive(Dqn_String const lhs, Dqn_String const rhs);
|
Dqn_b32 Dqn_String_CompareCaseInsensitive(Dqn_String const lhs, Dqn_String const rhs);
|
||||||
Dqn_String Dqn_String_Copy(Dqn_Allocator *allocator, Dqn_String const src);
|
Dqn_String Dqn_String_Copy(Dqn_Allocator *allocator, Dqn_String const src);
|
||||||
|
Dqn_String Dqn_String_TrimWhitespaceAround(Dqn_String src);
|
||||||
// -------------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// NOTE: File
|
// NOTE: File
|
||||||
|
@ -228,12 +228,12 @@ FILE_SCOPE void UnitTests()
|
|||||||
// ---------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------
|
||||||
{
|
{
|
||||||
TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_Array");
|
TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_Array");
|
||||||
// NOTE: Dqn_Array_InitMemory
|
// NOTE: Dqn_Array_InitWithMemory
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
TEST_START_SCOPE(testing_state, "Fixed Memory: Test add single item and can't allocate more");
|
TEST_START_SCOPE(testing_state, "Fixed Memory: Test add single item and can't allocate more");
|
||||||
int memory[4] = {};
|
int memory[4] = {};
|
||||||
Dqn_Array<int> array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
Dqn_Array<int> array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
||||||
Dqn_Array_Add(&array, 1);
|
Dqn_Array_Add(&array, 1);
|
||||||
Dqn_Array_Add(&array, 2);
|
Dqn_Array_Add(&array, 2);
|
||||||
Dqn_Array_Add(&array, 3);
|
Dqn_Array_Add(&array, 3);
|
||||||
@ -254,7 +254,7 @@ FILE_SCOPE void UnitTests()
|
|||||||
TEST_START_SCOPE(testing_state, "Fixed Memory: Test add array of items");
|
TEST_START_SCOPE(testing_state, "Fixed Memory: Test add array of items");
|
||||||
int memory[4] = {};
|
int memory[4] = {};
|
||||||
int DATA[] = {1, 2, 3};
|
int DATA[] = {1, 2, 3};
|
||||||
Dqn_Array<int> array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
Dqn_Array<int> array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
||||||
Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA));
|
Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA));
|
||||||
TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data %d", array.data[0]);
|
TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data %d", array.data[0]);
|
||||||
TEST_EXPECT_MSG(testing_state, array.data[1] == 2, "array.data %d", array.data[1]);
|
TEST_EXPECT_MSG(testing_state, array.data[1] == 2, "array.data %d", array.data[1]);
|
||||||
@ -267,7 +267,7 @@ FILE_SCOPE void UnitTests()
|
|||||||
TEST_START_SCOPE(testing_state, "Fixed Memory: Test clear and clear with memory zeroed");
|
TEST_START_SCOPE(testing_state, "Fixed Memory: Test clear and clear with memory zeroed");
|
||||||
int memory[4] = {};
|
int memory[4] = {};
|
||||||
int DATA[] = {1, 2, 3};
|
int DATA[] = {1, 2, 3};
|
||||||
Dqn_Array<int> array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
Dqn_Array<int> array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
||||||
Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA));
|
Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA));
|
||||||
Dqn_Array_Clear(&array, false /*zero_mem*/);
|
Dqn_Array_Clear(&array, false /*zero_mem*/);
|
||||||
TEST_EXPECT_MSG(testing_state, array.len == 0, "array.len: %d", array.len);
|
TEST_EXPECT_MSG(testing_state, array.len == 0, "array.len: %d", array.len);
|
||||||
@ -282,7 +282,7 @@ FILE_SCOPE void UnitTests()
|
|||||||
TEST_START_SCOPE(testing_state, "Fixed Memory: Test erase stable and erase unstable");
|
TEST_START_SCOPE(testing_state, "Fixed Memory: Test erase stable and erase unstable");
|
||||||
int memory[4] = {};
|
int memory[4] = {};
|
||||||
int DATA[] = {1, 2, 3, 4};
|
int DATA[] = {1, 2, 3, 4};
|
||||||
Dqn_Array<int> array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
Dqn_Array<int> array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
||||||
Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA));
|
Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA));
|
||||||
Dqn_Array_EraseUnstable(&array, 1);
|
Dqn_Array_EraseUnstable(&array, 1);
|
||||||
TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data %d", array.data[0]);
|
TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data %d", array.data[0]);
|
||||||
@ -300,7 +300,7 @@ FILE_SCOPE void UnitTests()
|
|||||||
TEST_START_SCOPE(testing_state, "Fixed Memory: Test array pop and peek");
|
TEST_START_SCOPE(testing_state, "Fixed Memory: Test array pop and peek");
|
||||||
int memory[4] = {};
|
int memory[4] = {};
|
||||||
int DATA[] = {1, 2, 3};
|
int DATA[] = {1, 2, 3};
|
||||||
Dqn_Array<int> array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
Dqn_Array<int> array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
||||||
Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA));
|
Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA));
|
||||||
Dqn_Array_Pop(&array, 2);
|
Dqn_Array_Pop(&array, 2);
|
||||||
TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data: %d", array.data[0]);
|
TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data: %d", array.data[0]);
|
||||||
@ -316,7 +316,7 @@ FILE_SCOPE void UnitTests()
|
|||||||
{
|
{
|
||||||
TEST_START_SCOPE(testing_state, "Fixed Memory: Test free on fixed memory array does nothing");
|
TEST_START_SCOPE(testing_state, "Fixed Memory: Test free on fixed memory array does nothing");
|
||||||
int memory[4] = {};
|
int memory[4] = {};
|
||||||
Dqn_Array<int> array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
Dqn_Array<int> array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/);
|
||||||
DQN_DEFER { Dqn_Array_Free(&array); };
|
DQN_DEFER { Dqn_Array_Free(&array); };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user