Skip to content

migrating radix-tree to swift3 #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions Radix Tree/RadixTree.playground/Sources/RadixTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class Edge: Root {
// For each child, erase it, then remove it from the children array.
for _ in 0...children.count-1 {
children[0].erase()
children.removeAtIndex(0)
children.remove(at: 0)
}
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public class RadixTree {
}

// Inserts a string into the tree.
public func insert(str: String) -> Bool {
public func insert(_ str: String) -> Bool {
//Account for a blank input. The empty string is already in the tree.
if str == "" {
return false
Expand Down Expand Up @@ -182,9 +182,9 @@ public class RadixTree {
currEdge = e
var tempIndex = searchStr.startIndex
for _ in 1...shared.characters.count {
tempIndex = tempIndex.successor()
tempIndex = searchStr.characters.index(after: tempIndex)
}
searchStr = searchStr.substringFromIndex(tempIndex)
searchStr = searchStr.substring(from: tempIndex)
found = true
break
}
Expand All @@ -197,13 +197,13 @@ public class RadixTree {

// Create index objects and move them to after the shared prefix
for _ in 1...shared.characters.count {
index = index.successor()
labelIndex = labelIndex.successor()
index = searchStr.characters.index(after: index)
labelIndex = e.label.characters.index(after: labelIndex)
}

// Substring both the search string and the label from the shared prefix
searchStr = searchStr.substringFromIndex(index)
e.label = e.label.substringFromIndex(labelIndex)
searchStr = searchStr.substring(from: index)
e.label = e.label.substring(from: labelIndex)

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

// Tells you if a string is in the tree
public func find(str: String) -> Bool {
public func find(_ str: String) -> Bool {
// A radix tree always contains the empty string
if str == "" {
return true
Expand Down Expand Up @@ -267,9 +267,9 @@ public class RadixTree {
currEdge = c
var tempIndex = searchStr.startIndex
for _ in 1...shared.characters.count {
tempIndex = tempIndex.successor()
tempIndex = searchStr.characters.index(after: tempIndex)
}
searchStr = searchStr.substringFromIndex(tempIndex)
searchStr = searchStr.substring(from: tempIndex)
found = true
break
}
Expand Down Expand Up @@ -300,12 +300,12 @@ public class RadixTree {
}

// Removes a string from the tree
public func remove(str: String) -> Bool {
public func remove(_ str: String) -> Bool {
// Removing the empty string removes everything in the tree
if str == "" {
for c in root.children {
c.erase()
root.children.removeAtIndex(0)
root.children.remove(at: 0)
}
return true
}
Expand All @@ -329,7 +329,7 @@ public class RadixTree {
// and everything below it in the tree
if currEdge.children[c].label == searchStr {
currEdge.children[c].erase()
currEdge.children.removeAtIndex(c)
currEdge.children.remove(at: c)
return true
}

Expand All @@ -341,9 +341,9 @@ public class RadixTree {
currEdge = currEdge.children[c]
var tempIndex = searchStr.startIndex
for _ in 1...shared.characters.count {
tempIndex = tempIndex.successor()
tempIndex = searchStr.characters.index(after: tempIndex)
}
searchStr = searchStr.substringFromIndex(tempIndex)
searchStr = searchStr.substring(from: tempIndex)
found = true
break
}
Expand All @@ -364,15 +364,15 @@ public class RadixTree {

// Returns the prefix that is shared between the two input strings
// i.e. sharedPrefix("court", "coral") -> "co"
public func sharedPrefix(str1: String, _ str2: String) -> String {
public func sharedPrefix(_ str1: String, _ str2: String) -> String {
var temp = ""
var c1 = str1.characters.startIndex
var c2 = str2.characters.startIndex
for _ in 0...min(str1.characters.count-1, str2.characters.count-1) {
if str1[c1] == str2[c2] {
temp.append( str1[c1] )
c1 = c1.successor()
c2 = c2.successor()
c1 = str1.characters.index(after:c1)
c2 = str2.characters.index(after:c2)
} else {
return temp
}
Expand Down
Loading