blob: 85833e5c0780597f720bcb90064116c5fbb74603 [file] [log] [blame]
Jens Geyer56e5b9b2015-10-09 22:01:55 +02001/*
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
20import Foundation
21
22
23public struct TSet<Element : TSerializable> : CollectionType, ArrayLiteralConvertible, TSerializable {
24
25 public static var thriftType : TType { return .SET }
26
27 public typealias Index = Storage.Index
28
29 typealias Storage = Set<Element>
30
31 private var storage : Storage
32
33 public init() {
34 storage = Storage()
35 }
36
37 public init(arrayLiteral elements: Element...) {
38 storage = Storage(elements)
39 }
40
41 public init<S : SequenceType where S.Generator.Element == Element>(_ sequence: S) {
42 storage = Storage(sequence)
43 }
44
45 public var startIndex : Index { return storage.startIndex }
46
47 public var endIndex : Index { return storage.endIndex }
48
49 public mutating func insert(member: Element) {
50 return storage.insert(member)
51 }
52
53 public mutating func remove(element: Element) -> Element? {
54 return storage.remove(element)
55 }
56
57 public mutating func removeAll(keepCapacity keepCapacity: Bool = false) {
58 return storage.removeAll(keepCapacity: keepCapacity)
59 }
60
61 public mutating func removeAtIndex(index: SetIndex<Element>) -> Element {
62 return storage.removeAtIndex(index)
63 }
64
65 public subscript (position: SetIndex<Element>) -> Element {
66 return storage[position]
67 }
68
69 public func union(other: TSet) -> TSet {
70 return TSet(storage.union(other))
71 }
72
73 public func intersect(other: TSet) -> TSet {
74 return TSet(storage.intersect(other))
75 }
76
77 public func exclusiveOr(other: TSet) -> TSet {
78 return TSet(storage.exclusiveOr(other))
79 }
80
81 public func subtract(other: TSet) -> TSet {
82 return TSet(storage.subtract(other))
83 }
84
85 public mutating func intersectInPlace(other: TSet) {
86 storage.intersectInPlace(other)
87 }
88
89 public mutating func exclusiveOrInPlace(other: TSet) {
90 storage.exclusiveOrInPlace(other)
91 }
92
93 public mutating func subtractInPlace(other: TSet) {
94 storage.subtractInPlace(other)
95 }
96
97 public func isSubsetOf(other: TSet) -> Bool {
98 return storage.isSubsetOf(other)
99 }
100
101 public func isDisjointWith(other: TSet) -> Bool {
102 return storage.isDisjointWith(other)
103 }
104
105 public func isSupersetOf(other: TSet) -> Bool {
106 return storage.isSupersetOf(other)
107 }
108
109 public var isEmpty: Bool { return storage.isEmpty }
110
111 public var hashValue : Int {
112 let prime = 31
113 var result = 1
114 for element in storage {
115 result = prime * result + element.hashValue
116 }
117 return result
118 }
119
120 public static func readValueFromProtocol(proto: TProtocol) throws -> TSet {
121 let (elementType, size) = try proto.readSetBegin()
122 if elementType != Element.thriftType {
123 throw NSError(
124 domain: TProtocolErrorDomain,
125 code: Int(TProtocolError.InvalidData.rawValue),
126 userInfo: [TProtocolErrorExtendedErrorKey: NSNumber(int: elementType.rawValue)])
127 }
128 var set = TSet()
129 for _ in 0..<size {
130 let element = try Element.readValueFromProtocol(proto)
131 set.storage.insert(element)
132 }
133 try proto.readSetEnd()
134 return set
135 }
136
137 public static func writeValue(value: TSet, toProtocol proto: TProtocol) throws {
138 try proto.writeSetBeginWithElementType(Element.thriftType, size: value.count)
139 for element in value.storage {
140 try Element.writeValue(element, toProtocol: proto)
141 }
142 try proto.writeSetEnd()
143 }
144
145}
146
147extension TSet : CustomStringConvertible, CustomDebugStringConvertible {
148
149 public var description : String {
150 return storage.description
151 }
152
153 public var debugDescription : String {
154 return storage.debugDescription
155 }
156
157}
158
159public func ==<Element>(lhs: TSet<Element>, rhs: TSet<Element>) -> Bool {
160 return lhs.storage == rhs.storage
161}