]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/swift/Sources/TSet.swift
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / swift / Sources / TSet.swift
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 public struct TSet<Element : TSerializable & Hashable> : SetAlgebra, Hashable, Collection, ExpressibleByArrayLiteral, TSerializable {
23 /// Typealias for Storage type
24 public typealias Storage = Set<Element>
25
26
27 /// Internal Storage used for TSet (Set\<Element\>)
28 internal var storage : Storage
29
30
31 /// Mark: Collection
32
33 public typealias Indices = Storage.Indices
34 public typealias Index = Storage.Index
35 public typealias IndexDistance = Int
36 public typealias SubSequence = Storage.SubSequence
37
38
39 public var indices: Indices { return storage.indices }
40
41 // Must implement isEmpty even though both SetAlgebra and Collection provide it due to their conflciting default implementations
42 public var isEmpty: Bool { return storage.isEmpty }
43
44 public func distance(from start: Index, to end: Index) -> IndexDistance {
45 return storage.distance(from: start, to: end)
46 }
47
48 public func index(_ i: Index, offsetBy n: IndexDistance) -> Index {
49 return storage.index(i, offsetBy: n)
50 }
51
52 public func index(_ i: Index, offsetBy n: IndexDistance, limitedBy limit: Index) -> Index? {
53 return storage.index(i, offsetBy: n, limitedBy: limit)
54 }
55
56 #if swift(>=3.2)
57 public subscript (position: Storage.Index) -> Element {
58 return storage[position]
59 }
60 #else
61 public subscript (position: Storage.Index) -> Element? {
62 return storage[position]
63 }
64 #endif
65
66 /// Mark: SetAlgebra
67 internal init(storage: Set<Element>) {
68 self.storage = storage
69 }
70
71 public func contains(_ member: Element) -> Bool {
72 return storage.contains(member)
73 }
74
75 public mutating func insert(_ newMember: Element) -> (inserted: Bool, memberAfterInsert: Element) {
76 return storage.insert(newMember)
77 }
78
79 public mutating func remove(_ member: Element) -> Element? {
80 return storage.remove(member)
81 }
82
83 public func union(_ other: TSet<Element>) -> TSet {
84 return TSet(storage: storage.union(other.storage))
85 }
86
87 public mutating func formIntersection(_ other: TSet<Element>) {
88 return storage.formIntersection(other.storage)
89 }
90
91 public mutating func formSymmetricDifference(_ other: TSet<Element>) {
92 return storage.formSymmetricDifference(other.storage)
93 }
94
95 public mutating func formUnion(_ other: TSet<Element>) {
96 return storage.formUnion(other.storage)
97 }
98
99 public func intersection(_ other: TSet<Element>) -> TSet {
100 return TSet(storage: storage.intersection(other.storage))
101 }
102
103 public func symmetricDifference(_ other: TSet<Element>) -> TSet {
104 return TSet(storage: storage.symmetricDifference(other.storage))
105 }
106
107 public mutating func update(with newMember: Element) -> Element? {
108 return storage.update(with: newMember)
109 }
110
111 /// Mark: IndexableBase
112
113 public var startIndex: Index { return storage.startIndex }
114 public var endIndex: Index { return storage.endIndex }
115 public func index(after i: Index) -> Index {
116 return storage.index(after: i)
117 }
118
119 public func formIndex(after i: inout Storage.Index) {
120 storage.formIndex(after: &i)
121 }
122
123 public subscript(bounds: Range<Index>) -> SubSequence {
124 return storage[bounds]
125 }
126
127
128 /// Mark: Hashable
129 public var hashValue : Int {
130 let prime = 31
131 var result = 1
132 for element in storage {
133 result = prime &* result &+ element.hashValue
134 }
135 return result
136 }
137
138 /// Mark: TSerializable
139 public static var thriftType : TType { return .set }
140
141 public init() {
142 storage = Storage()
143 }
144
145 public init(arrayLiteral elements: Element...) {
146 self.storage = Storage(elements)
147 }
148
149 public init<Source : Sequence>(_ sequence: Source) where Source.Iterator.Element == Element {
150 storage = Storage(sequence)
151 }
152
153 public static func read(from proto: TProtocol) throws -> TSet {
154 let (elementType, size) = try proto.readSetBegin()
155 if elementType != Element.thriftType {
156 throw TProtocolError(error: .invalidData,
157 extendedError: .unexpectedType(type: elementType))
158 }
159 var set = TSet()
160 for _ in 0..<size {
161 let element = try Element.read(from: proto)
162 set.storage.insert(element)
163 }
164 try proto.readSetEnd()
165 return set
166 }
167
168 public func write(to proto: TProtocol) throws {
169 try proto.writeSetBegin(elementType: Element.thriftType, size: Int32(self.count))
170 for element in self.storage {
171 try Element.write(element, to: proto)
172 }
173 try proto.writeSetEnd()
174 }
175 }
176
177 extension TSet: CustomStringConvertible, CustomDebugStringConvertible {
178 public var description : String {
179 return storage.description
180 }
181 public var debugDescription : String {
182 return storage.debugDescription
183 }
184
185 }
186
187 public func ==<Element>(lhs: TSet<Element>, rhs: TSet<Element>) -> Bool {
188 return lhs.storage == rhs.storage
189 }