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| {
|
for (materials.items) |material| {
|
||||||
// --- REGISTER SHADER ---
|
// --- REGISTER SHADER ---
|
||||||
const shader = shaders.getByKey(material.shader) orelse blk: {
|
const shader = try shaders.getOrPut(
|
||||||
const handle = try shaders.put(material.shader, .{ .raw = undefined });
|
material.shader,
|
||||||
break :blk shaders.get(handle).?;
|
.{ .raw = undefined },
|
||||||
};
|
);
|
||||||
_ = shader;
|
_ = shader;
|
||||||
|
|
||||||
// --- DEBUG ---
|
// --- DEBUG ---
|
||||||
|
|
|
||||||
|
|
@ -100,6 +100,29 @@ pub fn KeyedSlotMap(
|
||||||
return &entry.value;
|
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 {
|
pub fn getByKey(self: *Self, key: K) ?*V {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue