Skip to content

Commit aa1c3f7

Browse files
committed
RadixTree syntax updated to Swift 4
Characters removed (deprecated in Swift 4). Substring removed (deprecated in Swift 4). String slicing subscript used for substrings creation.
1 parent 841b443 commit aa1c3f7

File tree

2 files changed

+46
-46
lines changed

2 files changed

+46
-46
lines changed

Radix Tree/RadixTree.playground/Sources/RadixTree.swift

+23-23
Original file line numberDiff line numberDiff line change
@@ -181,29 +181,29 @@ public class RadixTree {
181181
else if shared == e.label {
182182
currEdge = e
183183
var tempIndex = searchStr.startIndex
184-
for _ in 1...shared.characters.count {
185-
tempIndex = searchStr.characters.index(after: tempIndex)
184+
for _ in 1...shared.count {
185+
tempIndex = searchStr.index(after: tempIndex)
186186
}
187-
searchStr = searchStr.substring(from: tempIndex)
187+
searchStr = String(searchStr[tempIndex...])
188188
found = true
189189
break
190190
}
191191

192192
// If the child's label and the search string share a partial prefix,
193193
// then both the label and the search string need to be substringed
194194
// and a new branch needs to be created
195-
else if shared.characters.count > 0 {
196-
var labelIndex = e.label.characters.startIndex
195+
else if shared.count > 0 {
196+
var labelIndex = e.label.startIndex
197197

198198
// Create index objects and move them to after the shared prefix
199-
for _ in 1...shared.characters.count {
200-
index = searchStr.characters.index(after: index)
201-
labelIndex = e.label.characters.index(after: labelIndex)
199+
for _ in 1...shared.count {
200+
index = searchStr.index(after: index)
201+
labelIndex = e.label.index(after: labelIndex)
202202
}
203203

204204
// Substring both the search string and the label from the shared prefix
205-
searchStr = searchStr.substring(from: index)
206-
e.label = e.label.substring(from: labelIndex)
205+
searchStr = String(searchStr[index...])
206+
e.label = String(e.label[labelIndex...])
207207

208208
// Create 2 new edges and update parent/children values
209209
let newEdge = Edge(e.label)
@@ -266,16 +266,16 @@ public class RadixTree {
266266
if shared == c.label {
267267
currEdge = c
268268
var tempIndex = searchStr.startIndex
269-
for _ in 1...shared.characters.count {
270-
tempIndex = searchStr.characters.index(after: tempIndex)
269+
for _ in 1...shared.count {
270+
tempIndex = searchStr.index(after: tempIndex)
271271
}
272-
searchStr = searchStr.substring(from: tempIndex)
272+
searchStr = String(searchStr[tempIndex...])
273273
found = true
274274
break
275275
}
276276

277277
// If the shared string is empty, go to the next child
278-
else if shared.characters.count == 0 {
278+
else if shared.count == 0 {
279279
continue
280280
}
281281

@@ -287,7 +287,7 @@ public class RadixTree {
287287
// If the search string and the child's label only share some characters,
288288
// the string is not in the tree, return false
289289
else if shared[shared.startIndex] == c.label[c.label.startIndex] &&
290-
shared.characters.count < c.label.characters.count {
290+
shared.count < c.label.count {
291291
return false
292292
}
293293
}
@@ -340,10 +340,10 @@ public class RadixTree {
340340
if shared == currEdge.children[c].label {
341341
currEdge = currEdge.children[c]
342342
var tempIndex = searchStr.startIndex
343-
for _ in 1...shared.characters.count {
344-
tempIndex = searchStr.characters.index(after: tempIndex)
343+
for _ in 1...shared.count {
344+
tempIndex = searchStr.index(after: tempIndex)
345345
}
346-
searchStr = searchStr.substring(from: tempIndex)
346+
searchStr = String(searchStr[tempIndex...])
347347
found = true
348348
break
349349
}
@@ -366,13 +366,13 @@ public class RadixTree {
366366
// i.e. sharedPrefix("court", "coral") -> "co"
367367
public func sharedPrefix(_ str1: String, _ str2: String) -> String {
368368
var temp = ""
369-
var c1 = str1.characters.startIndex
370-
var c2 = str2.characters.startIndex
371-
for _ in 0...min(str1.characters.count-1, str2.characters.count-1) {
369+
var c1 = str1.startIndex
370+
var c2 = str2.startIndex
371+
for _ in 0...min(str1.count-1, str2.count-1) {
372372
if str1[c1] == str2[c2] {
373373
temp.append( str1[c1] )
374-
c1 = str1.characters.index(after:c1)
375-
c2 = str2.characters.index(after:c2)
374+
c1 = str1.index(after:c1)
375+
c2 = str2.index(after:c2)
376376
} else {
377377
return temp
378378
}

Radix Tree/RadixTree.swift

+23-23
Original file line numberDiff line numberDiff line change
@@ -181,29 +181,29 @@ public class RadixTree {
181181
else if shared == e.label {
182182
currEdge = e
183183
var tempIndex = searchStr.startIndex
184-
for _ in 1...shared.characters.count {
185-
tempIndex = searchStr.characters.index(after: tempIndex)
184+
for _ in 1...shared.count {
185+
tempIndex = searchStr.index(after: tempIndex)
186186
}
187-
searchStr = searchStr.substring(from: tempIndex)
187+
searchStr = String(searchStr[tempIndex...])
188188
found = true
189189
break
190190
}
191191

192192
// If the child's label and the search string share a partial prefix,
193193
// then both the label and the search string need to be substringed
194194
// and a new branch needs to be created
195-
else if shared.characters.count > 0 {
196-
var labelIndex = e.label.characters.startIndex
195+
else if shared.count > 0 {
196+
var labelIndex = e.label.startIndex
197197

198198
// Create index objects and move them to after the shared prefix
199-
for _ in 1...shared.characters.count {
200-
index = searchStr.characters.index(after: index)
201-
labelIndex = e.label.characters.index(after: labelIndex)
199+
for _ in 1...shared.count {
200+
index = searchStr.index(after: index)
201+
labelIndex = e.label.index(after: labelIndex)
202202
}
203203

204204
// Substring both the search string and the label from the shared prefix
205-
searchStr = searchStr.substring(from: index)
206-
e.label = e.label.substring(from: labelIndex)
205+
searchStr = String(searchStr[index...])
206+
e.label = String(e.label[labelIndex...])
207207

208208
// Create 2 new edges and update parent/children values
209209
let newEdge = Edge(e.label)
@@ -266,16 +266,16 @@ public class RadixTree {
266266
if shared == c.label {
267267
currEdge = c
268268
var tempIndex = searchStr.startIndex
269-
for _ in 1...shared.characters.count {
270-
tempIndex = searchStr.characters.index(after: tempIndex)
269+
for _ in 1...shared.count {
270+
tempIndex = searchStr.index(after: tempIndex)
271271
}
272-
searchStr = searchStr.substring(from: tempIndex)
272+
searchStr = String(searchStr[tempIndex...])
273273
found = true
274274
break
275275
}
276276

277277
// If the shared string is empty, go to the next child
278-
else if shared.characters.count == 0 {
278+
else if shared.count == 0 {
279279
continue
280280
}
281281

@@ -287,7 +287,7 @@ public class RadixTree {
287287
// If the search string and the child's label only share some characters,
288288
// the string is not in the tree, return false
289289
else if shared[shared.startIndex] == c.label[c.label.startIndex] &&
290-
shared.characters.count < c.label.characters.count {
290+
shared.count < c.label.count {
291291
return false
292292
}
293293
}
@@ -340,10 +340,10 @@ public class RadixTree {
340340
if shared == currEdge.children[c].label {
341341
currEdge = currEdge.children[c]
342342
var tempIndex = searchStr.startIndex
343-
for _ in 1...shared.characters.count {
344-
tempIndex = searchStr.characters.index(after: tempIndex)
343+
for _ in 1...shared.count {
344+
tempIndex = searchStr.index(after: tempIndex)
345345
}
346-
searchStr = searchStr.substring(from: tempIndex)
346+
searchStr = String(searchStr[tempIndex...])
347347
found = true
348348
break
349349
}
@@ -366,13 +366,13 @@ public class RadixTree {
366366
// i.e. sharedPrefix("court", "coral") -> "co"
367367
public func sharedPrefix(_ str1: String, _ str2: String) -> String {
368368
var temp = ""
369-
var c1 = str1.characters.startIndex
370-
var c2 = str2.characters.startIndex
371-
for _ in 0...min(str1.characters.count-1, str2.characters.count-1) {
369+
var c1 = str1.startIndex
370+
var c2 = str2.startIndex
371+
for _ in 0...min(str1.count-1, str2.count-1) {
372372
if str1[c1] == str2[c2] {
373373
temp.append( str1[c1] )
374-
c1 = str1.characters.index(after:c1)
375-
c2 = str2.characters.index(after:c2)
374+
c1 = str1.index(after:c1)
375+
c2 = str2.index(after:c2)
376376
} else {
377377
return temp
378378
}

0 commit comments

Comments
 (0)