buffer reorganization

master
Allen Webster 2015-10-25 21:51:08 -04:00
parent d3d02da40e
commit 20706a2e5d
7 changed files with 215 additions and 233 deletions

View File

@ -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

View File

@ -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{

View File

@ -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

View File

@ -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

View File

@ -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;
}

View File

@ -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;

View File

@ -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"