Skip to content

Commit dd9e8d4

Browse files
committed
[feature] linked list to textbox
1 parent 4b78815 commit dd9e8d4

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

include/ekg/io/utf.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <string>
3131
#include <vector>
3232
#include <regex>
33+
#include <list>
3334

3435
namespace ekg {
3536
/**
@@ -241,7 +242,7 @@ namespace ekg::io {
241242
namespace ekg {
242243
class text {
243244
protected:
244-
std::vector<ekg::io::chunk_t> loaded_chunks {};
245+
std::list<ekg::io::chunk_t> loaded_chunks {};
245246
size_t lines_per_chunk_limit {100000};
246247
size_t total_lines {};
247248
size_t total_chars {};
@@ -252,7 +253,7 @@ namespace ekg {
252253
bool should_count {};
253254
protected:
254255
void swizzle(
255-
size_t chunk_index,
256+
std::list<ekg::io::chunk_t>::iterator chunk_it,
256257
size_t line_index,
257258
std::vector<std::string> &to_swizzle,
258259
bool skip_first_line
@@ -282,7 +283,7 @@ namespace ekg {
282283
size_t end
283284
);
284285

285-
std::vector<ekg::io::chunk_t> &chunks_data();
286+
std::list<ekg::io::chunk_t> &chunks_data();
286287
size_t length_of_chunks();
287288

288289
std::string at(size_t index);
@@ -291,6 +292,7 @@ namespace ekg {
291292

292293
bool audited();
293294
void unset_audited();
295+
void gc();
294296
};
295297
}
296298

include/ekg/ui/textbox/textbox.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ namespace ekg {
114114

115115
size_t last_layers_select_size {};
116116
size_t view_line_index {UINT64_MAX};
117-
size_t view_chunk_index {UINT64_MAX};
117+
std::list<ekg::io::chunk_t>::iterator view_chunk_it {};
118118
size_t view_chunk_line_index {UINT64_MAX};
119119

120120
ekg::vec2_t<size_t> picked_left {UINT64_MAX, UINT64_MAX};

src/io/utf.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -483,16 +483,15 @@ void ekg::utf8_concat(
483483
}
484484

485485
void ekg::text::swizzle(
486-
size_t chunk_index,
486+
std::list<ekg::io::chunk_t>::iterator chunk_it,
487487
size_t line_index,
488488
std::vector<std::string> &to_swizzle,
489489
bool skip_first_line
490490
) {
491491
this->was_audited = true;
492-
493492
bool is_empty {to_swizzle.empty()};
494-
ekg::io::chunk_t &chunk {this->loaded_chunks.at(chunk_index)};
495493

494+
ekg::io::chunk_t &chunk {*chunk_it};
496495
if (skip_first_line) {
497496
chunk.at(line_index) = is_empty ? "" : to_swizzle.at(0);
498497
}
@@ -528,18 +527,20 @@ void ekg::text::swizzle(
528527

529528
for (size_t jt {}; jt < newly_chunks; jt++) {
530529
this->loaded_chunks.insert(
531-
this->loaded_chunks.begin() + chunk_index + jt + 1,
530+
chunk_it,
532531
ekg::io::chunk_t {
533532
to_swizzle_chunk.begin() + (this->lines_per_chunk_limit * (jt + 0)),
534533
to_swizzle_chunk.begin() + (this->lines_per_chunk_limit * (jt + 1))
535534
}
536535
);
536+
537+
chunk_it++;
537538
}
538539

539540
size_t rest {to_swizzle_chunk_size - (this->lines_per_chunk_limit * newly_chunks)};
540541
if (rest > 0) {
541542
this->loaded_chunks.insert(
542-
this->loaded_chunks.begin() + chunk_index + newly_chunks + 1,
543+
chunk_it,
543544
ekg::io::chunk_t {
544545
to_swizzle_chunk.begin() + (this->lines_per_chunk_limit * (newly_chunks + 0)),
545546
to_swizzle_chunk.end()
@@ -561,8 +562,8 @@ size_t ekg::text::set(size_t index, std::string_view line, ekg::io::chunk_t &spl
561562
ekg::utf8_split_endings(line, split_endings);
562563

563564
bool ok {};
564-
for (size_t it {}; it < this->loaded_chunks.size(); it++) {
565-
ekg::io::chunk_t &chunk {this->loaded_chunks.at(it)};
565+
for (std::list<ekg::io::chunk_t>::iterator it {this->loaded_chunks.begin()}; it != this->loaded_chunks.end(); it++) {
566+
ekg::io::chunk_t &chunk {*it};
566567

567568
previous_lines = current_lines;
568569
current_lines += (chunk_size = chunk.size());
@@ -591,8 +592,8 @@ std::string ekg::text::at(size_t index) {
591592
size_t previous_lines {};
592593
size_t chunk_size {};
593594

594-
for (size_t it {}; it < chunks_size; it++) {
595-
ekg::io::chunk_t &chunk {this->loaded_chunks.at(it)};
595+
for (std::list<ekg::io::chunk_t>::iterator it {this->loaded_chunks.begin()}; it != this->loaded_chunks.end(); it++) {
596+
ekg::io::chunk_t &chunk {*it};
596597
previous_lines = current_lines;
597598
current_lines += (chunk_size = chunk.size());
598599

@@ -632,8 +633,8 @@ void ekg::text::insert(
632633
}
633634

634635
size_t total_of_chunks {this->loaded_chunks.size()};
635-
for (size_t it {}; it < total_of_chunks; it++) {
636-
ekg::io::chunk_t &chunk {this->loaded_chunks.at(it)};
636+
for (std::list<ekg::io::chunk_t>::iterator it {this->loaded_chunks.begin()}; it != this->loaded_chunks.end(); it++) {
637+
ekg::io::chunk_t &chunk {*it};
637638

638639
previous_lines = current_lines;
639640
current_lines += (chunk_size = chunk.size());
@@ -681,8 +682,8 @@ std::string ekg::text::read(
681682
bool oka_end {};
682683

683684
std::string builder {};
684-
for (size_t it {}; it < total_of_chunks; it++) {
685-
ekg::io::chunk_t &chunk {this->loaded_chunks.at(it)};
685+
for (std::list<ekg::io::chunk_t>::iterator it {this->loaded_chunks.begin()}; it != this->loaded_chunks.end(); it++) {
686+
ekg::io::chunk_t &chunk {*it};
686687

687688
previous_lines = current_lines;
688689
current_lines += (chunk_size = chunk.size());
@@ -767,8 +768,8 @@ void ekg::text::erase(
767768
bool empty_chunk {};
768769
bool goto_next_chunk {};
769770

770-
for (size_t it {}; it < this->loaded_chunks.size(); it++) {
771-
ekg::io::chunk_t &chunk {this->loaded_chunks.at(it)};
771+
for (std::list<ekg::io::chunk_t>::iterator it {this->loaded_chunks.begin()}; it != this->loaded_chunks.end(); it++) {
772+
ekg::io::chunk_t &chunk {*it};
772773

773774
previous_lines = lines;
774775
lines += (chunk_size = chunk.size());
@@ -778,7 +779,7 @@ void ekg::text::erase(
778779
begin = begin - previous_lines;
779780

780781
while (remains_lines != 0) {
781-
ekg::io::chunk_t &chunk {this->loaded_chunks.at(it)};
782+
ekg::io::chunk_t &chunk {*it};
782783

783784
chunk_size = chunk.size();
784785
goto_next_chunk = begin + remains_lines > chunk.size();
@@ -791,12 +792,12 @@ void ekg::text::erase(
791792
empty_chunk = chunk.empty();
792793
if (empty_chunk) {
793794
this->loaded_chunks.erase(
794-
this->loaded_chunks.begin() + it
795+
it
795796
);
796797
}
797798

798799
if (goto_next_chunk) {
799-
it += !empty_chunk;
800+
if (!empty_chunk) it++;
800801
remains_lines -= chunk_size - begin;
801802
begin = 0;
802803
continue;
@@ -819,23 +820,24 @@ void ekg::text::push_back(std::string_view line) {
819820

820821
if (this->loaded_chunks.empty()) {
821822
this->loaded_chunks.emplace_back().emplace_back();
822-
this->swizzle(0, 0, splitted, true);
823+
this->swizzle(this->loaded_chunks.begin(), 0, splitted, true);
823824
return;
824825
}
825826

827+
std::list<ekg::io::chunk_t>::iterator last_it {--this->loaded_chunks.end()};
826828
ekg::io::chunk_t &last_chunk {
827-
this->loaded_chunks.back()
829+
*last_it
828830
};
829831

830832
this->swizzle(
831-
this->loaded_chunks.size() - 1,
833+
last_it,
832834
last_chunk.size() - !last_chunk.empty(),
833835
splitted,
834836
false
835837
);
836838
}
837839

838-
std::vector<ekg::io::chunk_t> &ekg::text::chunks_data() {
840+
std::list<ekg::io::chunk_t> &ekg::text::chunks_data() {
839841
return this->loaded_chunks;
840842
}
841843

src/ui/textbox/widget.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ bool ekg::ui::find_index_by_interact(
205205

206206
size_t chunk_size {};
207207
size_t il {};
208-
size_t addition_chunk_index {};
208+
size_t sum_chunk_sizes {};
209209
size_t text_total_lines {textbox.text.length_of_lines()};
210210

211211
ekg::vec2_t<float> rendered {};
@@ -216,7 +216,7 @@ bool ekg::ui::find_index_by_interact(
216216
uint8_t uc8 {};
217217
char32_t c32 {};
218218

219-
std::vector<ekg::io::chunk_t> &chunks {textbox.text.chunks_data()};
219+
std::list<ekg::io::chunk_t> &chunks {textbox.text.chunks_data()};
220220
size_t chunks_size {chunks.size()};
221221

222222
index.y += textbox.widget.view_line_index;
@@ -234,13 +234,9 @@ bool ekg::ui::find_index_by_interact(
234234
std::string empty {"\n"};
235235
bool is_empty {};
236236

237-
for (size_t ic {textbox.widget.view_chunk_index}; ic < chunks_size; ic++) {
238-
ekg::io::chunk_t &chunk {chunks.at(ic)};
239-
240-
il = 0;
241-
if (ic == textbox.widget.view_chunk_index) {
242-
il = textbox.widget.view_chunk_line_index;
243-
}
237+
il = textbox.widget.view_chunk_line_index;
238+
for (std::list<ekg::io::chunk_t>::iterator ic {textbox.widget.view_chunk_it}; ic != chunks.end(); ic++) {
239+
ekg::io::chunk_t &chunk {*ic};
244240

245241
chunk_size = chunk.size();
246242
for (;il < chunk_size; il++) {
@@ -302,6 +298,7 @@ bool ekg::ui::find_index_by_interact(
302298
}
303299
}
304300

301+
il = 0;
305302
index.x = 0;
306303
pos.x = textbox.color_scheme.gutter_margin;
307304

@@ -634,7 +631,7 @@ void ekg::ui::reload(
634631
textbox.rect.h = aligned_dimension.h * textbox.rect.scaled_height;
635632

636633
size_t highest_size {};
637-
std::vector<ekg::io::chunk_t> &chunks {textbox.text.chunks_data()};
634+
std::list<ekg::io::chunk_t> &chunks {textbox.text.chunks_data()};
638635

639636
ekg::rect_t<float> &rect_abs {
640637
ekg::ui::get_abs_rect(property, textbox.rect)
@@ -1353,13 +1350,13 @@ void ekg::ui::buffering(
13531350
size_t text_total_chars {textbox.text.length_of_chars()};
13541351
size_t text_total_lines {textbox.text.length_of_lines()};
13551352
size_t il {};
1356-
size_t addition_chunk_index {};
1353+
size_t sum_chunk_sizes {};
13571354
size_t chunk_size {};
13581355
ekg::vec2_t<size_t> index {};
13591356

13601357
ekg::pixel_t line_wsize {};
13611358
ekg::textbox_t::cursor_t cursor {};
1362-
std::vector<ekg::io::chunk_t> &chunks {textbox.text.chunks_data()};
1359+
std::list<ekg::io::chunk_t> &chunks {textbox.text.chunks_data()};
13631360
size_t chunks_size {chunks.size()};
13641361
bool is_renderable {};
13651362

@@ -1403,26 +1400,26 @@ void ekg::ui::buffering(
14031400
bool was_empty_before {};
14041401

14051402
textbox.widget.layers_select.clear();
1406-
for (size_t ic {}; ic < chunks_size; ic++) {
1407-
ekg::io::chunk_t &chunk {chunks.at(ic)};
1403+
for (std::list<ekg::io::chunk_t>::iterator ic {chunks.begin()}; ic != chunks.end(); ic++) {
1404+
ekg::io::chunk_t &chunk {*ic};
14081405
chunk_size = chunk.size();
1409-
if (addition_chunk_index + chunk_size < textbox.widget.view_line_index) {
1406+
if (sum_chunk_sizes + chunk_size < textbox.widget.view_line_index) {
14101407
pos.x = 0.0f;
14111408
index.y += chunk_size;
1412-
addition_chunk_index += chunk_size;
1409+
sum_chunk_sizes += chunk_size;
14131410
continue;
14141411
}
14151412

14161413
il = 0;
14171414
if (!oka_found_visual_index) {
1418-
textbox.widget.view_chunk_index = ic;
1419-
il = textbox.widget.view_line_index - addition_chunk_index;
1415+
textbox.widget.view_chunk_it = ic;
1416+
il = textbox.widget.view_line_index - sum_chunk_sizes;
14201417
textbox.widget.view_chunk_line_index = il;
14211418
index.y += il;
14221419
oka_found_visual_index = true;
14231420
}
14241421

1425-
addition_chunk_index += chunk_size;
1422+
sum_chunk_sizes += chunk_size;
14261423
for (;il < chunk_size; il++) {
14271424
std::string &chunk_line {chunk.at(il)};
14281425
std::string &line {(is_empty = chunk_line.empty()) ? empty : chunk_line};

0 commit comments

Comments
 (0)