blob: 1508d90b8d74b824f072c358d3e46c614de8ab6d [file] [log] [blame]
Chris Simpsona9b6c702018-04-08 07:11:37 -04001/*
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 Edgeb4711a62020-04-24 14:43:03 +010020public struct TList<Element : TSerializable & Hashable> : RandomAccessCollection, MutableCollection, ExpressibleByArrayLiteral, TSerializable, Hashable {
Chris Simpson2566ecd2018-08-29 14:40:44 -040021 public typealias Storage = Array<Element>
Chris Simpsona9b6c702018-04-08 07:11:37 -040022 public typealias Indices = Storage.Indices
23
24 internal var storage = Storage()
25 public init() { }
26 public init(arrayLiteral elements: Element...) {
27 self.storage = Storage(elements)
28 }
29 public init<Source : Sequence>(_ sequence: Source) where Source.Iterator.Element == Element {
30 storage = Storage(sequence)
31 }
32
33 /// Mark: Hashable
Alexander Edgeb4711a62020-04-24 14:43:03 +010034 public func hash(into hasher: inout Hasher) {
35 hasher.combine(storage)
Chris Simpsona9b6c702018-04-08 07:11:37 -040036 }
37
38 /// Mark: TSerializable
39 public static var thriftType : TType { return .list }
40
41 public static func read(from proto: TProtocol) throws -> TList {
42 let (elementType, size) = try proto.readListBegin()
43 if elementType != Element.thriftType {
44 throw TProtocolError(error: .invalidData,
45 extendedError: .unexpectedType(type: elementType))
46 }
47 var list = TList()
48 for _ in 0..<size {
49 let element = try Element.read(from: proto)
50 list.storage.append(element)
51 }
52 try proto.readListEnd()
53 return list
54 }
55
56 public func write(to proto: TProtocol) throws {
57 try proto.writeListBegin(elementType: Element.thriftType, size: Int32(self.count))
58 for element in self.storage {
59 try Element.write(element, to: proto)
60 }
61 try proto.writeListEnd()
62 }
63
64 /// Mark: MutableCollection
65
66 public typealias SubSequence = Storage.SubSequence
67 public typealias Index = Storage.Index
68
69 public subscript(position: Storage.Index) -> Element {
70 get {
71 return storage[position]
72 }
73 set {
74 storage[position] = newValue
75 }
76 }
77
78 public subscript(range: Range<Index>) -> SubSequence {
79 get {
80 return storage[range]
81 }
82 set {
83 storage[range] = newValue
84 }
85 }
86
87 public var startIndex: Index {
88 return storage.startIndex
89 }
90 public var endIndex: Index {
91 return storage.endIndex
92 }
93
94 public func formIndex(after i: inout Index) {
95 storage.formIndex(after: &i)
96 }
97
98 public func formIndex(before i: inout Int) {
99 storage.formIndex(before: &i)
100 }
101
102 public func index(after i: Index) -> Index {
103 return storage.index(after: i)
104 }
105
106 public func index(before i: Int) -> Int {
107 return storage.index(before: i)
108 }
109
110}
111
112extension TList : RangeReplaceableCollection {
113 public mutating func replaceSubrange<C: Collection>(_ subrange: Range<Index>, with newElements: C)
114 where C.Iterator.Element == Element {
115 storage.replaceSubrange(subrange, with: newElements)
116 }
117}
118
119extension TList : CustomStringConvertible, CustomDebugStringConvertible {
120
121 public var description : String {
122 return storage.description
123 }
124
125 public var debugDescription : String {
126 return storage.debugDescription
127 }
128
129}
130
131public func ==<Element>(lhs: TList<Element>, rhs: TList<Element>) -> Bool {
132 return lhs.storage.elementsEqual(rhs.storage) { $0 == $1 }
133}