Skip to content

Commit ecbcfea

Browse files
committed
migrating radix-sort to swift3
1 parent f895ba1 commit ecbcfea

File tree

2 files changed

+384
-384
lines changed

2 files changed

+384
-384
lines changed

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

+19-19
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class Edge: Root {
8585
// For each child, erase it, then remove it from the children array.
8686
for _ in 0...children.count-1 {
8787
children[0].erase()
88-
children.removeAtIndex(0)
88+
children.remove(at: 0)
8989
}
9090
}
9191
}
@@ -137,7 +137,7 @@ public class RadixTree {
137137
}
138138

139139
// Inserts a string into the tree.
140-
public func insert(str: String) -> Bool {
140+
public func insert(_ str: String) -> Bool {
141141
//Account for a blank input. The empty string is already in the tree.
142142
if str == "" {
143143
return false
@@ -182,9 +182,9 @@ public class RadixTree {
182182
currEdge = e
183183
var tempIndex = searchStr.startIndex
184184
for _ in 1...shared.characters.count {
185-
tempIndex = tempIndex.successor()
185+
tempIndex = searchStr.characters.index(after: tempIndex)
186186
}
187-
searchStr = searchStr.substringFromIndex(tempIndex)
187+
searchStr = searchStr.substring(from: tempIndex)
188188
found = true
189189
break
190190
}
@@ -197,13 +197,13 @@ public class RadixTree {
197197

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

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

208208
// Create 2 new edges and update parent/children values
209209
let newEdge = Edge(e.label)
@@ -236,7 +236,7 @@ public class RadixTree {
236236
}
237237

238238
// Tells you if a string is in the tree
239-
public func find(str: String) -> Bool {
239+
public func find(_ str: String) -> Bool {
240240
// A radix tree always contains the empty string
241241
if str == "" {
242242
return true
@@ -267,9 +267,9 @@ public class RadixTree {
267267
currEdge = c
268268
var tempIndex = searchStr.startIndex
269269
for _ in 1...shared.characters.count {
270-
tempIndex = tempIndex.successor()
270+
tempIndex = searchStr.characters.index(after: tempIndex)
271271
}
272-
searchStr = searchStr.substringFromIndex(tempIndex)
272+
searchStr = searchStr.substring(from: tempIndex)
273273
found = true
274274
break
275275
}
@@ -300,12 +300,12 @@ public class RadixTree {
300300
}
301301

302302
// Removes a string from the tree
303-
public func remove(str: String) -> Bool {
303+
public func remove(_ str: String) -> Bool {
304304
// Removing the empty string removes everything in the tree
305305
if str == "" {
306306
for c in root.children {
307307
c.erase()
308-
root.children.removeAtIndex(0)
308+
root.children.remove(at: 0)
309309
}
310310
return true
311311
}
@@ -329,7 +329,7 @@ public class RadixTree {
329329
// and everything below it in the tree
330330
if currEdge.children[c].label == searchStr {
331331
currEdge.children[c].erase()
332-
currEdge.children.removeAtIndex(c)
332+
currEdge.children.remove(at: c)
333333
return true
334334
}
335335

@@ -341,9 +341,9 @@ public class RadixTree {
341341
currEdge = currEdge.children[c]
342342
var tempIndex = searchStr.startIndex
343343
for _ in 1...shared.characters.count {
344-
tempIndex = tempIndex.successor()
344+
tempIndex = searchStr.characters.index(after: tempIndex)
345345
}
346-
searchStr = searchStr.substringFromIndex(tempIndex)
346+
searchStr = searchStr.substring(from: tempIndex)
347347
found = true
348348
break
349349
}
@@ -364,15 +364,15 @@ public class RadixTree {
364364

365365
// Returns the prefix that is shared between the two input strings
366366
// i.e. sharedPrefix("court", "coral") -> "co"
367-
public func sharedPrefix(str1: String, _ str2: String) -> String {
367+
public func sharedPrefix(_ str1: String, _ str2: String) -> String {
368368
var temp = ""
369369
var c1 = str1.characters.startIndex
370370
var c2 = str2.characters.startIndex
371371
for _ in 0...min(str1.characters.count-1, str2.characters.count-1) {
372372
if str1[c1] == str2[c2] {
373373
temp.append( str1[c1] )
374-
c1 = c1.successor()
375-
c2 = c2.successor()
374+
c1 = str1.characters.index(after:c1)
375+
c2 = str2.characters.index(after:c2)
376376
} else {
377377
return temp
378378
}

0 commit comments

Comments
 (0)