From 306536c214e8c1881afb0210598e9c2bc020cff4 Mon Sep 17 00:00:00 2001 From: Andreew Gregory Date: Tue, 21 Apr 2026 14:55:26 +0300 Subject: [PATCH] Implemented expand method for Vec of primitive T. FINALLY. On my way to make Lucy better --- src/l1/codegen/util_template_inst.h | 19 ++++++++++++++++++- src/l2/alice/engine.h | 2 +- src/l2/lucy/glyph_cache.h | 8 ++------ src/l3/r4/r4b.c | 1 + 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/l1/codegen/util_template_inst.h b/src/l1/codegen/util_template_inst.h index 6672164..68aa41f 100644 --- a/src/l1/codegen/util_template_inst.h +++ b/src/l1/codegen/util_template_inst.h @@ -18,7 +18,6 @@ void codegen_append_VecT_struct(VecU8* res, SpanU8 T, bool skip_declaration_gen) "};\n\n", T, T); } -// todo: add resize method /* if !primitive, requires methods T_drop, and, if also clonable, requires method T_clone */ void codegen_append_VecT_base_methods(VecU8* res, SpanU8 T, bool primitive, bool clonable) { VecU8_append_fmt(res, "#define Vec%s_new() ((Vec%s){ 0 })\n\n", T, T); @@ -73,6 +72,24 @@ void codegen_append_VecT_base_methods(VecU8* res, SpanU8 T, bool primitive, bool SPACE SPACE "%s_drop(self->buf[i]);\n", /* T */ T)); + if (primitive) { + /* It is not a strict resize. If we are currently longer than new_length, we do nothing. + * New space gets initialized with garbage */ + VecU8_append_fmt(res, + "void Vec%s_expand(Vec%s* self, size_t new_length, %s filler) {\n" /* T, T, T */ + SPACE "if (self->len >= new_length)\n" + SPACE SPACE "return;\n" + SPACE "if (new_length > self->capacity) {\n" + SPACE SPACE "size_t new_capacity = Vec_get_new_capacity(self->capacity, new_length);\n" + SPACE SPACE "self->buf = (%s*)safe_realloc(self->buf, new_capacity * sizeof(%s));\n" /* T, T */ + SPACE SPACE "self->capacity = new_capacity;\n" + SPACE "}\n" + SPACE "for (size_t i = self->len; i < new_length; i++)\n" + SPACE SPACE "self->buf[i] = filler;\n" + SPACE "self->len = new_length;\n" + "}\n\n", T, T, T, T, T); + } + if (clonable) { VecU8_append_fmt(res, "NODISCARD Vec%s Vec%s_clone(const Vec%s* self) {\n" /* T, T, T */ diff --git a/src/l2/alice/engine.h b/src/l2/alice/engine.h index 3048372..b160aa9 100644 --- a/src/l2/alice/engine.h +++ b/src/l2/alice/engine.h @@ -1635,7 +1635,7 @@ Alice* Alice_new(){ return alice; } -/* Helper function. Automatically aquires the slot */ +/* Helper function. Automatically acquires the slot */ U32 Alice__find_free_texture_slot(Alice* alice) { for (U32 i = 0; i < alice->texture_slots.len; i++) { if (!alice->texture_slots.buf[i].is_some) { diff --git a/src/l2/lucy/glyph_cache.h b/src/l2/lucy/glyph_cache.h index 2a62ac8..b83b8f9 100644 --- a/src/l2/lucy/glyph_cache.h +++ b/src/l2/lucy/glyph_cache.h @@ -306,12 +306,8 @@ void LucyGlyphCache_add_glyphs(LucyGlyphCache* cache, VecLucyGlyphCachingRequest starting_x = 0; goto one_more_chance; } - // todo: replace with resize method - // todo: Add expand method to vector - for (U32 h = img_width; h < new_width_required; h++) { - VecU32_append(&landscape, 0); - } - img_width = MAX_U64(img_width, new_width_required); + VecU32_expand(&landscape, new_width_required, 0); + img_width = landscape.len; U32 height_here = 0; for (size_t x = 0; x < p_glyph->bitmap.width; x++) { height_here = MAX_U32(height_here, *VecU32_at(&landscape, starting_x + x)); diff --git a/src/l3/r4/r4b.c b/src/l3/r4/r4b.c index 386f031..3aeac28 100644 --- a/src/l3/r4/r4b.c +++ b/src/l3/r4/r4b.c @@ -341,6 +341,7 @@ void run_app(){ }); ranges_needed = VecU32Segment_new(); VecU32Segment_append(&ranges_needed, (U32Segment){.start = 0x430, .len = 0x44f - 0x430 + 1}); + VecU32Segment_append(&ranges_needed, (U32Segment){.start = 0x410, .len = 0x42f - 0x410 + 1}); VecLucyGlyphCachingRequest_append(&lucy_requests, (LucyGlyphCachingRequest){ .sized_face = st.font_face_of_size_40, .codepoint_ranges = ranges_needed, });