zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 0b4bb9b84fddd8fe03fe15e25bd82babb455f2a6 (tree)
parent 7069459a76ad507bb1ce56b2d8de923b93385def
Author: Isaac Freund <ifreund@ifreund.xyz>
Date:   Fri,  5 Feb 2021 11:40:22 +0100

std.MultiArrayList: implement review comments

Diffstat:
Mlib/std/multi_array_list.zig | 14+++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/lib/std/multi_array_list.zig b/lib/std/multi_array_list.zig @@ -20,7 +20,8 @@ pub fn MultiArrayList(comptime S: type) type { pub const Field = meta.FieldEnum(S); pub const Slice = struct { - /// The index corresponds to sizes.bytes, not in field order. + /// This array is indexed by the field index which can be obtained + /// by using @enumToInt() on the Field enum ptrs: [fields.len][*]u8, len: usize, capacity: usize, @@ -57,8 +58,7 @@ pub fn MultiArrayList(comptime S: type) type { const fields = meta.fields(S); /// `sizes.bytes` is an array of @sizeOf each S field. Sorted by alignment, descending. - /// `sizes.indexes` is an array mapping from field to its index in the `sizes.bytes` array. - /// `sizes.fields` is an array with the field indexes of the `sizes.bytes` array. + /// `sizes.fields` is an array mapping from `sizes.bytes` array index to field index. const sizes = blk: { const Data = struct { size: usize, @@ -81,16 +81,13 @@ pub fn MultiArrayList(comptime S: type) type { var trash: i32 = undefined; // workaround for stage1 compiler bug std.sort.sort(Data, &data, &trash, Sort.lessThan); var sizes_bytes: [fields.len]usize = undefined; - var sizes_indexes: [fields.len]usize = undefined; var field_indexes: [fields.len]usize = undefined; for (data) |elem, i| { sizes_bytes[i] = elem.size; - sizes_indexes[elem.size_index] = i; field_indexes[i] = elem.size_index; } break :blk .{ .bytes = sizes_bytes, - .indexes = sizes_indexes, .fields = field_indexes, }; }; @@ -183,8 +180,11 @@ pub fn MultiArrayList(comptime S: type) type { capacityInBytes(new_len), .exact, ) catch { + inline for (fields) |field_info, i| { + const field = @intToEnum(Field, i); + mem.set(field_info.field_type, self.slice().items(field)[new_len..], undefined); + } self.len = new_len; - // TODO memset the invalidated items to undefined return; }; var other = Self{