Chris Simpson | a9b6c70 | 2018-04-08 07:11:37 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Alexander Edge | b4711a6 | 2020-04-24 14:43:03 +0100 | [diff] [blame] | 20 | public struct TMap<Key : TSerializable & Hashable, Value : TSerializable & Hashable>: Collection, ExpressibleByDictionaryLiteral, Hashable, TSerializable { |
Chris Simpson | 2566ecd | 2018-08-29 14:40:44 -0400 | [diff] [blame] | 21 | public typealias Storage = Dictionary<Key, Value> |
Chris Simpson | a9b6c70 | 2018-04-08 07:11:37 -0400 | [diff] [blame] | 22 | public typealias Element = Storage.Element |
| 23 | public typealias Index = Storage.Index |
Antoine Cœur | 08a6eb6 | 2019-07-08 18:42:09 +0800 | [diff] [blame] | 24 | public typealias IndexDistance = Int |
Chris Simpson | a9b6c70 | 2018-04-08 07:11:37 -0400 | [diff] [blame] | 25 | public typealias Indices = Storage.Indices |
| 26 | public typealias SubSequence = Storage.SubSequence |
| 27 | internal var storage = Storage() |
| 28 | |
| 29 | /// Mark: Be Like Dictionary |
| 30 | |
| 31 | public func indexForKey(_ key: Key) -> Index? { |
| 32 | return storage.index(forKey: key) |
| 33 | } |
| 34 | |
| 35 | public mutating func updateValue(_ value: Value, forKey key: Key) -> Value? { |
Antoine Cœur | 08a6eb6 | 2019-07-08 18:42:09 +0800 | [diff] [blame] | 36 | return storage.updateValue(value, forKey: key) |
Chris Simpson | a9b6c70 | 2018-04-08 07:11:37 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | public mutating func removeValueForKey(_ key: Key) -> Value? { |
| 40 | return storage.removeValue(forKey: key) |
| 41 | } |
| 42 | |
| 43 | public init(minimumCapacity: Int) { |
| 44 | storage = Storage(minimumCapacity: minimumCapacity) |
| 45 | } |
| 46 | |
| 47 | /// init from Dictionary<K,V> |
| 48 | public init(_ dict: [Key: Value]) { |
| 49 | storage = dict |
| 50 | } |
| 51 | |
| 52 | /// read only access to storage if needed as Dictionary<K,V> |
| 53 | public var dictionary: [Key: Value] { |
| 54 | return storage |
| 55 | } |
| 56 | |
| 57 | public subscript (key: Key) -> Value? { |
| 58 | get { |
| 59 | return storage[key] |
| 60 | } |
| 61 | set { |
| 62 | storage[key] = newValue |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /// Mark: Collection |
| 67 | |
| 68 | public var indices: Indices { |
| 69 | return storage.indices |
| 70 | } |
| 71 | |
| 72 | public func distance(from start: Index, to end: Index) -> IndexDistance { |
| 73 | return storage.distance(from: start, to: end) |
| 74 | } |
| 75 | |
| 76 | public func index(_ i: Index, offsetBy n: IndexDistance) -> Index { |
| 77 | return storage.index(i, offsetBy: n) |
| 78 | } |
| 79 | |
| 80 | public func index(_ i: Index, offsetBy n: IndexDistance, limitedBy limit: Index) -> Index? { |
| 81 | return storage.index(i, offsetBy: n, limitedBy: limit) |
| 82 | } |
| 83 | |
| 84 | public subscript(position: Index) -> Element { |
| 85 | return storage[position] |
| 86 | } |
| 87 | |
| 88 | /// Mark: IndexableBase |
| 89 | |
| 90 | public var startIndex: Index { return storage.startIndex } |
| 91 | public var endIndex: Index { return storage.endIndex } |
| 92 | public func index(after i: Index) -> Index { |
| 93 | return storage.index(after: i) |
| 94 | } |
| 95 | |
| 96 | public func formIndex(after i: inout Index) { |
| 97 | storage.formIndex(after: &i) |
| 98 | } |
| 99 | |
| 100 | public subscript(bounds: Range<Index>) -> SubSequence { |
| 101 | return storage[bounds] |
| 102 | } |
| 103 | |
| 104 | /// Mark: DictionaryLiteralConvertible |
| 105 | |
| 106 | public init(dictionaryLiteral elements: (Key, Value)...) { |
| 107 | storage = Storage() |
| 108 | for (key, value) in elements { |
| 109 | storage[key] = value |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Mark: Hashable |
Alexander Edge | b4711a6 | 2020-04-24 14:43:03 +0100 | [diff] [blame] | 114 | |
| 115 | public func hash(into hasher: inout Hasher) { |
| 116 | hasher.combine(storage) |
Chris Simpson | a9b6c70 | 2018-04-08 07:11:37 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /// Mark: TSerializable |
| 120 | |
| 121 | public static var thriftType : TType { return .map } |
| 122 | public init() { |
| 123 | storage = Storage() |
| 124 | } |
| 125 | |
| 126 | public static func read(from proto: TProtocol) throws -> TMap { |
| 127 | |
| 128 | let (keyType, valueType, size) = try proto.readMapBegin() |
| 129 | if size > 0 { |
| 130 | if keyType != Key.thriftType { |
| 131 | throw TProtocolError(error: .invalidData, |
| 132 | message: "Unexpected TMap Key Type", |
| 133 | extendedError: .unexpectedType(type: keyType)) |
| 134 | } |
| 135 | if valueType != Value.thriftType { |
| 136 | throw TProtocolError(error: .invalidData, |
| 137 | message: "Unexpected TMap Value Type", |
| 138 | extendedError: .unexpectedType(type: valueType)) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | var map = TMap() |
| 143 | for _ in 0..<size { |
| 144 | let key = try Key.read(from: proto) |
| 145 | let value = try Value.read(from: proto) |
| 146 | map.storage[key] = value |
| 147 | } |
| 148 | try proto.readMapEnd() |
| 149 | return map |
| 150 | } |
| 151 | |
| 152 | public func write(to proto: TProtocol) throws { |
| 153 | try proto.writeMapBegin(keyType: Key.thriftType, |
| 154 | valueType: Value.thriftType, size: Int32(self.count)) |
| 155 | for (key, value) in self.storage { |
| 156 | try Key.write(key, to: proto) |
| 157 | try Value.write(value, to: proto) |
| 158 | } |
| 159 | try proto.writeMapEnd() |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /// Mark: CustomStringConvertible, CustomDebugStringConvertible |
| 164 | |
| 165 | extension TMap : CustomStringConvertible, CustomDebugStringConvertible { |
| 166 | |
| 167 | public var description : String { |
| 168 | return storage.description |
| 169 | } |
| 170 | |
| 171 | public var debugDescription : String { |
| 172 | return storage.debugDescription |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| 177 | /// Mark: Equatable |
| 178 | |
| 179 | public func ==<Key, Value>(lhs: TMap<Key,Value>, rhs: TMap<Key, Value>) -> Bool { |
| 180 | if lhs.count != rhs.count { |
| 181 | return false |
| 182 | } |
Kino Roy | a9da9eb | 2022-10-07 23:13:01 -0700 | [diff] [blame] | 183 | return lhs.storage == rhs.storage |
Chris Simpson | a9b6c70 | 2018-04-08 07:11:37 -0400 | [diff] [blame] | 184 | } |