Skip to content

Commit

Permalink
chore(relations): set some initial capacity upfront to avoid many calls
Browse files Browse the repository at this point in the history
  • Loading branch information
yamafaktory committed Sep 12, 2024
1 parent 9f87e70 commit fb81fb3
Showing 1 changed file with 10 additions and 54 deletions.
64 changes: 10 additions & 54 deletions src/hypergraphz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -122,39 +122,18 @@ pub fn HypergraphZ(comptime H: type, comptime V: type) type {
self.* = undefined;
}

const EntityTag = enum { arrayList, arrayHash };
const EntityUnion = union(EntityTag) {
arrayList: *EntityArrayList(H),
arrayHash: *EntityArrayHashMap(V),
};
/// Internal method to initialize entity relations if necessary.
fn _initRelationsIfEmpty(self: Self, entity: EntityUnion) void {
switch (entity) {
.arrayList => |a| {
if (a.relations.items.len == 0) {
a.relations = ArrayList(Uuid).init(self.allocator);
}
},
.arrayHash => |a| {
if (a.relations.count() == 0) {
a.relations = AutoArrayHashMap(Uuid, void).init(self.allocator);
}
},
}
}

/// Create a new hyperedge.
pub fn createHyperedge(self: *Self, hyperedge: H) HypergraphZError!Uuid {
const id = uuid.v7.new();
try self.hyperedges.put(id, .{ .relations = undefined, .data = hyperedge });
try self.hyperedges.put(id, .{ .relations = ArrayList(Uuid).init(self.allocator), .data = hyperedge });

return id;
}

/// Create a new hyperedge assuming there is enough capacity.
pub fn createHyperedgeAssumeCapacity(self: *Self, hyperedge: H) Uuid {
const id = uuid.v7.new();
self.hyperedges.putAssumeCapacity(id, .{ .relations = undefined, .data = hyperedge });
self.hyperedges.putAssumeCapacity(id, .{ .relations = ArrayList(Uuid).init(self.allocator), .data = hyperedge });

return id;
}
Expand All @@ -167,15 +146,15 @@ pub fn HypergraphZ(comptime H: type, comptime V: type) type {
/// Create a new vertex.
pub fn createVertex(self: *Self, vertex: V) HypergraphZError!Uuid {
const id = uuid.v7.new();
try self.vertices.put(id, .{ .relations = undefined, .data = vertex });
try self.vertices.put(id, .{ .relations = AutoArrayHashMap(Uuid, void).init(self.allocator), .data = vertex });

return id;
}

/// Create a new vertex assuming there is enough capacity.
pub fn createVertexAssumeCapacity(self: *Self, vertex: V) Uuid {
const id = uuid.v7.new();
self.vertices.putAssumeCapacity(id, .{ .relations = undefined, .data = vertex });
self.vertices.putAssumeCapacity(id, .{ .relations = AutoArrayHashMap(Uuid, void).init(self.allocator), .data = vertex });

return id;
}
Expand Down Expand Up @@ -483,15 +462,12 @@ pub fn HypergraphZ(comptime H: type, comptime V: type) type {
try self.checkIfHyperedgeExists(hyperedge_id);
try self.checkIfVertexExists(vertex_id);

const hyperedge = self.hyperedges.getPtr(hyperedge_id).?;
self._initRelationsIfEmpty(EntityUnion{ .arrayList = hyperedge });

// Append vertex to hyperedge relations.
const hyperedge = self.hyperedges.getPtr(hyperedge_id).?;
try hyperedge.relations.append(vertex_id);

// Add hyperedge to vertex relations.
const vertex = self.vertices.getPtr(vertex_id).?;
self._initRelationsIfEmpty(EntityUnion{ .arrayHash = vertex });

try vertex.relations.put(hyperedge_id, {});

debug("vertex {} appended to hyperedge {}", .{
Expand All @@ -505,15 +481,12 @@ pub fn HypergraphZ(comptime H: type, comptime V: type) type {
try self.checkIfHyperedgeExists(hyperedge_id);
try self.checkIfVertexExists(vertex_id);

const hyperedge = self.hyperedges.getPtr(hyperedge_id).?;
self._initRelationsIfEmpty(EntityUnion{ .arrayList = hyperedge });

// Prepend vertex to hyperedge relations.
const hyperedge = self.hyperedges.getPtr(hyperedge_id).?;
try hyperedge.relations.insertSlice(0, &.{vertex_id});

// Add hyperedge to vertex relations.
const vertex = self.vertices.getPtr(vertex_id).?;
self._initRelationsIfEmpty(EntityUnion{ .arrayHash = vertex });

try vertex.relations.put(hyperedge_id, {});

debug("vertex {} prepended to hyperedge {}", .{
Expand All @@ -531,14 +504,11 @@ pub fn HypergraphZ(comptime H: type, comptime V: type) type {
if (index > hyperedge.relations.items.len) {
return HypergraphZError.IndexOutOfBounds;
}
self._initRelationsIfEmpty(EntityUnion{ .arrayList = hyperedge });

// Insert vertex into hyperedge relations at given index.
try hyperedge.relations.insert(index, vertex_id);

const vertex = self.vertices.getPtr(vertex_id).?;
self._initRelationsIfEmpty(EntityUnion{ .arrayHash = vertex });

try vertex.relations.put(hyperedge_id, {});

debug("vertex {} inserted into hyperedge {} at index {}", .{
Expand All @@ -560,17 +530,12 @@ pub fn HypergraphZ(comptime H: type, comptime V: type) type {
try self.checkIfVertexExists(v);
}

const hyperedge = self.hyperedges.getPtr(hyperedge_id).?;
self._initRelationsIfEmpty(EntityUnion{ .arrayList = hyperedge });

// Append vertices to hyperedge relations.
const hyperedge = self.hyperedges.getPtr(hyperedge_id).?;
try hyperedge.relations.appendSlice(vertex_ids);

for (vertex_ids) |id| {
const vertex = self.vertices.getPtr(id).?;

self._initRelationsIfEmpty(EntityUnion{ .arrayHash = vertex });

try vertex.relations.put(hyperedge_id, {});
}

Expand All @@ -589,17 +554,12 @@ pub fn HypergraphZ(comptime H: type, comptime V: type) type {
try self.checkIfVertexExists(v);
}

const hyperedge = self.hyperedges.getPtr(hyperedge_id).?;
self._initRelationsIfEmpty(EntityUnion{ .arrayList = hyperedge });

// Prepend vertices to hyperedge relations.
const hyperedge = self.hyperedges.getPtr(hyperedge_id).?;
try hyperedge.relations.insertSlice(0, vertices_ids);

for (vertices_ids) |id| {
const vertex = self.vertices.getPtr(id).?;

self._initRelationsIfEmpty(EntityUnion{ .arrayHash = vertex });

try vertex.relations.put(hyperedge_id, {});
}

Expand All @@ -622,16 +582,12 @@ pub fn HypergraphZ(comptime H: type, comptime V: type) type {
if (index > hyperedge.relations.items.len) {
return HypergraphZError.IndexOutOfBounds;
}
self._initRelationsIfEmpty(EntityUnion{ .arrayList = hyperedge });

// Prepend vertices to hyperedge relations.
try hyperedge.relations.insertSlice(index, vertices_ids);

for (vertices_ids) |id| {
const vertex = self.vertices.getPtr(id).?;

self._initRelationsIfEmpty(EntityUnion{ .arrayHash = vertex });

try vertex.relations.put(hyperedge_id, {});
}

Expand Down

0 comments on commit fb81fb3

Please sign in to comment.