Type Function EnumArray [src]
An array keyed by an enum, backed by a dense array.
If the enum is not dense, a mapping will be constructed from
enum values to dense indices. This type does no dynamic
allocation and can be copied by value.
Prototype
pub fn EnumArray(comptime E: type, comptime V: type) type
Parameters
E: type
V: type
Source
pub fn EnumArray(comptime E: type, comptime V: type) type {
return struct {
const Self = @This();
/// The index mapping for this map
pub const Indexer = EnumIndexer(E);
/// The key type used to index this map
pub const Key = Indexer.Key;
/// The value type stored in this map
pub const Value = V;
/// The number of possible keys in the map
pub const len = Indexer.count;
values: [Indexer.count]Value,
pub fn init(init_values: EnumFieldStruct(E, Value, null)) Self {
return initDefault(null, init_values);
}
/// Initializes values in the enum array, with the specified default.
pub fn initDefault(comptime default: ?Value, init_values: EnumFieldStruct(E, Value, default)) Self {
@setEvalBranchQuota(2 * @typeInfo(E).@"enum".fields.len);
var result: Self = .{ .values = undefined };
inline for (0..Self.len) |i| {
const key = comptime Indexer.keyForIndex(i);
const tag = @tagName(key);
result.values[i] = @field(init_values, tag);
}
return result;
}
pub fn initUndefined() Self {
return Self{ .values = undefined };
}
pub fn initFill(v: Value) Self {
var self: Self = undefined;
@memset(&self.values, v);
return self;
}
/// Returns the value in the array associated with a key.
pub fn get(self: Self, key: Key) Value {
return self.values[Indexer.indexOf(key)];
}
/// Returns a pointer to the slot in the array associated with a key.
pub fn getPtr(self: *Self, key: Key) *Value {
return &self.values[Indexer.indexOf(key)];
}
/// Returns a const pointer to the slot in the array associated with a key.
pub fn getPtrConst(self: *const Self, key: Key) *const Value {
return &self.values[Indexer.indexOf(key)];
}
/// Sets the value in the slot associated with a key.
pub fn set(self: *Self, key: Key, value: Value) void {
self.values[Indexer.indexOf(key)] = value;
}
/// Iterates over the items in the array, in index order.
pub fn iterator(self: *Self) Iterator {
return .{
.values = &self.values,
};
}
/// An entry in the array.
pub const Entry = struct {
/// The key associated with this entry.
/// Modifying this key will not change the array.
key: Key,
/// A pointer to the value in the array associated
/// with this key. Modifications through this
/// pointer will modify the underlying data.
value: *Value,
};
pub const Iterator = struct {
index: usize = 0,
values: *[Indexer.count]Value,
pub fn next(self: *Iterator) ?Entry {
const index = self.index;
if (index < Indexer.count) {
self.index += 1;
return Entry{
.key = Indexer.keyForIndex(index),
.value = &self.values[index],
};
}
return null;
}
};
};
}