From fa1b8ea54646f1a0f3eadef33e3a660b875cc1ff Mon Sep 17 00:00:00 2001 From: dam Date: Thu, 17 Aug 2023 09:36:02 +0100 Subject: WIP Code cleanup. --- ttt.jai | 147 ++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 97 insertions(+), 50 deletions(-) (limited to 'ttt.jai') diff --git a/ttt.jai b/ttt.jai index 68d3e33..22c6156 100644 --- a/ttt.jai +++ b/ttt.jai @@ -30,10 +30,7 @@ // TODO List: -// [ ] Decide once and for all if we're calling them entries or tasks. -// [ ] Test shrinking mechanism... this may be done by using the coalescing function. -// [ ] Every time we add or remove entries to the database, it may be reallocated, -// thus making the selected_task and active_task pointers invalid. Check this is not messing up the app. +// [ ] Every time we add or remove tasks to the database, it may be reallocated, thus making the selected_task and active_task pointers invalid. Check if this is messing up the app. VERSION :: "2.0"; // Use only 3 chars (to fit layouts). YEAR :: "2023"; @@ -41,7 +38,7 @@ FIRST_DAY_OF_WEEK :: 1; // (0-6, Sunday = 0). NUM_WEEK_DAYS :: 7; // TODO This has to go - Just to be more clear about what we're looping about. NAME_SIZE :: 72; // TODO Use this instead of Task.name.count ? -APP_FOLDER_NAME :: ".task_time_tracker_test"; // TODO Using _v2 to avoid erasing my work data. +APP_FOLDER_NAME :: ".task_time_tracker_test"; // TODO Using different folder to avoid erasing my work data. DB_FILE_NAME :: "database.bin"; AR_FILE_NAME :: "archive.csv"; DB_FILE_SIGN_STR :: "TTT:B:02"; @@ -70,17 +67,6 @@ Database :: struct { tasks : [..] Task; } -SIZE_OF_TASK :: #run size_of(Task); -// const char DB_FILE_SIGN[] = DB_FILE_SIGN_STR; -// const size_t DB_FILE_SIGN_LENGTH = sizeof(DB_FILE_SIGN_STR)-1; -// const size_t SIZEOF_TASK_ST = sizeof(task_st); -// const size_t SIZEOF_DATABASE_ST = sizeof(database_st); -// const size_t SIZEOF_CHAR = sizeof(char); -// const size_t SIZEOF_INT64 = sizeof(int64_t); -// -// -// - database : Database; archive : Database; is_autosave_enabled := true; @@ -88,8 +74,7 @@ countdown_to_autosave := -1; app_directory : string; db_file_path : string; ar_file_path : string; -// char *string_buffer = NULL; // A temporary buffer for localized actions. Please avoid data leaks and out-of-bounds errors. -// size_t string_buffer_size = 0; + size_x : s32; size_y : s32; pos_x : s32; @@ -176,22 +161,17 @@ is_equal_to_any :: (to_compare :string, test_a :string, test_b :string) -> bool return to_compare == test_a || to_compare == test_b; } -// Given an UTF8 encoded string, truncate it to length bytes without breaking any UTF8 character. -// The string should have capacity for at least length + 1. -// The terminating null byte ('\0') is not included in length. -// Returns the truncated string length. +// Count digits required to represent number on base. Sign is discarded. +count_digits :: (number: s64, base: s64 = 10) -> s64 { + if number == 0 return 1; + return cast(s64) floor( log(cast(float64)abs(number)) / log(cast(float64)abs(base)) ) + 1; +} Text_Encoding :: enum u8 #specified { ASCII :: 1; UTF8 :: 2; } -// Count digits required to represent number on base. Sign is discarded. -count_digits :: (number: s64, base: s64 = 10) -> s64 { - if number == 0 return 1; - return cast(s64) floor( log(cast(float64)abs(number)) / log(cast(float64)abs(base)) ) + 1; -} - // Truncates the string to the length provided or shorter, in case of UTF8 strings that require so. // Truncation is done by zeroing the tail of the string in place. // Returns length of truncated string. @@ -360,9 +340,10 @@ delete_task :: (using db: *Database, index: s64) -> bool { // TODO Maybe use `us } // Try to shrink database capacity if using more than 2MB. - if (tasks.allocated >> 2) > tasks.count && tasks.allocated * SIZE_OF_TASK > 2_000_000 { + size_of_task := size_of(Task); + if (tasks.allocated >> 2) > tasks.count && tasks.allocated * size_of_task > 2_000_000 { new_capacity := tasks.allocated >> 1; - new_tasks_data := realloc(tasks.data, new_capacity * SIZE_OF_TASK, tasks.allocated * SIZE_OF_TASK, tasks.allocator); + new_tasks_data := realloc(tasks.data, new_capacity * size_of_task, tasks.allocated * size_of_task, tasks.allocator); if new_tasks_data != null { tasks.data = new_tasks_data; tasks.allocated = new_capacity; @@ -467,22 +448,88 @@ update_times :: (db: *Database) { } } +x_count: s64 = 0; +x_average: float64 = 0.0; + + // Recalculates database totals. update_total_times :: (db: *Database) { assert(db != null, ASSERT_NOT_NULL, "db"); + // print(">>>xpto: %<<<\n", totals); + // print(">>>sizeA: %<<<\n", size_of([7] s64)); + // print(">>>sizeB: %<<<\n", NUM_WEEK_DAYS * size_of(s64)); + // print(">>>sizeC: %<<<\n", size_of(type_of(db.total_times))); + // Initialize(*totals.data); + // print(">>>xpto: %<<<\n", totals); + // totals.data = .[]; + + // TODO WIP + Check what size there is for a [..] s64... + + print(">>%<<", db.total_times.count); + // memset(totals.data, 0, db.total_times.count * size_of(s64)); + // memset(db.total_times.data, 0, size_of(type_of(db.total_times))); + + for xpto: 1..20 { + + start := current_time_monotonic(); + + for * total : db.total_times { < string { + builder: String_Builder; + defer reset(*builder); + for 0..6 { + print_to_builder(*builder, code, it); + } + return builder_to_string(*builder); + } + totals: []s64 = db.total_times; - memset(totals.data, 0, NUM_WEEK_DAYS * size_of(s64)); for db.tasks { times : []s64 = it.times; - totals[0] = add(totals[0], times[0]); - totals[1] = add(totals[1], times[1]); - totals[2] = add(totals[2], times[2]); - totals[3] = add(totals[3], times[3]); - totals[4] = add(totals[4], times[4]); - totals[5] = add(totals[5], times[5]); - totals[6] = add(totals[6], times[6]); + #insert #run unroll_week("totals[%1] = add(totals[%1], times[%1]);"); } + + + + stop := current_time_monotonic(); + average: float64 = xx to_microseconds(stop-start); + x_average = (average + x_count * x_average) / (x_count + 1); + x_count += 1; + } + + print("Measured % us.\n", x_average); } // Resets the times of the provided task (and adjusts database totals). @@ -559,7 +606,7 @@ store_database :: (db: Database, path: string) -> success: bool { file_write(*file, DB_FILE_SIGN_STR); file_write(*file, *db, size_of(Database)); - file_write(*file, db.tasks.data, SIZE_OF_TASK * db.tasks.count); + file_write(*file, db.tasks.data, size_of(Task) * db.tasks.count); return true; } @@ -602,7 +649,7 @@ load_database :: (db: *Database, path: string) -> success: bool { array_reserve(*db.tasks, tasks_count); // Read database tasks. - file_read(file, db.tasks.data, SIZE_OF_TASK * tasks_count); + file_read(file, db.tasks.data, size_of(Task) * tasks_count); db.tasks.count = tasks_count; // Make sure we are reading all the file. @@ -1279,9 +1326,9 @@ main :: () { print("- All data files are stored in '%'.\n", app_directory); print(" If the home directory is undefined, './%' will be used.\n", APP_FOLDER_NAME); write_strings( - " The database entries are stored in binary format on the 'database.bin' file.\n", + " The database tasks are stored in binary format on the 'database.bin' file.\n", " The archived entries are stored in CSV format on the 'archive.csv' file.\n", - "- During intensive tasks such as saving to file or recalculating totals times,\n", + "- During intensive operations such as saving or recalculating totals times,\n", " a diamond symbol is shown on the top left corner.\n" ); exit(0); @@ -1435,7 +1482,7 @@ main :: () { case #char "n"; #through; case #char "N"; if is_database_full(db) { - read_enter_confirmation(selected_task_row, error_style, " Unable to create entry: database is full. "); + read_enter_confirmation(selected_task_row, error_style, " Unable to create task: database is full. "); continue; } @@ -1563,12 +1610,12 @@ main :: () { if selected_task == null continue; if is_database_full(db) { - read_enter_confirmation(selected_task_row, error_style, " Unable to duplicate entry: database is full. "); + read_enter_confirmation(selected_task_row, error_style, " Unable to duplicate task: database is full. "); continue; } if (add_task(db, selected_task) == null) { - print_error("Failed to duplicate entry."); + print_error("Failed to duplicate task."); continue; } trigger_autosave(); @@ -1612,7 +1659,7 @@ main :: () { if (db != *database || selected_task == null || selected_task == active_task) continue; if (append_to_csv(selected_task, ar_file_path) == false) { - print_error("Failed to archive entry."); + print_error("Failed to archive task."); continue; } delete_task(db, db.selected_idx); @@ -1624,12 +1671,12 @@ main :: () { if (db != *archive || selected_task == null) continue; if is_database_full(*database) { - read_enter_confirmation(selected_task_row, error_style, " Unable to restore entry: database is full. "); + read_enter_confirmation(selected_task_row, error_style, " Unable to restore task: database is full. "); continue; } if (add_task(*database, selected_task) == null) { - print_error("Failed to restore entry."); + print_error("Failed to restore task."); continue; } delete_task(db, db.selected_idx); @@ -1692,13 +1739,13 @@ main :: () { for db.tasks { if (append_to_csv(it, ar_file_path) == false) { - print_error("Failed to archive entry."); // TODO Improve this. + print_error("Failed to archive task."); // TODO Improve this. } reset_task_times(db, it_index); } trigger_autosave(); - // Coalesce similar entries. + // Coalesce similar tasks. case #char "c"; #through; case #char "C"; if (db.tasks.count <= 0) continue; -- cgit v1.2.3