mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-07 14:24:43 +00:00
std.MultiArrayList: implement review comments
This commit is contained in:
parent
7069459a76
commit
0b4bb9b84f
1 changed files with 7 additions and 7 deletions
|
|
@ -20,7 +20,8 @@ pub fn MultiArrayList(comptime S: type) type {
|
||||||
pub const Field = meta.FieldEnum(S);
|
pub const Field = meta.FieldEnum(S);
|
||||||
|
|
||||||
pub const Slice = struct {
|
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,
|
ptrs: [fields.len][*]u8,
|
||||||
len: usize,
|
len: usize,
|
||||||
capacity: usize,
|
capacity: usize,
|
||||||
|
|
@ -57,8 +58,7 @@ pub fn MultiArrayList(comptime S: type) type {
|
||||||
|
|
||||||
const fields = meta.fields(S);
|
const fields = meta.fields(S);
|
||||||
/// `sizes.bytes` is an array of @sizeOf each S field. Sorted by alignment, descending.
|
/// `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 mapping from `sizes.bytes` array index to field index.
|
||||||
/// `sizes.fields` is an array with the field indexes of the `sizes.bytes` array.
|
|
||||||
const sizes = blk: {
|
const sizes = blk: {
|
||||||
const Data = struct {
|
const Data = struct {
|
||||||
size: usize,
|
size: usize,
|
||||||
|
|
@ -81,16 +81,13 @@ pub fn MultiArrayList(comptime S: type) type {
|
||||||
var trash: i32 = undefined; // workaround for stage1 compiler bug
|
var trash: i32 = undefined; // workaround for stage1 compiler bug
|
||||||
std.sort.sort(Data, &data, &trash, Sort.lessThan);
|
std.sort.sort(Data, &data, &trash, Sort.lessThan);
|
||||||
var sizes_bytes: [fields.len]usize = undefined;
|
var sizes_bytes: [fields.len]usize = undefined;
|
||||||
var sizes_indexes: [fields.len]usize = undefined;
|
|
||||||
var field_indexes: [fields.len]usize = undefined;
|
var field_indexes: [fields.len]usize = undefined;
|
||||||
for (data) |elem, i| {
|
for (data) |elem, i| {
|
||||||
sizes_bytes[i] = elem.size;
|
sizes_bytes[i] = elem.size;
|
||||||
sizes_indexes[elem.size_index] = i;
|
|
||||||
field_indexes[i] = elem.size_index;
|
field_indexes[i] = elem.size_index;
|
||||||
}
|
}
|
||||||
break :blk .{
|
break :blk .{
|
||||||
.bytes = sizes_bytes,
|
.bytes = sizes_bytes,
|
||||||
.indexes = sizes_indexes,
|
|
||||||
.fields = field_indexes,
|
.fields = field_indexes,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
@ -183,8 +180,11 @@ pub fn MultiArrayList(comptime S: type) type {
|
||||||
capacityInBytes(new_len),
|
capacityInBytes(new_len),
|
||||||
.exact,
|
.exact,
|
||||||
) catch {
|
) 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;
|
self.len = new_len;
|
||||||
// TODO memset the invalidated items to undefined
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
var other = Self{
|
var other = Self{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue