Implemented that each road, node in managers will be stored as pointers (allocated in heap)
This commit is contained in:
@@ -4,7 +4,7 @@ const Node = @import("node.zig").Node;
|
||||
|
||||
pub const RoadManager = struct {
|
||||
next_id: usize,
|
||||
roads: std.ArrayList(Road),
|
||||
roads: std.ArrayList(*Road),
|
||||
|
||||
pub fn init() RoadManager {
|
||||
return .{
|
||||
@@ -14,6 +14,9 @@ pub const RoadManager = struct {
|
||||
}
|
||||
|
||||
pub fn deinit(self: *RoadManager, allocator: std.mem.Allocator) void {
|
||||
for (self.roads.items) |road| {
|
||||
allocator.destroy(road);
|
||||
}
|
||||
self.roads.deinit(allocator);
|
||||
}
|
||||
|
||||
@@ -25,9 +28,11 @@ pub const RoadManager = struct {
|
||||
|
||||
pub fn addRoad(self: *RoadManager, allocator: std.mem.Allocator, start: *Node, end: *Node) !void {
|
||||
const road: Road = .init(self.getNextID(), start, end);
|
||||
try self.roads.append(allocator, road);
|
||||
const road_ptr = try allocator.create(Road);
|
||||
road_ptr.* = road;
|
||||
try self.roads.append(allocator, road_ptr);
|
||||
|
||||
const ref = &self.roads.items[self.roads.items.len - 1];
|
||||
const ref = self.roads.items[self.roads.items.len - 1];
|
||||
try start.referenceRoad(allocator, ref);
|
||||
try end.referenceRoad(allocator, ref);
|
||||
}
|
||||
@@ -39,7 +44,10 @@ pub const RoadManager = struct {
|
||||
return id;
|
||||
}
|
||||
|
||||
pub fn clear(self: *RoadManager) void {
|
||||
pub fn clear(self: *RoadManager, allocator: std.mem.Allocator) void {
|
||||
for (self.roads.items) |road| {
|
||||
allocator.destroy(road);
|
||||
}
|
||||
self.roads.clearRetainingCapacity();
|
||||
self.next_id = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user