Skip to content

Commit 81846f1

Browse files
authored
Merge pull request #4 from adamnemecek/master
reafactoring
2 parents 76add40 + 316bffd commit 81846f1

File tree

7 files changed

+25
-119
lines changed

7 files changed

+25
-119
lines changed

Sources/Algorithm+Array.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ extension Array where Element: Equatable {
3737
*/
3838
@discardableResult
3939
mutating func remove(object: Element) -> Element? {
40-
guard let v = index(of: object) else {
41-
return nil
42-
}
43-
return remove(at: v)
40+
return index(of: object).map { self.remove(at: $0) }
4441
}
4542

4643
/**
@@ -82,10 +79,8 @@ extension Array where Element: Equatable {
8279
public func count(of elements: [Element]) -> Int {
8380
var c = 0
8481
for e in elements {
85-
for x in self {
86-
if e == x {
87-
c += 1
88-
}
82+
for x in self where e == x {
83+
c += 1
8984
}
9085
}
9186
return c

Sources/DoublyLinkedList.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,10 @@ public struct DoublyLinkedList<Element>: CustomStringConvertible, Sequence {
167167
public func makeIterator() -> DoublyLinkedList.Iterator {
168168
var it = head
169169
return AnyIterator {
170-
guard let e = it?.element else {
171-
return nil
172-
}
170+
defer {
173171
it = it?.next
174-
return e
172+
}
173+
return it?.element
175174
}
176175
}
177176

Sources/RedBlackTree.swift

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
7575
- returns: String
7676
*/
7777
public var description: String {
78-
var output = "["
79-
let l = count - 1
80-
for i in 0..<count {
81-
output += "\(self[i])"
82-
if i != l {
83-
output += ", "
84-
}
85-
}
86-
return output + "]"
78+
return "[" + map { "\($0)" }.joined(separator: ", ") + "]"
8779
}
8880

8981
/**
@@ -166,15 +158,8 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
166158
// :returns: RedBlackTree.Generator
167159
//
168160
public func makeIterator() -> RedBlackTree.Iterator {
169-
var index = startIndex
170-
return AnyIterator {
171-
if index < self.endIndex {
172-
let i: Int = index
173-
index += 1
174-
return self[i]
175-
}
176-
return nil
177-
}
161+
var i = indices.makeIterator()
162+
return AnyIterator { i.next().map { self[$0] } }
178163
}
179164

180165
/**

Sources/SortedDictionary.swift

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,12 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
8989

9090
/// Retrieves an Array of the key values in order.
9191
public var keys: [Key] {
92-
var s = [Key]()
93-
for x in self {
94-
s.append(x.key)
95-
}
96-
return s
92+
return map { $0.key }
9793
}
9894

9995
/// Retrieves an Array of the values that are sorted based
10096
public var values: [Value] {
101-
var s = [Value]()
102-
for x in self {
103-
s.append(x.value!)
104-
}
105-
return s
97+
return flatMap { $0.value }
10698
}
10799

108100
/// Initializer.
@@ -128,15 +120,8 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
128120
}
129121

130122
public func makeIterator() -> SortedDictionary.Iterator {
131-
var index = startIndex
132-
return AnyIterator {
133-
if index < self.endIndex {
134-
let i: Int = index
135-
index += 1
136-
return self[i]
137-
}
138-
return nil
139-
}
123+
var i = indices.makeIterator()
124+
return AnyIterator { i.next().map { self[$0] } }
140125
}
141126

142127
/**

Sources/SortedMultiDictionary.swift

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,12 @@ public struct SortedMultiDictionary<Key: Comparable, Value>: Probable, Collectio
8989

9090
/// Retrieves an Array of the key values in order.
9191
public var keys: [Key] {
92-
var s = [Key]()
93-
for x in self {
94-
s.append(x.key)
95-
}
96-
return s
92+
return map { $0.key }
9793
}
9894

9995
/// Retrieves an Array of the values that are sorted based
10096
public var values: [Value] {
101-
var s = [Value]()
102-
for x in self {
103-
s.append(x.value!)
104-
}
105-
return s
97+
return flatMap { $0.value }
10698
}
10799

108100
/// Initializer.
@@ -128,15 +120,8 @@ public struct SortedMultiDictionary<Key: Comparable, Value>: Probable, Collectio
128120
}
129121

130122
public func makeIterator() -> SortedMultiDictionary.Iterator {
131-
var index = startIndex
132-
return AnyIterator {
133-
if index < self.endIndex {
134-
let i: Int = index
135-
index += 1
136-
return self[i]
137-
}
138-
return nil
139-
}
123+
var i = indices.makeIterator()
124+
return AnyIterator { i.next().map { self[$0] } }
140125
}
141126

142127
/**

Sources/SortedMultiSet.swift

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,7 @@ public struct SortedMultiSet<T: Comparable>: Probable, Collection, Equatable, Cu
7272
- returns: String
7373
*/
7474
public var description: String {
75-
var output = "["
76-
let l = count - 1
77-
for i in 0..<count {
78-
output += "\(self[i])"
79-
if i != l {
80-
output += ", "
81-
}
82-
}
83-
return output + "]"
75+
return "[" + map { "\($0)" }.joined(separator: ", ") + "]"
8476
}
8577

8678
/**
@@ -165,15 +157,8 @@ public struct SortedMultiSet<T: Comparable>: Probable, Collection, Equatable, Cu
165157
// :returns: SortedMultiSet.Generator
166158
//
167159
public func makeIterator() -> SortedMultiSet.Iterator {
168-
var index = startIndex
169-
return AnyIterator {
170-
if index < self.endIndex {
171-
let i = index
172-
index += 1
173-
return self[i]
174-
}
175-
return nil
176-
}
160+
var i = indices.makeIterator()
161+
return AnyIterator { i.next().map { self[$0] } }
177162
}
178163

179164
/**

Sources/SortedSet.swift

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ public struct SortedSet<T: Comparable>: Probable, Collection, Equatable, CustomS
5858
:name: asArray
5959
*/
6060
public var asArray: [Element] {
61-
var a = [Element]()
62-
for x in self {
63-
a.append(x)
64-
}
65-
return a
61+
return map { $0 }
6662
}
6763

6864
/**
@@ -72,15 +68,7 @@ public struct SortedSet<T: Comparable>: Probable, Collection, Equatable, CustomS
7268
- returns: String
7369
*/
7470
public var description: String {
75-
var output = "["
76-
let l = count - 1
77-
for i in 0..<count {
78-
output += "\(self[i])"
79-
if i != l {
80-
output += ", "
81-
}
82-
}
83-
return output + "]"
71+
return "[" + map { "\($0)" }.joined(separator: ",") + "]"
8472
}
8573

8674
/**
@@ -105,15 +93,6 @@ public struct SortedSet<T: Comparable>: Probable, Collection, Equatable, CustomS
10593
return tree.last?.value
10694
}
10795

108-
/**
109-
:name: isEmpty
110-
:description: A boolean of whether the RedBlackTree is empty.
111-
- returns: Bool
112-
*/
113-
public var isEmpty: Bool {
114-
return 0 == count
115-
}
116-
11796
/**
11897
:name: startIndex
11998
:description: Conforms to the Collection Protocol.
@@ -158,15 +137,8 @@ public struct SortedSet<T: Comparable>: Probable, Collection, Equatable, CustomS
158137
}
159138

160139
public func makeIterator() -> SortedSet.Iterator {
161-
var index = startIndex
162-
return AnyIterator {
163-
if index < self.endIndex {
164-
let i = index
165-
index += 1
166-
return self[i]
167-
}
168-
return nil
169-
}
140+
var i = indices.makeIterator()
141+
return AnyIterator { i.next().map { self[$0] } }
170142
}
171143

172144
/**
@@ -283,7 +255,7 @@ public struct SortedSet<T: Comparable>: Probable, Collection, Equatable, CustomS
283255
*/
284256
mutating public func insert(_ elements: [Element]) {
285257
for x in elements {
286-
tree.insert(value: x, for: x)
258+
tree.insert(value: x, for: x)
287259
}
288260
count = tree.count
289261
}

0 commit comments

Comments
 (0)