mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			106 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			106 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Swift
		
	
| 
											7 years ago
										 | // | ||
|  | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
|  | // | ||
|  | 
 | ||
|  | import Foundation | ||
|  | 
 | ||
| 
											7 years ago
										 | public class OrderedDictionary<KeyType: Hashable, ValueType> { | ||
| 
											7 years ago
										 | 
 | ||
| 
											7 years ago
										 |     private var keyValueMap = [KeyType: ValueType]() | ||
| 
											7 years ago
										 | 
 | ||
| 
											7 years ago
										 |     public var orderedKeys = [KeyType]() | ||
| 
											7 years ago
										 | 
 | ||
| 
											7 years ago
										 |     public init() { } | ||
| 
											7 years ago
										 | 
 | ||
|  |     // Used to clone copies of instances of this class. | ||
|  |     public init(keyValueMap: [KeyType: ValueType], | ||
|  |                 orderedKeys: [KeyType]) { | ||
|  | 
 | ||
|  |         self.keyValueMap = keyValueMap | ||
|  |         self.orderedKeys = orderedKeys | ||
|  |     } | ||
|  | 
 | ||
|  |     // Since the contents are immutable, we only modify copies | ||
|  |     // made with this method. | ||
| 
											7 years ago
										 |     public func clone() -> OrderedDictionary<KeyType, ValueType> { | ||
| 
											7 years ago
										 |         return OrderedDictionary(keyValueMap: keyValueMap, orderedKeys: orderedKeys) | ||
|  |     } | ||
|  | 
 | ||
|  |     public func value(forKey key: KeyType) -> ValueType? { | ||
|  |         return keyValueMap[key] | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     public func hasValue(forKey key: KeyType) -> Bool { | ||
|  |         return keyValueMap[key] != nil | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     public func append(key: KeyType, value: ValueType) { | ||
|  |         if keyValueMap[key] != nil { | ||
|  |             owsFailDebug("Unexpected duplicate key in key map: \(key)") | ||
|  |         } | ||
|  |         keyValueMap[key] = value | ||
|  | 
 | ||
|  |         if orderedKeys.contains(key) { | ||
|  |             owsFailDebug("Unexpected duplicate key in key list: \(key)") | ||
|  |         } else { | ||
|  |             orderedKeys.append(key) | ||
|  |         } | ||
|  | 
 | ||
|  |         if orderedKeys.count != keyValueMap.count { | ||
|  |             owsFailDebug("Invalid contents.") | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     public func replace(key: KeyType, value: ValueType) { | ||
|  |         if keyValueMap[key] == nil { | ||
|  |             owsFailDebug("Missing key in key map: \(key)") | ||
|  |         } | ||
|  |         keyValueMap[key] = value | ||
|  | 
 | ||
|  |         if !orderedKeys.contains(key) { | ||
|  |             owsFailDebug("Missing key in key list: \(key)") | ||
|  |         } | ||
|  | 
 | ||
|  |         if orderedKeys.count != keyValueMap.count { | ||
|  |             owsFailDebug("Invalid contents.") | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     public func remove(key: KeyType) { | ||
|  |         if keyValueMap[key] == nil { | ||
|  |             owsFailDebug("Missing key in key map: \(key)") | ||
|  |         } else { | ||
|  |             keyValueMap.removeValue(forKey: key) | ||
|  |         } | ||
|  | 
 | ||
|  |         if !orderedKeys.contains(key) { | ||
|  |             owsFailDebug("Missing key in key list: \(key)") | ||
|  |         } else { | ||
|  |             orderedKeys = orderedKeys.filter { $0 != key } | ||
|  |         } | ||
|  | 
 | ||
|  |         if orderedKeys.count != keyValueMap.count { | ||
|  |             owsFailDebug("Invalid contents.") | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     public var count: Int { | ||
|  |         if orderedKeys.count != keyValueMap.count { | ||
|  |             owsFailDebug("Invalid contents.") | ||
|  |         } | ||
|  |         return orderedKeys.count | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     public var orderedValues: [ValueType] { | ||
| 
											7 years ago
										 |         var values = [ValueType]() | ||
|  |         for key in orderedKeys { | ||
|  |             guard let value = self.keyValueMap[key] else { | ||
|  |                 owsFailDebug("Missing value") | ||
|  |                 continue | ||
|  |             } | ||
|  |             values.append(value) | ||
|  |         } | ||
|  |         return values | ||
|  |     } | ||
|  | } |