Skip to content

Commit 07dbf81

Browse files
author
Daniel Dahan
committed
updates for Swift 3
1 parent 1f33eda commit 07dbf81

25 files changed

+340
-292
lines changed

Algorithm.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,11 @@
373373
TargetAttributes = {
374374
65744C9B1C554BA50011C977 = {
375375
CreatedOnToolsVersion = 7.2;
376+
LastSwiftMigration = 0800;
376377
};
377378
65744CA51C554BA50011C977 = {
378379
CreatedOnToolsVersion = 7.2;
380+
LastSwiftMigration = 0800;
379381
};
380382
65744CC51C554E370011C977 = {
381383
CreatedOnToolsVersion = 7.2;
@@ -637,6 +639,7 @@
637639
PRODUCT_NAME = Algorithm;
638640
SKIP_INSTALL = YES;
639641
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
642+
SWIFT_VERSION = 3.0;
640643
};
641644
name = Debug;
642645
};
@@ -654,6 +657,7 @@
654657
PRODUCT_BUNDLE_IDENTIFIER = "io.cosmicmind.Algorithm-iOS";
655658
PRODUCT_NAME = Algorithm;
656659
SKIP_INSTALL = YES;
660+
SWIFT_VERSION = 3.0;
657661
};
658662
name = Release;
659663
};
@@ -666,6 +670,7 @@
666670
PRODUCT_BUNDLE_IDENTIFIER = io.cosmicmind.AlgorithmTests;
667671
PRODUCT_NAME = "$(TARGET_NAME)";
668672
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
673+
SWIFT_VERSION = 3.0;
669674
};
670675
name = Debug;
671676
};
@@ -677,6 +682,7 @@
677682
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
678683
PRODUCT_BUNDLE_IDENTIFIER = io.cosmicmind.AlgorithmTests;
679684
PRODUCT_NAME = "$(TARGET_NAME)";
685+
SWIFT_VERSION = 3.0;
680686
};
681687
name = Release;
682688
};

Sources/Algorithm+Array.swift

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,40 @@
2929
*/
3030

3131
extension Array where Element: Equatable {
32-
mutating func removeObject(object: Element) -> Element? {
33-
if let v: Int = indexOf(object) {
34-
return removeAtIndex(v)
32+
mutating func remove(object: Element) -> Element? {
33+
if let v: Int = index(of: object) {
34+
return remove(at: v)
3535
}
3636
return nil
3737
}
3838

39-
mutating func removeObjects(objects: Element...) {
39+
mutating func remove(objects: Element...) {
4040
removeObjects(objects)
4141
}
4242

43-
mutating func removeObjects(objects: Array<Element>) {
43+
mutating func remove(objects: [Element]) {
4444
for x in objects {
4545
removeObject(x)
4646
}
4747
}
4848
}
4949

50-
extension Array : ProbableType {
50+
extension Array: ProbableType {
5151
/**
52-
The instance count of elements.
53-
*/
54-
public func countOf<Element: Equatable>(elements: Element...) -> Int {
55-
return countOf(elements)
52+
The total count for the given Elements.
53+
- Parameter of elements: A list of Elements.
54+
- Returns: An Int.
55+
*/
56+
public func count<Element: Equatable>(of elements: Element...) -> Int {
57+
return count(of: elements)
5658
}
5759

58-
/**
59-
The instance count of elements.
60-
*/
61-
public func countOf<Element: Equatable>(elements: Array<Element>) -> Int {
60+
/**
61+
The total count for the given Elements.
62+
- Parameter of elements: An Array of Elements.
63+
- Returns: An Int.
64+
*/
65+
public func count<Element: Equatable>(of elements: [Element]) -> Int {
6266
var c: Int = 0
6367
for v in elements {
6468
for x in self {
@@ -70,48 +74,65 @@ extension Array : ProbableType {
7074
return c
7175
}
7276

73-
/**
74-
The probability of elements.
75-
*/
76-
public func probabilityOf<Element: Equatable>(elements: Element...) -> Double {
77-
return probabilityOf(elements)
77+
/**
78+
The probability of getting the given Elements.
79+
- Parameter of elements: A list of Elements.
80+
- Returns: A Double.
81+
*/
82+
public func probability<Element: Equatable>(of elements: Element...) -> Double {
83+
return probability(of: elements)
7884
}
7985

80-
/**
81-
The probability of elements.
82-
*/
83-
public func probabilityOf<Element: Equatable>(elements: Array<Element>) -> Double {
84-
return 0 == count ? 0 : Double(countOf(elements)) / Double(count)
86+
/**
87+
The probability of getting the given Elements.
88+
- Parameter of elements: An Array of Elements.
89+
- Returns: A Double.
90+
*/
91+
public func probability<Element: Equatable>(of elements: [Element]) -> Double {
92+
guard 0 < count else {
93+
return 0
94+
}
95+
96+
return Double(count(of: elements)) / Double(count)
8597
}
8698

87-
/**
88-
The probability of elements.
89-
*/
90-
public func probabilityOf(block: (element: Element) -> Bool) -> Double {
91-
if 0 == count {
92-
return 0
93-
}
99+
/**
100+
A probability method that uses a block to determine the member state of a condition.
101+
- Parameter of elements: A list of Elements.
102+
- Returns: A Double.
103+
*/
104+
public func probability(of block: (element: Element) -> Bool) -> Double {
105+
guard 0 < count else {
106+
return 0
107+
}
94108

95109
var c: Int = 0
96110
for x in self {
97111
if block(element: x) {
98112
c += 1
99113
}
100114
}
115+
101116
return Double(c) / Double(count)
102117
}
103118

104-
/**
105-
The expected value of elements.
106-
*/
107-
public func expectedValueOf<Element: Equatable>(trials: Int, elements: Element...) -> Double {
108-
return expectedValueOf(trials, elements: elements)
119+
/**
120+
Calculates the expected value of elements based on a given number of trials.
121+
- Parameter trials: Number of trials.
122+
- Parameter elements: A list of Elements.
123+
- Returns: A Double.
124+
*/
125+
public func expectedValue<Element: Equatable>(trials: Int, elements: Element...) -> Double {
126+
return expectedValue(trials: trials, elements: elements)
109127
}
110128

111-
/**
112-
The expected value of elements.
113-
*/
114-
public func expectedValueOf<Element: Equatable>(trials: Int, elements: Array<Element>) -> Double {
115-
return Double(trials) * probabilityOf(elements)
129+
/**
130+
Calculates the expected value of elements based on a given number of trials.
131+
- Parameter trials: Number of trials.
132+
- Parameter elements: An Array of Elements.
133+
- Returns: A Double.
134+
*/
135+
public func expectedValue<Element: Equatable>(trials: Int, elements: [Element]) -> Double {
136+
return Double(trials) * probability(of: elements)
116137
}
117138
}

Sources/Algorithm+Set.swift

Lines changed: 88 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -28,71 +28,92 @@
2828
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31-
extension Set : ProbableType {
32-
/**
33-
The instance count of elements.
34-
*/
35-
public func countOf<Element: Equatable>(elements: Element...) -> Int {
36-
return countOf(elements)
37-
}
38-
39-
/**
40-
The instance count of elements.
41-
*/
42-
public func countOf<Element: Equatable>(elements: Array<Element>) -> Int {
43-
var c: Int = 0
44-
for v in elements {
45-
for x in self {
46-
if v == x as! Element {
47-
c += 1
48-
}
49-
}
50-
}
51-
return c
52-
}
53-
54-
/**
55-
The probability of elements.
56-
*/
57-
public func probabilityOf<Element: Equatable>(elements: Element...) -> Double {
58-
return probabilityOf(elements)
59-
}
60-
61-
/**
62-
The probability of elements.
63-
*/
64-
public func probabilityOf<Element: Equatable>(elements: Array<Element>) -> Double {
65-
return 0 == count ? 0 : Double(countOf(elements)) / Double(count)
66-
}
67-
68-
/**
69-
The probability of elements.
70-
*/
71-
public func probabilityOf(block: (element: Element) -> Bool) -> Double {
72-
if 0 == count {
73-
return 0
74-
}
75-
76-
var c: Int = 0
77-
for x in self {
78-
if block(element: x) {
79-
c += 1
80-
}
81-
}
82-
return Double(c) / Double(count)
83-
}
84-
85-
/**
86-
The expected value of elements.
87-
*/
88-
public func expectedValueOf<Element: Equatable>(trials: Int, elements: Element...) -> Double {
89-
return expectedValueOf(trials, elements: elements)
90-
}
91-
92-
/**
93-
The expected value of elements.
94-
*/
95-
public func expectedValueOf<Element: Equatable>(trials: Int, elements: Array<Element>) -> Double {
96-
return Double(trials) * probabilityOf(elements)
97-
}
31+
extension Set: ProbableType {
32+
/**
33+
The total count for the given Elements.
34+
- Parameter of elements: A list of Elements.
35+
- Returns: An Int.
36+
*/
37+
public func count<Element: Equatable>(of elements: Element...) -> Int {
38+
return count(of: elements)
39+
}
40+
41+
/**
42+
The total count for the given Elements.
43+
- Parameter of elements: An Array of Elements.
44+
- Returns: An Int.
45+
*/
46+
public func count<Element: Equatable>(of elements: [Element]) -> Int {
47+
var c: Int = 0
48+
for v in elements {
49+
for x in self {
50+
if v == x as! Element {
51+
c += 1
52+
}
53+
}
54+
}
55+
return c
56+
}
57+
58+
/**
59+
The probability of getting the given Elements.
60+
- Parameter of elements: A list of Elements.
61+
- Returns: A Double.
62+
*/
63+
public func probability<Element: Equatable>(of elements: Element...) -> Double {
64+
return probability(of: elements)
65+
}
66+
67+
/**
68+
The probability of getting the given Elements.
69+
- Parameter of elements: An Array of Elements.
70+
- Returns: A Double.
71+
*/
72+
public func probability<Element: Equatable>(of elements: [Element]) -> Double {
73+
guard 0 < count else {
74+
return 0
75+
}
76+
77+
return Double(count(of: elements)) / Double(count)
78+
}
79+
80+
/**
81+
A probability method that uses a block to determine the member state of a condition.
82+
- Parameter of elements: A list of Elements.
83+
- Returns: A Double.
84+
*/
85+
public func probability(of block: (element: Element) -> Bool) -> Double {
86+
guard 0 < count else {
87+
return 0
88+
}
89+
90+
var c: Int = 0
91+
for x in self {
92+
if block(element: x) {
93+
c += 1
94+
}
95+
}
96+
97+
return Double(c) / Double(count)
98+
}
99+
100+
/**
101+
Calculates the expected value of elements based on a given number of trials.
102+
- Parameter trials: Number of trials.
103+
- Parameter elements: A list of Elements.
104+
- Returns: A Double.
105+
*/
106+
public func expectedValue<Element: Equatable>(trials: Int, elements: Element...) -> Double {
107+
return expectedValue(trials: trials, elements: elements)
108+
}
109+
110+
/**
111+
Calculates the expected value of elements based on a given number of trials.
112+
- Parameter trials: Number of trials.
113+
- Parameter elements: An Array of Elements.
114+
- Returns: A Double.
115+
*/
116+
public func expectedValue<Element: Equatable>(trials: Int, elements: [Element]) -> Double {
117+
return Double(trials) * probability(of: elements)
118+
}
98119
}

Sources/Deque.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31-
public class Deque<Element> : CustomStringConvertible, SequenceType {
32-
public typealias Generator = AnyGenerator<Element?>
31+
public class Deque<Element>: CustomStringConvertible, Sequence {
32+
public typealias Iterator = AnyIterator<Element?>
3333

3434
/**
3535
:name: list
@@ -99,15 +99,15 @@ public class Deque<Element> : CustomStringConvertible, SequenceType {
9999
// the next value in the sequence of nodes.
100100
// :returns: Deque.Generator
101101
//
102-
public func generate() -> Deque.Generator {
103-
return list.generate()
102+
public func makeIterator() -> Deque.Iterator {
103+
return list.makeIterator()
104104
}
105105

106106
/**
107107
:name: insertAtFront
108108
:description: Insert a new element at the front of the Deque.
109109
*/
110-
public func insertAtFront(element: Element) {
110+
public func insertAtFront(_ element: Element) {
111111
list.insertAtFront(element)
112112
}
113113

@@ -125,7 +125,7 @@ public class Deque<Element> : CustomStringConvertible, SequenceType {
125125
:name: insertAtBack
126126
:description: Insert a new element at the back of the Deque.
127127
*/
128-
public func insertAtBack(element: Element) {
128+
public func insertAtBack(_ element: Element) {
129129
list.insertAtBack(element)
130130
}
131131

0 commit comments

Comments
 (0)