Content-Length: 593279 | pFad | http://github.com/azureplus/Algorithm/commit/cabc583d105cfa181b69816ced87b23ed4f419be

1C * some refactoring · azureplus/Algorithm@cabc583 · GitHub
Skip to content

Commit cabc583

Browse files
committed
* some refactoring
1 parent 6ffb4d1 commit cabc583

File tree

4 files changed

+18
-66
lines changed

4 files changed

+18
-66
lines changed

Sources/Algorithm+Array.swift

+3-8
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/SortedDictionary.swift

+8-17
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,12 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
8989

9090
//github.com/ 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
//github.com/ 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
//github.com/ Initializer.
@@ -129,13 +121,12 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
129121

130122
public func makeIterator() -> SortedDictionary.Iterator {
131123
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
124+
return AnyIterator {
125+
guard index < self.endIndex else { return nil }
126+
defer {
127+
index += 1
128+
}
129+
return self[index]
139130
}
140131
}
141132

Sources/SortedMultiDictionary.swift

+2-10
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,12 @@ public struct SortedMultiDictionary<Key: Comparable, Value>: Probable, Collectio
8989

9090
//github.com/ 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
//github.com/ 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
//github.com/ Initializer.

Sources/SortedSet.swift

+5-31
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,14 +137,9 @@ public struct SortedSet<T: Comparable>: Probable, Collection, Equatable, CustomS
158137
}
159138

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

@@ -283,7 +257,7 @@ public struct SortedSet<T: Comparable>: Probable, Collection, Equatable, CustomS
283257
*/
284258
mutating public func insert(_ elements: [Element]) {
285259
for x in elements {
286-
tree.insert(value: x, for: x)
260+
tree.insert(value: x, for: x)
287261
}
288262
count = tree.count
289263
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/azureplus/Algorithm/commit/cabc583d105cfa181b69816ced87b23ed4f419be

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy