diff --git a/zig-ecs/src/ecs/component_storage.zig b/zig-ecs/src/ecs/component_storage.zig index 9d53f38..6a2556d 100644 --- a/zig-ecs/src/ecs/component_storage.zig +++ b/zig-ecs/src/ecs/component_storage.zig @@ -187,10 +187,15 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type) type { }; /// Direct access to the array of entities - pub fn data(self: Self) *const []EntityT { + pub fn data(self: Self) []const EntityT { return self.set.data(); } + /// Direct access to the array of entities + pub fn dataPtr(self: Self) *const []EntityT { + return self.set.dataPtr(); + } + /// Swaps entities and objects in the internal packed arrays pub fn swap(self: *Self, lhs: EntityT, rhs: EntityT) void { self.safe_swap(self, lhs, rhs); @@ -240,7 +245,7 @@ test "iterate" { store.add(5, 66.45); store.add(7, 66.45); - for (store.data().*) |entity, i| { + for (store.data()) |entity, i| { if (i == 0) std.testing.expectEqual(entity, 3); if (i == 1) diff --git a/zig-ecs/src/ecs/groups.zig b/zig-ecs/src/ecs/groups.zig index d2ff8f6..5ccaace 100644 --- a/zig-ecs/src/ecs/groups.zig +++ b/zig-ecs/src/ecs/groups.zig @@ -14,22 +14,18 @@ pub const BasicGroup = struct { group_data: *Registry.GroupData, pub const Iterator = struct { - group: *Self, index: usize = 0, - entities: *const []Entity, + entities: []const Entity, - pub fn init(group: *Self) Iterator { - return .{ - .group = group, - .entities = group.group_data.entity_set.data(), - }; + pub fn init(entities: []const Entity) Iterator { + return .{ .entities = entities }; } pub fn next(it: *Iterator) ?Entity { if (it.index >= it.entities.len) return null; it.index += 1; - return it.entities.*[it.index - 1]; + return it.entities[it.index - 1]; } // Reset the iterator to the initial index @@ -50,7 +46,7 @@ pub const BasicGroup = struct { } /// Direct access to the array of entities - pub fn data(self: Self) *const []Entity { + pub fn data(self: Self) []const Entity { return self.group_data.entity_set.data(); } @@ -63,7 +59,7 @@ pub const BasicGroup = struct { } pub fn iterator(self: *Self) Iterator { - return Iterator.init(self); + return Iterator.init(self.group_data.entity_set.data()); } }; @@ -89,7 +85,7 @@ pub const OwningGroup = struct { } }; -test "BasicGroup creation" { +test "BasicGroup creation/iteration" { var reg = Registry.init(std.testing.allocator); defer reg.deinit(); @@ -109,6 +105,12 @@ test "BasicGroup creation" { } std.testing.expectEqual(iterated_entities, 1); + iterated_entities = 0; + for (group.data()) |entity| { + iterated_entities += 1; + } + std.testing.expectEqual(iterated_entities, 1); + reg.remove(i32, e0); std.debug.assert(group.len() == 0); } diff --git a/zig-ecs/src/ecs/registry.zig b/zig-ecs/src/ecs/registry.zig index cfd7089..0b6b6c8 100644 --- a/zig-ecs/src/ecs/registry.zig +++ b/zig-ecs/src/ecs/registry.zig @@ -111,7 +111,7 @@ pub const Registry = struct { // store.swap hides a safe version that types it correctly const store_ptr = self.registry.components.getValue(tid).?; var store = @intToPtr(*Storage(u1), store_ptr); - store.swap(store.data().*[self.current], entity); + store.swap(store.data()[self.current], entity); } self.current += 1; } @@ -132,7 +132,7 @@ pub const Registry = struct { for (self.owned) |tid| { const store_ptr = self.registry.components.getValue(tid).?; store = @intToPtr(*Storage(u1), store_ptr); - store.swap(store.data().*[self.current], entity); + store.swap(store.data()[self.current], entity); } } } @@ -565,7 +565,7 @@ pub const Registry = struct { } else { // ??? why not? we cannot iterate backwards because we want to leave behind valid entities in case of owned types var first_owned_storage = self.assure(owned[0]); - for (first_owned_storage.data().*) |entity| { + for (first_owned_storage.data()) |entity| { new_group_data.maybeValidIf(entity); } // for(auto *first = std::get<0>(cpools).data(), *last = first + std::get<0>(cpools).size(); first != last; ++first) { diff --git a/zig-ecs/src/ecs/sparse_set.zig b/zig-ecs/src/ecs/sparse_set.zig index 0ef78eb..91d3cbb 100644 --- a/zig-ecs/src/ecs/sparse_set.zig +++ b/zig-ecs/src/ecs/sparse_set.zig @@ -83,8 +83,11 @@ pub fn SparseSet(comptime SparseT: type) type { return self.dense.items.len == 0; } - // TODO: why return a pointer to the slice? - pub fn data(self: Self) *const []SparseT { + pub fn data(self: Self) []const SparseT { + return self.dense.items; + } + + pub fn dataPtr(self: Self) *const []SparseT { return &self.dense.items; } @@ -234,12 +237,12 @@ test "data() synced" { set.add(3); var data = set.data(); - std.testing.expectEqual(data.*[1], 1); + std.testing.expectEqual(data[1], 1); std.testing.expectEqual(set.len(), data.len); set.remove(0); set.remove(1); - std.testing.expectEqual(set.len(), data.len); + std.testing.expectEqual(set.len(), set.data().len); } test "respect" { diff --git a/zig-ecs/src/ecs/views.zig b/zig-ecs/src/ecs/views.zig index 481112a..0901b73 100644 --- a/zig-ecs/src/ecs/views.zig +++ b/zig-ecs/src/ecs/views.zig @@ -28,7 +28,7 @@ pub fn BasicView(comptime T: type) type { } /// Direct access to the array of entities - pub fn data(self: Self) *const []Entity { + pub fn data(self: Self) []const Entity { return self.storage.data(); } @@ -60,7 +60,7 @@ pub fn MultiView(comptime n_includes: usize, comptime n_excludes: usize) type { const ptr = view.registry.components.getValue(view.type_ids[0]).?; return .{ .view = view, - .entities = @intToPtr(*Storage(u8), ptr).data(), + .entities = @intToPtr(*Storage(u8), ptr).dataPtr(), }; } @@ -161,7 +161,7 @@ test "single basic view data" { std.testing.expectEqual(view.get(3).*, 30); - for (view.data().*) |entity, i| { + for (view.data()) |entity, i| { if (i == 0) std.testing.expectEqual(entity, 3); if (i == 1)