// // FamilyTraitSet.swift // FirebladeECS // // Created by Christian Treffs on 09.10.17. // public struct FamilyTraitSet { public let requiresAll: Set public let excludesAll: Set public let setHash: Int public init(requiresAll: [Component.Type], excludesAll: [Component.Type]) { let requiresAll = Set(requiresAll.map { $0.identifier }) let excludesAll = Set(excludesAll.map { $0.identifier }) precondition(FamilyTraitSet.isValid(requiresAll: requiresAll, excludesAll: excludesAll), "invalid family trait created - requiresAll: \(requiresAll), excludesAll: \(excludesAll)") self.requiresAll = requiresAll self.excludesAll = excludesAll self.setHash = FirebladeECS.hash(combine: [requiresAll, excludesAll]) } @inlinable public func isMatch(components: Set) -> Bool { hasAll(components) && hasNone(components) } @inlinable public func hasAll(_ components: Set) -> Bool { requiresAll.isSubset(of: components) } @inlinable public func hasNone(_ components: Set) -> Bool { excludesAll.isDisjoint(with: components) } @inlinable public static func isValid(requiresAll: Set, excludesAll: Set) -> Bool { !requiresAll.isEmpty && requiresAll.isDisjoint(with: excludesAll) } } extension FamilyTraitSet: Equatable { public static func == (lhs: FamilyTraitSet, rhs: FamilyTraitSet) -> Bool { lhs.setHash == rhs.setHash } } extension FamilyTraitSet: Hashable { public func hash(into hasher: inout Hasher) { hasher.combine(setHash) } } extension FamilyTraitSet: CustomStringConvertible { @inlinable public var description: String { "" } } extension FamilyTraitSet: CustomDebugStringConvertible { @inlinable public var debugDescription: String { "" } }