]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/lib/swift/Sources/TList.swift
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / swift / Sources / TList.swift
CommitLineData
f67539c2
TL
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
20public struct TList<Element : TSerializable> : RandomAccessCollection, MutableCollection, ExpressibleByArrayLiteral, TSerializable, Hashable {
21 public typealias Storage = Array<Element>
22 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
34 public var hashValue : Int {
35 let prime = 31
36 var result = 1
37 for element in storage {
38 result = prime &* result &+ element.hashValue
39 }
40 return result
41 }
42
43 /// Mark: TSerializable
44 public static var thriftType : TType { return .list }
45
46 public static func read(from proto: TProtocol) throws -> TList {
47 let (elementType, size) = try proto.readListBegin()
48 if elementType != Element.thriftType {
49 throw TProtocolError(error: .invalidData,
50 extendedError: .unexpectedType(type: elementType))
51 }
52 var list = TList()
53 for _ in 0..<size {
54 let element = try Element.read(from: proto)
55 list.storage.append(element)
56 }
57 try proto.readListEnd()
58 return list
59 }
60
61 public func write(to proto: TProtocol) throws {
62 try proto.writeListBegin(elementType: Element.thriftType, size: Int32(self.count))
63 for element in self.storage {
64 try Element.write(element, to: proto)
65 }
66 try proto.writeListEnd()
67 }
68
69 /// Mark: MutableCollection
70
71 public typealias SubSequence = Storage.SubSequence
72 public typealias Index = Storage.Index
73
74 public subscript(position: Storage.Index) -> Element {
75 get {
76 return storage[position]
77 }
78 set {
79 storage[position] = newValue
80 }
81 }
82
83 public subscript(range: Range<Index>) -> SubSequence {
84 get {
85 return storage[range]
86 }
87 set {
88 storage[range] = newValue
89 }
90 }
91
92 public var startIndex: Index {
93 return storage.startIndex
94 }
95 public var endIndex: Index {
96 return storage.endIndex
97 }
98
99 public func formIndex(after i: inout Index) {
100 storage.formIndex(after: &i)
101 }
102
103 public func formIndex(before i: inout Int) {
104 storage.formIndex(before: &i)
105 }
106
107 public func index(after i: Index) -> Index {
108 return storage.index(after: i)
109 }
110
111 public func index(before i: Int) -> Int {
112 return storage.index(before: i)
113 }
114
115}
116
117extension TList : RangeReplaceableCollection {
118 public mutating func replaceSubrange<C: Collection>(_ subrange: Range<Index>, with newElements: C)
119 where C.Iterator.Element == Element {
120 storage.replaceSubrange(subrange, with: newElements)
121 }
122}
123
124extension TList : CustomStringConvertible, CustomDebugStringConvertible {
125
126 public var description : String {
127 return storage.description
128 }
129
130 public var debugDescription : String {
131 return storage.debugDescription
132 }
133
134}
135
136public func ==<Element>(lhs: TList<Element>, rhs: TList<Element>) -> Bool {
137 return lhs.storage.elementsEqual(rhs.storage) { $0 == $1 }
138}