array
This commit is contained in:
11
build_file.c
11
build_file.c
@@ -41,10 +41,7 @@ void build_testing_target(void) {
|
|||||||
|
|
||||||
ok = os_systemf("testing.exe");
|
ok = os_systemf("testing.exe");
|
||||||
if (ok != 0) exit(ok);
|
if (ok != 0) exit(ok);
|
||||||
|
} else {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ok = os_systemf(
|
int ok = os_systemf(
|
||||||
"clang ../src/testing/testing_main.c -o testing.exe -g -I ../src"
|
"clang ../src/testing/testing_main.c -o testing.exe -g -I ../src"
|
||||||
" -fdiagnostics-absolute-paths -Wno-unsequenced -Wno-single-bit-bitfield-constant-conversion"
|
" -fdiagnostics-absolute-paths -Wno-unsequenced -Wno-single-bit-bitfield-constant-conversion"
|
||||||
@@ -56,6 +53,8 @@ void build_testing_target(void) {
|
|||||||
if (ok != 0) exit(ok);
|
if (ok != 0) exit(ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void build_win32_app_base_target(void) {
|
void build_win32_app_base_target(void) {
|
||||||
if (!cache_code_modified(s8("../src/app/app_win32_exe.c"), s8("app_win32.exe"))) {
|
if (!cache_code_modified(s8("../src/app/app_win32_exe.c"), s8("app_win32.exe"))) {
|
||||||
return;
|
return;
|
||||||
@@ -165,13 +164,13 @@ int main(int argc, char **argv) {
|
|||||||
generate_testing_code(tcx->temp);
|
generate_testing_code(tcx->temp);
|
||||||
generate_text_editor_code(tcx->temp);
|
generate_text_editor_code(tcx->temp);
|
||||||
|
|
||||||
b32 run_server = false;
|
|
||||||
b32 run_win32_app_base_target = true;
|
b32 run_win32_app_base_target = true;
|
||||||
b32 run_testing_target = true;
|
b32 run_testing_target = true;
|
||||||
b32 run_text_editor_dll_target = false;
|
b32 run_text_editor_dll_target = true;
|
||||||
b32 run_prototype_dll_target = false;
|
b32 run_prototype_dll_target = false;
|
||||||
b32 run_prototype_wasm_target = false;
|
b32 run_prototype_wasm_target = false;
|
||||||
b32 run_prototype_standalone_target = false;
|
b32 run_prototype_standalone_target = false;
|
||||||
|
b32 run_server = false;
|
||||||
|
|
||||||
if (run_server) {
|
if (run_server) {
|
||||||
os_systemf("start /D ..\\package ..\\package\\run_server.bat");
|
os_systemf("start /D ..\\package ..\\package\\run_server.bat");
|
||||||
|
|||||||
@@ -3,11 +3,14 @@
|
|||||||
#elif PLATFORM_WINDOWS
|
#elif PLATFORM_WINDOWS
|
||||||
#pragma comment(lib, "user32.lib")
|
#pragma comment(lib, "user32.lib")
|
||||||
#pragma comment(lib, "DbgHelp.lib")
|
#pragma comment(lib, "DbgHelp.lib")
|
||||||
|
#pragma comment(lib, "Shell32.lib")
|
||||||
|
#pragma comment(lib, "Ole32.lib")
|
||||||
#define NOMINMAX
|
#define NOMINMAX
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <TlHelp32.h>
|
#include <TlHelp32.h>
|
||||||
#include <DbgHelp.h>
|
#include <DbgHelp.h>
|
||||||
|
#include <Shlobj.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -59,6 +62,7 @@
|
|||||||
#include "core_type_info.c"
|
#include "core_type_info.c"
|
||||||
#include "core_hash.c"
|
#include "core_hash.c"
|
||||||
#include "core_hash_table.c"
|
#include "core_hash_table.c"
|
||||||
|
#include "core_array.c"
|
||||||
#ifndef DONT_INCLUDE_GENERATED_MATH
|
#ifndef DONT_INCLUDE_GENERATED_MATH
|
||||||
#include "core_math.gen.c"
|
#include "core_math.gen.c"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
98
src/core/core_array.c
Normal file
98
src/core/core_array.c
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
typedef struct array_header_t array_header_t;
|
||||||
|
struct array_header_t {
|
||||||
|
ma_arena_t *arena;
|
||||||
|
i64 len;
|
||||||
|
i64 cap;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define array_header(arr) ((array_header_t *)(arr) - 1)
|
||||||
|
#define array_len(arr) (array_header(arr)->len)
|
||||||
|
#define array_cap(arr) (array_header(arr)->cap)
|
||||||
|
#define array_add(arr, x) (array_grow(&(arr), sizeof(*(arr)), 1), (arr)[array_len(arr)++] = (x))
|
||||||
|
#define array_addn(arr, count) (array_grow(&(arr), sizeof(*(arr)), (count)), array_len(arr) += (count), &(arr)[array_len(arr) - (count)])
|
||||||
|
#define array_set_len(arr, x) (array_len(arr) = (x))
|
||||||
|
#define array_set_cap(arr, x) (array__set_cap(&(arr), sizeof(*(arr)), (x)))
|
||||||
|
#define array_pop(arr) ((arr)[--array_len(arr)])
|
||||||
|
#define array_swapdel(arr, i) (assert_expr((i) < array_len(arr)), (arr)[(i)] = (arr)[--array_len(arr)])
|
||||||
|
#define array_create(arena, type, count) (type *)(array__create((arena), sizeof(type), (count)) + 1)
|
||||||
|
|
||||||
|
fn array_header_t *array__create(ma_arena_t *arena, i64 item_size, i64 item_count) {
|
||||||
|
array_header_t *hdr = (array_header_t *)ma_push_size(arena, item_size * item_count + sizeof(array_header_t));
|
||||||
|
hdr->arena = arena;
|
||||||
|
hdr->cap = item_count;
|
||||||
|
return hdr;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn void array__set_cap(void **arr, i64 item_size, i64 item_count) {
|
||||||
|
array_header_t *hdr = array_header(*arr);
|
||||||
|
array_header_t *new_hdr = array__create(hdr->arena, item_size, item_count);
|
||||||
|
new_hdr->len = CLAMP(hdr->len, 0, item_count);
|
||||||
|
memory_copy(new_hdr + 1, hdr + 1, new_hdr->len * item_size);
|
||||||
|
*arr = new_hdr + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn void array_grow(void **arr, i64 item_size, i64 increment) {
|
||||||
|
assert(increment >= 1);
|
||||||
|
array_header_t *hdr = array_header(*arr);
|
||||||
|
if (hdr->len + increment > hdr->cap) {
|
||||||
|
array_header_t *new_hdr = array__create(hdr->arena, item_size, (hdr->cap + increment - 1) * 2);
|
||||||
|
memory_copy(new_hdr + 1, hdr + 1, hdr->len * item_size);
|
||||||
|
new_hdr->len = hdr->len;
|
||||||
|
*arr = new_hdr + 1;
|
||||||
|
// dealloc(hdr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn_test void test_array(void) {
|
||||||
|
ma_temp_t scratch = ma_begin_scratch();
|
||||||
|
int *arr = array_create(scratch.arena, int, 2);
|
||||||
|
for (int i = 0; i < 512; i += 1) {
|
||||||
|
array_add(arr, i);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 512; i += 1) {
|
||||||
|
assert(arr[i] == i);
|
||||||
|
}
|
||||||
|
assert(array_len(arr) == 512);
|
||||||
|
assert(array_cap(arr) == 512);
|
||||||
|
for (int i = 511; i >= 0; i -= 1) {
|
||||||
|
int a = array_pop(arr);
|
||||||
|
assert(a == i);
|
||||||
|
}
|
||||||
|
assert(array_len(arr) == 0);
|
||||||
|
|
||||||
|
{
|
||||||
|
int *a = array_addn(arr, 4);
|
||||||
|
assert(arr == a);
|
||||||
|
assert(array_header(arr)->len == 4);
|
||||||
|
|
||||||
|
int *b = array_addn(arr, 12);
|
||||||
|
assert(arr + 4 == b);
|
||||||
|
assert(array_header(arr)->len == 16);
|
||||||
|
|
||||||
|
array_set_len(arr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 512; i += 1) {
|
||||||
|
array_add(arr, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
array_swapdel(arr, 3);
|
||||||
|
assert(arr[3] == 511);
|
||||||
|
assert(array_len(arr) == 511);
|
||||||
|
|
||||||
|
array_set_len(arr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 512; i += 1) {
|
||||||
|
array_add(arr, i);
|
||||||
|
}
|
||||||
|
array_set_cap(arr, 256);
|
||||||
|
assert(array_len(arr) == 256);
|
||||||
|
assert(array_cap(arr) == 256);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ma_end_scratch(scratch);
|
||||||
|
}
|
||||||
@@ -432,7 +432,7 @@ fn void parser_panicf(b32 print_only_file, lex_t *token, const char *str, ...) {
|
|||||||
if (print_only_file) {
|
if (print_only_file) {
|
||||||
fatalf("%s: error: %S", token->file, str8);
|
fatalf("%s: error: %S", token->file, str8);
|
||||||
} else {
|
} else {
|
||||||
fatalf("%S(%d): error: %S", token->file, token->line, str8);
|
fatalf("%s(%d): error: %S", token->file, token->line, str8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -364,3 +364,32 @@ fn i32 os_thread_join(os_thread_t *thread) {
|
|||||||
// }
|
// }
|
||||||
// ma_end_scratch(scratch);
|
// ma_end_scratch(scratch);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
fn s8_t os_appdata(ma_arena_t *arena, s8_t name) {
|
||||||
|
assert(name.len != 0);
|
||||||
|
assert(name.str);
|
||||||
|
assert(arena);
|
||||||
|
|
||||||
|
wchar_t *out_path = NULL;
|
||||||
|
HRESULT hr = SHGetKnownFolderPath(&FOLDERID_RoamingAppData, KF_FLAG_CREATE, NULL, &out_path);
|
||||||
|
if (!SUCCEEDED(hr)) {
|
||||||
|
debugf(__FUNCTION__".SHGetKnownFolderPath failed with hr: %d", hr);
|
||||||
|
return s8_null;
|
||||||
|
}
|
||||||
|
|
||||||
|
s16_t appdata_path = s16_from_str16((u16 *)out_path);
|
||||||
|
|
||||||
|
ma_temp_t scratch = ma_begin_scratch1(arena);
|
||||||
|
s8_t tmp = s8_from_s16(scratch.arena, appdata_path);
|
||||||
|
s8_normalize_path_unsafe(tmp);
|
||||||
|
s8_t path = s8_printf(arena, "%S/%S/", tmp, name);
|
||||||
|
os_mkdir_t mkdir_status = os_mkdir(path);
|
||||||
|
if (mkdir_status != os_mkdir_success && mkdir_status != os_mkdir_file_exists) {
|
||||||
|
debugf(__FUNCTION__".os_mkdir failed with status: %d, for: %S", mkdir_status, path);
|
||||||
|
path = s8_null;
|
||||||
|
}
|
||||||
|
|
||||||
|
CoTaskMemFree(out_path);
|
||||||
|
ma_end_scratch(scratch);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
|
void test_string16(void);
|
||||||
|
void test_hash_table(void);
|
||||||
|
void test_intern_table(void);
|
||||||
|
void test_array(void);
|
||||||
fn void run_tests(void) {
|
fn void run_tests(void) {
|
||||||
test_string16();
|
test_string16();
|
||||||
test_hash_table();
|
test_hash_table();
|
||||||
test_intern_table();
|
test_intern_table();
|
||||||
|
test_array();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ void generate_testing_code(ma_arena_t *arena) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sb8_t *c = sb8_serial_begin(arena);
|
sb8_t *c = sb8_serial_begin(arena);
|
||||||
|
for (sb8_node_t *it = tests->first; it; it = it->next) {
|
||||||
|
sb8_printf(c, "void %S(void);\n", it->string);
|
||||||
|
}
|
||||||
sb8_printf(c, "fn void run_tests(void) {\n");
|
sb8_printf(c, "fn void run_tests(void) {\n");
|
||||||
for (sb8_node_t *it = tests->first; it; it = it->next) {
|
for (sb8_node_t *it = tests->first; it; it = it->next) {
|
||||||
sb8_printf(c, " %S();\n", it->string);
|
sb8_printf(c, " %S();\n", it->string);
|
||||||
|
|||||||
@@ -6,9 +6,12 @@
|
|||||||
#include "testing.gen.c"
|
#include "testing.gen.c"
|
||||||
|
|
||||||
fn void os_test(void) {
|
fn void os_test(void) {
|
||||||
|
ma_temp_t scratch = ma_begin_scratch();
|
||||||
|
|
||||||
os_date_t local_time = os_local_time();
|
os_date_t local_time = os_local_time();
|
||||||
os_date_t universal_time = os_universal_time();
|
os_date_t universal_time = os_universal_time();
|
||||||
debugf("OS local_time = %S | universal_time = %S", os_format_date(tcx->temp, local_time), os_format_date(tcx->temp, universal_time));
|
unused(universal_time); unused(local_time);
|
||||||
|
// debugf("OS local_time = %S | universal_time = %S", os_format_date(tcx->temp, local_time), os_format_date(tcx->temp, universal_time));
|
||||||
|
|
||||||
s8_t exe_dir = os_exe_dir(tcx->temp);
|
s8_t exe_dir = os_exe_dir(tcx->temp);
|
||||||
assert(exe_dir.str[exe_dir.len - 1] == '/');
|
assert(exe_dir.str[exe_dir.len - 1] == '/');
|
||||||
@@ -25,11 +28,19 @@ fn void os_test(void) {
|
|||||||
assert(os_is_abs(exe));
|
assert(os_is_abs(exe));
|
||||||
assert(os_is_abs(cwd));
|
assert(os_is_abs(cwd));
|
||||||
assert(!os_is_abs(s8("../path/")));
|
assert(!os_is_abs(s8("../path/")));
|
||||||
debugf("OS paths %S %S %S", exe_dir, exe, cwd);
|
// debugf("OS paths %S %S %S", exe_dir, exe, cwd);
|
||||||
|
|
||||||
s8_t file = os_read(tcx->temp, s8("../.gitignore"));
|
s8_t file = os_read(tcx->temp, s8("../.gitignore"));
|
||||||
assert(file.str != 0);
|
assert(file.str != 0);
|
||||||
assert(file.len != 0);
|
assert(file.len != 0);
|
||||||
|
|
||||||
|
#if PLATFORM_WINDOWS
|
||||||
|
s8_t path = os_appdata(scratch.arena, s8("testing"));
|
||||||
|
assert(path.str[path.len - 1] == '/');
|
||||||
|
assert(s8_starts_with(path, s8("C:/")));
|
||||||
|
assert(s8_ends_with(path, s8("/testing/")));
|
||||||
|
#endif
|
||||||
|
ma_end_scratch(scratch);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn void test_s8(void) {
|
fn void test_s8(void) {
|
||||||
|
|||||||
@@ -10,5 +10,5 @@ void run_all_tests(void) {
|
|||||||
test_string16();
|
test_string16();
|
||||||
test_hash_table();
|
test_hash_table();
|
||||||
test_intern_table();
|
test_intern_table();
|
||||||
ui_test_text_replace();
|
test_array();
|
||||||
}// run_all_tests()
|
}// run_all_tests()
|
||||||
|
|||||||
@@ -1,21 +1,61 @@
|
|||||||
#include "core/core.h"
|
#include "core/core.h"
|
||||||
#include "os/os.h"
|
#include "os/os.h"
|
||||||
#include "app/app.h"
|
#include "app/app.h"
|
||||||
#include "ui/ui.h"
|
// #include "ui/ui.h"
|
||||||
|
|
||||||
#include "core/core.c"
|
#include "core/core.c"
|
||||||
#include "os/os.c"
|
#include "os/os.c"
|
||||||
#include "app/app.c"
|
#include "app/app.c"
|
||||||
#include "render/render.c"
|
#include "render/render.c"
|
||||||
#include "ui/ui.c"
|
// #include "ui/ui.c"
|
||||||
|
|
||||||
#include "text_editor.gen.c"
|
#include "text_editor.gen.c"
|
||||||
|
|
||||||
// @todo:
|
/*todo:
|
||||||
// Create a complete string16 library
|
* OK Create a complete string16 library
|
||||||
// Win32 SDL_GetPrefPath, create configuration directory, get path
|
* OK (Win32) Win32 SDL_GetPrefPath, create configuration directory, get path
|
||||||
// Win32 set screen saver
|
* Array
|
||||||
// Win32 upload icon
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Win32 upload icon
|
||||||
|
**/
|
||||||
|
|
||||||
|
typedef struct caret_t caret_t;
|
||||||
|
struct caret_t {
|
||||||
|
i64 ifront;
|
||||||
|
union {
|
||||||
|
r1i64_t range;
|
||||||
|
i64 pos[2];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct xy_t xy_t;
|
||||||
|
struct xy_t {
|
||||||
|
i64 col;
|
||||||
|
i64 line;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct buffer_id_t buffer_id_t;
|
||||||
|
struct buffer_id_t { i64 e; };
|
||||||
|
|
||||||
|
typedef struct buffer_t buffer_t;
|
||||||
|
struct buffer_t {
|
||||||
|
buffer_id_t id;
|
||||||
|
|
||||||
|
union {
|
||||||
|
s16_t string;
|
||||||
|
struct {
|
||||||
|
u16 *data;
|
||||||
|
i64 len;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
i64 cap;
|
||||||
|
|
||||||
|
i64 *line_starts;
|
||||||
|
i64 line_count;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
fn_export b32 app_update(thread_ctx_t *thread_ctx, app_frame_t *frame) {
|
fn_export b32 app_update(thread_ctx_t *thread_ctx, app_frame_t *frame) {
|
||||||
tcx = thread_ctx;
|
tcx = thread_ctx;
|
||||||
@@ -40,6 +80,10 @@ fn_export b32 app_update(thread_ctx_t *thread_ctx, app_frame_t *frame) {
|
|||||||
rn_reload_font(font_size, frame->dpr);
|
rn_reload_font(font_size, frame->dpr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rn_begin_frame(frame);
|
||||||
|
rn_begin(white_color);
|
||||||
|
rn_end();
|
||||||
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1293,7 +1293,7 @@ fn void ui_demo_everything_lister(void) {
|
|||||||
|
|
||||||
ma_temp_t scratch = ma_begin_scratch();
|
ma_temp_t scratch = ma_begin_scratch();
|
||||||
s8_t needle = s8_make(text_input.str, text_input.len);
|
s8_t needle = s8_make(text_input.str, text_input.len);
|
||||||
fuzzy_pair_t *pairs = fuzzy_rate_array(scratch.arena, needle, cmds, lengthof(cmds));
|
fuzzy_pair_t *pairs = s8_fuzzy_rate_array(scratch.arena, needle, cmds, lengthof(cmds));
|
||||||
|
|
||||||
b32 set_focus = lister_just_opened || ti_sig.text_changed;
|
b32 set_focus = lister_just_opened || ti_sig.text_changed;
|
||||||
|
|
||||||
@@ -1370,7 +1370,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
|
|||||||
ui_top_rectp()[0] = r2f32_shrinks(ui_top_rect(), ui_em(1));
|
ui_top_rectp()[0] = r2f32_shrinks(ui_top_rect(), ui_em(1));
|
||||||
for (i32 i = 0; i < tweak_count; i += 1) {
|
for (i32 i = 0; i < tweak_count; i += 1) {
|
||||||
mt_tweak_t *tweak = tweak_table + i;
|
mt_tweak_t *tweak = tweak_table + i;
|
||||||
if (s8_starts_with(tweak->name, s8("_"), false)) {
|
if (s8_starts_with(tweak->name, s8("_"))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,5 +10,6 @@ void run_all_tests(void) {
|
|||||||
test_string16();
|
test_string16();
|
||||||
test_hash_table();
|
test_hash_table();
|
||||||
test_intern_table();
|
test_intern_table();
|
||||||
|
test_array();
|
||||||
ui_test_text_replace();
|
ui_test_text_replace();
|
||||||
}// run_all_tests()
|
}// run_all_tests()
|
||||||
|
|||||||
Reference in New Issue
Block a user