tiny.choir.NamedAttributeList
Defined in ir.attribute.
API (14)
Actions
Public operations.
capacityclearRetainingCapacitydeinitensureTotalCapacityerasefindIndexOrInsertPosgetgetNameditemsset
Types and contracts
Public types and contracts.
Fields and members
Public fields and members.
Source
Source: lib/choir/src/core/attribute.zig:219
zig
pub const NamedAttributeList = struct { const small_capacity = 4; len: usize = 0, owned_items: []NamedAttribute = &.{}, small_items: [small_capacity]NamedAttribute = undefined, pub const Lookup = struct { found: bool, index: usize, }; pub fn items(self: *const NamedAttributeList) []const NamedAttribute { if (self.owned_items.len != 0) return self.owned_items[0..self.len]; return self.small_items[0..self.len]; } pub fn capacity(self: *const NamedAttributeList) usize { if (self.owned_items.len != 0) return self.owned_items.len; return small_capacity; } fn storage(self: *NamedAttributeList) []NamedAttribute { if (self.owned_items.len != 0) return self.owned_items; return self.small_items[0..]; } pub fn deinit(self: *NamedAttributeList, allocator: std.mem.Allocator) void { if (self.owned_items.len != 0) allocator.free(self.owned_items); self.len = 0; self.owned_items = &.{}; } pub fn clearRetainingCapacity(self: *NamedAttributeList) void { self.len = 0; } pub fn ensureTotalCapacity( self: *NamedAttributeList, allocator: std.mem.Allocator, target_capacity: usize, ) std.mem.Allocator.Error!void { if (target_capacity <= self.capacity()) return; if (self.owned_items.len != 0) { self.owned_items = try allocator.realloc(self.owned_items, target_capacity); return; } const allocated_items = try allocator.alloc(NamedAttribute, target_capacity); @memcpy(allocated_items[0..self.len], self.small_items[0..self.len]); self.owned_items = allocated_items; } pub fn get(self: *const NamedAttributeList, name: []const u8) ?Attribute { const result = self.findIndexOrInsertPos(name); if (!result.found) return null; return self.items()[result.index].value; } pub fn getNamed(self: *const NamedAttributeList, name: []const u8) ?NamedAttribute { const result = self.findIndexOrInsertPos(name); if (!result.found) return null; return self.items()[result.index]; } pub fn set( self: *NamedAttributeList, allocator: std.mem.Allocator, attr_name: []const u8, value: Attribute, ) !?Attribute { const result = self.findIndexOrInsertPos(attr_name); if (result.found) { const storage_items = self.storage(); const previous = storage_items[result.index].value; storage_items[result.index].value = value; return previous; } const required_capacity = std.math.add(usize, self.len, 1) catch return error.OutOfMemory; if (required_capacity > self.capacity()) { try self.ensureTotalCapacity( allocator, @max(required_capacity, self.capacity() *| 2), ); } const storage_items = self.storage(); std.mem.copyBackwards(NamedAttribute, storage_items[result.index + 1 .. self.len + 1], storage_items[result.index..self.len]); storage_items[result.index] = .{ .name = attr_name, .value = value }; self.len += 1; return null; } pub fn erase(self: *NamedAttributeList, attr_name: []const u8) ?Attribute { const result = self.findIndexOrInsertPos(attr_name); if (!result.found) return null; const storage_items = self.storage(); const previous = storage_items[result.index].value; std.mem.copyForwards(NamedAttribute, storage_items[result.index .. self.len - 1], storage_items[result.index + 1 .. self.len]); self.len -= 1; return previous; } pub fn findIndexOrInsertPos(self: *const NamedAttributeList, name: []const u8) Lookup { var left: usize = 0; const current_items = self.items(); var right: usize = current_items.len; while (left < right) { const mid = left + (right - left) / 2; const cmp = std.mem.order(u8, current_items[mid].name, name); switch (cmp) { .eq => return .{ .found = true, .index = mid }, .lt => left = mid + 1, .gt => right = mid, } } return .{ .found = false, .index = left }; }};Source: lib/choir/src/root.zig:52
zig
pub const NamedAttributeList = ir.NamedAttributeList;Also reachable as
backends.wasm.emission.module_encoding.common.ir.NamedAttributeList, backends.wasm.emission.module_encoding.common.ir.attribute.NamedAttributeList, ir.NamedAttributeList.
Audit
| Definitions | 12 |
|---|---|
| Public names | 60 |
| Members | 5 |
| Version | 26.7.0 |
| Revision | daab053ee433 |