Fix family iteration

This commit is contained in:
Christian Treffs 2017-10-21 22:33:52 +02:00
parent 2386fab4c3
commit 00bb739880
8 changed files with 231 additions and 47 deletions

View File

@ -5,55 +5,137 @@
// Created by Christian Treffs on 20.10.17.
//
public protocol FamilyIterable {
func forEachMember(_ applyToMember: (Entity) -> Void)
func forEachMember<A>(_ applyToMember: (Entity, A) -> Void) where A: Component
func forEachMember<A, B>(_ applyToMember: (Entity, A, B) -> Void) where A: Component, B: Component
func forEachMember<A, B, C>(_ applyToMember: (Entity, A, B, C) -> Void) where A: Component, B: Component, C: Component
func forEachMember<A, B, C, D>(_ applyToMember: (Entity, A, B, C, D) -> Void) where A: Component, B: Component, C: Component, D: Component
func forEachMember<A, B, C, D, E>(_ applyToMember: (Entity, A, B, C, D, E) -> Void) where A: Component, B: Component, C: Component, D: Component, E: Component
func forEachMember<A, B, C, D, E, F>(_ applyToMember: (Entity, A, B, C, D, E, F) -> Void) where A: Component, B: Component, C: Component, D: Component, E: Component, F: Component
extension Family {
public func iterateMembers(_ apply: @escaping (EntityIdentifier, () -> Entity) -> Void) {
memberIds.forEach { (entityId: EntityIdentifier) -> Void in
func getEntity() -> Entity {
return nexus.get(entity: entityId)
}
apply(entityId, getEntity)
}
}
}
extension Family/*: FamilyIterable*/ {
public func forEachMember(_ applyToMember: (Entity) -> Void) {
members.forEach { applyToMember(nexus.get(entity: $0)!) }
}
extension Family {
public func forEachMember<A>(_ applyToMember: (Entity, () -> A?) -> Void) where A: Component {
forEachMember { (entity: Entity) in
applyToMember(entity, entity.component())
public func iterate<A>(components _: A.Type, _ apply: @escaping (() -> Entity, () -> A?) -> Void)
where A: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent)
}
}
public func forEachMember<A, B>(_ applyToMember: (Entity, () -> A?, () -> B?) -> Void) where A: Component, B: Component {
forEachMember { (entity: Entity) in
applyToMember(entity, entity.component(), entity.component())
public func iterate<A, B>(components _: A.Type, _: B.Type, _ apply: @escaping (() -> Entity, () -> A?, () -> B?) -> Void)
where A: Component, B: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent, getComponent)
}
}
public func forEachMember<A, B, C>(_ applyToMember: (Entity, () -> A?, () -> B?, () -> C?) -> Void) where A: Component, B: Component, C: Component {
forEachMember { (entity: Entity) in
applyToMember(entity, entity.component(), entity.component(), entity.component())
public func iterate<A, B, C>(components _: A.Type, _: B.Type, _: C.Type, _ apply: @escaping (() -> Entity, () -> A?, () -> B?, () -> C?) -> Void)
where A: Component, B: Component, C: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent, getComponent, getComponent)
}
}
public func forEachMember<A, B, C, D>(_ applyToMember: (Entity, () -> A?, () -> B?, () -> C?, () -> D?) -> Void) where A: Component, B: Component, C: Component, D: Component {
forEachMember { (entity: Entity) in
applyToMember(entity, entity.component(), entity.component(), entity.component(), entity.component())
public func iterate<A, B, C, D>(components _: A.Type, _: B.Type, _: C.Type, _: D.Type, _ apply: @escaping (() -> Entity, () -> A?, () -> B?, () -> C?, () -> D?) -> Void)
where A: Component, B: Component, C: Component, D: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent, getComponent, getComponent, getComponent)
}
}
public func iterate<A, B, C, D, E>(components _: A.Type, _: B.Type, _: C.Type, _: D.Type, _: E.Type, _ apply: @escaping (() -> Entity, () -> A?, () -> B?, () -> C?, () -> D?, () -> E?) -> Void)
where A: Component, B: Component, C: Component, D: Component, E: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent, getComponent, getComponent, getComponent, getComponent)
}
}
public func forEachMember<A, B, C, D, E>(_ applyToMember: (Entity, () -> A?, () -> B?, () -> C?, () -> D?, () -> E?) -> Void) where A: Component, B: Component, C: Component, D: Component, E: Component {
forEachMember { (entity: Entity) in
applyToMember(entity, entity.component(), entity.component(), entity.component(), entity.component(), entity.component())
public func iterate<A, B, C, D, E, F>(components _: A.Type, _: B.Type, _: C.Type, _: D.Type, _: E.Type, _: F.Type,
_ apply: @escaping (() -> Entity, () -> A?, () -> B?, () -> C?, () -> D?, () -> E?, () -> F?) -> Void)
where A: Component, B: Component, C: Component, D: Component, E: Component, F: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent, getComponent, getComponent, getComponent, getComponent, getComponent)
}
}
}
extension Family {
public func iterate<A>(_ apply: @escaping (() -> Entity, () -> A?) -> Void) where A: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent)
}
}
public func forEachMember<A, B, C, D, E, F>(_ applyToMember: (Entity, () -> A?, () -> B?, () -> C?, () -> D?, () -> E?, () -> F?) -> Void) where A: Component, B: Component, C: Component, D: Component, E: Component, F: Component {
forEachMember { (entity: Entity) in
applyToMember(entity, entity.component(), entity.component(), entity.component(), entity.component(), entity.component(), entity.component())
public func iterate<A, B>(_ apply: @escaping (() -> Entity, () -> A?, () -> B?) -> Void)
where A: Component, B: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent, getComponent)
}
}
public func iterate<A, B, C>(_ apply: @escaping (() -> Entity, () -> A?, () -> B?, () -> C?) -> Void)
where A: Component, B: Component, C: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent, getComponent, getComponent)
}
}
public func iterate<A, B, C, D>(_ apply: @escaping (() -> Entity, () -> A?, () -> B?, () -> C?, () -> D?) -> Void)
where A: Component, B: Component, C: Component, D: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent, getComponent, getComponent, getComponent)
}
}
public func iterate<A, B, C, D, E>(_ apply: @escaping (() -> Entity, () -> A?, () -> B?, () -> C?, () -> D?, () -> E?) -> Void)
where A: Component, B: Component, C: Component, D: Component, E: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent, getComponent, getComponent, getComponent, getComponent)
}
}
public func iterate<A, B, C, D, E, F>(_ apply: @escaping (() -> Entity, () -> A?, () -> B?, () -> C?, () -> D?, () -> E?, () -> F?) -> Void) where A: Component, B: Component, C: Component, D: Component, E: Component, F: Component {
iterateMembers { (entityId, getEntityInstance) in
func getComponent<Z>() -> Z? where Z: Component {
return self.nexus.get(component: Z.identifier, for: entityId) as? Z
}
apply(getEntityInstance, getComponent, getComponent, getComponent, getComponent, getComponent, getComponent)
}
}

View File

@ -30,7 +30,10 @@ extension Family {
return nexus.isMember(entity, in: self)
}
internal var members: EntitySet {
internal var members: LazyMapCollection<LazyFilterCollection<LazyMapCollection<EntityIdSet, Entity?>>, Entity> {
return nexus.members(of: self)
}
internal var memberIds: EntityIdSet {
return nexus.members(of: self)
}
}

View File

@ -71,6 +71,13 @@ extension Nexus {
return get(hash)
}
public func get(component componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component? {
let hash: EntityComponentHash = componentId.hashValue(using: entityId.index)
guard let componentIdx: ComponentIndex = componentIndexByEntityComponentHash[hash] else { return nil }
guard let uniformComponents: UniformComponents = componentsByType[componentId] else { return nil }
return uniformComponents[componentIdx]
}
fileprivate func get<C>(_ hash: EntityComponentHash) -> C? where C: Component {
Log.info("GETTING: \(C.self)")
let componentId: ComponentIdentifier = C.identifier

View File

@ -51,9 +51,7 @@ extension Nexus {
return isValid(entity: entityId)
}
public func get(entity entityId: EntityIdentifier) -> Entity? {
Log.info("GETTING ENTITY: \(entityId)")
guard has(entity: entityId) else { return nil }
public func get(entity entityId: EntityIdentifier) -> Entity {
return entities[entityId.index]
}

View File

@ -33,11 +33,15 @@ extension Nexus {
return family.traits.isMatch(components: componentSet)
}
public func members(of family: Family) -> EntitySet {
public func members(of family: Family) -> EntityIdSet {
let traitHash: FamilyTraitSetHash = family.traits.hashValue
return familyMembersByTraitHash[traitHash] ?? [] // FIXME: fail?
}
public func members(of family: Family) -> LazyMapCollection<LazyFilterCollection<LazyMapCollection<EntityIdSet, Entity?>>, Entity> {
return members(of: family).lazy.flatMap { self.get(entity: $0) }
}
public func isMember(_ entity: Entity, in family: Family) -> Bool {
let traitHash: FamilyTraitSetHash = family.traits.hashValue
let entityId = entity.identifier
@ -82,7 +86,7 @@ extension Nexus {
let (inserted, _) = familyMembersByTraitHash[traitHash]!.insert(entityId)
assert(inserted, "entity with id \(entityId) already in family")
} else {
familyMembersByTraitHash[traitHash] = EntitySet(minimumCapacity: 2)
familyMembersByTraitHash[traitHash] = EntityIdSet(minimumCapacity: 2)
familyMembersByTraitHash[traitHash]!.insert(entityId)
}

View File

@ -18,7 +18,7 @@ public typealias UniformComponents = ContiguousArray<Component>
public typealias ComponentIdentifiers = ContiguousArray<ComponentIdentifier>
public typealias ComponentSet = Set<ComponentIdentifier>
public typealias Entities = ContiguousArray<Entity>
public typealias EntitySet = Set<EntityIdentifier>
public typealias EntityIdSet = Set<EntityIdentifier>
public typealias FamilyTraitSetHash = Int
public class Nexus {
@ -47,7 +47,7 @@ public class Nexus {
var freeEntities: ContiguousArray<EntityIdentifier>
var familiyByTraitHash: [FamilyTraitSetHash: Family]
var familyMembersByTraitHash: [FamilyTraitSetHash: EntitySet]
var familyMembersByTraitHash: [FamilyTraitSetHash: EntityIdSet]
var componentIdsSetByEntity: [EntityIndex: ComponentSet]
public init() {
@ -67,7 +67,7 @@ public class Nexus {
extension Nexus {
func notify(_ event: Event) {
Log.debug(event)
//Log.debug(event)
// TODO: implement
}

View File

@ -77,11 +77,12 @@ class FamilyTests: XCTestCase {
var index: Int = 0
family.forEachMember { (e: Entity, p: () -> Position!, v: () -> Velocity!, n: () -> Name?) in
family.iterate { (e: () -> Entity, p: () -> Position!, v: () -> Velocity!, n: () -> Name?) in
p()!.x = 10
let pos: Position = p()
pos.x = 10
print(e, p(), n())
print(e(), pos, n())
if index == 0 {
print(v())
}
@ -89,9 +90,19 @@ class FamilyTests: XCTestCase {
index += 1
}
family.forEachMember { (e: Entity, p: () -> Position!, v: () -> Velocity!, n: () -> Name?) in
family.iterate(components: Position.self, Velocity.self, Name.self) { (_, pos, vel, nm) in
let position: Position = pos()!
let velocity: Velocity = vel()!
let name: Name? = nm()
print(e, p().x, n())
_ = position
_ = velocity
_ = name
}
family.iterate { (e: () -> Entity, p: () -> Position!, v: () -> Velocity!, n: () -> Name?) in
print(e(), p().x, n())
if index == 0 {
print(v())
}
@ -101,4 +112,83 @@ class FamilyTests: XCTestCase {
}
func testMeasureFamilyIteration() {
let nexus = Nexus()
let number: Int = 10_000
for i in 0..<number {
nexus.create(entity: "\(i)").assign(Position(x: 1+i, y: 2+i), Name(name: "myName\(i)"), Velocity(a: 3.14), EmptyComponent())
}
let family = nexus.family(requiresAll: [Position.self, Velocity.self], excludesAll: [Party.self], needsAtLeastOne: [Name.self, EmptyComponent.self])
nexus.entities.forEach { nexus.update(membership: family, for: $0) }
XCTAssert(family.members.count == number)
XCTAssert(family.memberIds.count == number)
XCTAssert(nexus.entities.count == number)
measure {
family.iterate(components: Position.self, Velocity.self, Name.self) { (_, pos, vel, nm) in
let position: Position = pos()!
let velocity: Velocity = vel()!
let name: Name? = nm()
_ = position
_ = velocity
_ = name
}
}
}
func testMeasureFamilyIteration2() {
let nexus = Nexus()
let number: Int = 10_000
for i in 0..<number {
nexus.create(entity: "\(i)").assign(Position(x: 1+i, y: 2+i), Name(name: "myName\(i)"), Velocity(a: 3.14), EmptyComponent())
}
let family = nexus.family(requiresAll: [Position.self, Velocity.self], excludesAll: [Party.self], needsAtLeastOne: [Name.self, EmptyComponent.self])
nexus.entities.forEach { nexus.update(membership: family, for: $0) }
XCTAssert(family.members.count == number)
XCTAssert(family.memberIds.count == number)
XCTAssert(nexus.entities.count == number)
measure {
family.iterate { (_: () -> Entity, pos: () -> Position!, vel: () -> Velocity!, nm: () -> Name?) in
let name: Name? = nm()
let velocity: Velocity = vel()!
let position: Position = pos()!
_ = position
_ = velocity
_ = name
}
}
}
func testMeasureEntityIteration() {
let nexus = Nexus()
let number: Int = 10_000
for i in 0..<number {
nexus.create(entity: "\(i)").assign(Position(x: 1+i, y: 2+i), Name(name: "myName\(i)"), Velocity(a: 3.14), EmptyComponent())
}
let family = nexus.family(requiresAll: [Position.self, Velocity.self], excludesAll: [Party.self], needsAtLeastOne: [Name.self, EmptyComponent.self])
nexus.entities.forEach { nexus.update(membership: family, for: $0) }
measure {
family.memberIds.forEach { (e) in
_ = e
}
}
}
}

View File

@ -47,7 +47,7 @@ class NexusTests: XCTestCase {
XCTAssert(e0.name == nil)
XCTAssert(e1.name == "Named e1")
let rE0 = nexus.get(entity: e0.identifier)!
let rE0 = nexus.get(entity: e0.identifier)
XCTAssert(rE0.name == e0.name)
XCTAssert(rE0.identifier == e0.identifier)
}
@ -109,7 +109,7 @@ class NexusTests: XCTestCase {
let nexus = Nexus()
let identifier: EntityIdentifier = nexus.create(entity: "e0").identifier
let e0 = nexus.get(entity: identifier)!
let e0 = nexus.get(entity: identifier)
XCTAssert(e0.numComponents == 0)
e0.remove(Position.self)