Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [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 | |
| 20 | import Foundation |
| 21 | |
| 22 | |
| 23 | |
| 24 | public struct TList<Element : TSerializable> : MutableCollectionType, Hashable, ArrayLiteralConvertible, TSerializable { |
| 25 | |
| 26 | public static var thriftType : TType { return .LIST } |
| 27 | |
| 28 | typealias Storage = Array<Element> |
| 29 | |
| 30 | public typealias Index = Storage.Index |
| 31 | |
| 32 | private var storage = Storage() |
| 33 | |
| 34 | public var startIndex : Index { |
| 35 | return storage.startIndex |
| 36 | } |
| 37 | |
| 38 | public var endIndex : Index { |
| 39 | return storage.endIndex |
| 40 | } |
| 41 | |
| 42 | public subscript (position: Index) -> Element { |
| 43 | get { |
| 44 | return storage[position] |
| 45 | } |
| 46 | set { |
| 47 | storage[position] = newValue |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public var hashValue : Int { |
| 52 | let prime = 31 |
| 53 | var result = 1 |
| 54 | for element in storage { |
| 55 | result = prime * result + element.hashValue |
| 56 | } |
| 57 | return result |
| 58 | } |
| 59 | |
| 60 | public init(arrayLiteral elements: Element...) { |
| 61 | self.storage = Storage(storage) |
| 62 | } |
| 63 | |
| 64 | public init() { |
| 65 | self.storage = Storage() |
| 66 | } |
| 67 | |
| 68 | public mutating func append(newElement: Element) { |
| 69 | self.storage.append(newElement) |
| 70 | } |
| 71 | |
| 72 | public mutating func appendContentsOf<C : CollectionType where C.Generator.Element == Element>(newstorage: C) { |
| 73 | self.storage.appendContentsOf(newstorage) |
| 74 | } |
| 75 | |
| 76 | public mutating func insert(newElement: Element, atIndex index: Int) { |
| 77 | self.storage.insert(newElement, atIndex: index) |
| 78 | } |
| 79 | |
| 80 | public mutating func insertContentsOf<C : CollectionType where C.Generator.Element == Element>(newElements: C, at index: Int) { |
| 81 | self.storage.insertContentsOf(newElements, at: index) |
| 82 | } |
| 83 | |
| 84 | public mutating func removeAll(keepCapacity keepCapacity: Bool = true) { |
| 85 | self.storage.removeAll(keepCapacity: keepCapacity) |
| 86 | } |
| 87 | |
| 88 | public mutating func removeAtIndex(index: Index) { |
| 89 | self.storage.removeAtIndex(index) |
| 90 | } |
| 91 | |
| 92 | public mutating func removeFirst(n: Int = 0) { |
| 93 | self.storage.removeFirst(n) |
| 94 | } |
| 95 | |
| 96 | public mutating func removeLast() -> Element { |
| 97 | return self.storage.removeLast() |
| 98 | } |
| 99 | |
| 100 | public mutating func removeRange(subRange: Range<Index>) { |
| 101 | self.storage.removeRange(subRange) |
| 102 | } |
| 103 | |
| 104 | public mutating func reserveCapacity(minimumCapacity: Int) { |
| 105 | self.storage.reserveCapacity(minimumCapacity) |
| 106 | } |
| 107 | |
| 108 | public static func readValueFromProtocol(proto: TProtocol) throws -> TList { |
| 109 | let (elementType, size) = try proto.readListBegin() |
| 110 | if elementType != Element.thriftType { |
| 111 | throw NSError( |
| 112 | domain: TProtocolErrorDomain, |
| 113 | code: Int(TProtocolError.InvalidData.rawValue), |
| 114 | userInfo: [TProtocolErrorExtendedErrorKey: NSNumber(int: TProtocolExtendedError.UnexpectedType.rawValue)]) |
| 115 | } |
| 116 | var list = TList() |
| 117 | for _ in 0..<size { |
| 118 | let element = try Element.readValueFromProtocol(proto) |
| 119 | list.storage.append(element) |
| 120 | } |
| 121 | try proto.readListEnd() |
| 122 | return list |
| 123 | } |
| 124 | |
| 125 | public static func writeValue(value: TList, toProtocol proto: TProtocol) throws { |
| 126 | try proto.writeListBeginWithElementType(Element.thriftType, size: value.count) |
| 127 | for element in value.storage { |
| 128 | try Element.writeValue(element, toProtocol: proto) |
| 129 | } |
| 130 | try proto.writeListEnd() |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | extension TList : CustomStringConvertible, CustomDebugStringConvertible { |
| 135 | |
| 136 | public var description : String { |
| 137 | return storage.description |
| 138 | } |
| 139 | |
| 140 | public var debugDescription : String { |
| 141 | return storage.debugDescription |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | |
| 146 | public func ==<Element>(lhs: TList<Element>, rhs: TList<Element>) -> Bool { |
| 147 | return lhs.storage == rhs.storage |
| 148 | } |