gb.hpp - Atomics

This commit is contained in:
gingerBill 2015-10-03 15:35:16 +01:00
commit 2357bdd227
3 changed files with 48 additions and 36 deletions

View File

@ -7,7 +7,7 @@ library | latest version | category | languages | description
----------------|----------------|----------|-----------|-------------
**gb_string.h** | 0.93 | strings | C, C++ | A better string library for C & C++
**gb_ini.h** | 0.91 | misc | C, C++ | A simple ini file loader library for C & C++
**gb.hpp** | 0.08 | misc | C++11 | (Experimental) A C++11 helper library without STL geared towards game development
**gb.hpp** | 0.09 | misc | C++11 | (Experimental) A C++11 helper library without STL geared towards game development
## FAQ

77
gb.hpp
View File

@ -1,7 +1,8 @@
// gb.hpp - v0.08 - public domain C++11 helper library - no warranty implied; use at your own risk
// gb.hpp - v0.09 - public domain C++11 helper library - no warranty implied; use at your own risk
// (Experimental) A C++11 helper library without STL geared towards game development
//
// Version History:
// 0.09 - Bug Fixes
// 0.08 - Matrix(2,3)
// 0.07 - Bug Fixes
// 0.06 - Os spec ideas
@ -141,6 +142,7 @@
#include <intrin.h>
#else
#include <pthread.h>
#include <sys/time.h>
#endif
#ifndef NDEBUG
@ -157,7 +159,7 @@ gb__assert_handler(bool condition, const char* condition_str,
if (condition)
return;
fprintf(stderr, "ASSERT! %s(%d): %s", filename, line, condition_str);
fprintf(stderr, "ASSERT! %s(%d): %s", filename, (int)line, condition_str);
if (error_text)
{
fprintf(stderr, " - ");
@ -415,7 +417,7 @@ inline void* alloc(Allocator& a, usize size, usize align = GB_DEFAULT_ALIGNMENT)
inline void dealloc(Allocator& a, void* ptr) { return a.dealloc(ptr); }
template <typename T>
inline T* alloc_struct(Allocator& a) { return static_cast<T*>a.alloc(sizeof(T), alignof(T)); }
inline T* alloc_struct(Allocator& a) { return static_cast<T*>(a.alloc(sizeof(T), alignof(T))); }
template <typename T>
@ -821,7 +823,7 @@ erase_from_hash_table(Hash_Table<T>& h, const Find_Result& fr)
if (fr.data_prev < 0)
h.hashes[fr.hash_index] = h.data[fr.data_index].next;
else
h.data[fr.data_prev].next = g.data[fr.data_index].next;
h.data[fr.data_prev].next = h.data[fr.data_index].next;
pop_back_array(h.data); // updated array count
@ -869,22 +871,23 @@ Find_Result
find_result_in_hash_table(const Hash_Table<T>& h, const typename Hash_Table<T>::Entry* e)
{
Find_Result fr;
fr.hash_index = -1;
fr.data_prev = -1;
fr.data_index = -1;
// TODO(bill):
// fr.hash_index = -1;
// fr.data_prev = -1;
// fr.data_index = -1;
if (h.hashes.count == 0 || !e)
return fr;
// if (h.hashes.count == 0 || !e)
// return fr;
fr.hash_index = key % h.hashes.count;
fr.data_index = h.hashes[fr.hash_index];
while (fr.data_index >= 0)
{
if (&h.data[fr.data_index] == e)
return fr;
fr.data_prev = fr.data_index;
fr.data_index = h.data[fr.data_index].next;
}
// fr.hash_index = key % h.hashes.count;
// fr.data_index = h.hashes[fr.hash_index];
// while (fr.data_index >= 0)
// {
// if (&h.data[fr.data_index] == e)
// return fr;
// fr.data_prev = fr.data_index;
// fr.data_index = h.data[fr.data_index].next;
// }
return fr;
}
@ -985,7 +988,7 @@ template <typename T>
inline bool
hash_table_has(const Hash_Table<T>& h, u64 key)
{
return imple::find_entry_or_fail_in_hash_table(h, key) >= 0;
return impl::find_entry_or_fail_in_hash_table(h, key) >= 0;
}
template <typename T>
@ -1070,7 +1073,7 @@ multiple_count_from_hash_table(const Hash_Table<T>& h, u64 key)
auto e = find_first_in_hash_table(h, key);
while (e)
{
count++
count++;
e = find_next_in_hash_table(h, e);
}
@ -1082,7 +1085,7 @@ template <typename T>
inline const typename Hash_Table<T>::Entry*
find_first_in_hash_table(const Hash_Table<T>& h, u64 key)
{
const s64 index = impl::find_first_in_hash_table(h, key);
const s64 index = find_first_in_hash_table(h, key);
if (index < 0)
return nullptr;
return &h.data[index];
@ -1125,7 +1128,7 @@ template <typename T>
inline void
remove_entry_from_hash_table(Hash_Table<T>& h, const typename Hash_Table<T>::Entry* e)
{
const auto fr = impl:;find_result_in_hash_table(h, e);
const auto fr = impl::find_result_in_hash_table(h, e);
if (fr.data_index >= 0)
impl::erase_from_hash_table(h, fr);
}
@ -2913,10 +2916,10 @@ Time time_now()
s64 t = (s64)mach_absolute_time();
return microseconds(t);
#else
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
struct timeval t;
gettimeofday(&t, nullptr);
return milliseconds((spec.tv_sec * 1000000ll) + (spec.tv_nsec * 1000ll));
return microseconds((t.tv_sec * 1000000ll) + (t.tv_usec * 1ll));
#endif
}
@ -3925,8 +3928,10 @@ inline f32 magnitude(const Vector2& a)
inline Vector2 normalize(const Vector2& a)
{
f32 m = 1.0f / magnitude(a);
return a * m;
f32 m = magnitude(a);
if (m > 0)
return a * (1.0f / m);
return {};
}
inline Vector2 hadamard(const Vector2& a, const Vector2& b)
@ -3956,8 +3961,10 @@ inline f32 magnitude(const Vector3& a)
inline Vector3 normalize(const Vector3& a)
{
f32 m = 1.0f / magnitude(a);
return a * m;
f32 m = magnitude(a);
if (m > 0)
return a * (1.0f / m);
return {};
}
inline Vector3 hadamard(const Vector3& a, const Vector3& b)
@ -3978,8 +3985,10 @@ inline f32 magnitude(const Vector4& a)
inline Vector4 normalize(const Vector4& a)
{
f32 m = 1.0f / magnitude(a);
return a * m;
f32 m = magnitude(a);
if (m > 0)
return a * (1.0f / m);
return {};
}
inline Vector4 hadamard(const Vector4& a, const Vector4& b)
@ -4008,8 +4017,10 @@ inline f32 magnitude(const Quaternion& a)
inline Quaternion normalize(const Quaternion& a)
{
f32 m = 1.0f / magnitude(a);
return a * m;
f32 m = magnitude(a);
if (m > 0)
return a * (1.0f / m);
return {};
}
inline Quaternion conjugate(const Quaternion& a)

View File

@ -224,6 +224,7 @@ enum
GB_INI_ERROR_COUNT,
};
typedef struct gb_Ini_Error gb_Ini_Error;
struct gb_Ini_Error
{
int type;
@ -242,8 +243,8 @@ typedef GB_INI_HANDLER(gb_Ini_Handler);
extern const char* GB_ERROR_STRINGS[GB_INI_ERROR_COUNT];
struct gb_Ini_Error gb_ini_parse(const char* filename, gb_Ini_Handler* handler_func, void* data);
struct gb_Ini_Error gb_ini_parse_file(FILE* file, gb_Ini_Handler* handler_func, void* data);
gb_Ini_Error gb_ini_parse(const char* filename, gb_Ini_Handler* handler_func, void* data);
gb_Ini_Error gb_ini_parse_file(FILE* file, gb_Ini_Handler* handler_func, void* data);
gb_inline const char*
gb_ini_error_string(const struct gb_Ini_Error err)