Skip to content

Commit 5d262b9

Browse files
committed
Rename UnionFindDS to UnionFind and make it struct
1 parent 743b2f6 commit 5d262b9

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed
File renamed without changes.

UnionFindDS/UnioinFindDS.playground/Contents.swift renamed to UnionFind/UnioinFind.playground/Contents.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
//: Playground - noun: a place where people can play
22

3-
public class UnionFindDS<T: Hashable> {
3+
public struct UnionFind<T: Hashable> {
44

55
private var index = [T:Int]()
66
private var parent = [Int]()
77
private var size = [Int]()
88

99

10-
public func addSetWithElement(element: T) {
10+
public mutating func addSetWithElement(element: T) {
1111
index[element] = parent.count
1212
parent.append(parent.count)
1313
size.append(1)
1414
}
1515

16-
private func findSetByIndexOfElement(index: Int) -> Int {
16+
private mutating func findSetByIndexOfElement(index: Int) -> Int {
1717
if parent[index] == index {
1818
return index
1919
} else {
@@ -22,12 +22,12 @@ public class UnionFindDS<T: Hashable> {
2222
}
2323
}
2424

25-
public func findSetOfElement(element: T) -> Int {
25+
public mutating func findSetOfElement(element: T) -> Int {
2626
let indexOfElement = index[element]!
2727
return findSetByIndexOfElement(indexOfElement)
2828
}
2929

30-
public func unionSetsWithElement(firstElement: T, andSecondElement secondElement: T) {
30+
public mutating func unionSetsWithElement(firstElement: T, andSecondElement secondElement: T) {
3131
let firstSet = findSetOfElement(firstElement)
3232
let secondSet = findSetOfElement(secondElement)
3333
if (firstSet != secondSet) {
@@ -43,7 +43,7 @@ public class UnionFindDS<T: Hashable> {
4343
}
4444

4545

46-
let dsu = UnionFindDS<Int>()
46+
var dsu = UnionFind<Int>()
4747

4848
for i in 1...10 {
4949
dsu.addSetWithElement(i)
@@ -74,7 +74,7 @@ print(dsu.findSetOfElement(8) == dsu.findSetOfElement(9))
7474
print(dsu.findSetOfElement(4) == dsu.findSetOfElement(3))
7575

7676

77-
let dsuForStrings = UnionFindDS<String>()
77+
var dsuForStrings = UnionFind<String>()
7878
let words = ["all", "border", "boy", "afternoon", "amazing", "awesome", "best"]
7979

8080
dsuForStrings.addSetWithElement("a")

UnionFindDS/UnionFindDS.swift renamed to UnionFind/UnionFind.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
*/
99

1010

11-
public class UnionFindDS<T: Hashable> {
11+
public struct UnionFind<T: Hashable> {
1212

1313
private var index = [T:Int]()
1414
private var parent = [Int]()
1515
private var size = [Int]()
1616

1717

18-
public func addSetWithElement(element: T) {
18+
public mutating func addSetWithElement(element: T) {
1919
index[element] = parent.count
2020
parent.append(parent.count)
2121
size.append(1)
2222
}
2323

24-
private func findSetByIndexOfElement(index: Int) -> Int {
24+
private mutating func findSetByIndexOfElement(index: Int) -> Int {
2525
if parent[index] == index {
2626
return index
2727
} else {
@@ -30,12 +30,12 @@ public class UnionFindDS<T: Hashable> {
3030
}
3131
}
3232

33-
public func findSetOfElement(element: T) -> Int {
33+
public mutating func findSetOfElement(element: T) -> Int {
3434
let indexOfElement = index[element]!
3535
return findSetByIndexOfElement(indexOfElement)
3636
}
3737

38-
public func unionSetsWithElement(firstElement: T, andSecondElement secondElement: T) {
38+
public mutating func unionSetsWithElement(firstElement: T, andSecondElement secondElement: T) {
3939
let firstSet = findSetOfElement(firstElement)
4040
let secondSet = findSetOfElement(secondElement)
4141
if (firstSet != secondSet) {

0 commit comments

Comments
 (0)