// // RelatedObjectIDsMap.swift // RSDatabase // // Created by Brent Simmons on 9/10/17. // Copyright © 2017 Ranchero Software, LLC. All rights reserved. // import Foundation // Maps objectIDs to Set where the Strings are relatedObjectIDs. struct RelatedObjectIDsMap { private let dictionary: [String: Set] // objectID: Set init(dictionary: [String: Set]) { self.dictionary = dictionary } init(lookupValues: Set) { var d = [String: Set]() for lookupValue in lookupValues { let objectID = lookupValue.objectID let relatedObjectID: String = lookupValue.relatedObjectID if d[objectID] == nil { d[objectID] = Set([relatedObjectID]) } else { d[objectID]!.insert(relatedObjectID) } } self.init(dictionary: d) } func objectIDs() -> Set { return Set(dictionary.keys) } func relatedObjectIDs() -> Set { var ids = Set() for (_, relatedObjectIDs) in dictionary { ids.formUnion(relatedObjectIDs) } return ids } subscript(_ objectID: String) -> Set? { return dictionary[objectID] } } struct LookupValue: Hashable { let objectID: String let relatedObjectID: String let hashValue: Int init(objectID: String, relatedObjectID: String) { self.objectID = objectID self.relatedObjectID = relatedObjectID self.hashValue = (objectID + relatedObjectID).hashValue } static func ==(lhs: LookupValue, rhs: LookupValue) -> Bool { return lhs.objectID == rhs.objectID && lhs.relatedObjectID == rhs.relatedObjectID } }