From 9b02e144b5f1392e96a197a4d758444d9739c3e2 Mon Sep 17 00:00:00 2001 From: lyken Date: Sun, 25 Aug 2024 23:14:49 +0800 Subject: [PATCH] WIP: list assignment --- nac3core/irrt/irrt/list.hpp | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/nac3core/irrt/irrt/list.hpp b/nac3core/irrt/irrt/list.hpp index 25011f11..8bbca0be 100644 --- a/nac3core/irrt/irrt/list.hpp +++ b/nac3core/irrt/irrt/list.hpp @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include @@ -16,4 +18,42 @@ template struct List uint8_t *items; SizeT len; }; + +namespace list +{ +template void range_assign(List *dst, SizeT itemsize, Range *range, List *src) +{ + debug_assert(range->step != 0); + SizeT assign_len = range->len(); + + if (assign_len < src->len) + { + // Encountered things like + // ``` + // xs = [1, 2, 3, 4, 5] + // xs[1:3] = [999, 1000, 1001, 1002] # Note that step has to be 1. + // xs = [1, 999, 1000, 1001, 1002, 4, 5] # xs is longer + // ``` + // + // We do not support extending lists since that requires allocation. + raise_exception(SizeT, EXN_VALUE_ERROR, + "List assignment does not support list extension. Attempting to assign {0} item(s) into a " + "space of {1} item(s).", + src->len, assign_len, NO_PARAM); + } + + if (range->step == 1) + { + // Assigning into a contiguous region. Optimized with memmove. + + uint8_t* p1 = dst->items + range->start * itemsize; + uint8_t* p2 = dst->items + range->start * itemsize + assign_len * itemsize; + + __builtin_memmove(cursor, src->items, assign_len * itemsize); + cursor += range_len * itemsize; + + __builtin_memmove(cursor, ); + } +} +} // namespace list } // namespace \ No newline at end of file