Unconform sparse set to collection, since index collision problem occurs
This commit is contained in:
parent
091e84667e
commit
209c19fa2f
|
|
@ -5,7 +5,7 @@
|
|||
// Created by Christian Treffs on 30.10.17.
|
||||
//
|
||||
|
||||
open class UnorderedSparseSet<Element>: MutableCollection, RandomAccessCollection {
|
||||
open class UnorderedSparseSet<Element> {
|
||||
public typealias Index = Int
|
||||
public typealias Key = Int
|
||||
|
||||
|
|
@ -28,7 +28,6 @@ open class UnorderedSparseSet<Element>: MutableCollection, RandomAccessCollectio
|
|||
|
||||
public var count: Int { return dense.count }
|
||||
public var isEmpty: Bool { return dense.isEmpty }
|
||||
public var capacity: Int { return sparse.count }
|
||||
|
||||
@inlinable
|
||||
public func contains(_ key: Key) -> Bool {
|
||||
|
|
@ -126,19 +125,10 @@ open class UnorderedSparseSet<Element>: MutableCollection, RandomAccessCollectio
|
|||
return (denseIndex, entry.element)
|
||||
}
|
||||
|
||||
// MARK: Collection
|
||||
@inlinable
|
||||
public func index(after index: Index) -> Int {
|
||||
return dense.index(after: index)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public subscript(position: Index) -> Element {
|
||||
get {
|
||||
guard let element: Element = get(at: position) else {
|
||||
fatalError("no element at index \(position)")
|
||||
}
|
||||
return element
|
||||
return get(unsafeAt: position)
|
||||
}
|
||||
|
||||
set(newValue) {
|
||||
|
|
@ -146,18 +136,17 @@ open class UnorderedSparseSet<Element>: MutableCollection, RandomAccessCollectio
|
|||
}
|
||||
}
|
||||
|
||||
@inlinable public var startIndex: Index {
|
||||
return dense.startIndex
|
||||
@inlinable public var first: Element? {
|
||||
return dense.first?.element
|
||||
}
|
||||
|
||||
@inlinable public var endIndex: Index {
|
||||
return dense.endIndex
|
||||
@inlinable public var last: Element? {
|
||||
return dense.last?.element
|
||||
}
|
||||
}
|
||||
|
||||
extension UnorderedSparseSet.Entry: Equatable where UnorderedSparseSet.Element: Equatable { }
|
||||
|
||||
extension UnorderedSparseSet: Equatable where UnorderedSparseSet.Element: Equatable {
|
||||
extension UnorderedSparseSet.Entry: Equatable where Element: Equatable { }
|
||||
extension UnorderedSparseSet: Equatable where Element: Equatable {
|
||||
public static func == (lhs: UnorderedSparseSet<Element>, rhs: UnorderedSparseSet<Element>) -> Bool {
|
||||
return lhs.dense == rhs.dense && lhs.sparse == rhs.sparse
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,4 +56,12 @@ class SingleTests: XCTestCase {
|
|||
XCTAssertEqual(single.component.playerHealth, gameState.playerHealth)
|
||||
|
||||
}
|
||||
|
||||
func testSingleCreationOnExistingFamilyMember() {
|
||||
_ = nexus.create(entity: "AnotherEntity", with: Position(x: 1, y: 2))
|
||||
let singleGame = SingleGameState()
|
||||
_ = nexus.create(entity: "CrashEntity", with: singleGame)
|
||||
let single = nexus.single(SingleGameState.self)
|
||||
XCTAssertTrue(singleGame === single.component)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -500,8 +500,8 @@ class SparseSetTests: XCTestCase {
|
|||
|
||||
XCTAssertEqual(characters.count, 11)
|
||||
|
||||
let string: String = characters.reduce("") { res, char in
|
||||
res + "\(char)"
|
||||
let string: String = characters.dense.reduce("") { res, char in
|
||||
res + "\(char.element)"
|
||||
}
|
||||
|
||||
// NOTE: this tests only dense insertion order, this is no guarantee for the real ordering.
|
||||
|
|
@ -539,4 +539,17 @@ class SparseSetTests: XCTestCase {
|
|||
XCTAssertEqual(characters[34], "l")
|
||||
XCTAssertEqual(characters[140], "d")
|
||||
}
|
||||
|
||||
func testStartEndIndex() {
|
||||
|
||||
let set = UnorderedSparseSet<Character>()
|
||||
|
||||
set.insert("C", at: 33)
|
||||
set.insert("A", at: 11)
|
||||
set.insert("B", at: 22)
|
||||
|
||||
let mapped = set.dense.map { $0.element }
|
||||
|
||||
XCTAssertEqual(mapped, ["C", "A", "B"])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue