Content-Length: 993564 | pFad | http://github.com/vikas100/Algorithm/commit/10c230b4f9d9d3e725efe2f340271f87dc13e00e

39 Merge pull request #5 from adamnemecek/master · vikas100/Algorithm@10c230b · GitHub
Skip to content

Commit 10c230b

Browse files
authored
Merge pull request CosmicMind#5 from adamnemecek/master
Refactoring a lot of things
2 parents db48bb2 + 09d270c commit 10c230b

6 files changed

+111
-181
lines changed

Diff for: Sources/Algorithm+Array.swift

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ extension Array where Element: Equatable {
7575
- Returns: An Int.
7676
*/
7777
public func count(of elements: [Element]) -> Int {
78+
7879
var c = 0
7980
for e in elements {
8081
for x in self where e == x {

Diff for: Sources/Algorithm+Set.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension Set: Probable {
3434
- Parameter of elements: A list of Elements.
3535
- Returns: An Int.
3636
*/
37-
public func count<Element: Equatable>(of elements: Element...) -> Int {
37+
public func count(of elements: Element...) -> Int {
3838
return count(of: elements)
3939
}
4040

@@ -43,7 +43,7 @@ extension Set: Probable {
4343
- Parameter of elements: An Array of Elements.
4444
- Returns: An Int.
4545
*/
46-
public func count<Element: Equatable>(of elements: [Element]) -> Int {
46+
public func count(of elements: [Element]) -> Int {
4747
var c = 0
4848
for e in elements {
4949
for x in self {
@@ -60,7 +60,7 @@ extension Set: Probable {
6060
- Parameter of elements: A list of Elements.
6161
- Returns: A Double.
6262
*/
63-
public func probability<Element: Equatable>(of elements: Element...) -> Double {
63+
public func probability(of elements: Element...) -> Double {
6464
return probability(of: elements)
6565
}
6666

@@ -69,7 +69,7 @@ extension Set: Probable {
6969
- Parameter of elements: An Array of Elements.
7070
- Returns: A Double.
7171
*/
72-
public func probability<Element: Equatable>(of elements: [Element]) -> Double {
72+
public func probability(of elements: [Element]) -> Double {
7373
return 0 < count ? Double(count(of: elements)) / Double(count) : 0
7474
}
7575

@@ -99,7 +99,7 @@ extension Set: Probable {
9999
- Parameter elements: A list of Elements.
100100
- Returns: A Double.
101101
*/
102-
public func expectedValue<Element: Equatable>(trials: Int, for elements: Element...) -> Double {
102+
public func expectedValue(trials: Int, for elements: Element...) -> Double {
103103
return expectedValue(trials: trials, for: elements)
104104
}
105105

@@ -109,7 +109,7 @@ extension Set: Probable {
109109
- Parameter elements: An Array of Elements.
110110
- Returns: A Double.
111111
*/
112-
public func expectedValue<Element: Equatable>(trials: Int, for elements: [Element]) -> Double {
112+
public func expectedValue(trials: Int, for elements: [Element]) -> Double {
113113
return Double(trials) * probability(of: elements)
114114
}
115115
}

Diff for: Sources/Queue.swift

+17-17
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public struct Queue<Element>: CustomStringConvertible, Sequence {
8080
:description: Constructor.
8181
*/
8282
public init() {
83-
list = DoublyLinkedList<Element>()
83+
list = DoublyLinkedList()
8484
}
8585

8686
//
@@ -89,7 +89,7 @@ public struct Queue<Element>: CustomStringConvertible, Sequence {
8989
// the next value in the sequence of nodes.
9090
// :returns: Queue.Generator
9191
//
92-
public func makeIterator() -> Queue.Iterator {
92+
public func makeIterator() -> Iterator {
9393
return list.makeIterator()
9494
}
9595

@@ -118,21 +118,21 @@ public struct Queue<Element>: CustomStringConvertible, Sequence {
118118
mutating public func removeAll() {
119119
list.removeAll()
120120
}
121-
}
122121

123-
public func +<Element>(lhs: Queue<Element>, rhs: Queue<Element>) -> Queue<Element> {
124-
var q = Queue<Element>()
125-
for x in lhs {
126-
q.enqueue(x)
127-
}
128-
for x in rhs {
129-
q.enqueue(x)
130-
}
131-
return q
132-
}
122+
public static func +(lhs: Queue, rhs: Queue) -> Queue<Element> {
123+
var q = Queue<Element>()
124+
for x in lhs {
125+
q.enqueue(x)
126+
}
127+
for x in rhs {
128+
q.enqueue(x)
129+
}
130+
return q
131+
}
133132

134-
public func +=<Element>(lhs: inout Queue<Element>, rhs: Queue<Element>) {
135-
for x in rhs {
136-
lhs.enqueue(x)
137-
}
133+
public static func +=(lhs: inout Queue, rhs: Queue) {
134+
for x in rhs {
135+
lhs.enqueue(x)
136+
}
137+
}
138138
}

Diff for: Sources/RedBlackNode.swift

+15-25
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ internal class RedBlackNode<Key: Comparable, Value>: Comparable, Equatable, Cust
3232
/**
3333
:name: parent
3434
:description: A reference to the parent node of a given node.
35-
- returns: RedBlackNode<Key, Value>!
35+
- returns: RedBlackNode!
3636
*/
37-
internal var parent: RedBlackNode<Key, Value>!
37+
internal var parent: RedBlackNode!
3838

3939
/**
4040
:name: left
4141
:description: A reference to the left child node of a given node.
42-
- returns: RedBlackNode<Key, Value>!
42+
- returns: RedBlackNode!
4343
*/
44-
internal var left: RedBlackNode<Key, Value>!
44+
internal var left: RedBlackNode!
4545

4646
/**
4747
:name: right
4848
:description: A reference to the right child node of a given node.
49-
- returns: RedBlackNode<Key, Value>!
49+
- returns: RedBlackNode!
5050
*/
51-
internal var right: RedBlackNode<Key, Value>!
51+
internal var right: RedBlackNode!
5252

5353
/**
5454
:name: isRed
@@ -102,7 +102,7 @@ internal class RedBlackNode<Key: Comparable, Value>: Comparable, Equatable, Cust
102102
:name: init
103103
:description: Constructor used for nodes that store data.
104104
*/
105-
internal init(parent: RedBlackNode<Key, Value>, sentinel: RedBlackNode<Key, Value>, key: Key, value: Value?) {
105+
internal init(parent: RedBlackNode, sentinel: RedBlackNode, key: Key, value: Value?) {
106106
self.key = key
107107
self.value = value
108108
self.parent = parent
@@ -111,24 +111,14 @@ internal class RedBlackNode<Key: Comparable, Value>: Comparable, Equatable, Cust
111111
isRed = true
112112
order = 1
113113
}
114+
115+
static func ==(lhs: RedBlackNode, rhs: RedBlackNode) -> Bool {
116+
return lhs.key == rhs.key
117+
}
118+
119+
static func <(lhs: RedBlackNode, rhs: RedBlackNode) -> Bool {
120+
return lhs.key < rhs.key
121+
}
114122
}
115123

116-
func ==<Key : Comparable, Value>(lhs: RedBlackNode<Key, Value>, rhs: RedBlackNode<Key, Value>) -> Bool {
117-
return lhs.key == rhs.key
118-
}
119-
120-
func <=<Key : Comparable, Value>(lhs: RedBlackNode<Key, Value>, rhs: RedBlackNode<Key, Value>) -> Bool {
121-
return lhs.key <= rhs.key
122-
}
123-
124-
func >=<Key : Comparable, Value>(lhs: RedBlackNode<Key, Value>, rhs: RedBlackNode<Key, Value>) -> Bool {
125-
return lhs.key >= rhs.key
126-
}
127-
128-
func ><Key : Comparable, Value>(lhs: RedBlackNode<Key, Value>, rhs: RedBlackNode<Key, Value>) -> Bool {
129-
return lhs.key > rhs.key
130-
}
131124

132-
func <<Key : Comparable, Value>(lhs: RedBlackNode<Key, Value>, rhs: RedBlackNode<Key, Value>) -> Bool {
133-
return lhs.key < rhs.key
134-
}

Diff for: Sources/RedBlackTree.swift

+40-76
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@
2828
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31-
public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, CustomStringConvertible {
31+
public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, BidirectionalCollection, CustomStringConvertible {
3232
//github.com/ Returns the position immediately after the given index.
3333
//github.com/
3434
//github.com/ - Parameter i: A valid index of the collection. `i` must be less than
3535
//github.com/ `endIndex`.
3636
//github.com/ - Returns: The index value immediately after `i`.
3737
public func index(after i: Int) -> Int {
38-
return i < endIndex ? i + 1 : 0
38+
return i + 1
39+
}
40+
41+
public func index(before i: Int) -> Int {
42+
return i - 1
3943
}
4044

4145
public typealias Iterator = AnyIterator<(key: Key, value: Value?)>
@@ -78,37 +82,6 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
7882
return "[" + map { "\($0)" }.joined(separator: ", ") + "]"
7983
}
8084

81-
/**
82-
:name: first
83-
:description: Get the first node value in the tree, this is
84-
the first node based on the order of keys where
85-
k1 <= k2 <= Key3 ... <= Keyn
86-
- returns: (key: Key, value: Value?)?
87-
*/
88-
public var first: (key: Key, value: Value?)? {
89-
return isEmpty ? nil : self[0]
90-
}
91-
92-
/**
93-
:name: last
94-
:description: Get the last node value in the tree, this is
95-
the last node based on the order of keys where
96-
k1 <= k2 <= Key3 ... <= Keyn
97-
- returns: (key: Key, value: Value?)?
98-
*/
99-
public var last: (key: Key, value: Value?)? {
100-
return isEmpty ? nil : self[count - 1]
101-
}
102-
103-
/**
104-
:name: isEmpty
105-
:description: A boolean of whether the RedBlackTree is empty.
106-
- returns: Bool
107-
*/
108-
public var isEmpty: Bool {
109-
return 0 == count
110-
}
111-
11285
/**
11386
:name: startIndex
11487
:description: Conforms to the Collection Protocol.
@@ -752,52 +725,43 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
752725
private func validateOrder(_ order: Int) {
753726
assert(order > startIndex || order <= endIndex, "[Algorithm Error: Order out of bounds.]")
754727
}
755-
}
756-
757-
public func ==<Key : Comparable, Value>(lhs: RedBlackTree<Key, Value>, rhs: RedBlackTree<Key, Value>) -> Bool {
758-
guard lhs.count == rhs.count else {
759-
return false
760-
}
761728

762-
for i in 0..<lhs.count {
763-
if lhs[i].key != rhs[i].key {
764-
return false
765-
}
766-
}
767-
return true
768-
}
769-
770-
public func !=<Key : Comparable, Value>(lhs: RedBlackTree<Key, Value>, rhs: RedBlackTree<Key, Value>) -> Bool {
771-
return !(lhs == rhs)
772-
}
773-
774-
public func +<Key : Comparable, Value>(lhs: RedBlackTree<Key, Value>, rhs: RedBlackTree<Key, Value>) -> RedBlackTree<Key, Value> {
775-
var t = RedBlackTree<Key, Value>()
776-
for (k, v) in lhs {
777-
t.insert(value: v, for: k)
729+
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+
}
778733
}
779-
for (k, v) in rhs {
780-
t.insert(value: v, for: k)
781-
}
782-
return t
783-
}
784734

785-
public func +=<Key : Comparable, Value>(lhs: inout RedBlackTree<Key, Value>, rhs: RedBlackTree<Key, Value>) {
786-
for (k, v) in rhs {
787-
lhs.insert(value: v, for: k)
788-
}
735+
public static func +(lhs: RedBlackTree, rhs: RedBlackTree) -> RedBlackTree<Key, Value> {
736+
var t = RedBlackTree()
737+
for (k, v) in lhs {
738+
t.insert(value: v, for: k)
739+
}
740+
for (k, v) in rhs {
741+
t.insert(value: v, for: k)
742+
}
743+
return t
744+
}
745+
746+
public static func +=(lhs: inout RedBlackTree, rhs: RedBlackTree) {
747+
for (k, v) in rhs {
748+
lhs.insert(value: v, for: k)
749+
}
750+
}
751+
752+
public static func -(lhs: RedBlackTree, rhs: RedBlackTree) -> RedBlackTree {
753+
var t = rhs
754+
for (k, _) in rhs {
755+
t.removeValue(for: k)
756+
}
757+
return t
758+
}
759+
760+
public static func -=(lhs: inout RedBlackTree, rhs: RedBlackTree) {
761+
for (k, _) in rhs {
762+
lhs.removeValue(for: k)
763+
}
764+
}
789765
}
790766

791-
public func -<Key : Comparable, Value>(lhs: RedBlackTree<Key, Value>, rhs: RedBlackTree<Key, Value>) -> RedBlackTree<Key, Value> {
792-
var t = rhs
793-
for (k, _) in rhs {
794-
t.removeValue(for: k)
795-
}
796-
return t
797-
}
798767

799-
public func -=<Key : Comparable, Value>(lhs: inout RedBlackTree<Key, Value>, rhs: RedBlackTree<Key, Value>) {
800-
for (k, _) in rhs {
801-
lhs.removeValue(for: k)
802-
}
803-
}

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/vikas100/Algorithm/commit/10c230b4f9d9d3e725efe2f340271f87dc13e00e

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy