From 20706a2e5df84532e6bef3908501d16a51ab7d7a Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Sun, 25 Oct 2015 21:51:08 -0400 Subject: [PATCH] buffer reorganization --- 4ed_file_view.cpp | 8 +- 4ed_file_view_golden_array.cpp | 2 + buffer/4coder_buffer_abstract.cpp | 26 +--- buffer/4coder_gap_buffer.cpp | 7 - buffer/4coder_golden_array.cpp | 204 +----------------------------- buffer/4coder_shared.cpp | 199 +++++++++++++++++++++++++++++ win32_4ed.cpp | 2 +- 7 files changed, 215 insertions(+), 233 deletions(-) diff --git a/4ed_file_view.cpp b/4ed_file_view.cpp index adfcbba0..d865b61d 100644 --- a/4ed_file_view.cpp +++ b/4ed_file_view.cpp @@ -11,6 +11,8 @@ #include "buffer/4coder_shared.cpp" #include "buffer/4coder_gap_buffer.cpp" + +#define Buffer_Type Gap_Buffer #include "buffer/4coder_buffer_abstract.cpp" struct Range{ @@ -2418,15 +2420,15 @@ view_do_single_edit(Mem_Options *mem, File_View *view, Editing_File *file, i32 start = spec.step.edit.start; i32 end = spec.step.edit.end; - char *str = (char*)spec.str; + char *str = (char*)spec.str; AllowLocal(str); i32 str_len = spec.step.edit.len; - i32 shift_amount; + i32 shift_amount = 0; +#if BUFFER_EXPERIMENT_SCALPEL while (gap_buffer_replace_range(&file->buffer, start, end, str, str_len, &shift_amount)) file_grow_as_needed(general, file, shift_amount); // NOTE(allen): fixing stuff afterwards -#if BUFFER_EXPERIMENT_SCALPEL if (file->tokens_exist) file_relex_parallel(mem, file, start, end, shift_amount); #endif diff --git a/4ed_file_view_golden_array.cpp b/4ed_file_view_golden_array.cpp index a1575347..2f96aa8c 100644 --- a/4ed_file_view_golden_array.cpp +++ b/4ed_file_view_golden_array.cpp @@ -11,6 +11,8 @@ #include "buffer/4coder_shared.cpp" #include "buffer/4coder_golden_array.cpp" + +#define Buffer_Type Buffer #include "buffer/4coder_buffer_abstract.cpp" struct Range{ diff --git a/buffer/4coder_buffer_abstract.cpp b/buffer/4coder_buffer_abstract.cpp index 9916f875..4a931067 100644 --- a/buffer/4coder_buffer_abstract.cpp +++ b/buffer/4coder_buffer_abstract.cpp @@ -4,7 +4,7 @@ * * public domain -- no warranty is offered or implied; use this code at your own risk * - * 16.10.2015 + * 24.10.2015 * * Buffer data object * type - Golden Array @@ -13,12 +13,6 @@ // TOP -#if BUFFER_EXPERIMENT_SCALPEL -#define Buffer_Type Buffer -#else -#define Buffer_Type Gap_Buffer -#endif - #define CAT_(a,b) a##b #define CAT(a,b) CAT_(a,b) @@ -335,6 +329,7 @@ buffer_get_line_index(Buffer_Type *buffer, int pos, int l_bound, int u_bound){ return(start); } +#ifndef NON_ABSTRACT_PORTION_4TECH internal_4tech int buffer_get_line_index_from_wrapped_y(float *wraps, float y, float font_height, int l_bound, int u_bound){ int start, end, i, result; @@ -355,19 +350,7 @@ buffer_get_line_index_from_wrapped_y(float *wraps, float y, float font_height, i } return(result); } - -inline_4tech Full_Cursor -make_cursor_hint(int line_index, int *starts, float *wrap_ys, float font_height){ - Full_Cursor hint; - hint.pos = starts[line_index]; - hint.line = line_index + 1; - hint.character = 1; - hint.unwrapped_y = (f32)(line_index * font_height); - hint.unwrapped_x = 0; - hint.wrapped_y = wrap_ys[line_index]; - hint.wrapped_x = 0; - return(hint); -} +#endif internal_4tech Full_Cursor buffer_cursor_from_pos(Buffer_Type *buffer, int pos, float *wraps, @@ -537,5 +520,8 @@ buffer_get_render_data(Buffer_Type *buffer, float *wraps, Buffer_Render_Item *it width, height, advance_data, stride, font_height); } +#ifndef NON_ABSTRACT_PORTION_4TECH +#define NON_ABSTRACT_PORTION_4TECH 1 +#endif // BOTTOM diff --git a/buffer/4coder_gap_buffer.cpp b/buffer/4coder_gap_buffer.cpp index b160353e..3e115320 100644 --- a/buffer/4coder_gap_buffer.cpp +++ b/buffer/4coder_gap_buffer.cpp @@ -291,12 +291,5 @@ buffer_cursor_seek(Gap_Buffer *buffer, Buffer_Seek seek, float max_width, float return(state.cursor); } -// TODO(allen): unfinished below here - -internal_4tech int -gap_buffer_replace_range(Gap_Buffer *buffer, int start, int end, char *str, int len, int *shift_amount){ - return(0); -} - // BOTTOM diff --git a/buffer/4coder_golden_array.cpp b/buffer/4coder_golden_array.cpp index 17842cdd..68f0cd4b 100644 --- a/buffer/4coder_golden_array.cpp +++ b/buffer/4coder_golden_array.cpp @@ -128,26 +128,6 @@ buffer_stringify(Buffer *buffer, int start, int end, char *out){ } } -#if 0 -internal_4tech void -buffer_measure_wrap_y(Buffer *buffer, float *wraps, - float font_height, float max_width){ - float *widths; - float y_pos; - int i, line_count; - - line_count = buffer->line_count; - widths = buffer->line_widths; - y_pos = 0; - - for (i = 0; i < line_count; ++i){ - wraps[i] = y_pos; - if (widths[i] == 0) y_pos += font_height; - else y_pos += font_height*ceil_4tech(widths[i]/max_width); - } -} -#endif - internal_4tech int buffer_replace_range(Buffer *buffer, int start, int end, char *str, int len, int *shift_amount){ char *data; @@ -170,83 +150,6 @@ buffer_replace_range(Buffer *buffer, int start, int end, char *str, int len, int return(result); } -typedef struct{ - int pos, index; -} Cursor_With_Index; - -inline_4tech void -write_cursor_with_index(Cursor_With_Index *positions, int *count, int pos){ - positions[*count].index = *count; - positions[*count].pos = pos; - ++*count; -} - -#define CursorSwap__(a,b) { Cursor_With_Index t = a; a = b; b = t; } - -internal_4tech void -buffer_quick_sort_cursors(Cursor_With_Index *positions, int start, int pivot){ - int i, mid; - int pivot_pos; - - mid = start; - pivot_pos = positions[pivot].pos; - for (i = mid; i < pivot; ++i){ - if (positions[i].pos < pivot_pos){ - CursorSwap__(positions[mid], positions[i]); - ++mid; - } - } - CursorSwap__(positions[mid], positions[pivot]); - - if (start < mid - 1) buffer_quick_sort_cursors(positions, start, mid - 1); - if (mid + 1 < pivot) buffer_quick_sort_cursors(positions, mid + 1, pivot); -} - -inline_4tech void -buffer_sort_cursors(Cursor_With_Index *positions, int count){ - assert_4tech(count > 0); - buffer_quick_sort_cursors(positions, 0, count-1); -} - -internal_4tech void -buffer_quick_unsort_cursors(Cursor_With_Index *positions, int start, int pivot){ - int i, mid; - int pivot_index; - - mid = start; - pivot_index = positions[pivot].index; - for (i = mid; i < pivot; ++i){ - if (positions[i].index < pivot_index){ - CursorSwap__(positions[mid], positions[i]); - ++mid; - } - } - CursorSwap__(positions[mid], positions[pivot]); - - if (start < mid - 1) buffer_quick_unsort_cursors(positions, start, mid - 1); - if (mid + 1 < pivot) buffer_quick_unsort_cursors(positions, mid + 1, pivot); -} - -#undef CursorSwap__ - -inline_4tech void -buffer_unsort_cursors(Cursor_With_Index *positions, int count){ - assert_4tech(count > 0); - buffer_quick_unsort_cursors(positions, 0, count-1); -} - -internal_4tech void -buffer_update_cursors(Cursor_With_Index *sorted_positions, int count, int start, int end, int len){ - Cursor_With_Index *position; - int shift_amount; - - shift_amount = (len - (end - start)); - - position = sorted_positions + count - 1; - for (; position >= sorted_positions && position->pos >= end; --position) position->pos += shift_amount; - for (; position >= sorted_positions && position->pos >= start; --position) position->pos = start; -} - internal_4tech Full_Cursor buffer_cursor_seek(Buffer *buffer, Buffer_Seek seek, float max_width, float font_height, void *advance_data, int stride, Full_Cursor cursor){ @@ -337,44 +240,6 @@ buffer_invert_batch(Buffer_Invert_Batch *state, Buffer *buffer, Buffer_Edit *edi return(result); } -internal_4tech int -buffer_batch_debug_sort_check(Buffer_Edit *sorted_edits, int edit_count){ - Buffer_Edit *edit; - int i, result, start_point; - - result = 1; - start_point = 0; - - edit = sorted_edits; - for (i = 0; i < edit_count; ++i, ++edit){ - if (start_point > edit->start){ - result = 0; break; - } - start_point = (edit->end < edit->start + 1)?edit->start + 1:edit->end; - } - - return(result); -} - -internal_4tech int -buffer_batch_edit_max_shift(Buffer_Edit *sorted_edits, int edit_count){ - Buffer_Edit *edit; - int i, result; - int shift_total, shift_max; - - result = 0; - shift_total = 0; - shift_max = 0; - - edit = sorted_edits; - for (i = 0; i < edit_count; ++i, ++edit){ - shift_total += (edit->len - (edit->end - edit->start)); - if (shift_total > shift_max) shift_max = shift_total; - } - - return(shift_max); -} - typedef struct{ int i; int shift_total; @@ -417,41 +282,6 @@ buffer_batch_edit(Buffer *buffer, Buffer_Edit *sorted_edits, char *strings, int assert_4tech(result == 0); } -internal_4tech void -buffer_batch_edit_update_cursors(Cursor_With_Index *sorted_positions, int count, Buffer_Edit *sorted_edits, int edit_count){ - Cursor_With_Index *position, *end_position; - Buffer_Edit *edit, *end_edit; - int start, end; - int shift_amount; - - position = sorted_positions; - end_position = sorted_positions + count; - - edit = sorted_edits; - end_edit = sorted_edits + edit_count; - - shift_amount = 0; - - for (; edit < end_edit && position < end_position; ++edit){ - start = edit->start; - end = edit->end; - - for (; position->pos < start && position < end_position; ++position){ - position->pos += shift_amount; - } - - for (; position->pos < end && position < end_position; ++position){ - position->pos = start + shift_amount; - } - - shift_amount += (edit->len - (end - start)); - } - - for (; position < end_position; ++position){ - position->pos += shift_amount; - } -} - internal_4tech int buffer_find_hard_start(Buffer *buffer, int line_start, int *all_whitespace, int *all_space, int *preferred_indent, int tab_width){ @@ -485,23 +315,7 @@ buffer_find_hard_start(Buffer *buffer, int line_start, int *all_whitespace, int internal_4tech void buffer_eol_convert_in(Buffer *buffer){ - char *data; - int size; - int i; - - data = buffer->data; - size = buffer->size; - assert_4tech(size < buffer->max); - data[size] = 0; - - for (i = 0; i < size; ++i){ - if (data[i] == '\r' && data[i+1] == '\n'){ - memmove_4tech(data + i, data + i + 1, size - i); - size -= 1; - } - } - - buffer->size = size; + buffer->size = eol_convert_in(buffer->data, buffer->size); } inline_4tech int @@ -513,23 +327,9 @@ buffer_eol_convert_out_size(Buffer *buffer){ internal_4tech void buffer_eol_convert_out(Buffer *buffer){ - char *data; int size; - int i; - - data = buffer->data; - size = buffer->size; assert_4tech(buffer_eol_convert_out_size(buffer) < buffer->max); - - for (i = 0; i < size; ++i){ - if (data[i] == '\n'){ - memmove_4tech(data + i + 1, data + i, size - i); - data[i] = '\r'; - ++i; - ++size; - } - } - + eol_convert_out(buffer->data, buffer->size, buffer->max, &size); buffer->size = size; } diff --git a/buffer/4coder_shared.cpp b/buffer/4coder_shared.cpp index 59816c2b..941d7c81 100644 --- a/buffer/4coder_shared.cpp +++ b/buffer/4coder_shared.cpp @@ -134,6 +134,19 @@ write_render_item_inline(Buffer_Render_Item *item, int index, int glyphid, return(ch_width); } +inline_4tech Full_Cursor +make_cursor_hint(int line_index, int *starts, float *wrap_ys, float font_height){ + Full_Cursor hint; + hint.pos = starts[line_index]; + hint.line = line_index + 1; + hint.character = 1; + hint.unwrapped_y = (f32)(line_index * font_height); + hint.unwrapped_x = 0; + hint.wrapped_y = wrap_ys[line_index]; + hint.wrapped_x = 0; + return(hint); +} + typedef struct{ Full_Cursor cursor, prev_cursor; } Seek_State; @@ -245,6 +258,192 @@ cursor_seek_step_end: return(result); } +typedef struct{ + int pos, index; +} Cursor_With_Index; + +inline_4tech void +write_cursor_with_index(Cursor_With_Index *positions, int *count, int pos){ + positions[*count].index = *count; + positions[*count].pos = pos; + ++*count; +} + +#define CursorSwap__(a,b) { Cursor_With_Index t = a; a = b; b = t; } + +internal_4tech void +buffer_quick_sort_cursors(Cursor_With_Index *positions, int start, int pivot){ + int i, mid; + int pivot_pos; + + mid = start; + pivot_pos = positions[pivot].pos; + for (i = mid; i < pivot; ++i){ + if (positions[i].pos < pivot_pos){ + CursorSwap__(positions[mid], positions[i]); + ++mid; + } + } + CursorSwap__(positions[mid], positions[pivot]); + + if (start < mid - 1) buffer_quick_sort_cursors(positions, start, mid - 1); + if (mid + 1 < pivot) buffer_quick_sort_cursors(positions, mid + 1, pivot); +} + +internal_4tech void +buffer_quick_unsort_cursors(Cursor_With_Index *positions, int start, int pivot){ + int i, mid; + int pivot_index; + + mid = start; + pivot_index = positions[pivot].index; + for (i = mid; i < pivot; ++i){ + if (positions[i].index < pivot_index){ + CursorSwap__(positions[mid], positions[i]); + ++mid; + } + } + CursorSwap__(positions[mid], positions[pivot]); + + if (start < mid - 1) buffer_quick_unsort_cursors(positions, start, mid - 1); + if (mid + 1 < pivot) buffer_quick_unsort_cursors(positions, mid + 1, pivot); +} + +#undef CursorSwap__ + +inline_4tech void +buffer_sort_cursors(Cursor_With_Index *positions, int count){ + assert_4tech(count > 0); + buffer_quick_sort_cursors(positions, 0, count-1); +} + +inline_4tech void +buffer_unsort_cursors(Cursor_With_Index *positions, int count){ + assert_4tech(count > 0); + buffer_quick_unsort_cursors(positions, 0, count-1); +} + +internal_4tech void +buffer_update_cursors(Cursor_With_Index *sorted_positions, int count, int start, int end, int len){ + Cursor_With_Index *position; + int shift_amount; + + shift_amount = (len - (end - start)); + + position = sorted_positions + count - 1; + for (; position >= sorted_positions && position->pos >= end; --position) position->pos += shift_amount; + for (; position >= sorted_positions && position->pos >= start; --position) position->pos = start; +} + +internal_4tech int +buffer_batch_debug_sort_check(Buffer_Edit *sorted_edits, int edit_count){ + Buffer_Edit *edit; + int i, result, start_point; + + result = 1; + start_point = 0; + + edit = sorted_edits; + for (i = 0; i < edit_count; ++i, ++edit){ + if (start_point > edit->start){ + result = 0; break; + } + start_point = (edit->end < edit->start + 1)?edit->start + 1:edit->end; + } + + return(result); +} + +internal_4tech int +buffer_batch_edit_max_shift(Buffer_Edit *sorted_edits, int edit_count){ + Buffer_Edit *edit; + int i, result; + int shift_total, shift_max; + + result = 0; + shift_total = 0; + shift_max = 0; + + edit = sorted_edits; + for (i = 0; i < edit_count; ++i, ++edit){ + shift_total += (edit->len - (edit->end - edit->start)); + if (shift_total > shift_max) shift_max = shift_total; + } + + return(shift_max); +} + +internal_4tech void +buffer_batch_edit_update_cursors(Cursor_With_Index *sorted_positions, int count, Buffer_Edit *sorted_edits, int edit_count){ + Cursor_With_Index *position, *end_position; + Buffer_Edit *edit, *end_edit; + int start, end; + int shift_amount; + + position = sorted_positions; + end_position = sorted_positions + count; + + edit = sorted_edits; + end_edit = sorted_edits + edit_count; + + shift_amount = 0; + + for (; edit < end_edit && position < end_position; ++edit){ + start = edit->start; + end = edit->end; + + for (; position->pos < start && position < end_position; ++position){ + position->pos += shift_amount; + } + + for (; position->pos < end && position < end_position; ++position){ + position->pos = start + shift_amount; + } + + shift_amount += (edit->len - (end - start)); + } + + for (; position < end_position; ++position){ + position->pos += shift_amount; + } +} + +internal_4tech int +eol_convert_in(char *data, int size){ + int i; + + for (i = 0; i < size; ++i){ + if (data[i] == '\r' && data[i+1] == '\n'){ + memmove_4tech(data + i, data + i + 1, size - i); + size -= 1; + } + } + + return(size); +} + +internal_4tech int +eol_convert_out(char *data, int size, int max, int *size_out){ + int result; + int i; + + // TODO(allen): iterative memory check + result = 1; + i = 0; + + for (; i < size; ++i){ + if (data[i] == '\n'){ + memmove_4tech(data + i + 1, data + i, size - i); + data[i] = '\r'; + ++i; + ++size; + } + } + + *size_out = size; + return(result); +} + inline_4tech int is_whitespace(char c){ int result; diff --git a/win32_4ed.cpp b/win32_4ed.cpp index 4417e0ab..0357ce68 100644 --- a/win32_4ed.cpp +++ b/win32_4ed.cpp @@ -54,7 +54,7 @@ #define FPS 30 #define FRAME_TIME (1000000 / FPS) -#define BUFFER_EXPERIMENT_SCALPEL 0 +#define BUFFER_EXPERIMENT_SCALPEL 1 #include "4ed_meta.h"