From e1ac76513c65296755f9ab803fa76ac7ff791973 Mon Sep 17 00:00:00 2001 From: Christian Treffs Date: Thu, 30 Apr 2020 19:50:46 +0200 Subject: [PATCH] Remove abandoned code --- Sources/FirebladeECS/Component+Access.swift | 37 ------ Sources/FirebladeECS/Identifiable.swift | 34 ------ .../FirebladeECS/ManagedContiguousArray.swift | 103 ----------------- Sources/FirebladeECS/Nexus+Codable.swift | 107 ------------------ Tests/FirebladeECSTests/AccessTests.swift | 41 ------- .../FirebladeECSTests/NexusCodingTests.swift | 50 -------- 6 files changed, 372 deletions(-) delete mode 100644 Sources/FirebladeECS/Component+Access.swift delete mode 100644 Sources/FirebladeECS/Identifiable.swift delete mode 100644 Sources/FirebladeECS/ManagedContiguousArray.swift delete mode 100644 Sources/FirebladeECS/Nexus+Codable.swift delete mode 100644 Tests/FirebladeECSTests/AccessTests.swift delete mode 100644 Tests/FirebladeECSTests/NexusCodingTests.swift diff --git a/Sources/FirebladeECS/Component+Access.swift b/Sources/FirebladeECS/Component+Access.swift deleted file mode 100644 index ebced3f..0000000 --- a/Sources/FirebladeECS/Component+Access.swift +++ /dev/null @@ -1,37 +0,0 @@ -// -// Component+Access.swift -// -// -// Created by Christian Treffs on 25.06.19. -// - -#if swift(>=5.1) -@dynamicMemberLookup -public struct ReadableOnly where Comp: Component { - @usableFromInline let component: Comp - - public init(_ component: Comp) { - self.component = component - } - - @inlinable - public subscript(dynamicMember keyPath: KeyPath) -> C { - return component[keyPath: keyPath] - } -} - -@dynamicMemberLookup -public struct Writable where Comp: Component { - @usableFromInline let component: Comp - - public init(_ component: Comp) { - self.component = component - } - - @inlinable - public subscript(dynamicMember keyPath: ReferenceWritableKeyPath) -> C { - nonmutating get { return component[keyPath: keyPath] } - nonmutating set { component[keyPath: keyPath] = newValue } - } -} -#endif diff --git a/Sources/FirebladeECS/Identifiable.swift b/Sources/FirebladeECS/Identifiable.swift deleted file mode 100644 index bd0f924..0000000 --- a/Sources/FirebladeECS/Identifiable.swift +++ /dev/null @@ -1,34 +0,0 @@ -// -// Identifiable.swift -// -// -// Created by Christian Treffs on 05.10.19. -// - -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -#if swift(<5.1) -/// A class of types whose instances hold the value of an entity with stable identity. -public protocol Identifiable { - /// A type representing the stable identity of the entity associated with `self`. - associatedtype ID: Hashable - - /// The stable identity of the entity associated with `self`. - var id: ID { get } -} - -extension Identifiable where Self: AnyObject { - public var id: ObjectIdentifier { - return ObjectIdentifier(self) - } -} -#endif diff --git a/Sources/FirebladeECS/ManagedContiguousArray.swift b/Sources/FirebladeECS/ManagedContiguousArray.swift deleted file mode 100644 index a8b7938..0000000 --- a/Sources/FirebladeECS/ManagedContiguousArray.swift +++ /dev/null @@ -1,103 +0,0 @@ -// -// ManagedContiguousArray.swift -// FirebladeECS -// -// Created by Christian Treffs on 28.10.17. -// - -public struct ManagedContiguousArray { - public typealias Index = Int - - @usableFromInline let chunkSize: Int - @usableFromInline var size: Int = 0 - @usableFromInline var store: ContiguousArray = [] - - public init(minCount: Int = 4096) { - chunkSize = minCount - store = ContiguousArray(repeating: nil, count: minCount) - } - - @inline(__always) - public var count: Int { - return size - } - - @discardableResult - @inlinable - public mutating func insert(_ element: Element, at index: Int) -> Bool { - if needsToGrow(index) { - grow(to: index) - } - if store[index] == nil { - size += 1 - } - store[index] = element - return true - } - - @inlinable - public func contains(_ index: Index) -> Bool { - if store.count <= index { - return false - } - return store[index] != nil - } - - @inline(__always) - public func get(at index: Index) -> Element? { - return store[index] - } - - @inline(__always) - public func get(unsafeAt index: Index) -> Element { - return store[index].unsafelyUnwrapped - } - - @discardableResult - @inlinable - public mutating func remove(at index: Index) -> Bool { - if store[index] != nil { - size -= 1 - } - store[index] = nil - if size == 0 { - clear() - } - return true - } - - @inlinable - public mutating func clear(keepingCapacity: Bool = false) { - size = 0 - store.removeAll(keepingCapacity: keepingCapacity) - } - - @inlinable - func needsToGrow(_ index: Index) -> Bool { - return index > store.count - 1 - } - - @inlinable - mutating func grow(to index: Index) { - let newCapacity: Int = calculateCapacity(to: index) - let newCount: Int = newCapacity - store.count - store += ContiguousArray(repeating: nil, count: newCount) - } - - @inlinable - func calculateCapacity(to index: Index) -> Int { - let delta = Float(index) / Float(chunkSize) - let multiplier = Int(delta.rounded(.up)) + 1 - return multiplier * chunkSize - } -} - -// MARK: - Equatable -extension ManagedContiguousArray: Equatable where Element: Equatable { - public static func == (lhs: ManagedContiguousArray, rhs: ManagedContiguousArray) -> Bool { - return lhs.store == rhs.store - } -} - -// MARK: - Codable -extension ManagedContiguousArray: Codable where Element: Codable { } diff --git a/Sources/FirebladeECS/Nexus+Codable.swift b/Sources/FirebladeECS/Nexus+Codable.swift deleted file mode 100644 index 8ed3e51..0000000 --- a/Sources/FirebladeECS/Nexus+Codable.swift +++ /dev/null @@ -1,107 +0,0 @@ -// -// Nexus+Codable.swift -// -// -// Created by Christian Treffs on 05.10.19. -// - -extension Nexus: Codable { - public enum CodingKeys: String, CodingKey { - case version - case entities - case freeEntities - case childrenByParent - case componentsByType - case familyMembersByTraits - case componentIdsByEntity - - public enum Components: String, CodingKey { - case componentId - case components - } - - public enum SparseSet: String, CodingKey { - case dense - case sparse - - public enum Entry: String, CodingKey { - case key - case element - } - } - } -} -// MARK: - Encodable -extension Nexus { - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - - try container.encode(Nexus.version, forKey: .version) - try container.encode(entityStorage, forKey: .entities) - try container.encode(freeEntities, forKey: .freeEntities) - try container.encode(childrenByParentEntity, forKey: .childrenByParent) - - try container.encode(familyMembersByTraits, forKey: .familyMembersByTraits) - try container.encode(componentIdsByEntity, forKey: .componentIdsByEntity) - - // encode componentsByType - var contComponentsByType = container.nestedUnkeyedContainer(forKey: .componentsByType) - for (stableId, components) in componentsByType { - var contComponents = contComponentsByType.nestedContainer(keyedBy: CodingKeys.Components.self) - try contComponents.encode(stableId, forKey: .componentId) - var compSparseSet = contComponents.nestedContainer(keyedBy: CodingKeys.SparseSet.self, forKey: .components) - try compSparseSet.encode(components.sparse, forKey: .sparse) - var denseContainer = compSparseSet.nestedUnkeyedContainer(forKey: .dense) - try components.dense.forEach { (entry: UnorderedSparseSet.Entry) in - var entryContainer = denseContainer.nestedContainer(keyedBy: CodingKeys.SparseSet.Entry.self) - try entryContainer.encode(entry.key, forKey: .key) - try entry.element.encode(to: entryContainer.superEncoder(forKey: .element)) - } - } - } -} - -// MARK: - Decodable -extension Nexus { - public convenience init(from decoder: Decoder) throws { - let container = try decoder.container(keyedBy: CodingKeys.self) - let version = try container.decode(String.self, forKey: .version) - if version != Nexus.version { - throw Error.versionMismatch(required: Nexus.version, provided: version) - } - - let entityStorage = try container.decode(UnorderedSparseSet.self, forKey: .entities) - let freeEntities = try container.decode([EntityIdentifier].self, forKey: .freeEntities) - let childrenByParentEntity = try container.decode([EntityIdentifier: Set].self, forKey: .childrenByParent) - let familyMembersByTraits = try container.decode([FamilyTraitSet: UnorderedSparseSet].self, forKey: .familyMembersByTraits) - let componentIdsByEntity = try container.decode([EntityIdentifier: Set].self, forKey: .componentIdsByEntity) - - // decode componentsByType - var contComponentsByType = try container.nestedUnkeyedContainer(forKey: .componentsByType) - var componentsByType = [ComponentIdentifier: UnorderedSparseSet]() - for _ in 0..<(contComponentsByType.count ?? 0) { - let contComponents = try contComponentsByType.nestedContainer(keyedBy: CodingKeys.Components.self) - let stableId = try contComponents.decode(ComponentIdentifier.self, forKey: .componentId) - - let compSparseSet = try contComponents.nestedContainer(keyedBy: CodingKeys.SparseSet.self, forKey: .components) - let sparse = try compSparseSet.decode([Int: Int].self, forKey: .sparse) - var denseContainer = try compSparseSet.nestedUnkeyedContainer(forKey: .dense) - var dense = ContiguousArray.Entry>() - for _ in 0..<(denseContainer.count ?? 0) { - let entryContainer = try denseContainer.nestedContainer(keyedBy: CodingKeys.SparseSet.Entry.self) - let key = try entryContainer.decode(UnorderedSparseSet.Key.self, forKey: .key) - let element: Component = try Nexus.componentDecoderMap[stableId]!(entryContainer.superDecoder(forKey: .element)) - dense.append(UnorderedSparseSet.Entry(key: key, element: element)) - } - - componentsByType[stableId] = UnorderedSparseSet(sparse: sparse, dense: dense) - } - - self.init(entityStorage: entityStorage, - componentsByType: componentsByType, - componentsByEntity: componentIdsByEntity, - freeEntities: freeEntities, - familyMembersByTraits: familyMembersByTraits, - childrenByParentEntity: childrenByParentEntity) - } -} diff --git a/Tests/FirebladeECSTests/AccessTests.swift b/Tests/FirebladeECSTests/AccessTests.swift deleted file mode 100644 index 0c51137..0000000 --- a/Tests/FirebladeECSTests/AccessTests.swift +++ /dev/null @@ -1,41 +0,0 @@ -// -// AccessTests.swift -// -// -// Created by Christian Treffs on 25.06.19. -// - -import FirebladeECS -import XCTest - -#if swift(>=5.1) -class AccessTests: XCTestCase { - func testReadOnly() { - let pos = Position(x: 1, y: 2) - - let readable = ReadableOnly(pos) - - XCTAssertEqual(readable.x, 1) - XCTAssertEqual(readable.y, 2) - - // readable.x = 3 // does not work and that's correct! - } - - func testWrite() { - let pos = Position(x: 1, y: 2) - - let writable = Writable(pos) - - XCTAssertEqual(writable.x, 1) - XCTAssertEqual(writable.y, 2) - - writable.x = 3 - - XCTAssertEqual(writable.x, 3) - XCTAssertEqual(pos.x, 3) - - XCTAssertEqual(writable.y, 2) - XCTAssertEqual(pos.y, 2) - } -} -#endif diff --git a/Tests/FirebladeECSTests/NexusCodingTests.swift b/Tests/FirebladeECSTests/NexusCodingTests.swift deleted file mode 100644 index d122e19..0000000 --- a/Tests/FirebladeECSTests/NexusCodingTests.swift +++ /dev/null @@ -1,50 +0,0 @@ -// -// NexusCodingTests.swift -// FirebladeECSTests -// -// Created by Christian Treffs on 05.10.19. -// - -import XCTest -import FirebladeECS - -class NexusCodingTests: XCTestCase { - - var nexus: Nexus! - - override func setUp() { - super.setUp() - nexus = Nexus() - - let e0 = nexus.createEntity(with: Position(x: 1, y: 2), Name(name: "Entity0")) - let e1 = nexus.createEntity(with: Position(x: 1, y: 2), Name(name: "Entity1")) - let e2 = nexus.createEntity(with: Velocity(a: 2.34), Name(name: "Entity1")) - nexus.destroy(entity: e0) - let e3 = nexus.createEntity(with: Velocity(a: 5.67), Name(name: "Entity3")) - e1.addChild(e2) - e1.addChild(e3) - - _ = nexus.family(requiresAll: Position.self, Name.self, excludesAll: EmptyComponent.self) - } - - override func tearDown() { - super.tearDown() - nexus = nil - } - - func testEncodeDecodeNexusJSON() { - let encoder = JSONEncoder() - let decoder = JSONDecoder() - var data: Data! - XCTAssertNoThrow(data = try encoder.encode(nexus)) - XCTAssertGreaterThan(data.count, 0) - - var nexus2: Nexus! - XCTAssertNoThrow(nexus2 = try decoder.decode(Nexus.self, from: data)) - XCTAssertEqual(nexus2, nexus) - - var data2: Data! - XCTAssertNoThrow(data2 = try encoder.encode(nexus2)) - XCTAssertGreaterThan(data2.count, 0) - } -}