Rename SparseSet to UnorderedSparseSet
This commit is contained in:
parent
d8ee72569e
commit
ffbe1e87de
|
|
@ -10,15 +10,16 @@ public typealias EntityComponentHash = Int
|
||||||
public typealias ComponentIdsByEntityIndex = Int
|
public typealias ComponentIdsByEntityIndex = Int
|
||||||
public typealias ComponentTypeHash = Int // component object identifier hash value
|
public typealias ComponentTypeHash = Int // component object identifier hash value
|
||||||
public typealias UniformComponents = ManagedContiguousArray<Component>
|
public typealias UniformComponents = ManagedContiguousArray<Component>
|
||||||
public typealias UniformEntityIdentifiers = SparseEntityIdentifierSet
|
public typealias UniformEntityIdentifiers = UnorderedSparseSet<EntityIdentifier>
|
||||||
public typealias ComponentIdentifiers = ContiguousArray<ComponentIdentifier>
|
public typealias ComponentIdentifiers = ContiguousArray<ComponentIdentifier>
|
||||||
public typealias ComponentSet = Set<ComponentIdentifier>
|
public typealias ComponentSet = Set<ComponentIdentifier>
|
||||||
public typealias Entities = SparseEntitySet
|
public typealias Entities = UnorderedSparseSet<Entity>
|
||||||
public typealias EntityIdSet = Set<EntityIdentifier>
|
public typealias EntityIdSet = Set<EntityIdentifier>
|
||||||
public typealias FamilyTraitSetHash = Int
|
public typealias FamilyTraitSetHash = Int
|
||||||
public typealias TraitEntityIdHash = Int
|
public typealias TraitEntityIdHash = Int
|
||||||
public typealias EntityIdInFamilyIndex = Int
|
public typealias EntityIdInFamilyIndex = Int
|
||||||
public typealias TraitEntityIdHashSet = [TraitEntityIdHash: EntityIdInFamilyIndex]
|
public typealias TraitEntityIdHashSet = [TraitEntityIdHash: EntityIdInFamilyIndex]
|
||||||
|
public typealias SparseComponentIdentifierSet = UnorderedSparseSet<ComponentIdentifier>
|
||||||
|
|
||||||
public protocol NexusDelegate: class {
|
public protocol NexusDelegate: class {
|
||||||
func nexusEventOccurred(_ event: ECSEvent)
|
func nexusEventOccurred(_ event: ECSEvent)
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
//
|
//
|
||||||
// SparseSet.swift
|
// UnorderedSparseSet.swift
|
||||||
// FirebladeECS
|
// FirebladeECS
|
||||||
//
|
//
|
||||||
// Created by Christian Treffs on 30.10.17.
|
// Created by Christian Treffs on 30.10.17.
|
||||||
//
|
//
|
||||||
|
|
||||||
public class SparseSet<Element>: Sequence {
|
public class UnorderedSparseSet<Element>: Sequence {
|
||||||
public typealias Index = Int
|
public typealias Index = Int
|
||||||
public typealias Key = Int
|
public typealias Key = Int
|
||||||
|
|
||||||
|
|
@ -95,8 +95,8 @@ public class SparseSet<Element>: Sequence {
|
||||||
dense.removeAll(keepingCapacity: keepingCapacity)
|
dense.removeAll(keepingCapacity: keepingCapacity)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func makeIterator() -> SparseSetIterator<Element> {
|
public func makeIterator() -> UnorderedSparseSetIterator<Element> {
|
||||||
return SparseSetIterator<Element>(self)
|
return UnorderedSparseSetIterator<Element>(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes an element from the set and retuns it in O(1).
|
/// Removes an element from the set and retuns it in O(1).
|
||||||
|
|
@ -121,58 +121,32 @@ public class SparseSet<Element>: Sequence {
|
||||||
return (denseIndex, entry.element)
|
return (denseIndex, entry.element)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - SparseIterator
|
// MARK: - UnorderedSparseSetIterator
|
||||||
public struct SparseSetIterator<Element>: IteratorProtocol {
|
|
||||||
private let sparseSet: SparseSet<Element>
|
|
||||||
private var sortedSparseIterator: IndexingIterator<[(key: SparseSet.Index, value: SparseSet.Key)]>
|
|
||||||
|
|
||||||
init(_ sparseSet: SparseSet<Element>) {
|
public struct UnorderedSparseSetIterator<Element>: IteratorProtocol {
|
||||||
self.sparseSet = sparseSet
|
|
||||||
|
|
||||||
let sortedSparse = sparseSet.sparse.sorted { first, next -> Bool in
|
private var iterator: IndexingIterator<ContiguousArray<UnorderedSparseSet<Element>.Entry>>
|
||||||
first.key < next.key
|
|
||||||
}
|
|
||||||
|
|
||||||
sortedSparseIterator = sortedSparse.makeIterator()
|
init(_ sparseSet: UnorderedSparseSet<Element>) {
|
||||||
|
iterator = sparseSet.dense.makeIterator()
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating public func next() -> Element? {
|
mutating public func next() -> Element? {
|
||||||
guard let (key, _) = sortedSparseIterator.next() else {
|
return iterator.next()?.element
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sparseSet.get(at: key)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension SparseSet.Entry: Equatable where SparseSet.Element: Equatable {
|
extension UnorderedSparseSet.Entry: Equatable where UnorderedSparseSet.Element: Equatable {
|
||||||
public static func == (lhs: SparseSet.Entry, rhs: SparseSet.Entry) -> Bool {
|
public static func == (lhs: UnorderedSparseSet.Entry, rhs: UnorderedSparseSet.Entry) -> Bool {
|
||||||
return lhs.element == rhs.element && lhs.key == rhs.key
|
return lhs.element == rhs.element && lhs.key == rhs.key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Equatable
|
// MARK: - Equatable
|
||||||
extension SparseSet: Equatable where SparseSet.Element: Equatable {
|
extension UnorderedSparseSet: Equatable where UnorderedSparseSet.Element: Equatable {
|
||||||
public static func == (lhs: SparseSet<Element>, rhs: SparseSet<Element>) -> Bool {
|
public static func == (lhs: UnorderedSparseSet<Element>, rhs: UnorderedSparseSet<Element>) -> Bool {
|
||||||
return lhs.dense == rhs.dense &&
|
return lhs.dense == rhs.dense &&
|
||||||
lhs.sparse == rhs.sparse
|
lhs.sparse == rhs.sparse
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - specialized sparse sets
|
|
||||||
|
|
||||||
public class SparseEntitySet: SparseSet<Entity> {
|
|
||||||
public typealias Index = EntityIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SparseEntityIdentifierSet: SparseSet<EntityIdentifier> {
|
|
||||||
public typealias Index = EntityIndex
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SparseComponentIdentifierSet: SparseSet<ComponentIdentifier> {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -10,11 +10,11 @@ import XCTest
|
||||||
|
|
||||||
class SparseSetTests: XCTestCase {
|
class SparseSetTests: XCTestCase {
|
||||||
|
|
||||||
var set: SparseSet<Position>!
|
var set: UnorderedSparseSet<Position>!
|
||||||
|
|
||||||
override func setUp() {
|
override func setUp() {
|
||||||
super.setUp()
|
super.setUp()
|
||||||
set = SparseSet<Position>()
|
set = UnorderedSparseSet<Position>()
|
||||||
}
|
}
|
||||||
|
|
||||||
override func tearDown() {
|
override func tearDown() {
|
||||||
|
|
@ -335,7 +335,7 @@ class SparseSetTests: XCTestCase {
|
||||||
|
|
||||||
func testSparseSetDoubleRemove() {
|
func testSparseSetDoubleRemove() {
|
||||||
class AClass { }
|
class AClass { }
|
||||||
let set = SparseSet<AClass>()
|
let set = UnorderedSparseSet<AClass>()
|
||||||
let a = AClass()
|
let a = AClass()
|
||||||
let b = AClass()
|
let b = AClass()
|
||||||
set.insert(a, at: 0)
|
set.insert(a, at: 0)
|
||||||
|
|
@ -425,7 +425,7 @@ class SparseSetTests: XCTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSparseSetReduce() {
|
func testSparseSetReduce() {
|
||||||
let characters = SparseSet<Character>()
|
let characters = UnorderedSparseSet<Character>()
|
||||||
|
|
||||||
|
|
||||||
characters.insert("H", at: 4)
|
characters.insert("H", at: 4)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue