This commit is contained in:
Christian Treffs 2017-11-16 20:49:17 +01:00
parent e0cb8bfc62
commit dcea98ff8a
6 changed files with 1 additions and 208 deletions

View File

@ -1,11 +0,0 @@
//
// Activatable.swift
// FirebladeECSPackageDescription
//
// Created by Christian Treffs on 02.11.17.
//
public protocol Activatable {
func activate()
func deactivate()
}

View File

@ -5,7 +5,7 @@
// Created by Christian Treffs on 08.10.17.
//
public protocol Component: class, TypeIdentifiable, Activatable {}
public protocol Component: class, TypeIdentifiable {}
extension Component {
public var identifier: ComponentIdentifier { return typeObjectIdentifier }

View File

@ -20,16 +20,6 @@ public final class Entity: UniqueEntityIdentifiable {
}
// MARK: - activatable protocol
extension Entity: Activatable {
public func activate() {
//TODO: nexus.activate(entity: self)
}
public func deactivate() {
//TODO: nexus.deactivate(entity: self)
}
}
// MARK: - Invalidate
extension Entity {

View File

@ -83,17 +83,3 @@ extension FamilyTraitSet: Hashable {
}
}
// MARK: - description
/*extension FamilyTraits: CustomStringConvertible {
public var description: String {
let all: String = hasAll.map { "\($0.self)" }.joined(separator: " AND ")
let any: String = hasAny.map { "\($0.self)" }.joined(separator: " OR ")
let none: String = hasNone.map { "!\($0.self)"}.joined(separator: " NOT ")
let out: String = ["\(all)", "\(any)", "\(none)"].joined(separator: " AND ")
//TODO: nicer
return "FamilyTraits(\(out))"
}
}
*/

View File

@ -1,50 +0,0 @@
//
// Timer.swift
// FirebladeECS
//
// Created by Christian Treffs on 28.10.17.
//
import Darwin.Mach.mach_time
public struct Timer {
private let numerator: UInt64
private let denominator: UInt64
private var startTime: UInt64 = 0
private var stopTime: UInt64 = 0
public init() {
var timeBaseInfo = mach_timebase_info.init(numer: 0, denom: 0 )
let success: kern_return_t = mach_timebase_info(&timeBaseInfo)
assert(KERN_SUCCESS == success)
numerator = UInt64(timeBaseInfo.numer)
denominator = UInt64(timeBaseInfo.denom)
}
public mutating func start() {
startTime = mach_absolute_time()
}
public mutating func stop() {
stopTime = mach_absolute_time()
}
public mutating func reset() {
startTime = 0
stopTime = 0
}
public var nanoSeconds: UInt64 {
return ((stopTime - startTime) * numerator) / denominator
}
public var microSeconds: Double {
return Double(nanoSeconds) / 1.0e3
}
public var milliSeconds: Double {
return Double(nanoSeconds) / 1.0e6
}
public var seconds: Double {
return Double(nanoSeconds) / 1.0e9
}
}

View File

@ -1,122 +0,0 @@
//
// UUID.swift
// FirebladeECSPackageDescription
//
// Created by Christian Treffs on 04.11.17.
//
#if os(macOS) || os(iOS) || os(tvOS)
import Darwin.C.stdlib
#else
// TODO: support linux and other platforms
#endif
public struct UUID {
public static let count: Int = 16 // https://tools.ietf.org/html/rfc4122#section-4.1
private let bytes: ContiguousArray<UInt8>
public init(_ bytes: ContiguousArray<UInt8>) {
assert(bytes.count == UUID.count, "An UUID must have a count of exactly \(UUID.count).")
self.bytes = bytes
}
public init() {
self.init(UUID.generateUUID())
}
public init(_ bytes: [UInt8]) {
self.init(ContiguousArray(bytes))
}
public init?(uuidString: String) {
guard uuidString.count == 2*UUID.count+4 else {
// "An UUID string must have a count of exactly 36."
return nil
}
var uuid: ContiguousArray<UInt8> = ContiguousArray<UInt8>(repeating: 0, count: UUID.count)
let contiguousString: String = uuidString.split(separator: "-").joined()
guard contiguousString.count == 2*UUID.count else {
// An UUID string must have exactly 4 separators
return nil
}
var endIdx: String.Index = contiguousString.startIndex
for i in 0..<UUID.count {
let startIdx: String.Index = endIdx
endIdx = contiguousString.index(endIdx, offsetBy: 2)
let substring: Substring = contiguousString[startIdx..<endIdx] // take 2 characters as one byte
guard let byte: UInt8 = UInt8(substring, radix: UUID.count) else {
return nil
}
uuid[i] = byte
}
self.init(uuid)
}
private static func generateUUID() -> ContiguousArray<UInt8> {
var uuid: ContiguousArray<UInt8> = ContiguousArray<UInt8>(repeating: 0, count: UUID.count)
uuid.withUnsafeMutableBufferPointer { (uuidPtr: inout UnsafeMutableBufferPointer<UInt8>) -> Void in
// TODO: use /dev/urandom
arc4random_buf(uuidPtr.baseAddress, UUID.count) // TODO: linux
}
makeRFC4122compliant(uuid: &uuid)
return uuid
}
private static func makeRFC4122compliant(uuid: inout ContiguousArray<UInt8>) {
uuid[6] = (uuid[6] & 0x0F) | 0x40 // version https://tools.ietf.org/html/rfc4122#section-4.1.3
uuid[8] = (uuid[8] & 0x3f) | 0x80 // variant https://tools.ietf.org/html/rfc4122#section-4.1.1
}
}
extension UUID: Equatable {
public static func == (lhs: UUID, rhs: UUID) -> Bool {
return lhs.bytes == rhs.bytes
}
}
extension UUID: Hashable {
/// One-at-a-Time hash
/// http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
private var oat_hash: Int {
var hash: Int = 0
for i: Int in 0..<UUID.count {
hash = hash &+ numericCast(bytes[i])
hash = hash &+ (hash << 10)
hash ^= (hash >> 6)
}
hash = hash &+ (hash << 3)
hash ^= (hash << 11)
hash = hash &+ (hash << 15)
return hash
}
public var hashValue: Int { return oat_hash }
}
extension UUID: CustomStringConvertible, CustomDebugStringConvertible {
public var uuidString: String {
var out: String = String()
out.reserveCapacity(UUID.count)
let separatorLayout: [Int] = [0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0]
let separator: String = "-"
var idx: Int = 0
for byte in bytes {
let char = String(byte, radix: UUID.count, uppercase: true)
switch char.count {
case 2:
out.append(char)
default:
out.append("0" + char)
}
if separatorLayout[idx] == 1 {
out.append(separator)
}
idx += 1
}
assert(idx == UUID.count)
assert(out.count == 36)
return out
}
public var description: String { return uuidString }
public var debugDescription: String { return uuidString }
}