From b4d1a2205d40b301a4fc49cb80521789a4115e77 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Sun, 18 Sep 2016 11:30:19 -0400 Subject: [PATCH] cleared out old buffer code --- 4ed_app_target.cpp | 8 +- 4ed_file.cpp | 18 - buffer/4coder_external_name.h | 1 - buffer/4coder_golden_array.cpp | 235 ----- buffer/4coder_multi_gap_buffer.cpp | 706 ------------- buffer/4coder_rope_buffer.cpp | 1216 ---------------------- buffer/4coder_test_abstract.cpp | 184 ---- buffer/4coder_test_main.cpp | 1498 ---------------------------- buffer/history_to_replay.cpp | 316 ------ buffer/shared_test_config.cpp | 58 -- buffer/shared_test_utils.cpp | 154 --- 11 files changed, 6 insertions(+), 4388 deletions(-) delete mode 100644 buffer/4coder_external_name.h delete mode 100644 buffer/4coder_golden_array.cpp delete mode 100644 buffer/4coder_multi_gap_buffer.cpp delete mode 100644 buffer/4coder_rope_buffer.cpp delete mode 100644 buffer/4coder_test_abstract.cpp delete mode 100644 buffer/4coder_test_main.cpp delete mode 100644 buffer/history_to_replay.cpp delete mode 100644 buffer/shared_test_config.cpp delete mode 100644 buffer/shared_test_utils.cpp diff --git a/4ed_app_target.cpp b/4ed_app_target.cpp index 715b683c..322f082c 100644 --- a/4ed_app_target.cpp +++ b/4ed_app_target.cpp @@ -19,8 +19,6 @@ #include "4coder_custom.h" -#define BUFFER_EXPERIMENT_SCALPEL 1 - #include "4ed_math.h" #include "4ed_system.h" @@ -41,7 +39,13 @@ #include "4ed_style.h" #include "4ed_style.cpp" #include "4ed_command.cpp" + +#include "buffer/4coder_shared.cpp" +#include "buffer/4coder_gap_buffer.cpp" +#define Buffer_Type Gap_Buffer +#include "buffer/4coder_buffer_abstract.cpp" #include "4ed_file.cpp" + #include "4ed_gui.cpp" #include "4ed_layout.cpp" #include "4ed_app_models.h" diff --git a/4ed_file.cpp b/4ed_file.cpp index 71a93f69..449ce61c 100644 --- a/4ed_file.cpp +++ b/4ed_file.cpp @@ -9,24 +9,6 @@ // TOP -#include "buffer/4coder_shared.cpp" - -#if BUFFER_EXPERIMENT_SCALPEL == 0 -#include "buffer/4coder_golden_array.cpp" -#define Buffer_Type Buffer -#elif BUFFER_EXPERIMENT_SCALPEL == 1 -#include "buffer/4coder_gap_buffer.cpp" -#define Buffer_Type Gap_Buffer -#elif BUFFER_EXPERIMENT_SCALPEL == 2 -#include "buffer/4coder_multi_gap_buffer.cpp" -#define Buffer_Type Multi_Gap_Buffer -#else -#include "buffer/4coder_rope_buffer.cpp" -#define Buffer_Type Rope_Buffer -#endif - -#include "buffer/4coder_buffer_abstract.cpp" - enum Edit_Pos_Set_Type{ EditPos_None, EditPos_CursorSet, diff --git a/buffer/4coder_external_name.h b/buffer/4coder_external_name.h deleted file mode 100644 index afb40263..00000000 --- a/buffer/4coder_external_name.h +++ /dev/null @@ -1 +0,0 @@ -#define external_name "awebster_windows" diff --git a/buffer/4coder_golden_array.cpp b/buffer/4coder_golden_array.cpp deleted file mode 100644 index 16a2e43c..00000000 --- a/buffer/4coder_golden_array.cpp +++ /dev/null @@ -1,235 +0,0 @@ -/* - * Mr. 4th Dimention - Allen Webster - * Four Tech - * - * public domain -- no warranty is offered or implied; use this code at your own risk - * - * 16.10.2015 - * - * Buffer data object - * type - Golden Array - * - */ - -// TOP - -typedef struct Buffer{ - char *data; - int size, max; - - int *line_starts; - float *line_widths; - int line_count; - int widths_count; - int line_max; - int widths_max; -} Buffer; - -inline_4tech int -buffer_good(Buffer *buffer){ - int good; - good = (buffer->data != 0); - return(good); -} - -inline_4tech int -buffer_size(Buffer *buffer){ - return buffer->size; -} - -typedef struct Buffer_Init{ - Buffer *buffer; - char *data; - int size; -} Buffer_Init; - -internal_4tech Buffer_Init -buffer_begin_init(Buffer *buffer, char *data, int size){ - Buffer_Init init; - init.buffer = buffer; - init.data = data; - init.size = size; - return(init); -} - -inline_4tech int -buffer_init_need_more(Buffer_Init *init){ - int result; - result = 1; - if (init->buffer->data) result = 0; - return(result); -} - -inline_4tech int -buffer_init_page_size(Buffer_Init *init){ - int result; - result = init->size * 2; - return(result); -} - -inline_4tech void -buffer_init_provide_page(Buffer_Init *init, void *page, int page_size){ - Buffer *buffer; - buffer = init->buffer; - buffer->data = (char*)page; - buffer->max = page_size; -} - -internal_4tech int -buffer_end_init(Buffer_Init *init, void *scratch, int scratch_size){ - Buffer *buffer; - int result; - - result = 0; - buffer = init->buffer; - if (buffer->data){ - if (buffer->max >= init->size){ - buffer->size = eol_convert_in(buffer->data, init->data, init->size); - result = 1; - } - } - - return(result); -} - -typedef struct Buffer_Stringify_Loop{ - Buffer *buffer; - char *data, *end; - int absolute_pos; - int size; -} Buffer_Stringify_Loop; - -inline_4tech Buffer_Stringify_Loop -buffer_stringify_loop(Buffer *buffer, int start, int end){ - Buffer_Stringify_Loop result; - if (0 <= start && start < end && end <= buffer->size){ - result.buffer = buffer; - result.absolute_pos = start; - result.data = buffer->data + start; - result.size = end - start; - result.end = buffer->data + end; - } - else result.buffer = 0; - return(result); -} - -inline_4tech int -buffer_stringify_good(Buffer_Stringify_Loop *loop){ - int result; - result = (loop->buffer != 0); - return(result); -} - -inline_4tech void -buffer_stringify_next(Buffer_Stringify_Loop *loop){ - loop->buffer = 0; -} - -typedef struct Buffer_Backify_Loop{ - Buffer *buffer; - char *data, *end; - int absolute_pos; - int size; -} Buffer_Backify_Loop; - -inline_4tech Buffer_Backify_Loop -buffer_backify_loop(Buffer *buffer, int start, int end){ - Buffer_Backify_Loop result; - - ++start; - if (0 <= end && end < start && start <= buffer->size){ - result.buffer = buffer; - result.end = buffer->data + end; - result.size = start - end; - result.absolute_pos = start - result.size; - result.data = buffer->data + result.absolute_pos; - } - else result.buffer = 0; - return(result); -} - -inline_4tech int -buffer_backify_good(Buffer_Backify_Loop *loop){ - int result; - result = (loop->buffer != 0); - return(result); -} - -inline_4tech void -buffer_backify_next(Buffer_Backify_Loop *loop){ - loop->buffer = 0; -} - -internal_4tech int -buffer_replace_range(Buffer *buffer, int start, int end, char *str, int len, int *shift_amount, - void *scratch, int scratch_size, int *request_amount){ - - char *data; - int result; - int size; - - size = buffer_size(buffer); - assert_4tech(0 <= start); - assert_4tech(start <= end); - assert_4tech(end <= size); - - *shift_amount = (len - (end - start)); - if (*shift_amount + size <= buffer->max){ - data = (char*)buffer->data; - memmove_4tech(data + end + *shift_amount, data + end, buffer->size - end); - buffer->size += *shift_amount; - if (len != 0) memcpy_4tech(data + start, str, len); - - result = 0; - } - else{ - *request_amount = round_up_4tech(2*(*shift_amount + size), 4 << 10); - result = 1; - } - - return(result); -} - -internal_4tech int -buffer_batch_edit_step(Buffer_Batch_State *state, Buffer *buffer, Buffer_Edit *sorted_edits, - char *strings, int edit_count, void *scratch, int scratch_size, int *request_amount){ - Buffer_Edit *edit; - int i, result; - int shift_total, shift_amount; - - result = 0; - shift_total = state->shift_total; - i = state->i; - - edit = sorted_edits + i; - for (; i < edit_count; ++i, ++edit){ - assert(edit->end + shift_total <= buffer_size(buffer)); - result = buffer_replace_range(buffer, edit->start + shift_total, edit->end + shift_total, - strings + edit->str_start, edit->len, &shift_amount, - scratch, scratch_size, request_amount); - if (result) break; - shift_total += shift_amount; - } - - state->shift_total = shift_total; - state->i = i; - - return(result); -} - -internal_4tech void* -buffer_edit_provide_memory(Buffer *buffer, void *new_data, int new_max){ - void *result; - - assert_4tech(new_max >= buffer->size); - - result = buffer->data; - memcpy_4tech(new_data, buffer->data, buffer->size); - buffer->data = (char*)new_data; - buffer->max = new_max; - - return(result); -} - -// BOTTOM - diff --git a/buffer/4coder_multi_gap_buffer.cpp b/buffer/4coder_multi_gap_buffer.cpp deleted file mode 100644 index 3b385d6b..00000000 --- a/buffer/4coder_multi_gap_buffer.cpp +++ /dev/null @@ -1,706 +0,0 @@ -/* - * Mr. 4th Dimention - Allen Webster - * Four Tech - * - * public domain -- no warranty is offered or implied; use this code at your own risk - * - * 30.10.2015 - * - * Buffer data object - * type - Multi Gap Buffer - * - * This scheme was originally introduced to me by Martin Cohen, - * who calls it a "Fixed Width Gap Buffer". - * - */ - -// TOP - -typedef struct Fixed_Width_Gap_Buffer{ - char *data; - int size1, gap_size, size2; - int start_pos; -} Fixed_Width_Gap_Buffer; - -#define fixed_width_buffer_size (8 << 10) -#define fixed_width_buffer_half_size (4 << 10) - -typedef struct Multi_Gap_Buffer{ - Fixed_Width_Gap_Buffer *gaps; - int chunk_count; - int chunk_alloced; - int chunk_max; - int size; - - float *line_widths; - int *line_starts; - int line_count; - int widths_count; - int line_max; - int widths_max; - - int grow_gaps; - int edit_stage; -} Multi_Gap_Buffer; - -inline_4tech int -buffer_good(Multi_Gap_Buffer *buffer){ - int good; - good = (buffer->gaps != 0); - return(good); -} - -inline_4tech int -buffer_size(Multi_Gap_Buffer *buffer){ - int size; - size = buffer->size; - return(size); -} - -typedef struct Multi_Gap_Buffer_Init{ - Multi_Gap_Buffer *buffer; - char *data; - int size; - int chunk_i; - int chunk_count; - int chunk_alloc; -} Multi_Gap_Buffer_Init; - -internal_4tech Multi_Gap_Buffer_Init -buffer_begin_init(Multi_Gap_Buffer *buffer, char *data, int size){ - Multi_Gap_Buffer_Init init; - init.buffer = buffer; - init.data = data; - init.size = size; - init.chunk_i = 0; - init.chunk_alloc = div_ceil_4tech(size, fixed_width_buffer_half_size); - init.chunk_count = init.chunk_alloc; - if (init.chunk_alloc < 4) init.chunk_alloc = 4; - return(init); -} - -inline_4tech int -buffer_init_need_more(Multi_Gap_Buffer_Init *init){ - int result; - result = 1; - if (init->buffer->gaps && init->chunk_i == init->chunk_alloc) - result = 0; - return(result); -} - -inline_4tech int -buffer_init_page_size(Multi_Gap_Buffer_Init *init){ - Multi_Gap_Buffer *buffer; - int result; - buffer = init->buffer; - if (buffer->gaps) result = fixed_width_buffer_size; - else result = init->chunk_alloc * 2 * sizeof(*buffer->gaps); - return(result); -} - -internal_4tech void -buffer_init_provide_page(Multi_Gap_Buffer_Init *init, void *page, int page_size){ - Multi_Gap_Buffer *buffer; - buffer = init->buffer; - - if (buffer->gaps){ - assert_4tech(page_size >= fixed_width_buffer_size); - buffer->gaps[init->chunk_i].data = (char*)page; - ++init->chunk_i; - } - else{ - buffer->gaps = (Fixed_Width_Gap_Buffer*)page; - buffer->chunk_max = page_size / sizeof(*buffer->gaps); - } -} - -internal_4tech int -buffer_end_init(Multi_Gap_Buffer_Init *init, void *scratch, int scratch_size){ - Multi_Gap_Buffer *buffer; - Fixed_Width_Gap_Buffer *gap; - int result; - int i, count; - char *data; - int pos, size, total_size, start_pos; - int osize1, size1, size2; - - result = 0; - buffer = init->buffer; - if (buffer->gaps){ - if (buffer->chunk_max >= div_ceil_4tech(init->size, fixed_width_buffer_half_size)){ - buffer->chunk_count = init->chunk_count; - if (buffer->chunk_count == 0) buffer->chunk_count = 1; - buffer->chunk_alloced = init->chunk_alloc; - result = 1; - - data = init->data; - total_size = init->size; - gap = buffer->gaps; - count = buffer->chunk_count; - size = fixed_width_buffer_half_size; - pos = 0; - start_pos = 0; - - for (i = 0; i < count; ++i, ++gap, pos += size){ - assert_4tech(size == fixed_width_buffer_half_size); - if (pos + size > total_size) size = total_size - pos; - - if (gap->data){ - size2 = size >> 1; - size1 = osize1 = size - size2; - - if (size1 > 0){ - size1 = eol_convert_in(gap->data, data + pos, size1); - if (size2 > 0){ - size2 = eol_convert_in(gap->data + size1, data + pos + osize1, size2); - } - } - - gap->size1 = size1; - gap->size2 = size2; - gap->gap_size = fixed_width_buffer_size - size1 - size2; - memmove_4tech(gap->data + size1 + gap->gap_size, gap->data + size1, size2); - - gap->start_pos = start_pos; - start_pos += size1 + size2; - } - else{ - result = 0; - break; - } - } - buffer->size = start_pos; - } - } - - return(result); -} - -internal_4tech int -buffer_find_chunk(Multi_Gap_Buffer *buffer, int pos){ - Fixed_Width_Gap_Buffer *gaps; - int start, end, m, this_pos; - - gaps = buffer->gaps; - start = 0; - end = buffer->chunk_count; - for(;;){ - m = (start + end) / 2; - this_pos = gaps[m].start_pos; - if (this_pos < pos) start = m; - else if (this_pos > pos) end = m; - else{ - --m; - if (m < 0) m = 0; - break; - } - if (start+1 == end){ - m = start; break; - } - assert_4tech(start < end); - } - - return(m); -} - -typedef struct Multi_Gap_Buffer_Stringify_Loop{ - Multi_Gap_Buffer *buffer; - Fixed_Width_Gap_Buffer *gaps; - char *data; - int absolute_pos; - int size; - int chunk_i; - int chunk_end; - int pos, end; -} Multi_Gap_Buffer_Stringify_Loop; - -internal_4tech Multi_Gap_Buffer_Stringify_Loop -buffer_stringify_loop(Multi_Gap_Buffer *buffer, int start, int end){ - Multi_Gap_Buffer_Stringify_Loop result; - Fixed_Width_Gap_Buffer *gap; - int temp_end; - - if (0 <= start && start < end && end <= buffer->size){ - result.buffer = buffer; - result.gaps = buffer->gaps; - result.absolute_pos = start; - - result.chunk_i = buffer_find_chunk(buffer, start); - result.chunk_end = buffer_find_chunk(buffer, end); - - gap = result.gaps + result.chunk_end; - end -= gap->start_pos; - if (end <= gap->size1) result.end = end; - else result.end = end + gap->gap_size; - - gap = result.gaps + result.chunk_i; - start -= gap->start_pos; - if (start < gap->size1){ - result.pos = start; - temp_end = gap->size1; - } - else{ - result.pos = start + gap->gap_size; - temp_end = fixed_width_buffer_size; - } - - if (result.chunk_i == result.chunk_end && temp_end > result.end) temp_end = result.end; - result.size = temp_end - result.pos; - result.data = gap->data + result.pos; - } - else result.buffer = 0; - return(result); -} - -inline_4tech int -buffer_stringify_good(Multi_Gap_Buffer_Stringify_Loop *loop){ - int result; - result = (loop->buffer != 0); - return(result); -} - -internal_4tech void -buffer_stringify_next(Multi_Gap_Buffer_Stringify_Loop *loop){ - Fixed_Width_Gap_Buffer *gap; - int temp_end; - - gap = loop->gaps + loop->chunk_i; - if (loop->chunk_i == loop->chunk_end && loop->pos + loop->size == loop->end){ - loop->buffer = 0; - } - else{ - if (loop->pos < gap->size1){ - loop->pos = gap->size1 + gap->gap_size; - loop->absolute_pos = gap->start_pos + gap->size1; - temp_end = fixed_width_buffer_size; - } - else{ - ++loop->chunk_i; - ++gap; - loop->pos = 0; - loop->absolute_pos = gap->start_pos; - temp_end = gap->size1; - if (gap->size1 == 0){ - loop->pos = gap->gap_size; - temp_end = fixed_width_buffer_size; - } - } - if (loop->chunk_i == loop->chunk_end && temp_end > loop->end) temp_end = loop->end; - loop->size = temp_end - loop->pos; - loop->data = gap->data + loop->pos; - } -} - -typedef struct Multi_Gap_Buffer_Backify_Loop{ - Multi_Gap_Buffer *buffer; - Fixed_Width_Gap_Buffer *gaps; - char *data; - int absolute_pos; - int size; - int chunk_i; - int chunk_end; - int pos, end; -} Multi_Gap_Buffer_Backify_Loop; - -internal_4tech Multi_Gap_Buffer_Backify_Loop -buffer_backify_loop(Multi_Gap_Buffer *buffer, int start, int end){ - Multi_Gap_Buffer_Backify_Loop result; - Fixed_Width_Gap_Buffer *gap; - int temp_end, temp_start; - - ++start; - if (0 <= end && end < start && start <= buffer->size){ - result.buffer = buffer; - result.gaps = buffer->gaps; - - result.chunk_i = buffer_find_chunk(buffer, start); - result.chunk_end = buffer_find_chunk(buffer, end); - - gap = result.gaps + result.chunk_end; - end -= gap->start_pos; - if (end < gap->size1) result.end = end; - else result.end = end + gap->gap_size; - - gap = result.gaps + result.chunk_i; - start -= gap->start_pos; - if (start < gap->size1){ - temp_end = start; - temp_start = 0; - } - else{ - temp_end = start + gap->gap_size; - temp_start = gap->size1 + gap->gap_size; - } - - if (result.chunk_i == result.chunk_end && temp_start < result.end) temp_start = result.end; - result.pos = temp_start; - result.absolute_pos = temp_start + gap->start_pos; - if (temp_start >= gap->size1) result.absolute_pos -= gap->gap_size; - result.size = temp_end - temp_start; - result.data = gap->data + result.pos; - } - else result.buffer = 0; - return(result); -} - -inline_4tech int -buffer_backify_good(Multi_Gap_Buffer_Backify_Loop *loop){ - int result; - result = (loop->buffer != 0); - return(result); -} - -internal_4tech void -buffer_backify_next(Multi_Gap_Buffer_Backify_Loop *loop){ - Fixed_Width_Gap_Buffer *gap; - int temp_end, temp_start; - - gap = loop->gaps + loop->chunk_i; - if (loop->chunk_i == loop->chunk_end && loop->pos == loop->end){ - loop->buffer = 0; - } - else{ - if (loop->pos < gap->size1){ - --gap; - --loop->chunk_i; - temp_start = gap->size1 + gap->gap_size; - temp_end = fixed_width_buffer_size; - } - else{ - temp_start = 0; - temp_end = gap->size1; - } - if (loop->chunk_i == loop->chunk_end && temp_start < loop->end) temp_start = loop->end; - loop->absolute_pos = temp_start + gap->start_pos; - if (temp_start >= gap->size1) loop->absolute_pos -= gap->gap_size; - loop->pos = temp_start; - loop->size = temp_end - temp_start; - loop->data = gap->data + loop->pos; - } -} - -internal_4tech int -buffer_replace_range(Multi_Gap_Buffer *buffer, int start, int end, char *str, int len, int *shift_amount_out, - void *scratch, int scratch_size, int *request_amount){ - Fixed_Width_Gap_Buffer *gaps, *gap, *dgap; - char *data; - int move_size; - int gap_start, gap_end; - int result; - int size; - int local_end; - int shift_amount; - int required_empty_buffers; - int supplanted_gaps; - int dpos; - int head_size, middle_size, tail_size; - int mem_pos, local_start_pos; - debug_4tech(int osize); - - size = buffer_size(buffer); - assert_4tech(0 <= start); - assert_4tech(start <= end); - assert_4tech(end <= size); - - *shift_amount_out = (len - (end - start)); - - gaps = buffer->gaps; - gap_start = buffer_find_chunk(buffer, start); - gap_end = buffer_find_chunk(buffer, end); - - if (buffer->edit_stage == 0){ - buffer->size += *shift_amount_out; - for (gap = gaps + gap_end + 1; - gap < gaps + buffer->chunk_count; - ++gap){ - gap->start_pos += *shift_amount_out; - } - buffer->edit_stage = 1; - } - - gap = gaps + gap_start; - if (buffer->edit_stage == 1){ - if (gap_start < gap_end && gap_start+1 < buffer->chunk_count){ - supplanted_gaps = gap_end - gap_start - 1; - if (buffer->chunk_max - buffer->chunk_alloced >= supplanted_gaps){ - ++gap; - memcpy_4tech(gaps + buffer->chunk_alloced, gap, sizeof(*gaps)*supplanted_gaps); - memmove_4tech(gap, gaps + gap_end, sizeof(*gaps)*(buffer->chunk_alloced - gap_start - 1)); - buffer->chunk_count -= (gap_end - gap_start - 1); - - local_end = end - gap->start_pos; - - if (local_end >= gap->size1){ - gap->size2 -= (local_end - gap->size1); - gap->size1 = 0; - } - else{ - memmove_4tech(gap->data, gap->data + local_end, gap->size1 - local_end); - gap->size1 -= local_end; - } - gap->gap_size = fixed_width_buffer_size - gap->size2 - gap->size1; - - --gap; - } - else{ - buffer->grow_gaps = 1; - *request_amount = round_up_4tech(sizeof(*gaps)*(supplanted_gaps+buffer->chunk_max*2), 4<<10); - result = 1; - goto mugab_replace_range_end; - } - } - buffer->edit_stage = 2; - } - - start -= gap->start_pos; - end -= gap->start_pos; - assert_4tech(start >= 0 && end >= 0); - if (end > gap->size1 + gap->size2) end = gap->size1 + gap->size2; - shift_amount = (len - (end - start)); - - if (shift_amount + gap->size1 + gap->size2 <= fixed_width_buffer_size){ - data = gap->data; - debug_4tech(osize = gap->size1 + gap->size2); - if (end < gap->size1){ - move_size = gap->size1 - end; - memmove_4tech(data + gap->size1 + gap->gap_size - move_size, data + end, move_size); - gap->size1 -= move_size; - gap->size2 += move_size; - } - if (start > gap->size1){ - move_size = start - gap->size1; - memmove_4tech(data + gap->size1, data + gap->size1 + gap->gap_size, move_size); - gap->size1 += move_size; - gap->size2 -= move_size; - } - - memcpy_4tech(data + start, str, len); - gap->size2 = fixed_width_buffer_size - (end + gap->gap_size); - gap->size1 = start + len; - gap->gap_size -= shift_amount; - - assert_4tech(gap->size1 + gap->size2 == osize + shift_amount); - assert_4tech(gap->size1 + gap->gap_size + gap->size2 == fixed_width_buffer_size); - - result = 0; - buffer->edit_stage = 0; - - if (gap_start < gap_end){ - ++gap; - gap->start_pos += shift_amount; - } - } - else{ - required_empty_buffers = div_ceil_4tech(shift_amount, fixed_width_buffer_half_size); - if (buffer->chunk_alloced - buffer->chunk_count >= required_empty_buffers){ - if (buffer->chunk_max - buffer->chunk_alloced >= required_empty_buffers){ - memcpy_4tech(gaps + buffer->chunk_alloced, gaps + buffer->chunk_count, sizeof(*gaps)*required_empty_buffers); - memmove_4tech(gap + required_empty_buffers + 1, gap + 1, sizeof(*gaps)*(buffer->chunk_count - gap_start - 1)); - memcpy_4tech(gap + 1, gaps + buffer->chunk_alloced, sizeof(*gaps)*required_empty_buffers); - - data = gap->data; - if (end < gap->size1){ - move_size = gap->size1 - end; - memmove_4tech(data + gap->size1 + gap->gap_size - move_size, data + end, move_size); - gap->size1 -= move_size + (end - start); - gap->size2 += move_size; - } - else if (start > gap->size1){ - move_size = start - gap->size1; - memmove_4tech(data + gap->size1, data + gap->size1 + gap->gap_size, move_size); - gap->size1 += move_size; - gap->size2 -= move_size + (end - start); - } - else{ - if (end > gap->size1){ - gap->size2 -= (end - gap->size1); - } - gap->size1 = start; - } - - dgap = gap + required_empty_buffers; - dpos = gap->size1 + gap->gap_size; - memcpy_4tech(dgap->data + dpos, data + dpos, gap->size2); - dgap->size2 = gap->size2; - gap->size2 = 0; - - tail_size = fixed_width_buffer_half_size - dgap->size2; - if (tail_size < 0) tail_size = 0; - - if (tail_size < len){ - middle_size = div_ceil_4tech(len - tail_size, (required_empty_buffers * 2)); - - head_size = middle_size; - - if (head_size + gap->size1 + 256 > fixed_width_buffer_size){ - head_size = fixed_width_buffer_size - gap->size1 - 256; - if (head_size < 0) head_size = 0; - } - - if (required_empty_buffers-1 > 0){ - middle_size = div_ceil_4tech(len - head_size - tail_size, (required_empty_buffers-1)*2); - } - else{ - middle_size = 0; - head_size = len - tail_size; - assert_4tech(head_size + gap->size1 <= fixed_width_buffer_size); - } - } - else{ - middle_size = 0; - head_size = 0; - } - - mem_pos = 0; - if (head_size > len - mem_pos) head_size = len - mem_pos; - - gap->size2 = head_size; - gap->gap_size = fixed_width_buffer_size - gap->size1 - gap->size2; - memcpy_4tech(gap->data + fixed_width_buffer_size - head_size, str + mem_pos, head_size); - mem_pos += head_size; - local_start_pos = gap->start_pos + gap->size1 + gap->size2; - - ++gap; - for (;gap < dgap; ++gap){ - gap->start_pos = local_start_pos; - - if (middle_size > len - mem_pos) middle_size = len - mem_pos; - gap->size1 = middle_size; - memcpy_4tech(gap->data, str + mem_pos, middle_size); - mem_pos += middle_size; - - if (middle_size > len - mem_pos) middle_size = len - mem_pos; - gap->size2 = middle_size; - memcpy_4tech(gap->data + fixed_width_buffer_size - middle_size, str + mem_pos, middle_size); - mem_pos += middle_size; - - gap->gap_size = fixed_width_buffer_size - (gap->size1 + gap->size2); - local_start_pos += gap->size1 + gap->size2; - } - - if (tail_size > len - mem_pos) tail_size = len - mem_pos; - gap->start_pos = local_start_pos; - gap->size1 = tail_size; - gap->gap_size = fixed_width_buffer_size - gap->size1 - gap->size2; - memcpy_4tech(gap->data, str + mem_pos, tail_size); - mem_pos += tail_size; - assert_4tech(mem_pos == len); - debug_4tech(local_start_pos += gap->size1 + gap->size2); - //assert_4tech(local_start_pos == buffer->size); - - buffer->chunk_count += required_empty_buffers; - - result = 0; - buffer->edit_stage = 0; - } - else{ - buffer->grow_gaps = 1; - *request_amount = round_up_4tech(sizeof(*gaps)*(required_empty_buffers+buffer->chunk_max*2), 4<<10); - result = 1; - goto mugab_replace_range_end; - } - } - else{ - if (buffer->chunk_alloced < buffer->chunk_max){ - *request_amount = fixed_width_buffer_size; - result = 1; - } - else{ - buffer->grow_gaps = 1; - *request_amount = round_up_4tech(sizeof(*gaps)*(1+buffer->chunk_max*2), 4<<10); - result = 1; - } - } - } - -mugab_replace_range_end: - return(result); -} - -internal_4tech int -buffer_mugab_check(Multi_Gap_Buffer *buffer){ - Fixed_Width_Gap_Buffer *gap; - int result; - int total_size; - int i, count; - - assert_4tech(buffer->chunk_count <= buffer->chunk_alloced); - assert_4tech(buffer->chunk_alloced <= buffer->chunk_max); - - count = buffer->chunk_count; - - total_size = 0; - gap = buffer->gaps; - for (i = 0; i < count; ++i, ++gap){ - assert_4tech(gap->size1 + gap->size2 + gap->gap_size == fixed_width_buffer_size); - assert_4tech(gap->start_pos == total_size); - total_size += gap->size1 + gap->size2; - } - assert_4tech(total_size == buffer->size); - - assert_4tech(buffer->edit_stage == 0); - assert_4tech(buffer->grow_gaps == 0); - - result = 1; - return(result); -} - -// NOTE(allen): This could should be optimized for Multi_Gap_Buffer -internal_4tech int -buffer_batch_edit_step(Buffer_Batch_State *state, Multi_Gap_Buffer *buffer, Buffer_Edit *sorted_edits, - char *strings, int edit_count, void *scratch, int scratch_size, int *request_amount){ - Buffer_Edit *edit; - int i, result; - int shift_total, shift_amount; - - result = 0; - shift_total = state->shift_total; - i = state->i; - - edit = sorted_edits + i; - for (; i < edit_count; ++i, ++edit){ - result = buffer_replace_range(buffer, edit->start + shift_total, edit->end + shift_total, - strings + edit->str_start, edit->len, &shift_amount, - scratch, scratch_size, request_amount); - if (result) break; - buffer_mugab_check(buffer); - shift_total += shift_amount; - } - - state->shift_total = shift_total; - state->i = i; - - return(result); -} - -internal_4tech void* -buffer_edit_provide_memory(Multi_Gap_Buffer *buffer, void *new_data, int size){ - void *result; - Fixed_Width_Gap_Buffer *gap; - - if (buffer->grow_gaps){ - assert_4tech(size >= buffer->chunk_max*sizeof(*buffer->gaps)); - - result = buffer->gaps; - memcpy_4tech(new_data, buffer->gaps, buffer->chunk_alloced*sizeof(*buffer->gaps)); - buffer->gaps = (Fixed_Width_Gap_Buffer*)new_data; - buffer->chunk_max = size / sizeof(*buffer->gaps); - buffer->grow_gaps = 0; - } - - else{ - assert_4tech(buffer->chunk_max > buffer->chunk_alloced); - assert_4tech(size >= fixed_width_buffer_size); - - gap = &buffer->gaps[buffer->chunk_alloced++]; - memzero_4tech(*gap); - gap->data = (char*)new_data; - result = 0; - } - - return(result); -} - -// BOTTOM - - diff --git a/buffer/4coder_rope_buffer.cpp b/buffer/4coder_rope_buffer.cpp deleted file mode 100644 index afac0e52..00000000 --- a/buffer/4coder_rope_buffer.cpp +++ /dev/null @@ -1,1216 +0,0 @@ -/* - * Mr. 4th Dimention - Allen Webster - * Four Tech - * - * public domain -- no warranty is offered or implied; use this code at your own risk - * - * 03.11.2015 - * - * Buffer data object - * type - Rope - * - */ - -// TOP - -typedef struct Rope_Node{ - int left, right, parent; - int left_weight, weight; - int str_start; -} Rope_Node; - -typedef struct Rope_String{ - int next_free; -} Rope_String; - -#define rope_string_full_size 256 -#define rope_string_width (rope_string_full_size-sizeof(Rope_String)) - -typedef struct Rope_Buffer_Edit_State{ - int left, right, throw_away, middle; -} Rope_Buffer_Edit_State; - -typedef struct Rope_Buffer{ - void *data; - int free_rope_string; - int string_count; - - Rope_Node *nodes; - int free_rope_node; - int node_count; - - float *line_widths; - int *line_starts; - int line_count; - int widths_count; - int line_max; - int widths_max; - - int grow_string_memory; - int edit_stage; - Rope_Buffer_Edit_State edit_state; -} Rope_Buffer; - -inline_4tech int -buffer_good(Rope_Buffer *buffer){ - int good; - good = (buffer->data != 0); - return(good); -} - -inline_4tech int -buffer_size(Rope_Buffer *buffer){ - int size; - size = buffer->nodes->left_weight; - return(size); -} - -typedef struct{ - Rope_Buffer *buffer; - char *data; - int size; - - int rope_string_count; - int node_count; -} Rope_Buffer_Init; - -internal_4tech Rope_Buffer_Init -buffer_begin_init(Rope_Buffer *buffer, char *data, int size){ - Rope_Buffer_Init init; - - init.buffer = buffer; - init.data = data; - init.size = eol_in_place_convert_in(data, size); - - init.node_count = div_ceil_4tech(size, rope_string_width); - - if (init.node_count < 4){ - init.node_count = 7; - init.rope_string_count = 4; - } - else{ - init.rope_string_count = round_pot_4tech(init.node_count); - init.node_count = init.rope_string_count*2; - } - - return(init); -} - -internal_4tech int -buffer_init_need_more(Rope_Buffer_Init *init){ - Rope_Buffer *buffer; - int result; - buffer = init->buffer; - result = 1; - if (buffer->data != 0 && buffer->nodes != 0) - result = 0; - return(result); -} - -inline_4tech int -buffer_init_page_size(Rope_Buffer_Init *init){ - Rope_Buffer *buffer; - int result; - buffer = init->buffer; - if (buffer->data) result = init->node_count*sizeof(Rope_Node); - else result = init->rope_string_count*rope_string_full_size; - return(result); -} - -internal_4tech void -buffer_init_provide_page(Rope_Buffer_Init *init, void *page, int page_size){ - Rope_Buffer *buffer; - buffer = init->buffer; - - if (buffer->data){ - assert_4tech(buffer->nodes == 0); - assert_4tech(page_size >= init->node_count*sizeof(Rope_Node)); - buffer->nodes = (Rope_Node*)page; - init->node_count = page_size / sizeof(Rope_Node); - } - else{ - assert_4tech(page_size >= init->rope_string_count*rope_string_full_size); - buffer->data = page; - init->rope_string_count = page_size / rope_string_full_size; - } -} - -internal_4tech int -buffer_alloc_rope_string(Rope_Buffer *buffer, int *result){ - Rope_String *rope_string; - int success; - - success = 0; - if (buffer->free_rope_string >= 0){ - success = 1; - *result = buffer->free_rope_string; - rope_string = (Rope_String*)((char*)buffer->data + *result); - buffer->free_rope_string = rope_string->next_free; - *result += sizeof(Rope_String); - } - - return(success); -} - -internal_4tech void -buffer_free_rope_string(Rope_Buffer *buffer, int str_start){ - Rope_String *rope_string; - - str_start -= sizeof(Rope_String); - rope_string = (Rope_String*)((char*)buffer->data + str_start); - rope_string->next_free = buffer->free_rope_string; - buffer->free_rope_string = str_start; -} - -internal_4tech int -buffer_alloc_rope_node(Rope_Buffer *buffer, int *result){ - Rope_Node *node; - int success; - - success = 0; - if (buffer->free_rope_node > 0){ - success = 1; - *result = buffer->free_rope_node; - node = buffer->nodes + *result; - buffer->free_rope_node = node->parent; - } - - return(success); -} - -internal_4tech void -buffer_free_rope_node(Rope_Buffer *buffer, int node_index){ - Rope_Node *node; - - node = buffer->nodes + node_index; - node->parent = buffer->free_rope_node; - buffer->free_rope_node = node_index; -} - -inline_4tech void -buffer_node_check(Rope_Buffer *buffer, int node_index){ - Rope_Node *nodes, *node; - nodes = buffer->nodes; - node = nodes + node_index; - assert_4tech(node->left == 0 || nodes[node->left].parent == node_index); - assert_4tech(node->right == 0 || nodes[node->right].parent == node_index); - assert_4tech((node->left == 0 && node->right == 0) || (node->left != 0 && node->right != 0)); -} - -internal_4tech void -buffer_cheap_check(Rope_Buffer *buffer){ - Rope_Node *nodes; - nodes = buffer->nodes; - - assert_4tech(nodes->parent == 0); - assert_4tech(nodes->right == 0); - assert_4tech(nodes->left != 0); - assert_4tech(nodes->weight == nodes->left_weight); -} - -internal_4tech void -buffer_rope_check(Rope_Buffer *buffer, void *scratch, int scratch_size){ - int *int_stack; - Rope_Node *node, *l, *r, *nodes; - int top, max_stack; - int node_index; - - nodes = buffer->nodes; - int_stack = (int*)scratch; - top = 0; - max_stack = scratch_size / sizeof(int); - buffer_cheap_check(buffer); - - int_stack[top++] = nodes->left; - for (;top > 0;){ - node_index = int_stack[--top]; - node = nodes + node_index; - if (node->left || node->right){ - assert_4tech(node->left && node->right); - - int_stack[top++] = node->left; - int_stack[top++] = node->right; - - l = nodes + node->left; - assert_4tech(l->parent == node_index); - - r = nodes + node->right; - assert_4tech(r->parent == node_index); - - assert_4tech(l->weight + r->weight == node->weight); - assert_4tech(l->weight == node->left_weight); - } - } -} - -typedef struct Rope_Construct_Stage{ - int parent_index; - int is_right_side; - int weight; -} Rope_Construct_Stage; - -inline_4tech Rope_Construct_Stage -buffer_construct_stage(int parent, int right, int weight){ - Rope_Construct_Stage result; - result.parent_index = parent; - result.is_right_side = right; - result.weight = weight; - return(result); -} - -internal_4tech void -buffer_free_tree(Rope_Buffer *buffer, int node_index, void *scratch, int scratch_size){ - Rope_Node *nodes, *node; - int *int_stack; - int top, stack_max; - - int_stack = (int*)scratch; - stack_max = scratch_size / sizeof(int); - top = 0; - - nodes = buffer->nodes; - int_stack[top++] = node_index; - - for (;top > 0;){ - node_index = int_stack[--top]; - node = nodes + node_index; - assert_4tech(top < stack_max); - if (node->left) int_stack[top++] = node->left; - assert_4tech(top < stack_max); - if (node->right) int_stack[top++] = node->right; - if (node->str_start) buffer_free_rope_string(buffer, node->str_start); - buffer_free_rope_node(buffer, node_index); - } -} - -internal_4tech int -buffer_build_tree(Rope_Buffer *buffer, char *str, int len, int root, - void *scratch, int scratch_size, int *request_amount){ - Rope_Construct_Stage *stack, *stage; - Rope_Node *node, *nodes, *parent; - char *dest; - int top, stack_max; - int result; - int node_index; - int is_right_side; - int read_pos; - - nodes = buffer->nodes; - stack = (Rope_Construct_Stage*)scratch; - stack_max = scratch_size / sizeof(Rope_Construct_Stage); - top = 0; - result = 1; - read_pos = 0; - - stack[top++] = buffer_construct_stage(root, 0, len); - for (;top > 0;){ - stage = stack + (--top); - - if (buffer_alloc_rope_node(buffer, &node_index)){ - node = nodes + node_index; - node->parent = stage->parent_index; - node->weight = stage->weight; - node->left = 0; - node->right = 0; - node->str_start = 0; - - is_right_side = stage->is_right_side; - - parent = nodes + node->parent; - if (is_right_side) parent->right = node_index; - else parent->left = node_index; - - if (stage->weight > rope_string_width){ - node->str_start = 0; - node->left_weight = stage->weight / 2; - assert_4tech(top < stack_max); - stack[top++] = buffer_construct_stage(node_index, 1, node->weight - node->left_weight); - assert_4tech(top < stack_max); - stack[top++] = buffer_construct_stage(node_index, 0, node->left_weight); - } - else{ - node->left_weight = 0; - if (buffer_alloc_rope_string(buffer, &node->str_start)){ - dest = (char*)buffer->data + node->str_start; - assert_4tech(read_pos <= len); - memcpy_4tech(dest, str + read_pos, node->weight); - read_pos += node->weight; - } - else{ - result = 0; - if (request_amount){ - buffer->grow_string_memory = 1; - *request_amount = buffer->string_count*rope_string_full_size*2; - } - break; - } - } - } - else{ - result = 0; - if (request_amount){ - buffer->grow_string_memory = 0; - *request_amount = buffer->node_count*sizeof(Rope_Node)*2; - } - break; - } - } - - if (!result && request_amount){ - node = nodes + root; - if (node->left){ - buffer_free_tree(buffer, node->left, scratch, scratch_size); - } - } - - return(result); -} - -internal_4tech int -buffer_end_init(Rope_Buffer_Init *init, void *scratch, int scratch_size){ - Rope_Buffer *buffer; - Rope_String *rope_string; - Rope_Node *node; - int i; - int result; - int count; - - result = 0; - buffer = init->buffer; - if (buffer->nodes && buffer->data){ - buffer->string_count = init->rope_string_count; - buffer->free_rope_string = 0; - - rope_string = (Rope_String*)buffer->data; - count = init->rope_string_count; - for (i = 0; i < count-1; ++i){ - rope_string->next_free = rope_string_full_size*(i+1); - rope_string = (Rope_String*)((char*)rope_string + rope_string_full_size); - } - rope_string->next_free = -1; - - buffer->node_count = init->node_count; - buffer->free_rope_node = 1; - - node = buffer->nodes + 1; - count = init->node_count; - for (i = 1; i < count-1; ++i, ++node){ - node->parent = i+1; - } - node->parent = 0; - - result = 1; - - node = buffer->nodes; - memzero_4tech(*node); - node->weight = init->size; - node->left_weight = init->size; - - result = buffer_build_tree(buffer, init->data, init->size, 0, scratch, scratch_size, 0); - } - - return(result); -} - -internal_4tech int -buffer_find_node(Rope_Buffer *buffer, int pos, int *node_start){ - Rope_Node *nodes, *node; - - *node_start = 0; - nodes = buffer->nodes; - node = nodes + nodes->left; - for (;node->str_start == 0;){ - if (pos < node->left_weight){ - node = nodes + node->left; - } - else{ - *node_start += node->left_weight; - pos -= node->left_weight; - node = nodes + node->right; - } - } - - return (int)(node - nodes); -} - -typedef struct Rope_Buffer_Stringify_Loop{ - Rope_Buffer *buffer; - char *data; - int absolute_pos; - int next_absolute_pos; - int size; - int pos, end_pos; - int node, node_end; -} Rope_Buffer_Stringify_Loop; - -internal_4tech Rope_Buffer_Stringify_Loop -buffer_stringify_loop(Rope_Buffer *buffer, int start, int end){ - Rope_Buffer_Stringify_Loop result; - Rope_Node *node; - int size, node_start_pos, temp_end; - - size = buffer_size(buffer); - if (0 <= start && start < end && end <= size){ - result.buffer = buffer; - result.absolute_pos = start; - - result.node = buffer_find_node(buffer, start, &node_start_pos); - result.pos = start - node_start_pos; - - result.node_end = buffer_find_node(buffer, end, &node_start_pos); - result.end_pos = end - node_start_pos; - - node = buffer->nodes + result.node; - temp_end = node->weight; - if (result.node == result.node_end) temp_end = result.end_pos; - - result.data = (char*)buffer->data + node->str_start + result.pos; - result.size = temp_end - result.pos; - } - else result.buffer = 0; - return(result); -} - -inline_4tech int -buffer_stringify_good(Rope_Buffer_Stringify_Loop *loop){ - int result; - result = (loop->buffer != 0); - return(result); -} - -internal_4tech void -buffer_stringify_next(Rope_Buffer_Stringify_Loop *loop){ - Rope_Node *node, *child, *nodes; - int temp_end; - - if (loop->node == loop->node_end && loop->pos + loop->size == loop->end_pos){ - loop->buffer = 0; - } - else{ - nodes = loop->buffer->nodes; - node = nodes + loop->node; - - for (;;){ - assert_4tech(node->parent != 0); - child = node; - node = nodes + node->parent; - if (nodes + node->left == child){ - break; - } - else{ - assert_4tech(nodes + node->right == child); - } - } - - node = nodes + node->right; - - for (;node->left;){ - node = nodes + node->left; - } - - loop->pos = 0; - loop->node = (int)(node - nodes); - loop->absolute_pos += loop->size; - temp_end = node->weight; - if (loop->node == loop->node_end) temp_end = loop->end_pos; - loop->size = temp_end; - loop->data = (char*)loop->buffer->data + node->str_start; - } -} - -typedef struct Rope_Buffer_Backify_Loop{ - Rope_Buffer *buffer; - char *data; - int absolute_pos; - int size; - int pos, end_pos; - int node, node_end; -} Rope_Buffer_Backify_Loop; - -internal_4tech Rope_Buffer_Backify_Loop -buffer_backify_loop(Rope_Buffer *buffer, int start, int end){ - Rope_Buffer_Backify_Loop result; - int size, node_start_pos, temp_end; - - size = buffer_size(buffer); - ++start; - if (0 <= end && end < start && start <= size){ - result.buffer = buffer; - - result.node_end = buffer_find_node(buffer, end, &node_start_pos); - result.end_pos = end - node_start_pos; - - result.node = buffer_find_node(buffer, start, &node_start_pos); - temp_end = start - node_start_pos; - - if (result.node_end == result.node){ - result.pos = result.end_pos; - result.absolute_pos = end; - } - else{ - result.pos = 0; - result.absolute_pos = node_start_pos; - } - - result.size = temp_end - result.pos; - result.data = (char*)buffer->data + buffer->nodes[result.node].str_start + result.pos; - } - else result.buffer = 0; - return(result); -} - -inline_4tech int -buffer_backify_good(Rope_Buffer_Backify_Loop *loop){ - int result; - result = (loop->buffer != 0); - return(result); -} - -internal_4tech void -buffer_backify_next(Rope_Buffer_Backify_Loop *loop){ - Rope_Node *node, *child, *nodes; - int temp_start; - - if (loop->node == loop->node_end && loop->pos == loop->end_pos){ - loop->buffer = 0; - } - else{ - nodes = loop->buffer->nodes; - node = nodes + loop->node; - - for (;;){ - assert_4tech(node->parent != 0); - child = node; - node = nodes + node->parent; - if (nodes + node->right == child){ - break; - } - else{ - assert_4tech(nodes + node->left == child); - } - } - - node = nodes + node->left; - - for (;node->right;){ - node = nodes + node->right; - } - - loop->pos = 0; - loop->node = (int)(node - nodes); - loop->absolute_pos -= node->weight; - temp_start = 0; - if (loop->node == loop->node_end) temp_start = loop->end_pos; - loop->size = node->weight - temp_start; - loop->data = (char*)loop->buffer->data + node->str_start; - } -} - -internal_4tech void -buffer_rotate_right(Rope_Buffer *buffer, int *node_index){ - Rope_Node *nodes, *r, *l, *b; - int ri, li, bi; - - nodes = buffer->nodes; - ri = *node_index; - r = nodes + ri; - li = r->left; - l = nodes + li; - bi = l->right; - b = nodes + bi; - - assert_4tech(bi != 0); - assert_4tech(ri != 0); - assert_4tech(li != 0); - - r->parent = li; - l->right = ri; - - b->parent = ri; - r->left = bi; - - r->weight = nodes[r->right].weight + nodes[r->left].weight; - r->left_weight = nodes[r->left].weight; - - l->weight = nodes[l->right].weight + nodes[l->left].weight; - l->left_weight = nodes[l->left].weight; - - *node_index = li; -} - -internal_4tech void -buffer_rotate_left(Rope_Buffer *buffer, int *node_index){ - Rope_Node *nodes, *r, *l, *b; - int ri, li, bi; - - nodes = buffer->nodes; - li = *node_index; - l = nodes + li; - ri = l->right; - r = nodes + ri; - bi = r->left; - b = nodes + bi; - - assert_4tech(bi != 0); - assert_4tech(ri != 0); - assert_4tech(li != 0); - - l->parent = ri; - r->left = li; - - b->parent = li; - l->right = bi; - - l->weight = nodes[l->right].weight + nodes[l->left].weight; - l->left_weight = nodes[l->left].weight; - - r->weight = nodes[r->right].weight + nodes[r->left].weight; - r->left_weight = nodes[r->left].weight; - - *node_index = ri; -} - -internal_4tech int -buffer_merge(Rope_Buffer *buffer, int node_index){ - Rope_Node *nodes, *node, *node_a, *node_b; - int a, b; - int did_merge; - - assert_4tech(node_index != 0); - nodes = buffer->nodes; - node = nodes + node_index; - did_merge = 0; - - if (node->weight <= rope_string_width && node->left != 0){ - assert_4tech(node->right != 0); - - a = node->left; - b = node->right; - - node_a = nodes + a; - node_b = nodes + b; - - if (node_a->str_start == 0) buffer_merge(buffer, a); - if (node_b->str_start == 0) buffer_merge(buffer, b); - - did_merge = 1; - memcpy_4tech((char*)buffer->data + node_a->str_start + node_a->weight, - (char*)buffer->data + node_b->str_start, node_b->weight); - node->weight = node_a->weight + node_b->weight; - node->left_weight = 0; - node->left = node->right = 0; - node->str_start = node_a->str_start; - - buffer_free_rope_node(buffer, a); - buffer_free_rope_node(buffer, b); - } - - return(did_merge); -} - -internal_4tech int -buffer_balance(Rope_Buffer *buffer, int *root){ - Rope_Node *node, *a, *nodes; - int wa, wb, wp, wdif, wdifp, left; - int node_a; - int improved; - - nodes = buffer->nodes; - node = nodes + *root; - - improved = 0; - if (node->left != 0){ - assert_4tech(node->right); - - if (!buffer_merge(buffer, *root)){ - wa = node->left_weight; - wb = node->weight - wa; - assert_4tech(wa + wb > rope_string_width); - - if (wa < wb){ - wp = wa; - wa = wb; - wb = wp; - left = 0; - node_a = node->right; - } - else{ - left = 1; - node_a = node->left; - } - - wdif = wa - wb; - a = nodes + node_a; - - if (a->left != 0){ - assert_4tech(a->right != 0); - - wp = a->left_weight; - if (left) wp = a->weight - wp; - wdifp = wdif - 2*wp; - if (wdifp < 0) wdifp = -wdifp; - if (wdifp < wdif){ - improved = 1; - if (left){ - if (a->weight - a->left_weight > a->left_weight && a->right != 0 && nodes[a->right].left != 0){ - buffer_rotate_left(buffer, &node_a); - a = nodes + node_a; - a->parent = *root; - node->left = node_a; - } - buffer_rotate_right(buffer, root); - } - else{ - if (a->left_weight > a->weight - a->left_weight && a->left != 0 && nodes[a->left].right != 0){ - buffer_rotate_right(buffer, &node_a); - a = nodes + node_a; - a->parent = *root; - node->right = node_a; - } - buffer_rotate_left(buffer, root); - } - } - } - } - } - - return(improved); -} - -internal_4tech int -buffer_concat(Rope_Buffer *buffer, int node_a, int node_b, int *root, int *request_amount){ - Rope_Node *r, *a, *b, *nodes; - int result; - - result = 0; - - nodes = buffer->nodes; - a = nodes + node_a; - b = nodes + node_b; - - if (a->weight == 0){ - assert_4tech(a->left == 0 && a->right == 0); - *root = node_b; - buffer_free_rope_node(buffer, node_a); - } - else if (b->weight == 0){ - assert_4tech(b->left == 0 && b->right == 0); - *root = node_a; - buffer_free_rope_node(buffer, node_b); - } - else if (buffer_alloc_rope_node(buffer, root)){ - r = nodes + *root; - - r->left = node_a; - r->right = node_b; - r->weight = a->weight + b->weight; - r->left_weight = a->weight; - r->str_start = 0; - r->parent = 0; - - a->parent = *root; - b->parent = *root; - - buffer_node_check(buffer, node_a); - buffer_node_check(buffer, node_b); - } - else{ - result = 1; - buffer->grow_string_memory = 0; - *request_amount = buffer->node_count*sizeof(Rope_Node)*2; - } - - return(result); -} - -internal_4tech int -buffer_string_split(Rope_Buffer *buffer, int *node_index, int pos, int *request_amount){ - Rope_Node *node, *a, *b; - int node_a, node_b; - int new_string_start, string_start; - int result; - result = 0; - - if (pos > 0){ - if (buffer_alloc_rope_string(buffer, &new_string_start)){ - if (buffer_alloc_rope_node(buffer, &node_a)){ - if (buffer_alloc_rope_node(buffer, &node_b)){ - node = buffer->nodes + *node_index; - string_start = node->str_start; - memcpy_4tech((char*)buffer->data + new_string_start, (char*)buffer->data + string_start + pos, node->weight - pos); - node->str_start = 0; - node->left_weight = pos; - node->left = node_a; - node->right = node_b; - - a = buffer->nodes + node_a; - b = buffer->nodes + node_b; - - a->parent = *node_index; - a->left = 0; - a->right = 0; - a->left_weight = 0; - a->weight = pos; - a->str_start = string_start; - - b->parent = *node_index;; - b->left = 0; - b->right = 0; - b->left_weight = 0; - b->weight = node->weight - pos; - b->str_start = new_string_start; - - *node_index = node_b; - } - else{ - buffer_free_rope_string(buffer, new_string_start); - buffer_free_rope_node(buffer, node_a); - result = 1; - buffer->grow_string_memory = 0; - *request_amount = buffer->node_count*sizeof(Rope_Node)*2; - } - } - else{ - buffer_free_rope_string(buffer, new_string_start); - result = 1; - buffer->grow_string_memory = 0; - *request_amount = buffer->node_count*sizeof(Rope_Node)*2; - } - } - else{ - result = 1; - buffer->grow_string_memory = 1; - *request_amount = buffer->string_count*rope_string_full_size*2; - } - } - - return(result); -} - -internal_4tech void -buffer_reduce_node(Rope_Buffer *buffer, int node_index){ - Rope_Node *parent, *child, *node, *nodes; - int child_index; - - nodes = buffer->nodes; - node = nodes + node_index; - parent = nodes + node->parent; - child_index = node->left; - if (child_index == 0) child_index = node->right; - assert_4tech(child_index != 0); - child = nodes + child_index; - - if (parent->left == node_index) parent->left = child_index; - else parent->right = child_index; - child->parent = node->parent; - - for (;parent != nodes;){ - parent->weight = nodes[parent->left].weight + nodes[parent->right].weight; - parent->left_weight = nodes[parent->left].weight; - parent = nodes + parent->parent; - } - - parent->weight = parent->left_weight = nodes[parent->left].weight; - - buffer_free_rope_node(buffer, node_index); -} - -internal_4tech int -buffer_split(Rope_Buffer *buffer, int pos, int *node_a, int *node_b, int *request_amount){ - Rope_Node *node, *nodes, *child; - int node_index, node_start_pos, split_root; - int result; - debug_4tech(int dbg_check); - - nodes = buffer->nodes; - result = 0; - - assert_4tech(pos != 0); - - node_index = buffer_find_node(buffer, pos, &node_start_pos); - if (node_start_pos < pos){ - if (buffer_string_split(buffer, &node_index, pos - node_start_pos, request_amount)){ - result = 1; - goto buffer_split_end; - } - } - - split_root = 0; - node = nodes + node_index; - for (;;){ - child = node; - node = nodes + node->parent; - if (child == nodes + node->right){ - break; - } - else{ - assert_4tech(child == nodes + node->left); - } - } - assert_4tech(node != nodes); - - for (;;){ - if (split_root == 0){ - split_root = node->right; - } - else{ - debug_4tech(dbg_check =) - buffer_concat(buffer, split_root, node->right, &split_root, request_amount); - assert_4tech(!dbg_check); - } - - node_index = (int)(node - nodes); - node->right = 0; - node = nodes + node->left; - buffer_reduce_node(buffer, node_index); - - for (;;){ - child = node; - node = nodes + child->parent; - if (nodes + node->left == child){ - break; - } - else{ - assert_4tech(nodes + node->right == child); - } - } - - if (node == nodes) break; - } - - *node_a = nodes->left; - *node_b = split_root; - -buffer_split_end: - return(result); -} - -internal_4tech int -buffer_build_tree_floating(Rope_Buffer *buffer, char *str, int len, int *out, - void *scratch, int scratch_size, int *request_amount){ - Rope_Node *super_root_node; - int result; - int super_root; - - result = 0; - if (buffer_alloc_rope_node(buffer, &super_root)){ - super_root_node = buffer->nodes + super_root; - memset_4tech(super_root_node, 0, sizeof(*super_root_node)); - if (buffer_build_tree(buffer, str, len, super_root, scratch, scratch_size, request_amount)){ - *out = buffer->nodes[super_root].left; - buffer_free_rope_node(buffer, super_root); - } - else{ - result = 1; - buffer_free_rope_node(buffer, super_root); - } - } - else{ - result = 1; - buffer->grow_string_memory = 0; - *request_amount = buffer->node_count*sizeof(Rope_Node)*2; - } - - return(result); -} - -internal_4tech int -buffer_replace_range(Rope_Buffer *buffer, int start, int end, char *str, int len, int *shift_amount, - void *scratch, int scratch_size, int *request_amount){ - Rope_Node *nodes; - Rope_Buffer_Edit_State state; - int result; - int size; - - state = buffer->edit_state; - - size = buffer_size(buffer); - assert_4tech(0 <= start); - assert_4tech(start <= end); - assert_4tech(end <= size); - - *shift_amount = (len - (end - start)); - nodes = buffer->nodes; - result = 0; - - buffer_cheap_check(buffer); - for (; buffer->edit_stage < 9; ++buffer->edit_stage){ - buffer_cheap_check(buffer); - switch (buffer->edit_stage){ - case 0: - if (end == 0){ - state.throw_away = 0; - state.right = nodes->left; - } - else if (end == size){ - state.throw_away = nodes->left; - state.right = 0; - } - else{ - result = buffer_split(buffer, end, &state.throw_away, &state.right, request_amount); - } - if (state.right) buffer_node_check(buffer, state.right); - break; - - case 1: - if (start == 0){ - state.left = 0; - } - else if (start == end){ - state.left = state.throw_away; - state.throw_away = 0; - } - else{ - result = buffer_split(buffer, start, &state.left, &state.throw_away, request_amount); - } - if (state.left) buffer_node_check(buffer, state.left); - break; - - case 2: - if (state.throw_away){ - buffer_free_tree(buffer, state.throw_away, scratch, scratch_size); - } - break; - - case 3: - if (len == 0) buffer->edit_stage += 4; - break; - - case 4: - if (state.left) buffer_node_check(buffer, state.left); - if (state.right) buffer_node_check(buffer, state.right); - result = buffer_build_tree_floating(buffer, str, len, &state.middle, scratch, scratch_size, request_amount); - if (state.left) buffer_node_check(buffer, state.left); - if (state.right) buffer_node_check(buffer, state.right); - break; - - case 5: - if (state.left){ - result = buffer_concat(buffer, state.left, state.middle, &state.middle, request_amount); - } - break; - - case 6: - if (state.right){ - buffer_balance(buffer, &state.middle); - buffer_node_check(buffer, state.middle); - result = buffer_concat(buffer, state.middle, state.right, &state.middle, request_amount); - } - break; - - case 7: - buffer_balance(buffer, &state.middle); - buffer_node_check(buffer, state.middle); - buffer->edit_stage = 9; - break; - - case 8: - if (state.left && state.right){ - result = buffer_concat(buffer, state.left, state.right, &state.middle, request_amount); - } - else if (state.left){ - state.middle = state.left; - } - else{ - state.middle = state.right; - } - break; - } - buffer_cheap_check(buffer); - - if (result) goto rope_buffer_replace_range_end; - } - - buffer->edit_stage = 0; - nodes[state.middle].parent = 0; - nodes->left = state.middle; - nodes->weight = nodes->left_weight = nodes[state.middle].weight; - - memset_4tech(&state, 0, sizeof(state)); - - buffer_rope_check(buffer, scratch, scratch_size); - -rope_buffer_replace_range_end: - buffer->edit_state = state; - - return(result); -} - -// NOTE(allen): This could should be optimized for Gap_Buffer -internal_4tech int -buffer_batch_edit_step(Buffer_Batch_State *state, Rope_Buffer *buffer, Buffer_Edit *sorted_edits, - char *strings, int edit_count, void *scratch, int scratch_size, int *request_amount){ - Buffer_Edit *edit; - int i, result; - int shift_total, shift_amount; - - result = 0; - shift_total = state->shift_total; - i = state->i; - - edit = sorted_edits + i; - for (; i < edit_count; ++i, ++edit){ - result = buffer_replace_range(buffer, edit->start + shift_total, edit->end + shift_total, - strings + edit->str_start, edit->len, &shift_amount, - scratch, scratch_size, request_amount); - if (result) break; - shift_total += shift_amount; - } - - state->shift_total = shift_total; - state->i = i; - - return(result); -} - -internal_4tech void* -buffer_edit_provide_memory(Rope_Buffer *buffer, void *new_data, int size){ - void *result; - Rope_String *rope_string; - Rope_Node *node; - int start, end, i; - - if (buffer->grow_string_memory){ - assert_4tech(size >= buffer->string_count * rope_string_full_size); - result = buffer->data; - memcpy_4tech(new_data, buffer->data, buffer->string_count * rope_string_full_size); - - buffer->data = new_data; - start = buffer->string_count*rope_string_full_size; - end = size/rope_string_full_size; - buffer->string_count = end; - end = (end-1)*rope_string_full_size; - for (i = start; i < end; i += rope_string_full_size){ - rope_string = (Rope_String*)((char*)new_data + i); - rope_string->next_free = i + rope_string_full_size; - } - rope_string = (Rope_String*)((char*)new_data + i); - rope_string->next_free = buffer->free_rope_string; - buffer->free_rope_string = start; - } - else{ - assert_4tech(size >= buffer->node_count * sizeof(Rope_Node)); - result = buffer->nodes; - memcpy_4tech(new_data, buffer->nodes, buffer->node_count * sizeof(Rope_Node)); - - buffer->nodes = (Rope_Node*)new_data; - start = buffer->node_count; - end = size/sizeof(Rope_Node); - buffer->node_count = end; - end -= 1; - node = buffer->nodes + start; - for (i = start; i < end; ++i, ++node){ - node->parent = i+1; - } - node->parent = buffer->free_rope_node; - buffer->free_rope_node = start; - } - - return(result); -} - -// BOTTOM - - - - diff --git a/buffer/4coder_test_abstract.cpp b/buffer/4coder_test_abstract.cpp deleted file mode 100644 index 767c180d..00000000 --- a/buffer/4coder_test_abstract.cpp +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Mr. 4th Dimention - Allen Webster - * Four Tech - * - * public domain -- no warranty is offered or implied; use this code at your own risk - * - * 08.11.2015 - * - * Buffer experiment testing layer, abstract portion - * - */ - -// TOP - -#define Buffer_Init_Type cat_4tech(Buffer_Type, _Init) -#define Buffer_Stringify_Type cat_4tech(Buffer_Type, _Stringify_Loop) -#define Buffer_Backify_Type cat_4tech(Buffer_Type, _Backify_Loop) - -void -init_buffer(Buffer_Type *buffer, File_Data file, void *scratch, int scratch_size){ - memzero_4tech(*buffer); - Buffer_Init_Type init; - for (init = buffer_begin_init(buffer, file.data, file.size); - buffer_init_need_more(&init);){ - int page_size = buffer_init_page_size(&init); - void *page = malloc(page_size); - buffer_init_provide_page(&init, page, page_size); - } - debug_4tech(int result =) - buffer_end_init(&init, scratch, scratch_size); - assert_4tech(result); -} - -void -measure_starts_widths(Buffer_Type *buffer, float *font_widths){ - if (buffer->line_max == 0){ - assert_4tech(buffer->line_starts == 0); - assert_4tech(buffer->line_widths == 0); - assert_4tech(buffer->widths_max == 0); - int max = 1 << 10; - buffer->line_starts = (int*)malloc(max*sizeof(int)); - buffer->line_max = max; - buffer->line_widths = (float*)malloc(max*sizeof(float)); - buffer->widths_max = max; - } - assert_4tech(buffer->line_starts != 0); - assert_4tech(buffer->widths_max != 0); - assert_4tech(buffer->line_widths != 0); - - Buffer_Measure_Starts state; - memzero_4tech(state); - for (;buffer_measure_starts_widths(&state, buffer, font_widths);){ - int max = buffer->line_max; - int count = state.count; - int target = count + 1; - - max = target*2; - int *new_lines = (int*)malloc(max*sizeof(int)); - memcpy_4tech(new_lines, buffer->line_starts, count*sizeof(int)); - free(buffer->line_starts); - buffer->line_starts = new_lines; - buffer->line_max = max; - - float *new_widths = (float*)malloc(max*sizeof(float)); - memcpy_4tech(new_widths, buffer->line_widths, count*sizeof(int)); - free(buffer->line_widths); - buffer->line_widths = new_widths; - buffer->widths_max = max; - } - buffer->line_count = state.count; - buffer->widths_count = state.count; -} - -void -edit(Buffer_Type *buffer, int start, int end, char *word, int len, - float *advance_data, void *scratch, int scratch_size){ - int shift_amount, request_amount; - - for (;buffer_replace_range(buffer, start, end, word, len, &shift_amount, - scratch, scratch_size, &request_amount);){ - void *new_data = 0; - if (request_amount > 0) new_data = malloc(request_amount); - void *old_data = buffer_edit_provide_memory(buffer, new_data, request_amount); - if (old_data) free(old_data); - } - - int line_start = buffer_get_line_index(buffer, start); - int line_end = buffer_get_line_index(buffer, end); - int replaced_line_count = line_end - line_start; - int new_line_count = buffer_count_newlines(buffer, start, start+len); - int line_shift = new_line_count - replaced_line_count; - - { - assert_4tech(buffer->line_starts); - int count = buffer->line_count; - if (count + line_shift > buffer->line_max){ - int new_max = round_up_4tech(buffer->line_max + line_shift, 1 << 10); - int *new_lines = (int*)malloc(sizeof(int)*new_max); - memcpy_4tech(new_lines, buffer->line_starts, sizeof(int)*count); - free(buffer->line_starts); - - buffer->line_starts = new_lines; - buffer->line_max = new_max; - } - - buffer_remeasure_starts(buffer, line_start, line_end, line_shift, shift_amount); - } - - { - assert_4tech(buffer->line_widths); - if (buffer->line_count > buffer->widths_max){ - int new_max = round_up_4tech(buffer->line_max, 1 << 10); - float *new_widths = (float*)malloc(sizeof(float)*new_max); - memcpy_4tech(new_widths, buffer->line_widths, sizeof(float)*buffer->widths_count); - free(buffer->line_widths); - - buffer->line_widths = new_widths; - buffer->widths_max = new_max; - } - - buffer_remeasure_widths(buffer, advance_data, line_start, line_end, line_shift); - } -} - -void -insert_bottom(Buffer_Type *buffer, char *word, int len, float *advance_data, void *scratch, int scratch_size){ - int size; - size = buffer_size(buffer); - edit(buffer, size, size, word, len, advance_data, scratch, scratch_size); -} - -void -insert_top(Buffer_Type *buffer, char *word, int len, float *advance_data, void *scratch, int scratch_size){ - edit(buffer, 0, 0, word, len, advance_data, scratch, scratch_size); -} - -void -delete_bottom(Buffer_Type *buffer, int len, float *advance_data, void *scratch, int scratch_size){ - int size; - size = buffer_size(buffer); - edit(buffer, size - len, size, 0, 0, advance_data, scratch, scratch_size); -} - -void -delete_top(Buffer_Type *buffer, int len, float *advance_data, void *scratch, int scratch_size){ - edit(buffer, 0, len, 0, 0, advance_data, scratch, scratch_size); -} - -void -natural_edits(Buffer_Type *buffer, float *advance_data, Replay *replay, int pos, void *scratch, int scratch_size){ - Edit_Step *steps = replay->replay.edits; - char *base_str = replay->replay.strings; - int edit_count = replay->replay.count; - - for (int i = 0; i < edit_count; ++i){ - Edit_Step step = steps[i]; - - if (step.child_count == 0 && step.edit.end <= buffer_size(buffer)){ - edit(buffer, pos + step.edit.start, pos + step.edit.end, base_str + step.edit.str_start, step.edit.len, - advance_data, scratch, scratch_size); - } - } -} - -void -batch_edit(Buffer_Type *buffer, float *advance_data, Buffer_Edit *batch, char *str_base, int batch_size, - void *scratch, int scratch_size){ - Buffer_Batch_State state; - int request_amount; - - memzero_4tech(state); - for (;buffer_batch_edit_step(&state, buffer, batch, str_base, batch_size, - scratch, scratch_size, &request_amount);){ - void *new_data = 0; - if (request_amount > 0) new_data = malloc(request_amount); - void *old_data = buffer_edit_provide_memory(buffer, new_data, request_amount); - if (old_data) free(old_data); - } - - measure_starts_widths(buffer, advance_data); -} - -// BOTTOM - diff --git a/buffer/4coder_test_main.cpp b/buffer/4coder_test_main.cpp deleted file mode 100644 index 1a384a1d..00000000 --- a/buffer/4coder_test_main.cpp +++ /dev/null @@ -1,1498 +0,0 @@ -/* - * Mr. 4th Dimention - Allen Webster - * Four Tech - * - * public domain -- no warranty is offered or implied; use this code at your own risk - * - * 06.11.2015 - * - * Buffer experiment testing layer - * - */ - -// TOP - -#include "4coder_external_name.h" - -#include "shared_test_config.cpp" - -#ifdef fast_test -#define debug_4tech(x) -#define assert_4tech(x) -#endif -#define hard_assert_4tech(x) assert(x) - -#include "4coder_shared.cpp" -#include "4coder_golden_array.cpp" -#include "4coder_gap_buffer.cpp" -#include "4coder_multi_gap_buffer.cpp" -#include "4coder_rope_buffer.cpp" - -#define Buffer_Type Buffer -#include "4coder_buffer_abstract.cpp" -#undef Buffer_Type - -#define Buffer_Type Gap_Buffer -#include "4coder_buffer_abstract.cpp" -#undef Buffer_Type - -#define Buffer_Type Multi_Gap_Buffer -#include "4coder_buffer_abstract.cpp" -#undef Buffer_Type - -#define Buffer_Type Rope_Buffer -#include "4coder_buffer_abstract.cpp" -#undef Buffer_Type - -#include "shared_test_utils.cpp" - -int int_into_str(char *out, int *rem, unsigned int x){ - char *start = out; - int size = *rem; - int result; - char t; - - if (x == 0 && size > 1){ - *out = '0'; - ++out; - --size; - } - else{ - for (;x > 0 && size > 1;--size, ++out){ - *out = (x%10 + '0'); - x /= 10; - } - } - - *rem = size; - *out = 0; - - result = (int)(out - start); - - --out; - for (; start < out; ++start, --out){ - t = *out; - *out = *start; - *start = t; - } - - return(result); -} - -int uscore_into_str(char *out, int *rem){ - int result; - result = 0; - if (*rem > 1){ - --*rem; - *out++ = '_'; - *out = 0; - result = 1; - } - return(result); -} - -#if defined(_WIN32) -#include - -typedef unsigned long long time_int; - -unsigned long long win32_counts_per_second_4tech; - -int time_init(unsigned long long *resolution){ - int result; - LARGE_INTEGER time; - result = 0; - if (QueryPerformanceFrequency(&time)){ - win32_counts_per_second_4tech = (unsigned long long)(time.QuadPart); - result = 1; - *resolution = win32_counts_per_second_4tech; - } - return(result); -} - -time_int get_time(){ - LARGE_INTEGER time; - time_int result; - - result = 0; - if (QueryPerformanceCounter(&time)){ - result = (time_int)(time.QuadPart); - result = result * 1000000 / win32_counts_per_second_4tech; - } - - return(result); -} - -void time_into_str(char *out, int max){ - SYSTEMTIME systime; - int pos; - - GetSystemTime(&systime); - pos = uscore_into_str(out, &max); - pos += int_into_str(out + pos, &max, systime.wYear); - - pos += uscore_into_str(out + pos, &max); - pos += int_into_str(out + pos, &max, systime.wMonth); - - pos += uscore_into_str(out + pos, &max); - pos += int_into_str(out + pos, &max, systime.wDay); - - pos += uscore_into_str(out + pos, &max); - pos += int_into_str(out + pos, &max, systime.wHour); - - pos += uscore_into_str(out + pos, &max); - pos += int_into_str(out + pos, &max, systime.wMinute); - - pos += uscore_into_str(out + pos, &max); - pos += int_into_str(out + pos, &max, systime.wSecond); - - pos += uscore_into_str(out + pos, &max); - pos += int_into_str(out + pos, &max, systime.wMilliseconds); -} - -#elif defined(__linux__) -#include - -typedef unsigned long long time_int; - -int time_init(unsigned long long *resolution){ - int result; - struct timespec res; - result = 0; - - if (!clock_getres(CLOCK_MONOTONIC, &res)){ - result = 1; - if (res.tv_sec > 0 || res.tv_nsec == 0) *resolution = 0; - else *resolution = (unsigned long long)(1000000/res.tv_nsec); - } - - return(result); -} - -time_int get_time(){ - time_int result; - struct timespec time; - - result = 0; - if (!clock_gettime(CLOCK_MONOTONIC, &time)){ - result = (time.tv_sec * 1000000) + (time.tv_nsec / 1000); - } - - return(result); -} - -#else -#error Timer not supported on this platform -#endif - -#define STB_TRUETYPE_IMPLEMENTATION -#include "stb_truetype.h" - -float* get_font_data(const char *font_file, float *font_height){ - float *data = 0; - stbtt_bakedchar *baked; - File_Data file = get_file(font_file); - if (!file.data) exit(1); - - if (file.data){ - int size = sizeof(*baked)*256; - baked = (stbtt_bakedchar*)malloc(size); - memset_4tech(baked, 0, sizeof(*baked)*256); - - stbtt_fontinfo font; - if (stbtt_InitFont(&font, (unsigned char*)file.data, 0)){ - float scale; - int a,d,g; - - scale = stbtt_ScaleForPixelHeight(&font, 17.f); - stbtt_GetFontVMetrics(&font, &a, &d, &g); - *font_height = scale*(a - d + g); - - int w, h; - w = 10*256; - h = 25; - unsigned char *pixels = (unsigned char*)malloc(w * h); - stbtt_BakeFontBitmap((unsigned char*)file.data, 0, 17.f, pixels, w, h, 0, 128, baked); - free(pixels); - free_file(file); - - data = (float*)malloc(sizeof(float)*256); - memset_4tech(data, 0, sizeof(float)*256); - - stbtt_bakedchar *baked_ptr = baked; - for (int i = 0; i < 128; ++i, ++baked_ptr){ - data[i] = baked_ptr->xadvance; - } - } - free(baked); - } - else{ - printf("error: cannot continue without font\n"); - } - - return data; -} - -void setup(){ - unsigned long long resolution; - if (!time_init(&resolution)){ - printf("error: could not initialize timer"); - exit(1); - } - - if (resolution < 1000000) - printf("warning: timer is not actually at high enough resolution for good measurements!\n"); - -} - -typedef struct Time_Record{ - time_int buffer; - time_int gap_buffer; - time_int multi_gap_buffer; - time_int rope_buffer; -} Time_Record; - -typedef struct Record_Statistics{ - Time_Record max, min; - Time_Record expected; - int count; -} Record_Statistics; - -typedef struct Log_Section{ - int *counter; -} Log_Section; - -typedef struct Stats_Log{ - char *out; - int size, max; - - Log_Section *sections; - int sec_top, sec_max; - - unsigned int error; -} Stats_Log; - -#define log_er_buffer_overflow 0x1 -#define log_er_stack_overflow 0x2 -#define log_er_stack_underflow 0x4 -#define log_er_time_too_large 0x8 - -#define logid_begin_section 0 -#define logid_data_item 1 - -#if fast_test -#define use_stats_log 1 -#else -#define use_stats_log 0 -#endif - -void log_write_int(Stats_Log *log, int x){ -#if use_stats_log - if (log->error == 0){ - if (log->size+4 <= log->max){ - *(int*)(log->out + log->size) = x; - log->size += 4; - } - else{ - log->error |= log_er_buffer_overflow; - } - } -#endif -} - -void log_write_time(Stats_Log *log, time_int x){ -#if use_stats_log - if (log->error == 0){ - if (x < 0x7FFFFFFF){ - if (log->size+4 <= log->max){ - *(int*)(log->out + log->size) = (int)x; - log->size += 4; - } - else{ - log->error |= log_er_buffer_overflow; - } - } - else{ - log->error |= log_er_time_too_large; - } - } -#endif -} - -void log_write_str(Stats_Log *log, char *str, int len){ -#if use_stats_log - int up = (len + 3) & ~3; - if (log->error == 0){ - if (log->size+4+up <= log->max){ - *(int*)(log->out + log->size) = up; - memcpy_4tech(log->out + log->size + 4, str, len); - log->size += 4+up; - } - else{ - log->error |= log_er_buffer_overflow; - } - } -#endif -} - -void log_begin_section(Stats_Log *log, char *name, int name_len){ -#if use_stats_log - Log_Section *section; - if (log->error == 0){ - if (log->sec_top < log->sec_max){ - if (log->sec_top > 0){ - section = log->sections + log->sec_top - 1; - ++section->counter; - } - - section = log->sections + (log->sec_top++); - - log_write_int(log, logid_begin_section); - log_write_str(log, name, name_len); - - section->counter = (int*)(log->out + log->size); - log_write_int(log, 0); - } - else{ - log->error |= log_er_stack_overflow; - } - } -#endif -} - -void log_end_section(Stats_Log *log){ -#if use_stats_log - if (log->error == 0){ - if (log->sec_top > 0){ - --log->sec_top; - } - else{ - log->error |= log_er_stack_underflow; - } - } -#endif -} - -void log_data_item(Stats_Log *log, char *name, int name_len, time_int t){ -#if use_stats_log - Log_Section *section; - if (log->error == 0){ - if (log->sec_top > 0){ - section = log->sections + log->sec_top - 1; - ++section->counter; - } - - log_write_int(log, logid_data_item); - log_write_str(log, name, name_len); - log_write_time(log, t); - } -#endif -} - -void log_finish(Stats_Log *log){ -#if use_stats_log - assert_4tech(sizeof(external_name) < 512); - if (log->error == 0){ - char fname[1024]; - memcpy_4tech(fname, "out/", 4); - memcpy_4tech(fname + 4, external_name, sizeof(external_name)-1); - time_into_str(fname + 4 + sizeof(external_name) - 1, 1023 - sizeof(external_name) + 1); - - File_Data log_file; - log_file.data = log->out; - log_file.size = log->size; - - save_file(fname, log_file); - } - else{ - printf("\n"); - if (log->error & log_er_buffer_overflow) - printf("log error: buffer overflow\n"); - if (log->error & log_er_stack_overflow) - printf("log error: stack overflow\n"); - if (log->error & log_er_stack_underflow) - printf("log error: stack underflow\n"); - printf("there were log error so the log was not saved\n\n"); - } -#endif -} - -#define litstr(s) (char*)(s), (sizeof(s)-1) - -void log_time_record(Stats_Log *log, char *name, int name_len, Time_Record record){ - log_begin_section(log, name, name_len); - log_data_item(log, litstr("golden-array"), record.buffer); - log_data_item(log, litstr("gap-buffer"), record.gap_buffer); - log_data_item(log, litstr("multi-gap-buffer"), record.multi_gap_buffer); - log_data_item(log, litstr("rope"), record.rope_buffer); - log_end_section(log); -} - -Time_Record -operator+(const Time_Record &a, const Time_Record &b){ - Time_Record r; - r.buffer = a.buffer + b.buffer; - r.gap_buffer = a.gap_buffer + b.gap_buffer; - r.multi_gap_buffer = a.multi_gap_buffer + b.multi_gap_buffer; - r.rope_buffer = a.rope_buffer + b.rope_buffer; - return r; -} - -Time_Record& -operator/=(Time_Record &r, int x){ - r.buffer /= x; - r.gap_buffer /= x; - r.multi_gap_buffer /= x; - r.rope_buffer /= x; - - return(r); -} - -#define minify(a,b) if ((a)>(b)) (a) = (b) -#define maxify(a,b) if ((a)<(b)) (a) = (b) - -void get_record_statistics(Record_Statistics *stats_out, Time_Record *records, int count){ - Record_Statistics stats; - stats.max = records[0]; - stats.min = records[0]; - stats.expected = records[0]; - stats.count = count; - - Time_Record *record = records + 1; - - for (int i = 1; i < count; ++i, ++record){ - stats.expected = stats.expected + *record; - - minify(stats.min.buffer, record->buffer); - minify(stats.min.gap_buffer, record->gap_buffer); - minify(stats.min.multi_gap_buffer, record->multi_gap_buffer); - minify(stats.min.rope_buffer, record->rope_buffer); - - maxify(stats.max.buffer, record->buffer); - maxify(stats.max.gap_buffer, record->gap_buffer); - maxify(stats.max.multi_gap_buffer, record->multi_gap_buffer); - maxify(stats.max.rope_buffer, record->rope_buffer); - } - - stats.expected /= count; - - *stats_out = stats; -} - -int test_is_silenced; - -void silence_test(){ - test_is_silenced = 1; -} - -void print_record(Time_Record record){ - printf("%-16s - %25lluus\n%-16s - %25lluus\n%-16s - %25lluus\n%-16s - %25lluus\n", - "Golden Array", record.buffer, - "Gap Buffer", record.gap_buffer, - "Multi-Gap Buffer", record.multi_gap_buffer, - "Rope", record.rope_buffer); -} - -void print_statistics(Time_Record *records, int count, Record_Statistics *stats_out){ - Record_Statistics stats; - get_record_statistics(&stats, records, count); - if (!test_is_silenced){ - printf("samples: %d\n", count); - printf("---averages---\n"); - print_record(stats.expected); - printf("---max---\n"); - print_record(stats.max); - printf("---min---\n"); - print_record(stats.min); - } - - if (stats_out) *stats_out = stats; -} - -typedef struct Buffer_Set{ - Buffer buffer; - Gap_Buffer gap_buffer; - Multi_Gap_Buffer multi_gap_buffer; - Rope_Buffer rope_buffer; -} Buffer_Set; - -#define Buffer_Type Buffer -#include "4coder_test_abstract.cpp" -#undef Buffer_Type - -#define Buffer_Type Gap_Buffer -#include "4coder_test_abstract.cpp" -#undef Buffer_Type - -#define Buffer_Type Multi_Gap_Buffer -#include "4coder_test_abstract.cpp" -#undef Buffer_Type - -#define Buffer_Type Rope_Buffer -#include "4coder_test_abstract.cpp" -#undef Buffer_Type - -void log_sample_set(Stats_Log *log, char *name, int name_len, Record_Statistics *stats, - Time_Record *samples, int sample_count){ - log_begin_section(log, name, name_len); - - log_data_item(log, litstr("sample-count"), sample_count); - log_time_record(log, litstr("max"), stats->max); - log_time_record(log, litstr("min"), stats->min); - log_time_record(log, litstr("average"), stats->expected); - - for (int i = 0; i < sample_count; ++i){ - log_time_record(log, litstr("item"), samples[i]); - } - - log_end_section(log); -} - -typedef struct Sample_Machine{ - time_int tstart, tend; - Time_Record *samples; - int count; -} Sample_Machine; - -Sample_Machine begin_machine(int count, void **data, int *max){ - Sample_Machine result; - - result.count = count; - result.samples = (Time_Record*)*data; - *data = result.samples + count; - assert_4tech(count*sizeof(*result.samples) < *max); - *max -= count*sizeof(*result.samples); - - return(result); -} - -void end_machine(Sample_Machine *machine, Record_Statistics *stats_out, char *func_name){ - if (!test_is_silenced) printf("%s\n", func_name); - print_statistics(machine->samples, machine->count, stats_out); - if (!test_is_silenced) printf("\n"); - test_is_silenced = 0; -} - -void start(Sample_Machine *machine){ - machine->tstart = get_time(); -} - -time_int stop(Sample_Machine *machine){ - machine->tend = get_time(); - return machine->tend - machine->tstart; -} - -void initialization_test(Stats_Log *log, Buffer_Set *set, File_Data file, int test_repitions, - void *scratch, int scratch_size, Record_Statistics *stats_out){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - for (int i = 0; i < test_repitions; ++i){ - start(&machine); - init_buffer(&set->buffer, file, scratch, scratch_size); - machine.samples[i].buffer = stop(&machine); - - start(&machine); - init_buffer(&set->gap_buffer, file, scratch, scratch_size); - machine.samples[i].gap_buffer = stop(&machine); - - start(&machine); - init_buffer(&set->multi_gap_buffer, file, scratch, scratch_size); - machine.samples[i].multi_gap_buffer = stop(&machine); - - start(&machine); - init_buffer(&set->rope_buffer, file, scratch, scratch_size); - machine.samples[i].rope_buffer = stop(&machine); - - if (i+1 != test_repitions){ - free(set->buffer.data); - free(set->gap_buffer.data); - for (int j = 0; j < set->multi_gap_buffer.chunk_alloced; ++j){ - free(set->multi_gap_buffer.gaps[j].data); - } - free(set->multi_gap_buffer.gaps); - free(set->rope_buffer.data); - free(set->rope_buffer.nodes); - } - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("initialization"), stats_out, machine.samples, machine.count); -} - -void measure_starts_widths_test(Stats_Log *log, Buffer_Set *set, int test_repitions, void *scratch, - int scratch_size, Record_Statistics *stats_out, float *font_widths){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - for (int i = 0; i < test_repitions; ++i){ - start(&machine); - measure_starts_widths(&set->buffer, font_widths); - machine.samples[i].buffer = stop(&machine); - - start(&machine); - measure_starts_widths(&set->gap_buffer, font_widths); - machine.samples[i].gap_buffer = stop(&machine); - - start(&machine); - measure_starts_widths(&set->multi_gap_buffer, font_widths); - machine.samples[i].multi_gap_buffer = stop(&machine); - - start(&machine); - measure_starts_widths(&set->rope_buffer, font_widths); - machine.samples[i].rope_buffer = stop(&machine); - - if (i+1 != test_repitions){ - free(set->buffer.line_starts); - free(set->gap_buffer.line_starts); - free(set->multi_gap_buffer.line_starts); - free(set->rope_buffer.line_starts); - - free(set->buffer.line_widths); - free(set->gap_buffer.line_widths); - free(set->multi_gap_buffer.line_widths); - free(set->rope_buffer.line_widths); - - set->buffer.line_max = 0; - set->buffer.line_starts = 0; - set->buffer.widths_max = 0; - set->buffer.line_widths = 0; - - set->gap_buffer.line_max = 0; - set->gap_buffer.line_starts = 0; - set->gap_buffer.widths_max = 0; - set->gap_buffer.line_widths = 0; - - set->multi_gap_buffer.line_max = 0; - set->multi_gap_buffer.line_starts = 0; - set->multi_gap_buffer.widths_max = 0; - set->multi_gap_buffer.line_widths = 0; - - set->rope_buffer.line_max = 0; - set->rope_buffer.line_starts = 0; - set->rope_buffer.widths_max = 0; - set->rope_buffer.line_widths = 0; - } - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("measure-starts-widths"), stats_out, machine.samples, machine.count); -} - -int page_compare(void *page_1_, void *page_2_, int page_size){ - char *page_1 = (char*)page_1_; - char *page_2 = (char*)page_2_; - int result = 1; - for (int i = 0; i < page_size; ++i){ - hard_assert_4tech(page_1[i] == page_2[i]); - } - return result; -} - -float* measure_wraps_test(Stats_Log *log, Buffer_Set *buffers, int test_repitions, void *scratch, - int scratch_size, Record_Statistics *stats_out, float font_height, float max_width){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - float *wrap_ys, *wrap_ys2; - wrap_ys = (float*)malloc(sizeof(float)*buffers->buffer.line_count); - wrap_ys2 = (float*)malloc(sizeof(float)*buffers->buffer.line_count); - - for (int i = 0; i < test_repitions; ++i){ - start(&machine); - buffer_measure_wrap_y(&buffers->buffer, wrap_ys, font_height, max_width); - machine.samples[i].buffer = stop(&machine); - - start(&machine); - buffer_measure_wrap_y(&buffers->gap_buffer, wrap_ys2, font_height, max_width); - machine.samples[i].gap_buffer = stop(&machine); - if (i == 0) - page_compare((char*)wrap_ys, (char*)wrap_ys2, sizeof(float)*buffers->buffer.line_count); - - start(&machine); - buffer_measure_wrap_y(&buffers->multi_gap_buffer, wrap_ys2, font_height, max_width); - machine.samples[i].multi_gap_buffer = stop(&machine); - if (i == 0) - page_compare((char*)wrap_ys, (char*)wrap_ys2, sizeof(float)*buffers->buffer.line_count); - - start(&machine); - buffer_measure_wrap_y(&buffers->rope_buffer, wrap_ys2, font_height, max_width); - machine.samples[i].rope_buffer = stop(&machine); - if (i == 0) - page_compare((char*)wrap_ys, (char*)wrap_ys2, sizeof(float)*buffers->buffer.line_count); - } - - free(wrap_ys2); - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("measure-wrap-ys"), stats_out, machine.samples, machine.count); - - return wrap_ys; -} - -int cursor_eq(Full_Cursor c1, Full_Cursor c2){ - int result = 0; - if (c1.pos == c2.pos && c1.line == c2.line && c1.character == c2.character && - c1.wrapped_x == c2.wrapped_x && c1.wrapped_y == c2.wrapped_y && - c1.unwrapped_x == c2.unwrapped_x && c1.unwrapped_y == c2.unwrapped_y){ - result = 1; - } - return(result); -} - -void full_cursor_test(Stats_Log *log, Buffer_Set *buffers, int pos, - float *wrap_ys, float *advance_data, float font_height, float max_width, - int test_repitions, void *scratch, int scratch_size, Record_Statistics *stats_out){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - Full_Cursor cursor, cursor2; - - for (int i = 0; i < test_repitions; ++i){ - start(&machine); - cursor = buffer_cursor_from_pos(&buffers->buffer, pos, wrap_ys, max_width, font_height, advance_data); - machine.samples[i].buffer = stop(&machine); - - start(&machine); - cursor2 = buffer_cursor_from_pos(&buffers->gap_buffer, pos, wrap_ys, max_width, font_height, advance_data); - machine.samples[i].gap_buffer = stop(&machine); - if (i == 0) assert_4tech(cursor_eq(cursor, cursor2)); - - start(&machine); - cursor2 = buffer_cursor_from_pos(&buffers->multi_gap_buffer, pos, wrap_ys, max_width, font_height, advance_data); - machine.samples[i].multi_gap_buffer = stop(&machine); - if (i == 0) assert_4tech(cursor_eq(cursor, cursor2)); - - start(&machine); - cursor2 = buffer_cursor_from_pos(&buffers->rope_buffer, pos, wrap_ys, max_width, font_height, advance_data); - machine.samples[i].rope_buffer = stop(&machine); - if (i == 0) assert_4tech(cursor_eq(cursor, cursor2)); - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("full-cursor-seek"), stats_out, machine.samples, machine.count); -} - -void full_cursor_line_test(Stats_Log *log, Buffer_Set *buffers, int line, int character, - float *wrap_ys, float *advance_data, float font_height, float max_width, - int test_repitions, void *scratch, int scratch_size, Record_Statistics *stats_out){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - Full_Cursor cursor, cursor2; - - for (int i = 0; i < test_repitions; ++i){ - start(&machine); - cursor = buffer_cursor_from_line_character(&buffers->buffer, line, character, - wrap_ys, max_width, font_height, advance_data); - machine.samples[i].buffer = stop(&machine); - - start(&machine); - cursor2 = buffer_cursor_from_line_character(&buffers->gap_buffer, line, character, - wrap_ys, max_width, font_height, advance_data); - machine.samples[i].gap_buffer = stop(&machine); - if (i == 0) assert_4tech(cursor_eq(cursor, cursor2)); - - start(&machine); - cursor2 = buffer_cursor_from_line_character(&buffers->multi_gap_buffer, line, character, - wrap_ys, max_width, font_height, advance_data); - machine.samples[i].multi_gap_buffer = stop(&machine); - if (i == 0) assert_4tech(cursor_eq(cursor, cursor2)); - - start(&machine); - cursor2 = buffer_cursor_from_line_character(&buffers->rope_buffer, line, character, - wrap_ys, max_width, font_height, advance_data); - machine.samples[i].rope_buffer = stop(&machine); - if (i == 0) assert_4tech(cursor_eq(cursor, cursor2)); - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("full-cursor-seek"), stats_out, machine.samples, machine.count); -} - -void full_cursor_xy_test(Stats_Log *log, Buffer_Set *buffers, float x, float y, int round_down, - float *wrap_ys, float *advance_data, float font_height, float max_width, - int test_repitions, void *scratch, int scratch_size, Record_Statistics *stats_out){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - Full_Cursor cursor, cursor2; - - for (int i = 0; i < test_repitions; ++i){ - start(&machine); - cursor = buffer_cursor_from_unwrapped_xy(&buffers->buffer, x, y, round_down, - wrap_ys, max_width, font_height, advance_data); - machine.samples[i].buffer = stop(&machine); - - start(&machine); - cursor2 = buffer_cursor_from_unwrapped_xy(&buffers->gap_buffer, x, y, round_down, - wrap_ys, max_width, font_height, advance_data); - machine.samples[i].gap_buffer = stop(&machine); - if (i == 0) assert_4tech(cursor_eq(cursor, cursor2)); - - start(&machine); - cursor2 = buffer_cursor_from_unwrapped_xy(&buffers->multi_gap_buffer, x, y, round_down, - wrap_ys, max_width, font_height, advance_data); - machine.samples[i].multi_gap_buffer = stop(&machine); - if (i == 0) assert_4tech(cursor_eq(cursor, cursor2)); - - start(&machine); - cursor2 = buffer_cursor_from_unwrapped_xy(&buffers->rope_buffer, x, y, round_down, - wrap_ys, max_width, font_height, advance_data); - machine.samples[i].rope_buffer = stop(&machine); - if (i == 0) assert_4tech(cursor_eq(cursor, cursor2)); - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("full-cursor-seek"), stats_out, machine.samples, machine.count); -} - -void word_seek_test(Stats_Log *log, Buffer_Set *buffers, int test_repitions, - int incremental_position, char *word, int len, - void *scratch, int scratch_size, Record_Statistics *stats_out){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - assert_4tech(scratch_size >= len); - - int pos, pos2, old_pos; - old_pos = 0; - - for (int i = 0; i < machine.count; ++i){ - start(&machine); - pos = buffer_find_string(&buffers->buffer, old_pos, word, len, (char*)scratch); - machine.samples[i].buffer = stop(&machine); - - start(&machine); - pos2 = buffer_find_string(&buffers->gap_buffer, old_pos, word, len, (char*)scratch); - machine.samples[i].gap_buffer = stop(&machine); - assert_4tech(pos2 == pos); - - start(&machine); - pos2 = buffer_find_string(&buffers->multi_gap_buffer, old_pos, word, len, (char*)scratch); - machine.samples[i].multi_gap_buffer = stop(&machine); - assert_4tech(pos2 == pos); - - start(&machine); - pos2 = buffer_find_string(&buffers->rope_buffer, old_pos, word, len, (char*)scratch); - machine.samples[i].rope_buffer = stop(&machine); - assert_4tech(pos2 == pos); - - if (incremental_position) old_pos = pos; - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("word-seek"), stats_out, machine.samples, machine.count); -} - -void stream_check_test(Buffer_Set *buffers, void *scratch, int scratch_size){ - int i, page_size, size; - - size = buffer_size(&buffers->buffer); - { - int size2; - size2 = buffer_size(&buffers->gap_buffer); - hard_assert_4tech(size == size2); - size2 = buffer_size(&buffers->multi_gap_buffer); - hard_assert_4tech(size == size2); - size2 = buffer_size(&buffers->rope_buffer); - hard_assert_4tech(size == size2); - } - - page_size = 1 << 10; - - char *page_1 = (char*)scratch; - char *page_2 = page_1 + page_size; - scratch_size -= page_size*2; - hard_assert_4tech(scratch_size > 0); - - for (i = 0; i < size; i += page_size){ - int end = i + page_size; - if (end > size) end = size; - - buffer_stringify(&buffers->buffer, i, end, page_1); - - buffer_stringify(&buffers->gap_buffer, i, end, page_2); - page_compare(page_1, page_2, page_size); - - buffer_stringify(&buffers->multi_gap_buffer, i, end, page_2); - page_compare(page_1, page_2, page_size); - - buffer_stringify(&buffers->rope_buffer, i, end, page_2); - page_compare(page_1, page_2, page_size); - } - - for (i = size-1; i > 0; i -= page_size){ - int end = i - page_size; - if (end < 0) end = 0; - - buffer_backify(&buffers->buffer, i, end, page_1); - - buffer_backify(&buffers->gap_buffer, i, end, page_2); - page_compare(page_1, page_2, page_size); - - buffer_backify(&buffers->multi_gap_buffer, i, end, page_2); - page_compare(page_1, page_2, page_size); - - buffer_backify(&buffers->rope_buffer, i, end, page_2); - page_compare(page_1, page_2, page_size); - } -} - -void insert_bottom_test(Stats_Log *log, Buffer_Set *buffers, int test_repitions, float *advance_data, - int edit_count, void *scratch, int scratch_size, Record_Statistics *stats_out){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - char word[] = "stuff"; - int word_len = sizeof(word) - 1; - - int i, j; - for (i = 0; i < test_repitions; ++i){ - start(&machine); - for (j = 0; j < edit_count; ++j){ - insert_bottom(&buffers->buffer, word, word_len, - advance_data, scratch, scratch_size); - } - machine.samples[i].buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - insert_bottom(&buffers->gap_buffer, word, word_len, - advance_data, scratch, scratch_size); - } - machine.samples[i].gap_buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - insert_bottom(&buffers->multi_gap_buffer, word, word_len, - advance_data, scratch, scratch_size); - } - machine.samples[i].multi_gap_buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - insert_bottom(&buffers->rope_buffer, word, word_len, - advance_data, scratch, scratch_size); - } - machine.samples[i].rope_buffer = stop(&machine); - - if (i == 0){ - stream_check_test(buffers, scratch, scratch_size); - } - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("insert-bottom"), stats_out, machine.samples, machine.count); -} - -void insert_top_test(Stats_Log *log, Buffer_Set *buffers, int test_repitions, float *advance_data, - int edit_count, void *scratch, int scratch_size, Record_Statistics *stats_out){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - char word[] = "stuff"; - int word_len = sizeof(word) - 1; - - int i, j; - for (i = 0; i < test_repitions; ++i){ - start(&machine); - for (j = 0; j < edit_count; ++j){ - insert_top(&buffers->buffer, word, word_len, - advance_data, scratch, scratch_size); - } - machine.samples[i].buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - insert_top(&buffers->gap_buffer, word, word_len, - advance_data, scratch, scratch_size); - } - machine.samples[i].gap_buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - insert_top(&buffers->multi_gap_buffer, word, word_len, - advance_data, scratch, scratch_size); - } - machine.samples[i].multi_gap_buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - insert_top(&buffers->rope_buffer, word, word_len, - advance_data, scratch, scratch_size); - } - machine.samples[i].rope_buffer = stop(&machine); - - if (i == 0){ - stream_check_test(buffers, scratch, scratch_size); - } - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("insert-top"), stats_out, machine.samples, machine.count); -} - -void delete_bottom_test(Stats_Log *log, Buffer_Set *buffers, int test_repitions, float *advance_data, - int edit_count, void *scratch, int scratch_size, Record_Statistics *stats_out){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - int len = 5; - - int i, j; - for (i = 0; i < test_repitions; ++i){ - start(&machine); - for (j = 0; j < edit_count; ++j){ - delete_bottom(&buffers->buffer, len, - advance_data, scratch, scratch_size); - } - machine.samples[i].buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - delete_bottom(&buffers->gap_buffer, len, - advance_data, scratch, scratch_size); - } - machine.samples[i].gap_buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - delete_bottom(&buffers->multi_gap_buffer, len, - advance_data, scratch, scratch_size); - } - machine.samples[i].multi_gap_buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - delete_bottom(&buffers->rope_buffer, len, - advance_data, scratch, scratch_size); - } - machine.samples[i].rope_buffer = stop(&machine); - - if (i == 0){ - stream_check_test(buffers, scratch, scratch_size); - } - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("delete-bottom"), stats_out, machine.samples, machine.count); -} - -void delete_top_test(Stats_Log *log, Buffer_Set *buffers, int test_repitions, float *advance_data, - int edit_count, void *scratch, int scratch_size, Record_Statistics *stats_out){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - int len = 5; - - int i, j; - for (i = 0; i < test_repitions; ++i){ - start(&machine); - for (j = 0; j < edit_count; ++j){ - delete_top(&buffers->buffer, len, - advance_data, scratch, scratch_size); - } - machine.samples[i].buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - delete_top(&buffers->gap_buffer, len, - advance_data, scratch, scratch_size); - } - machine.samples[i].gap_buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - delete_top(&buffers->multi_gap_buffer, len, - advance_data, scratch, scratch_size); - } - machine.samples[i].multi_gap_buffer = stop(&machine); - - start(&machine); - for (j = 0; j < edit_count; ++j){ - delete_top(&buffers->rope_buffer, len, - advance_data, scratch, scratch_size); - } - machine.samples[i].rope_buffer = stop(&machine); - - if (i == 0){ - stream_check_test(buffers, scratch, scratch_size); - } - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("delete-top"), stats_out, machine.samples, machine.count); -} - -void natural_edits_test(Stats_Log *log, Buffer_Set *buffers, int test_repitions, float *advance_data, - Replay *replay, void *scratch, int scratch_size, Record_Statistics *stats_out){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - printf("edit count: %d\n", replay->replay.count); - log_data_item(log, litstr("edit-count"), replay->replay.count); - int i, j; - for (i = 0; i < test_repitions; ++i){ - j = i*buffer_size(&buffers->buffer) / (1+test_repitions); - - start(&machine); - natural_edits(&buffers->buffer, advance_data, replay, j, scratch, scratch_size); - machine.samples[i].buffer = stop(&machine); - - start(&machine); - natural_edits(&buffers->gap_buffer, advance_data, replay, j, scratch, scratch_size); - machine.samples[i].gap_buffer = stop(&machine); - - start(&machine); - natural_edits(&buffers->multi_gap_buffer, advance_data, replay, j, scratch, scratch_size); - machine.samples[i].multi_gap_buffer = stop(&machine); - - start(&machine); - natural_edits(&buffers->rope_buffer, advance_data, replay, j, scratch, scratch_size); - machine.samples[i].rope_buffer = stop(&machine); - - if (i == 0){ - stream_check_test(buffers, scratch, scratch_size); - } - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("natural-edits"), stats_out, machine.samples, machine.count); -} - -void batch_edit_test(Stats_Log *log, Buffer_Set *buffers, int test_repitions, float *advance_data, - Buffer_Edit *batch, char *str_base, int batch_size, - void *scratch, int scratch_size, Record_Statistics *stats_out){ - Sample_Machine machine; - machine = begin_machine(test_repitions, &scratch, &scratch_size); - - printf("batch size: %d\n", batch_size); - log_data_item(log, litstr("batch-size"), batch_size); - int i; - for (i = 0; i < test_repitions; ++i){ - start(&machine); - batch_edit(&buffers->buffer, advance_data, batch, str_base, batch_size, scratch, scratch_size); - machine.samples[i].buffer = stop(&machine); - - start(&machine); - batch_edit(&buffers->gap_buffer, advance_data, batch, str_base, batch_size, scratch, scratch_size); - machine.samples[i].gap_buffer = stop(&machine); - - start(&machine); - batch_edit(&buffers->multi_gap_buffer, advance_data, batch, str_base, batch_size, scratch, scratch_size); - machine.samples[i].multi_gap_buffer = stop(&machine); - - start(&machine); - batch_edit(&buffers->rope_buffer, advance_data, batch, str_base, batch_size, scratch, scratch_size); - machine.samples[i].rope_buffer = stop(&machine); - - if (i == 0){ - stream_check_test(buffers, scratch, scratch_size); - } - } - - end_machine(&machine, stats_out, __FUNCTION__); - - log_sample_set(log, litstr("batch-edit"), stats_out, machine.samples, machine.count); -} - -void measure_check_test(Buffer_Set *buffers){ - int count; - count = buffers->buffer.line_count; - assert_4tech(count == buffers->buffer.widths_count); - - assert_4tech(count == buffers->gap_buffer.line_count); - assert_4tech(count == buffers->multi_gap_buffer.line_count); - assert_4tech(count == buffers->rope_buffer.line_count); - - assert_4tech(count == buffers->gap_buffer.widths_count); - assert_4tech(count == buffers->multi_gap_buffer.widths_count); - assert_4tech(count == buffers->rope_buffer.widths_count); - - page_compare(buffers->buffer.line_starts, buffers->gap_buffer.line_starts, sizeof(int)*count); - page_compare(buffers->buffer.line_starts, buffers->multi_gap_buffer.line_starts, sizeof(int)*count); - page_compare(buffers->buffer.line_starts, buffers->rope_buffer.line_starts, sizeof(int)*count); - - page_compare(buffers->buffer.line_widths, buffers->gap_buffer.line_widths, sizeof(float)*count); - page_compare(buffers->buffer.line_widths, buffers->multi_gap_buffer.line_widths, sizeof(float)*count); - page_compare(buffers->buffer.line_widths, buffers->rope_buffer.line_widths, sizeof(float)*count); -} - -#define reps 50 - -int main(int argc, char **argv){ - Buffer_Set buffers; - File_Data file; - float *widths_data; - float *wrap_ys; - float font_height; - float max_width; - - void *scratch; - int scratch_size; - - Stats_Log log; - - int do_replay = 0; - char *replay_filename = 0; - - if (argc < 2){ - printf("usage: buffer_test [-h <.hst file>]\n"); - exit(1); - } - - setup(); - - for (int i = 3; i < argc; ++i){ - if (do_replay){ - replay_filename = argv[i]; - do_replay = 0; - } - if (strcmp(argv[i], "-h") == 0){ - if (replay_filename != 0){ - printf("found -h twice, ignoring duplicates\n"); - } - else{ - do_replay = 1; - } - } - } - - do_replay = (replay_filename != 0); - - memzero_4tech(buffers); - - log.max = 1 << 20; - log.size = 0; - log.out = (char*)malloc(log.max); - - log.sec_max = 32; - log.sec_top = 0; - log.sections = (Log_Section*)malloc(sizeof(Log_Section)*log.sec_max); - - log.error = 0; - - scratch_size = 1 << 20; - scratch = malloc(scratch_size); - - file = get_file(argv[1]); - if (!file.data) exit(1); - widths_data = get_font_data("LiberationSans-Regular.ttf", &font_height); - max_width = 500.f; - - log_begin_section(&log, litstr("which-test")); - { - log_write_str(&log, argv[1], (int)strlen(argv[1])); - log_write_int(&log, file.size); - log_write_str(&log, argv[2], (int)strlen(argv[2])); - } - log_end_section(&log); - - log_begin_section(&log, litstr("file-open")); - { - Record_Statistics init_rec, starts_widths_rec, wraps_rec; - - initialization_test(&log, &buffers, file, reps, scratch, scratch_size, &init_rec); - stream_check_test(&buffers, scratch, scratch_size); - - measure_starts_widths_test(&log, &buffers, reps, scratch, scratch_size, - &starts_widths_rec, widths_data); - measure_check_test(&buffers); - - wrap_ys = measure_wraps_test(&log, &buffers, reps, scratch, scratch_size, - &wraps_rec, font_height, max_width); - - Time_Record expected_file_open; - expected_file_open = init_rec.expected + starts_widths_rec.expected + wraps_rec.expected; - - printf("average file open:\n"); - print_record(expected_file_open); - printf("\n"); - log_time_record(&log, litstr("average"), expected_file_open); - } - log_end_section(&log); - - log_begin_section(&log, litstr("cursor-seek")); - { - Record_Statistics full_cursor; - Time_Record full_cursor_average; - - log_begin_section(&log, litstr("to-pos")); - { - memzero_4tech(full_cursor_average); - for (int i = 0; i < 5; ++i){ - silence_test(); - int pos = (file.size*i) / 5; - full_cursor_test(&log, &buffers, pos, - wrap_ys, widths_data, font_height, max_width, - 5, scratch, scratch_size, &full_cursor); - full_cursor_average = full_cursor_average + full_cursor.expected; - } - full_cursor_average /= 5; - printf("average cursor from position:\n"); - print_record(full_cursor_average); - printf("\n"); - log_time_record(&log, litstr("average"), full_cursor_average); - } - log_end_section(&log); - - log_begin_section(&log, litstr("to-line-character")); - { - memzero_4tech(full_cursor_average); - for (int i = 0; i < 5; ++i){ - silence_test(); - int line = (buffers.buffer.line_count*i) / 5; - full_cursor_line_test(&log, &buffers, line, 20, - wrap_ys, widths_data, font_height, max_width, - 5, scratch, scratch_size, &full_cursor); - full_cursor_average = full_cursor_average + full_cursor.expected; - } - full_cursor_average /= 5; - printf("average cursor from line & character:\n"); - print_record(full_cursor_average); - printf("\n"); - log_time_record(&log, litstr("average"), full_cursor_average); - } - log_end_section(&log); - - log_begin_section(&log, litstr("to-unwrapped-x-y")); - { - memzero_4tech(full_cursor_average); - for (int i = 0; i < 5; ++i){ - silence_test(); - float y = font_height * (buffers.buffer.line_count*i) / 4.f; - full_cursor_xy_test(&log, &buffers, y, 37.f, 0, - wrap_ys, widths_data, font_height, max_width, - 5, scratch, scratch_size, &full_cursor); - full_cursor_average = full_cursor_average + full_cursor.expected; - } - full_cursor_average /= 5; - printf("average cursor from line & character:\n"); - print_record(full_cursor_average); - printf("\n"); - log_time_record(&log, litstr("average"), full_cursor_average); - } - log_end_section(&log); - - } - log_end_section(&log); - - log_begin_section(&log, litstr("word-seek")); - { - Record_Statistics word_seek; - - { - char word[] = "not-going-to-find-this"; - int word_len = sizeof(word) - 1; - word_seek_test(&log, &buffers, reps, 0, word, word_len, scratch, scratch_size, &word_seek); - } - - { - char word[] = "return"; - int word_len = sizeof(word) - 1; - word_seek_test(&log, &buffers, reps, 1, word, word_len, scratch, scratch_size, &word_seek); - } - } - log_end_section(&log); - - log_begin_section(&log, litstr("one-hundred-single-edits")); - { - Record_Statistics edits; - insert_bottom_test(&log, &buffers, reps, widths_data, 100, scratch, scratch_size, &edits); - insert_top_test(&log, &buffers, reps, widths_data, 100, scratch, scratch_size, &edits); - delete_bottom_test(&log, &buffers, reps, widths_data, 100, scratch, scratch_size, &edits); - delete_top_test(&log, &buffers, reps, widths_data, 100, scratch, scratch_size, &edits); - } - log_end_section(&log); - - File_Data replay_file = {}; - Replay replay; - - if (do_replay){ - replay_file = get_file(replay_filename); - prepare_replay(replay_file, &replay); - } - - if (replay_file.data){ - log_begin_section(&log, litstr("natural-edits")); - { - Record_Statistics edits; - natural_edits_test(&log, &buffers, reps, widths_data, &replay, - scratch, scratch_size, &edits); - } - log_end_section(&log); - } - else{ - printf("skipped natural-edits test\n\n"); - } - - log_begin_section(&log, litstr("batch-edit")); - { - Buffer_Edit *batch; - char *str_base; - int batch_size, str_size; - - batch_size = 1000; - str_size = 10000; - batch = (Buffer_Edit*)malloc(sizeof(Buffer_Edit)*batch_size); - str_base = (char*)malloc(str_size); - - int character_stride; - character_stride = buffer_size(&buffers.buffer); - if (character_stride < batch_size * 10){ - batch_size = character_stride / 10; - character_stride = 10; - } - else{ - character_stride /= batch_size; - } - - int p, curs; - curs = 0; - p = 0; - for (int i = 0; i < batch_size; ++i){ - Buffer_Edit edit; - edit.start = p; - edit.end = p+8; - p += character_stride; - - edit.str_start = curs; - if (i & 1){ - edit.len = 9; - memcpy_4tech(str_base + curs, "123456789", 9); - curs += 9; - } - else{ - edit.len = 7; - memcpy_4tech(str_base + curs, "abcdefg", 7); - curs += 7; - } - - batch[i] = edit; - } - - Record_Statistics batch_stats; - batch_edit_test(&log, &buffers, reps, widths_data, batch, str_base, batch_size, - scratch, scratch_size, &batch_stats); - } - log_end_section(&log); - - log_finish(&log); - - return(0); -} - -// BOTTOM - diff --git a/buffer/history_to_replay.cpp b/buffer/history_to_replay.cpp deleted file mode 100644 index c5f64205..00000000 --- a/buffer/history_to_replay.cpp +++ /dev/null @@ -1,316 +0,0 @@ -/* - * Mr. 4th Dimention - Allen Webster - * Four Tech - * - * public domain -- no warranty is offered or implied; use this code at your own risk - * - * 11.11.2015 - * - * Converts code file and saved history from 4coder - * into replay file for the test system. - * - */ - -// TOP - -#include "shared_test_config.cpp" - -#include "4coder_shared.cpp" -#include "4coder_golden_array.cpp" - -#define Buffer_Type Buffer -#include "4coder_buffer_abstract.cpp" - -#include "shared_test_utils.cpp" - -void do_single_edit(Replay *replay, Buffer *buffer, Edit_Step step, char *str, void *scratch, int scratch_size){ - { - Edit_Stack *stack; - char *new_data; - Edit_Step inv_step; - int size, new_max, new_size; - - stack = &replay->replay; - - memzero_4tech(inv_step); - size = 0; - buffer_invert_edit(buffer, step.edit, &inv_step.edit, (char*)scratch, &size, scratch_size); - - assert_4tech(stack->count < replay->ed_max); - stack = &replay->replay; - stack->edits[replay->ed_max - (++stack->count)] = inv_step; - - new_size = stack->size + size; - - if (new_size > replay->str_max){ - new_max = (replay->str_max + size) * 2; - new_data = (char*)malloc(new_max); - memcpy_4tech(new_data + new_max - stack->size, stack->strings + replay->str_max - stack->size, stack->size); - free(stack->strings); - stack->strings = new_data; - replay->str_max = new_max; - } - - memcpy_4tech(stack->strings + replay->str_max - new_size, scratch, size); - stack->size = new_size; - - printf("just wrote: [[ %.*s ]]\n", size, scratch); - } - - int start = step.edit.start; - int end = step.edit.end; - int str_start = step.edit.str_start; - int len = step.edit.len; - - int shift_amount; - int request_amount; - - for (;buffer_replace_range(buffer, start, end, str + str_start, len, &shift_amount, - scratch, scratch_size, &request_amount);){ - void *new_data = 0; - if (request_amount > 0) new_data = malloc(request_amount); - void *old_data = buffer_edit_provide_memory(buffer, new_data, request_amount); - if (old_data) free(old_data); - } -} - -void do_batch_edit(Replay *replay, Buffer *buffer, Edit_Step step, void *scratch, int scratch_size){ - { - Edit_Stack *stack; - Edit_Step inv_step; - - stack = &replay->replay; - - memzero_4tech(inv_step); - inv_step.first_child = step.inverse_first_child; - inv_step.inverse_first_child = step.first_child; - inv_step.child_count = step.inverse_child_count; - inv_step.inverse_child_count = step.child_count; - - assert_4tech(stack->count < replay->ed_max); - stack = &replay->replay; - stack->edits[replay->ed_max - (++stack->count)] = inv_step; - } - - char *str; - Buffer_Edit *batch; - int batch_size; - - int request_amount; - Buffer_Batch_State state; - - str = replay->children.strings; - batch = replay->children.edits + step.first_child; - batch_size = step.child_count; - - memzero_4tech(state); - for (;buffer_batch_edit_step(&state, buffer, batch, str, batch_size, - scratch, scratch_size, &request_amount);){ - void *new_data = 0; - if (request_amount > 0) new_data = malloc(request_amount); - void *old_data = buffer_edit_provide_memory(buffer, new_data, request_amount); - if (old_data) free(old_data); - } -} - -#if 0 - -int main(int argc, char **argv){ - if (argc < 3){ - printf("usage: hst_to_rply \n"); - exit(1); - } - - File_Data history_file, code_file; - history_file = get_file(argv[1]); - code_file = get_file(argv[2]); - - if (!history_file.data || !code_file.data) exit(1); - - History history; - char *curs = history_file.data; - - history.undo.count = read_int(&curs); - history.redo.count = read_int(&curs); - history.history.count = read_int(&curs); - history.children.count = read_int(&curs); - - history.undo.size = read_int(&curs); - history.redo.size = read_int(&curs); - history.history.size = read_int(&curs); - history.children.size = read_int(&curs); - - - history.undo.edits = (Edit_Step*)curs; - curs += sizeof(Edit_Step)*history.undo.count; - - history.redo.edits = (Edit_Step*)curs; - curs += sizeof(Edit_Step)*history.redo.count; - - history.history.edits = (Edit_Step*)curs; - curs += sizeof(Edit_Step)*history.history.count; - - history.children.edits = (Buffer_Edit*)curs; - curs += sizeof(Buffer_Edit)*history.children.count; - - - history.undo.strings = (char*)curs; - curs += history.undo.size; - - history.redo.strings = (char*)curs; - curs += history.redo.size; - - history.history.strings = (char*)curs; - curs += history.history.size; - - history.children.strings = (char*)curs; - curs += history.children.size; - - - void *scratch; - int scratch_size; - scratch_size = 1 << 20; - scratch = malloc(scratch_size); - - - Buffer buffer; - Buffer_Init_Type init; - - memzero_4tech(buffer); - memzero_4tech(init); - for (init = buffer_begin_init(&buffer, code_file.data, code_file.size); - buffer_init_need_more(&init);){ - int page_size = buffer_init_page_size(&init); - page_size = round_up_4tech(page_size, 4 << 10); - void *data = malloc(page_size); - buffer_init_provide_page(&init, data, page_size); - } - buffer_end_init(&init, scratch, scratch_size); - - - Replay replay; - - replay.children = history.children; - replay.ed_max = history.history.count; - replay.replay.edits = (Edit_Step*)malloc(sizeof(Edit_Step)*replay.ed_max); - replay.replay.count = 0; - - replay.str_max = history.history. size * 4; - replay.replay.strings = (char*)malloc(replay.str_max); - replay.replay.size = 0; - - - for (int i = history.history.count - 1; i >= 0; --i){ - Edit_Step step = history.history.edits[i]; - if (step.child_count == 0){ - do_single_edit(&replay, &buffer, step, history.history.strings, scratch, scratch_size); - } - else{ - assert(step.special_type == 1); - do_batch_edit(&replay, &buffer, step, scratch, scratch_size); - } - } - - assert(history.history.count == replay.replay.count); - - int str_start = 0; - for (int i = 0; i < history.history.count; ++i){ - replay.replay.edits[i].edit.str_start = str_start; - str_start += replay.replay.edits[i].edit.len; - } - - File_Data out_file; - out_file.size = 0; - out_file.size += replay.replay.count*sizeof(Edit_Step) + sizeof(int); - out_file.size += replay.replay.size + sizeof(int); - out_file.size += replay.children.count*sizeof(Buffer_Edit) + sizeof(int); - out_file.size += replay.children.size + sizeof(int); - - out_file.data = (char*)malloc(out_file.size); - - curs = out_file.data; - curs = write_int(curs, replay.replay.count); - curs = write_int(curs, replay.children.count); - - curs = write_int(curs, replay.replay.size); - curs = write_int(curs, replay.children.size); - - memcpy(curs, replay.replay.edits, replay.replay.count*sizeof(Edit_Step)); - curs += replay.replay.count*sizeof(Edit_Step); - - memcpy(curs, replay.children.edits, replay.children.count*sizeof(Buffer_Edit)); - curs += replay.children.count*sizeof(Buffer_Edit); - - memcpy(curs, replay.replay.strings + replay.str_max - replay.replay.size, replay.replay.size); - curs += replay.replay.size; - - memcpy(curs, replay.children.strings, replay.children.size); - curs += replay.children.size; - - assert((int)(curs - out_file.data) == out_file.size); - - save_file(argv[3], out_file); - - return(0); -} - -#else - -int main(int argc, char **argv){ - if (argc < 2){ - printf("usage: \n"); - exit(1); - } - - File_Data file = get_file(argv[1]); - //char *curs = file.data; - - Replay replay; - prepare_replay(file, &replay); -#if 0 - replay.replay.count = read_int(&curs); - replay.children.count = read_int(&curs); - replay.replay.size = read_int(&curs); - replay.children.size = read_int(&curs); - - replay.replay.edits = (Edit_Step*)curs; - curs += sizeof(Edit_Step)*replay.replay.count; - - replay.children.edits = (Buffer_Edit*)curs; - curs += sizeof(Buffer_Edit)*replay.children.count; - - replay.replay.strings = curs; - curs += replay.replay.size; - - replay.children.strings = curs; - curs += replay.children.size; - - assert_4tech((int)(curs - file.data) == file.size); -#endif - - for (int i = 0; i < replay.replay.count; ++i){ - Edit_Step step = replay.replay.edits[i]; - if (step.first_child == 0){ - if (step.edit.len > 0 && step.edit.str_start >= 0 && step.edit.str_start < replay.replay.size){ - printf("replace [%d,%d] with %.*s\n", step.edit.start, step.edit.end, - step.edit.len, replay.replay.strings + step.edit.str_start); - } - else if (step.edit.len > 0){ - printf("str_start out of bounds!\n"); - } - else{ - printf("replace [%d,%d] with ~~empty string~~\n", step.edit.start, step.edit.end); - } - } - else{ - printf("batch edit\n"); - } - } - - printf("string section:\n%.*s\n", replay.replay.size, replay.replay.strings); -} - -#endif - -// BOTTOM - diff --git a/buffer/shared_test_config.cpp b/buffer/shared_test_config.cpp deleted file mode 100644 index 672a2a5f..00000000 --- a/buffer/shared_test_config.cpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Mr. 4th Dimention - Allen Webster - * Four Tech - * - * public domain -- no warranty is offered or implied; use this code at your own risk - * - * 11.11.2015 - * - * Code shared between history_to_replay.cpp and 4coder_test_main.cpp - * - */ - -// TOP - -#include -#include -#include -#include - -#define inline_4tech inline - -inline_4tech int -CEIL32(float x){ - int extra; - extra = ((x!=(int)(x) && x>0)?1:0); - extra += (int)(x); - return(extra); -} - -inline_4tech int -DIVCEIL32(int n, int d) { - int q = (n/d); - q += (q*d < n); - return(q); -} - -inline_4tech unsigned int -ROUNDPOT32(unsigned int v){ - v--; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - v++; - return(v); -} - -// TODO(allen): this should actually be compiler specific, it doesn't -// have anything to do with platform -#if defined(__linux__) -#define memzero_4tech(x) memset_4tech(&(x), 0, sizeof(x)) -#else -#define memzero_4tech(x) (x = {}) -#endif - -// BOTTOM - diff --git a/buffer/shared_test_utils.cpp b/buffer/shared_test_utils.cpp deleted file mode 100644 index d6e54c56..00000000 --- a/buffer/shared_test_utils.cpp +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Mr. 4th Dimention - Allen Webster - * Four Tech - * - * public domain -- no warranty is offered or implied; use this code at your own risk - * - * 11.11.2015 - * - * Code shared between history_to_replay.cpp and 4coder_test_main.cpp - * - */ - -// TOP - -typedef struct File_Data{ - char *data; - int size; -} File_Data; - -File_Data get_file(const char *filename){ - FILE *file; - File_Data result; - - memzero_4tech(result); - - file = fopen(filename, "rb"); - if (!file){ - printf("error: could not find file %s\n", filename); - } - else{ - fseek(file, 0, SEEK_END); - result.size = ftell(file); - fseek(file, 0, SEEK_SET); - - if (result.size == 0){ - printf("error: file %s was empty\n", filename); - } - else{ - result.data = (char*)malloc(result.size); - fread(result.data, result.size, 1, file); - } - - fclose(file); - } - - return(result); -} - -void save_file(const char *fname, File_Data file){ - FILE *f = fopen(fname, "wb"); - if (f){ - fwrite(file.data, 1, file.size, f); - fclose(f); - } - else{ - printf("error: could not open %s\n", fname); - } -} - -void free_file(File_Data file){ - free(file.data); -} - -typedef struct Edit_Step{ - int type; - union{ - struct{ - int can_merge; - Buffer_Edit edit; - int pre_pos; - int post_pos; - int next_block, prev_block; - }; - struct{ - int first_child; - int inverse_first_child; - int inverse_child_count; - int special_type; - }; - }; - int child_count; -} Edit_Step; - -typedef struct Edit_Stack{ - char *strings; - int size; - - Edit_Step *edits; - int count; -} Edit_Stack; - -typedef struct Small_Edit_Stack{ - char *strings; - int size; - - Buffer_Edit *edits; - int count; -} Small_Edit_Stack; - -typedef struct History{ - Edit_Stack undo; - Edit_Stack redo; - Edit_Stack history; - Small_Edit_Stack children; -} History; - -typedef struct Replay{ - Edit_Stack replay; - Small_Edit_Stack children; - - int str_max; - int ed_max; -} Replay; - -int read_int(char **curs){ - int result; - result = *(int*)(*curs); - *curs += 4; - return(result); -} - -char* write_int(char *curs, int x){ - *(int*)(curs) = x; - curs += 4; - return(curs); -} - -void prepare_replay(File_Data file, Replay *replay){ - char *curs = file.data; - - replay->replay.count = read_int(&curs); - replay->children.count = read_int(&curs); - replay->replay.size = read_int(&curs); - replay->children.size = read_int(&curs); - - replay->replay.edits = (Edit_Step*)curs; - curs += sizeof(Edit_Step)*replay->replay.count; - - replay->children.edits = (Buffer_Edit*)curs; - curs += sizeof(Buffer_Edit)*replay->children.count; - - replay->replay.strings = curs; - curs += replay->replay.size; - - replay->children.strings = curs; - curs += replay->children.size; - - assert_4tech((int)(curs - file.data) == file.size); -} - -// BOTTOM - - -