diff options
| author | dam <dam@gudinoff> | 2023-07-09 20:47:14 +0100 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2023-07-09 20:47:14 +0100 |
| commit | deaa914add9200fe66c09ea930c367ed190a46aa (patch) | |
| tree | 6b97a9e1c4ac4644799fd1a0772cd8cf9268599d | |
| parent | c0727584e11ace38d50835b45d8a999c3568cf00 (diff) | |
| download | task-time-tracker-deaa914add9200fe66c09ea930c367ed190a46aa.tar.zst task-time-tracker-deaa914add9200fe66c09ea930c367ed190a46aa.zip | |
Now using saturating arithmetic lib. Fix jai breaking changes (no longer possible to use forward declared procedures).
| -rw-r--r-- | libncurses.so | bin | 165808 -> 165808 bytes | |||
| -rw-r--r-- | ttt.jai | 105 |
2 files changed, 54 insertions, 51 deletions
diff --git a/libncurses.so b/libncurses.so Binary files differindex 1e7352e..46767e4 100644 --- a/libncurses.so +++ b/libncurses.so @@ -26,6 +26,7 @@ #import "File_Utilities"; #import "String"; #import "curses"; +#load "Integer_Saturating_Arithmetic.jai"; VERSION :: "2.0"; // Use only 3 chars (to fit layouts). @@ -34,7 +35,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_v2"; // TODO Using _v2 to avoid erasing my work data. +APP_FOLDER_NAME :: ".task_time_tracker_test"; // TODO Using _v2 to avoid erasing my work data. DB_FILE_NAME :: "database.bin"; AR_FILE_NAME :: "archive.csv"; DB_FILE_SIGN_STR :: "TTT:B:02"; @@ -291,20 +292,6 @@ mvprintw_time :: (y: s32, x: s32, time: s64, space: s32) -> int { } } -add_int64 :: (x :s64, y: s64) -> s64 #dump { // TODO Comparing implementations. - return - ifx (y > 0 && x > S64_MAX - y) then S64_MAX else - ifx (y < 0 && x < S64_MIN - y) then S64_MIN else - x + y; -} - -sub_int64 :: (x :s64, y :s64) -> s64 { - return - ifx (y < 0 && x > S64_MAX + y) then S64_MAX else - ifx (y > 0 && x < S64_MIN + y) then S64_MIN else - x - y; -} - // Returns active task or NULL if none applies. get_active_task :: inline (db: Database) -> *Task { return ifx db.active_idx >= 0 then *db.tasks[db.active_idx] else null; @@ -324,7 +311,7 @@ add_task :: (db: *Database, task: Task = .{}) -> task: *Task, index: s64 { array_add(*db.tasks, task); for * db.total_times { - <<it = add_int64(<<it, task.times[it_index]); + <<it = add(<<it, task.times[it_index]); } idx := db.tasks.count-1; @@ -340,7 +327,7 @@ delete_task :: (using db: *Database, index: s64) -> bool { // TODO Maybe use `us // Remove task timer values from total timers. for tasks[index].times { - total_times[it_index] = sub_int64(total_times[it_index], it); + total_times[it_index] = sub(total_times[it_index], it); } // Move tasks after the index position to their new positions. @@ -360,15 +347,8 @@ delete_task :: (using db: *Database, index: s64) -> bool { // TODO Maybe use `us else if (active_idx == index) { active_idx = -1; } - - // If possible, shrink database capacity. - // TODO Do we really want to make this fuss? - current_capacity := tasks.allocated; - if (tasks.count < (current_capacity >> 2)) { - new_capacity := 1 << (get_msb(tasks.count) + 2); - my_array_reserve_nonpoly(xx *tasks, new_capacity, SIZE_OF_TASK); - } - + + // TODO Helper function. get_msb :: (value: s64) -> msb: s64, found: bool { result: s64 = ---; #asm { @@ -377,7 +357,7 @@ delete_task :: (using db: *Database, index: s64) -> bool { // TODO Maybe use `us } return result * xx cast(bool)value, xx value; // If value is zero: return `0, false`. } - + my_array_reserve_nonpoly :: (array: *[..] *void, desired_items: s64, size: s64) -> success: bool { if !array.allocator.proc remember_allocators(array); @@ -389,6 +369,14 @@ delete_task :: (using db: *Database, index: s64) -> bool { // TODO Maybe use `us return true; } + // If possible, shrink database capacity. + // TODO Do we really want to make this fuss? + current_capacity := tasks.allocated; + if (tasks.count < (current_capacity >> 2)) { + new_capacity := 1 << (get_msb(tasks.count) + 2); + my_array_reserve_nonpoly(xx *tasks, new_capacity, SIZE_OF_TASK); + } + return true; } @@ -476,13 +464,13 @@ update_total_times :: (db: *Database) { for db.tasks { // TODO Try to use local variables instead of total sub...something... the indexes thingy. times : []s64 = it.times; - totals[0] = add_int64(totals[0], times[0]); - totals[1] = add_int64(totals[1], times[1]); - totals[2] = add_int64(totals[2], times[2]); - totals[3] = add_int64(totals[3], times[3]); - totals[4] = add_int64(totals[4], times[4]); - totals[5] = add_int64(totals[5], times[5]); - totals[6] = add_int64(totals[6], times[6]); + 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]); } } @@ -495,7 +483,7 @@ reset_task_times :: (db: *Database, index: s64) { update_times(db); for * db.tasks[index].times { - db.total_times[it_index] = sub_int64(db.total_times[it_index], <<it); + db.total_times[it_index] = sub(db.total_times[it_index], <<it); <<it = 0; } } @@ -508,7 +496,7 @@ set_task_time :: (db: *Database, index: s64, day: int, time: s64) { // Make sure we sync before applying the changes. update_times(db); - db.total_times[day] = add_int64(db.total_times[day], time - db.tasks[index].times[day]); + db.total_times[day] = add(db.total_times[day], time - db.tasks[index].times[day]); db.tasks[index].times[day] = time; } @@ -520,8 +508,8 @@ add_task_time :: (db: *Database, index: s64, day: int, time: s64) { // Make sure we sync before applying the changes. update_times(db); - db.total_times[day] = add_int64(db.total_times[day], time); - db.tasks[index].times[day] = add_int64(db.tasks[index].times[day], time); + db.total_times[day] = add(db.total_times[day], time); + db.tasks[index].times[day] = add(db.tasks[index].times[day], time); } // Resets database to the initial state and deallocates all memory taken by tasks. @@ -660,6 +648,16 @@ import_from_csv :: (db: *Database, path: string) -> bool { print_error("Failed to read file '%' while loading database: ERROR_FROM_LOG", path); // TODO Get error from logger ?! return false; } + + + // TODO Helper function. + advance :: inline (array: *[] $T, amount: int = 1) { + assert(amount >= 0); + assert(array.count >= amount); + array.count -= amount; + array.data += amount; + } + // TODO Helper function. consume_next_line :: (sp: *string) -> string, bool { @@ -694,14 +692,6 @@ import_from_csv :: (db: *Database, path: string) -> bool { //Skip header line. consume_next_line(*csv); - // TODO Helper function. - advance :: inline (array: *[] $T, amount: int = 1) { - assert(amount >= 0); - assert(array.count >= amount); - array.count -= amount; - array.data += amount; - } - next_line :: inline (csv: *string) -> line: string, success: bool { for 0..csv.count { if csv.data[it] == #char "\n" { @@ -1041,7 +1031,7 @@ draw_tui :: (db: *Database, layout: *Layout) { day_idx := (it + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS; column_width = layout.columns[L_DAYS_IDX + day_idx].width; task_time := task.times[day_idx]; - total_time = add_int64(total_time, task_time); + total_time = add(total_time, task_time); mvprintw_time(xx y, xx x, task_time, xx column_width); x += column_width; } @@ -1085,7 +1075,7 @@ draw_tui :: (db: *Database, layout: *Layout) { } column_width = layout.columns[L_DAYS_IDX + idx].width; - total_time = add_int64(total_time, daily_total); + total_time = add(total_time, daily_total); mvprintw_time(xx y, xx x, daily_total, xx column_width); x += column_width; @@ -1216,6 +1206,19 @@ main :: () { is_exit_requested := false; for 1..args.count-1 { + + if is_equal_to_any(args[it], "--test", "-t") { + if (load_database(*database, db_file_path) == false) { + print_error("Failed to load database."); + exit(1); + } + start := current_time_monotonic(); + update_total_times(*database); + stop := current_time_monotonic(); + print("Took % ms to update total times for % entries.\n", to_seconds(stop-start), database.tasks.count); + exit(0); + } + if is_equal_to_any(args[it], "--help", "-h") { write_strings( "Usage: ttt [OPTION]... [FILE]...\n", @@ -1645,9 +1648,9 @@ main :: () { case #char "T"; compare_tasks :: (x: Task, y: Task) -> s64 { total_x, total_y: s64; - for x.times { total_x = add_int64(total_x, it); }; - for y.times { total_y = add_int64(total_y, it); }; - return sub_int64(total_x, total_y); + for x.times { total_x = add(total_x, it); }; + for y.times { total_y = add(total_y, it); }; + return sub(total_x, total_y); }; quick_sort(db.tasks, compare_tasks); |
