Skip to content

Commit f104e7a

Browse files
author
Daniel Dahan
committed
updated for Swift 4 and version 3.0.0
1 parent c394d70 commit f104e7a

File tree

9 files changed

+178
-67
lines changed

9 files changed

+178
-67
lines changed

Algorithm.xcodeproj/project.pbxproj

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@
584584
ONLY_ACTIVE_ARCH = YES;
585585
SDKROOT = iphoneos;
586586
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
587+
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
587588
SWIFT_VERSION = 3.0;
588589
TARGETED_DEVICE_FAMILY = "1,2";
589590
VERSIONING_SYSTEM = "apple-generic";
@@ -629,6 +630,7 @@
629630
MTL_ENABLE_DEBUG_INFO = NO;
630631
SDKROOT = iphoneos;
631632
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
633+
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
632634
SWIFT_VERSION = 3.0;
633635
TARGETED_DEVICE_FAMILY = "1,2";
634636
VALIDATE_PRODUCT = YES;
@@ -653,7 +655,8 @@
653655
PRODUCT_NAME = Algorithm;
654656
SKIP_INSTALL = YES;
655657
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
656-
SWIFT_VERSION = 3.0;
658+
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
659+
SWIFT_VERSION = 4.0;
657660
};
658661
name = Debug;
659662
};
@@ -672,7 +675,8 @@
672675
PRODUCT_BUNDLE_IDENTIFIER = com.cosmicmind.Algorithm;
673676
PRODUCT_NAME = Algorithm;
674677
SKIP_INSTALL = YES;
675-
SWIFT_VERSION = 3.0;
678+
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
679+
SWIFT_VERSION = 4.0;
676680
};
677681
name = Release;
678682
};
@@ -685,7 +689,7 @@
685689
PRODUCT_BUNDLE_IDENTIFIER = io.cosmicmind.AlgorithmTests;
686690
PRODUCT_NAME = "$(TARGET_NAME)";
687691
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
688-
SWIFT_VERSION = 3.0;
692+
SWIFT_VERSION = 4.0;
689693
};
690694
name = Debug;
691695
};
@@ -697,7 +701,7 @@
697701
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
698702
PRODUCT_BUNDLE_IDENTIFIER = io.cosmicmind.AlgorithmTests;
699703
PRODUCT_NAME = "$(TARGET_NAME)";
700-
SWIFT_VERSION = 3.0;
704+
SWIFT_VERSION = 4.0;
701705
};
702706
name = Release;
703707
};
@@ -720,6 +724,8 @@
720724
SDKROOT = macosx;
721725
SKIP_INSTALL = YES;
722726
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
727+
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
728+
SWIFT_VERSION = 4.0;
723729
};
724730
name = Debug;
725731
};
@@ -741,6 +747,8 @@
741747
PRODUCT_NAME = Algorithm;
742748
SDKROOT = macosx;
743749
SKIP_INSTALL = YES;
750+
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
751+
SWIFT_VERSION = 4.0;
744752
};
745753
name = Release;
746754
};
@@ -757,6 +765,7 @@
757765
PRODUCT_NAME = "$(TARGET_NAME)";
758766
SDKROOT = macosx;
759767
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
768+
SWIFT_VERSION = 4.0;
760769
};
761770
name = Debug;
762771
};
@@ -772,6 +781,7 @@
772781
PRODUCT_BUNDLE_IDENTIFIER = "io.cosmicmind.Algorithm-OSXTests";
773782
PRODUCT_NAME = "$(TARGET_NAME)";
774783
SDKROOT = macosx;
784+
SWIFT_VERSION = 4.0;
775785
};
776786
name = Release;
777787
};

Sources/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>2.0.3</string>
18+
<string>3.0.0</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

Sources/Probable.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,49 @@
2929
*/
3030

3131
internal protocol Probable {
32-
associatedtype Element: Equatable
32+
associatedtype ProbableElement: Equatable
3333

3434
/**
3535
The instance count of elements.
36-
- Parameter of elements: A list of Elements
36+
- Parameter of elements: A list of ProbableElements
3737
- Returns: An Int.
3838
*/
39-
func count(of elements: Element...) -> Int
39+
func count(of elements: ProbableElement...) -> Int
4040

4141
/**
4242
The instance count of elements.
43-
- Parameter of elements: An Array of Elements.
43+
- Parameter of elements: An Array of ProbableElements.
4444
- Returns: An Int.
4545
*/
46-
func count(of elements: [Element]) -> Int
46+
func count(of elements: [ProbableElement]) -> Int
4747

4848
/**
4949
The probability of given elements.
50-
- Parameter of elements: A list of Elements.
50+
- Parameter of elements: A list of ProbableElements.
5151
- Returns: A Double.
5252
*/
53-
func probability(of elements: Element...) -> Double
53+
func probability(of elements: ProbableElement...) -> Double
5454

5555
/**
5656
The probability of given elements.
57-
- Parameter of elements: An Array of Elements.
57+
- Parameter of elements: An Array of ProbableElements.
5858
- Returns: A Double.
5959
*/
60-
func probability(of elements: [Element]) -> Double
60+
func probability(of elements: [ProbableElement]) -> Double
6161

6262
/**
6363
The expected value of given elements based on a number of trials.
6464
- Parameter trials: An Int.
65-
- Parameter for elements: A list of Elements.
65+
- Parameter for elements: A list of ProbableElements.
6666
- Returns: A Double.
6767
*/
68-
func expectedValue(trials: Int, for elements: Element...) -> Double
68+
func expectedValue(trials: Int, for elements: ProbableElement...) -> Double
6969

7070
/**
7171
The expected value of given elements based on a number of trials.
7272
- Parameter trials: An Int.
73-
- Parameter for elements: An Array of Elements.
73+
- Parameter for elements: An Array of ProbableElements.
7474
- Returns: A Double.
7575
*/
76-
func expectedValue(trials: Int, for elements: [Element]) -> Double
76+
func expectedValue(trials: Int, for elements: [ProbableElement]) -> Double
7777
}

Sources/RedBlackTree.swift

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
*/
3030

3131
public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, BidirectionalCollection, CustomStringConvertible {
32+
public typealias Element = (key: Key, value: Value?)
33+
public typealias ProbableElement = Key
34+
3235
/// Returns the position immediately after the given index.
3336
///
3437
/// - Parameter i: A valid index of the collection. `i` must be less than
@@ -42,7 +45,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
4245
return i - 1
4346
}
4447

45-
public typealias Iterator = AnyIterator<(key: Key, value: Value?)>
48+
public typealias Iterator = AnyIterator<Element>
4649

4750
/**
4851
Total number of elements within the RedBlackTree
@@ -99,6 +102,46 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
99102
public var endIndex: Int {
100103
return count
101104
}
105+
106+
/**
107+
:name: first
108+
:description: Get the first node value in the tree, this is
109+
the first node based on the order of keys where
110+
k1 <= k2 <= K3 ... <= Kn
111+
- returns: Element?
112+
*/
113+
public var first: Element? {
114+
guard 0 < count else {
115+
return nil
116+
}
117+
118+
return self[0]
119+
}
120+
121+
/**
122+
:name: last
123+
:description: Get the last node value in the tree, this is
124+
the last node based on the order of keys where
125+
k1 <= k2 <= K3 ... <= Kn
126+
- returns: Element?
127+
*/
128+
public var last: Element? {
129+
guard 0 < count else {
130+
return nil
131+
}
132+
133+
return self[count - 1]
134+
}
135+
136+
/// Retrieves an Array of the key values in order.
137+
public var keys: [Key] {
138+
return map { $0.key }
139+
}
140+
141+
/// Retrieves an Array of the values that are sorted based.
142+
public var values: [Value] {
143+
return flatMap { $0.value }
144+
}
102145

103146
/**
104147
:name: init
@@ -123,6 +166,10 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
123166
root = sentinel
124167
}
125168

169+
public func _customIndexOfEquatableElement(_ element: Key) -> Int?? {
170+
return nil
171+
}
172+
126173
//
127174
// :name: generate
128175
// :description: Conforms to the SequenceType Protocol. Returns
@@ -147,9 +194,11 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
147194
*/
148195
public func count(of keys: [Key]) -> Int {
149196
var c = 0
150-
for key in keys {
151-
internalCount(key, node: root, count: &c)
197+
198+
for k in keys {
199+
internalCount(k, node: root, count: &c)
152200
}
201+
153202
return c
154203
}
155204

@@ -176,11 +225,13 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
176225
}
177226

178227
var c = 0
179-
for (k, v) in self {
228+
229+
for (k, v) in self {
180230
if block(k, v) {
181231
c += 1
182232
}
183233
}
234+
184235
return Double(c) / Double(count)
185236
}
186237

@@ -249,6 +300,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
249300
mutating public func removeValue(for keys: [Key]) {
250301
for x in keys {
251302
var z = internalRemoveValueForKey(x)
303+
252304
while sentinel !== z {
253305
z = internalRemoveValueForKey(x)
254306
}
@@ -296,6 +348,15 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
296348
return internalFindNodeForKey(key).value
297349
}
298350

351+
/**
352+
Returns the Key value at a given position.
353+
- Parameter position: An Int.
354+
- Returns: A Key.
355+
*/
356+
public subscript(position: Int) -> Key {
357+
return self[position].key
358+
}
359+
299360
/**
300361
:name: operator [0...count - 1]
301362
:description: Allows array like access of the index.
@@ -664,13 +725,16 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
664725
*/
665726
private func internalSelect(_ x: RedBlackNode<Key, Value>, order: Int) -> RedBlackNode<Key, Value> {
666727
validateOrder(order)
728+
667729
let r = x.left.order + 1
668-
if order == r {
730+
731+
if order == r {
669732
return x
670733
} else if order < r {
671734
return internalSelect(x.left, order: order)
672735
}
673-
return internalSelect(x.right, order: order - r)
736+
737+
return internalSelect(x.right, order: order - r)
674738
}
675739

676740
/**
@@ -682,6 +746,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
682746
if key == node.key {
683747
count += 1
684748
}
749+
685750
internalCount(key, node: node.left, count: &count)
686751
internalCount(key, node: node.right, count: &count)
687752
}
@@ -696,6 +761,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
696761
if key == node.key {
697762
node.value = value
698763
}
764+
699765
internalUpdateValue(value, for: key, node: node.left)
700766
internalUpdateValue(value, for: key, node: node.right)
701767
}
@@ -709,12 +775,14 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
709775
private func internalOrder(_ node: RedBlackNode<Key, Value>) -> Int {
710776
var x = node
711777
var r: Int = x.left.order + 1
712-
while root !== x {
778+
779+
while root !== x {
713780
if x.parent.right === x {
714781
r += x.parent.left.order + 1
715782
}
716783
x = x.parent
717784
}
785+
718786
return r
719787
}
720788

@@ -727,19 +795,26 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
727795
}
728796

729797
public static func ==(lhs: RedBlackTree, rhs: RedBlackTree) -> Bool {
730-
return lhs.count == rhs.count && lhs.elementsEqual(rhs) {
731-
$0.0.key == $0.1.key
732-
}
798+
return lhs.count == rhs.count && lhs.elementsEqual(rhs, by: { a, b -> Bool in
799+
return a.key == b.key
800+
})
801+
}
802+
803+
public static func !=(lhs: RedBlackTree, rhs: RedBlackTree) -> Bool {
804+
return !(lhs == rhs)
733805
}
734806

735807
public static func +(lhs: RedBlackTree, rhs: RedBlackTree) -> RedBlackTree<Key, Value> {
736808
var t = RedBlackTree()
809+
737810
for (k, v) in lhs {
738811
t.insert(value: v, for: k)
739812
}
813+
740814
for (k, v) in rhs {
741815
t.insert(value: v, for: k)
742816
}
817+
743818
return t
744819
}
745820

@@ -751,9 +826,11 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
751826

752827
public static func -(lhs: RedBlackTree, rhs: RedBlackTree) -> RedBlackTree {
753828
var t = rhs
829+
754830
for (k, _) in rhs {
755831
t.removeValue(for: k)
756832
}
833+
757834
return t
758835
}
759836

0 commit comments

Comments
 (0)