authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-05 11:40:22+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-05 10:51:45-08:00
log0b4bb9b84fddd8fe03fe15e25bd82babb455f2a6
tree062003511f4a45fb34f972a531a5ee2de3011920
parent7069459a76ad507bb1ce56b2d8de923b93385def

std.MultiArrayList: implement review comments


1 files changed, 7 insertions(+), 7 deletions(-)

lib/std/multi_array_list.zig+7-7
...@@ -20,7 +20,8 @@ pub fn MultiArrayList(comptime S: type) type {...@@ -20,7 +20,8 @@ pub fn MultiArrayList(comptime S: type) type {
20 pub const Field = meta.FieldEnum(S);20 pub const Field = meta.FieldEnum(S);
2121
22 pub const Slice = struct {22 pub const Slice = struct {
23 /// The index corresponds to sizes.bytes, not in field order.23 /// This array is indexed by the field index which can be obtained
24 /// by using @enumToInt() on the Field enum
24 ptrs: [fields.len][*]u8,25 ptrs: [fields.len][*]u8,
25 len: usize,26 len: usize,
26 capacity: usize,27 capacity: usize,
...@@ -57,8 +58,7 @@ pub fn MultiArrayList(comptime S: type) type {...@@ -57,8 +58,7 @@ pub fn MultiArrayList(comptime S: type) type {
5758
58 const fields = meta.fields(S);59 const fields = meta.fields(S);
59 /// `sizes.bytes` is an array of @sizeOf each S field. Sorted by alignment, descending.60 /// `sizes.bytes` is an array of @sizeOf each S field. Sorted by alignment, descending.
60 /// `sizes.indexes` is an array mapping from field to its index in the `sizes.bytes` array.61 /// `sizes.fields` is an array mapping from `sizes.bytes` array index to field index.
61 /// `sizes.fields` is an array with the field indexes of the `sizes.bytes` array.
62 const sizes = blk: {62 const sizes = blk: {
63 const Data = struct {63 const Data = struct {
64 size: usize,64 size: usize,
...@@ -81,16 +81,13 @@ pub fn MultiArrayList(comptime S: type) type {...@@ -81,16 +81,13 @@ pub fn MultiArrayList(comptime S: type) type {
81 var trash: i32 = undefined; // workaround for stage1 compiler bug81 var trash: i32 = undefined; // workaround for stage1 compiler bug
82 std.sort.sort(Data, &data, &trash, Sort.lessThan);82 std.sort.sort(Data, &data, &trash, Sort.lessThan);
83 var sizes_bytes: [fields.len]usize = undefined;83 var sizes_bytes: [fields.len]usize = undefined;
84 var sizes_indexes: [fields.len]usize = undefined;
85 var field_indexes: [fields.len]usize = undefined;84 var field_indexes: [fields.len]usize = undefined;
86 for (data) |elem, i| {85 for (data) |elem, i| {
87 sizes_bytes[i] = elem.size;86 sizes_bytes[i] = elem.size;
88 sizes_indexes[elem.size_index] = i;
89 field_indexes[i] = elem.size_index;87 field_indexes[i] = elem.size_index;
90 }88 }
91 break :blk .{89 break :blk .{
92 .bytes = sizes_bytes,90 .bytes = sizes_bytes,
93 .indexes = sizes_indexes,
94 .fields = field_indexes,91 .fields = field_indexes,
95 };92 };
96 };93 };
...@@ -183,8 +180,11 @@ pub fn MultiArrayList(comptime S: type) type {...@@ -183,8 +180,11 @@ pub fn MultiArrayList(comptime S: type) type {
183 capacityInBytes(new_len),180 capacityInBytes(new_len),
184 .exact,181 .exact,
185 ) catch {182 ) catch {
183 inline for (fields) |field_info, i| {
184 const field = @intToEnum(Field, i);
185 mem.set(field_info.field_type, self.slice().items(field)[new_len..], undefined);
186 }
186 self.len = new_len;187 self.len = new_len;
187 // TODO memset the invalidated items to undefined
188 return;188 return;
189 };189 };
190 var other = Self{190 var other = Self{