ptr and slice access to entities

master
Mike 4 years ago
parent 7dd8a28dbe
commit 81ccd19b65

@ -187,10 +187,15 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type) type {
}; };
/// Direct access to the array of entities /// 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(); 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 /// Swaps entities and objects in the internal packed arrays
pub fn swap(self: *Self, lhs: EntityT, rhs: EntityT) void { pub fn swap(self: *Self, lhs: EntityT, rhs: EntityT) void {
self.safe_swap(self, lhs, rhs); self.safe_swap(self, lhs, rhs);
@ -240,7 +245,7 @@ test "iterate" {
store.add(5, 66.45); store.add(5, 66.45);
store.add(7, 66.45); store.add(7, 66.45);
for (store.data().*) |entity, i| { for (store.data()) |entity, i| {
if (i == 0) if (i == 0)
std.testing.expectEqual(entity, 3); std.testing.expectEqual(entity, 3);
if (i == 1) if (i == 1)

@ -14,22 +14,18 @@ pub const BasicGroup = struct {
group_data: *Registry.GroupData, group_data: *Registry.GroupData,
pub const Iterator = struct { pub const Iterator = struct {
group: *Self,
index: usize = 0, index: usize = 0,
entities: *const []Entity, entities: []const Entity,
pub fn init(group: *Self) Iterator { pub fn init(entities: []const Entity) Iterator {
return .{ return .{ .entities = entities };
.group = group,
.entities = group.group_data.entity_set.data(),
};
} }
pub fn next(it: *Iterator) ?Entity { pub fn next(it: *Iterator) ?Entity {
if (it.index >= it.entities.len) return null; if (it.index >= it.entities.len) return null;
it.index += 1; it.index += 1;
return it.entities.*[it.index - 1]; return it.entities[it.index - 1];
} }
// Reset the iterator to the initial index // Reset the iterator to the initial index
@ -50,7 +46,7 @@ pub const BasicGroup = struct {
} }
/// Direct access to the array of entities /// 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(); return self.group_data.entity_set.data();
} }
@ -63,7 +59,7 @@ pub const BasicGroup = struct {
} }
pub fn iterator(self: *Self) Iterator { 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); var reg = Registry.init(std.testing.allocator);
defer reg.deinit(); defer reg.deinit();
@ -109,6 +105,12 @@ test "BasicGroup creation" {
} }
std.testing.expectEqual(iterated_entities, 1); 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); reg.remove(i32, e0);
std.debug.assert(group.len() == 0); std.debug.assert(group.len() == 0);
} }

@ -111,7 +111,7 @@ pub const Registry = struct {
// store.swap hides a safe version that types it correctly // store.swap hides a safe version that types it correctly
const store_ptr = self.registry.components.getValue(tid).?; const store_ptr = self.registry.components.getValue(tid).?;
var store = @intToPtr(*Storage(u1), store_ptr); var store = @intToPtr(*Storage(u1), store_ptr);
store.swap(store.data().*[self.current], entity); store.swap(store.data()[self.current], entity);
} }
self.current += 1; self.current += 1;
} }
@ -132,7 +132,7 @@ pub const Registry = struct {
for (self.owned) |tid| { for (self.owned) |tid| {
const store_ptr = self.registry.components.getValue(tid).?; const store_ptr = self.registry.components.getValue(tid).?;
store = @intToPtr(*Storage(u1), store_ptr); 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 { } else {
// ??? why not? we cannot iterate backwards because we want to leave behind valid entities in case of owned types // ??? 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]); 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); new_group_data.maybeValidIf(entity);
} }
// for(auto *first = std::get<0>(cpools).data(), *last = first + std::get<0>(cpools).size(); first != last; ++first) { // for(auto *first = std::get<0>(cpools).data(), *last = first + std::get<0>(cpools).size(); first != last; ++first) {

@ -83,8 +83,11 @@ pub fn SparseSet(comptime SparseT: type) type {
return self.dense.items.len == 0; 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; return &self.dense.items;
} }
@ -234,12 +237,12 @@ test "data() synced" {
set.add(3); set.add(3);
var data = set.data(); var data = set.data();
std.testing.expectEqual(data.*[1], 1); std.testing.expectEqual(data[1], 1);
std.testing.expectEqual(set.len(), data.len); std.testing.expectEqual(set.len(), data.len);
set.remove(0); set.remove(0);
set.remove(1); set.remove(1);
std.testing.expectEqual(set.len(), data.len); std.testing.expectEqual(set.len(), set.data().len);
} }
test "respect" { test "respect" {

@ -28,7 +28,7 @@ pub fn BasicView(comptime T: type) type {
} }
/// Direct access to the array of entities /// 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(); 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]).?; const ptr = view.registry.components.getValue(view.type_ids[0]).?;
return .{ return .{
.view = view, .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); std.testing.expectEqual(view.get(3).*, 30);
for (view.data().*) |entity, i| { for (view.data()) |entity, i| {
if (i == 0) if (i == 0)
std.testing.expectEqual(entity, 3); std.testing.expectEqual(entity, 3);
if (i == 1) if (i == 1)

Loading…
Cancel
Save