forked from M-Labs/nac3
1
0
Fork 0

WIP: list assignment

This commit is contained in:
lyken 2024-08-25 23:14:49 +08:00
parent e62509ae67
commit 9b02e144b5
No known key found for this signature in database
GPG Key ID: 3BD5FC6AC8325DD8
1 changed files with 40 additions and 0 deletions

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <irrt/debug.hpp>
#include <irrt/exception.hpp>
#include <irrt/int_types.hpp> #include <irrt/int_types.hpp>
#include <irrt/slice.hpp> #include <irrt/slice.hpp>
@ -16,4 +18,42 @@ template <typename SizeT> struct List
uint8_t *items; uint8_t *items;
SizeT len; SizeT len;
}; };
namespace list
{
template <typename SizeT> void range_assign(List<SizeT> *dst, SizeT itemsize, Range<SizeT> *range, List<SizeT> *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 } // namespace