feature. See also
. The project being documented here (as the example) is the Zig library itself.
Builder.getOrPutConstantAggregateAssumeCapacity
fn getOrPutConstantAggregateAssumeCapacity(
self: *Builder,
tag: Constant.Tag,
ty: Type,
vals: []const Constant,
) struct
File
Code
fn getOrPutConstantAggregateAssumeCapacity(
self: *Builder,
tag: Constant.Tag,
ty: Type,
vals: []const Constant,
) struct { new: bool, constant: Constant } {
switch (tag) {
.structure, .packed_structure, .array, .vector => {},
else => unreachable,
}
const Key = struct { tag: Constant.Tag, type: Type, vals: []const Constant };
const Adapter = struct {
builder: *const Builder,
pub fn hash(_: @This(), key: Key) u32 {
var hasher = std.hash.Wyhash.init(std.hash.int(@backingInt(key.tag)));
hasher.update(std.mem.asBytes(&key.type));
hasher.update(std.mem.sliceAsBytes(key.vals));
return @truncate(hasher.final());
}
pub fn eql(ctx: @This(), lhs_key: Key, _: void, rhs_index: usize) bool {
if (lhs_key.tag != ctx.builder.constant_items.items(.tag)[rhs_index]) return false;
const rhs_data = ctx.builder.constant_items.items(.data)[rhs_index];
var rhs_extra = ctx.builder.constantExtraDataTrail(Constant.Aggregate, rhs_data);
if (lhs_key.type != rhs_extra.data.type) return false;
const rhs_vals = rhs_extra.trail.next(@intCast(lhs_key.vals.len), Constant, ctx.builder);
return std.mem.eql(Constant, lhs_key.vals, rhs_vals);
}
};
const gop = self.constant_map.getOrPutAssumeCapacityAdapted(
Key{ .tag = tag, .type = ty, .vals = vals },
Adapter{ .builder = self },
);
if (!gop.found_existing) {
gop.key_ptr.* = {};
gop.value_ptr.* = {};
self.constant_items.appendAssumeCapacity(.{
.tag = tag,
.data = self.addConstantExtraAssumeCapacity(Constant.Aggregate{ .type = ty }),
});
self.constant_extra.appendSliceAssumeCapacity(@ptrCast(vals));
}
return .{ .new = !gop.found_existing, .constant = @fromBackingInt(@intCast(gop.index)) };
}