Added getOrPut & getOrPutHandle
This commit is contained in:
parent
7e1f1c5e4a
commit
3c7fcfcb12
2 changed files with 27 additions and 4 deletions
|
|
@ -71,10 +71,10 @@ test "SlotMap" {
|
|||
|
||||
for (materials.items) |material| {
|
||||
// --- REGISTER SHADER ---
|
||||
const shader = shaders.getByKey(material.shader) orelse blk: {
|
||||
const handle = try shaders.put(material.shader, .{ .raw = undefined });
|
||||
break :blk shaders.get(handle).?;
|
||||
};
|
||||
const shader = try shaders.getOrPut(
|
||||
material.shader,
|
||||
.{ .raw = undefined },
|
||||
);
|
||||
_ = shader;
|
||||
|
||||
// --- DEBUG ---
|
||||
|
|
|
|||
|
|
@ -100,6 +100,29 @@ pub fn KeyedSlotMap(
|
|||
return &entry.value;
|
||||
}
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
pub fn getOrPut(
|
||||
self: *Self,
|
||||
key: K,
|
||||
value: V,
|
||||
) !*V {
|
||||
if (self.getByKey(key)) |v| return v;
|
||||
const handle = try self.put(key, value);
|
||||
return self.get(handle).?;
|
||||
}
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
pub fn getOrPutHandle(
|
||||
self: *Self,
|
||||
key: K,
|
||||
value: V,
|
||||
) !*Handle(V) {
|
||||
if (self.getHandle(key)) |h| return h;
|
||||
return try self.put(key, value);
|
||||
}
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
pub fn getByKey(self: *Self, key: K) ?*V {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue