@@ -34,14 +34,14 @@ func ** (radix: Double, power: Int) -> Double {
34
34
}
35
35
36
36
extension Character {
37
- var asInt : Int {
37
+ var asInt : Int {
38
38
let s = String ( self ) . unicodeScalars
39
39
return Int ( s [ s. startIndex] . value)
40
40
}
41
41
}
42
42
43
43
// Find first position of pattern in the text using Rabin Karp algorithm
44
- public func search( text: String , pattern: String ) -> Int {
44
+ public func search( text: String , pattern: String ) -> Int {
45
45
// convert to array of ints
46
46
let patternArray = pattern. characters. flatMap { $0. asInt }
47
47
let textArray = text. characters. flatMap { $0. asInt }
@@ -55,7 +55,7 @@ public func search(text: String , pattern: String) -> Int {
55
55
let firstChars = Array ( textArray [ 0 ... endIdx] )
56
56
let firstHash = hash ( array: firstChars)
57
57
58
- if ( patternHash == firstHash) {
58
+ if patternHash == firstHash {
59
59
// Verify this was not a hash collison
60
60
if firstChars == patternArray {
61
61
return 0
@@ -67,7 +67,12 @@ public func search(text: String , pattern: String) -> Int {
67
67
for idx in 1 ... ( textArray. count - patternArray. count) {
68
68
endIdx = idx + ( patternArray. count - 1 )
69
69
let window = Array ( textArray [ idx... endIdx] )
70
- let windowHash = nextHash ( prevHash: prevHash, dropped: textArray [ idx - 1 ] , added: textArray [ endIdx] , patternSize: patternArray. count - 1 )
70
+ let windowHash = nextHash (
71
+ prevHash: prevHash,
72
+ dropped: textArray [ idx - 1 ] ,
73
+ added: textArray [ endIdx] ,
74
+ patternSize: patternArray. count - 1
75
+ )
71
76
72
77
if windowHash == patternHash {
73
78
if patternArray == window {
@@ -82,7 +87,7 @@ public func search(text: String , pattern: String) -> Int {
82
87
}
83
88
84
89
public func hash( array: Array < Int > ) -> Double {
85
- var total : Double = 0
90
+ var total : Double = 0
86
91
var exponent = array. count - 1
87
92
for i in array {
88
93
total += Double ( i) * ( Double ( Constants . hashMultiplier) ** exponent)
@@ -93,11 +98,17 @@ public func hash(array: Array<Int>) -> Double {
93
98
}
94
99
95
100
public func nextHash( prevHash: Double , dropped: Int , added: Int , patternSize: Int ) -> Double {
96
- let oldHash = prevHash - ( Double ( dropped) * ( Double ( Constants . hashMultiplier) ** patternSize) )
101
+ let oldHash = prevHash - ( Double ( dropped) *
102
+ ( Double ( Constants . hashMultiplier) ** patternSize) )
97
103
return Double ( Constants . hashMultiplier) * oldHash + Double( added)
98
104
}
99
105
100
106
// TESTS
101
- assert ( search ( text: " The big dog jumped over the fox " , pattern: " ump " ) == 13 , " Invalid index returned " )
102
- assert ( search ( text: " The big dog jumped over the fox " , pattern: " missed " ) == - 1 , " Invalid index returned " )
103
- assert ( search ( text: " The big dog jumped over the fox " , pattern: " T " ) == 0 , " Invalid index returned " )
107
+ assert ( search ( text: " The big dog jumped over the fox " ,
108
+ pattern: " ump " ) == 13 , " Invalid index returned " )
109
+
110
+ assert ( search ( text: " The big dog jumped over the fox " ,
111
+ pattern: " missed " ) == - 1 , " Invalid index returned " )
112
+
113
+ assert ( search ( text: " The big dog jumped over the fox " ,
114
+ pattern: " T " ) == 0 , " Invalid index returned " )
0 commit comments