Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aeafa95

Browse filesBrowse files
authored
Merge pull request #918 from gentlesystems/add-prefix-search-to-trie
Modified Trie to add prefix support to boolean contains(), plus unit …
2 parents 6de22fc + 901aaea commit aeafa95
Copy full SHA for aeafa95

File tree

Expand file treeCollapse file tree

2 files changed

+32
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+32
-4
lines changed
Open diff view settings
Collapse file

‎Trie/Trie/Trie/Trie.swift‎

Copy file name to clipboardExpand all lines: Trie/Trie/Trie/Trie.swift
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,12 @@ extension Trie {
117117

118118
/// Determines whether a word is in the trie.
119119
///
120-
/// - Parameter word: the word to check for
120+
/// - Parameters:
121+
/// - word: the word to check for
122+
/// - matchPrefix: whether the search word should match
123+
/// if it is only a prefix of other nodes in the trie
121124
/// - Returns: true if the word is present, false otherwise.
122-
func contains(word: String) -> Bool {
125+
func contains(word: String, matchPrefix: Bool = false) -> Bool {
123126
guard !word.isEmpty else {
124127
return false
125128
}
@@ -130,7 +133,7 @@ extension Trie {
130133
}
131134
currentNode = childNode
132135
}
133-
return currentNode.isTerminating
136+
return matchPrefix || currentNode.isTerminating
134137
}
135138

136139
/// Attempts to walk to the last node of a word. The
Collapse file

‎Trie/Trie/TrieTests/TrieTests.swift‎

Copy file name to clipboardExpand all lines: Trie/Trie/TrieTests/TrieTests.swift
+26-1Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ class TrieTests: XCTestCase {
167167
let trieCopy = NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? Trie
168168
XCTAssertEqual(trieCopy?.count, trie.count)
169169
}
170-
170+
171+
/// Tests whether word prefixes are properly found and returned.
171172
func testFindWordsWithPrefix() {
172173
let trie = Trie()
173174
trie.insert(word: "test")
@@ -190,4 +191,28 @@ class TrieTests: XCTestCase {
190191
let wordsUpperCase = trie.findWordsWithPrefix(prefix: "Te")
191192
XCTAssertEqual(wordsUpperCase.sorted(), ["team", "test"])
192193
}
194+
195+
/// Tests whether word prefixes are properly detected on a boolean contains() check.
196+
func testContainsWordMatchPrefix() {
197+
let trie = Trie()
198+
trie.insert(word: "test")
199+
trie.insert(word: "another")
200+
trie.insert(word: "exam")
201+
let wordsAll = trie.contains(word: "", matchPrefix: true)
202+
XCTAssertEqual(wordsAll, true)
203+
let words = trie.contains(word: "ex", matchPrefix: true)
204+
XCTAssertEqual(words, true)
205+
trie.insert(word: "examination")
206+
let words2 = trie.contains(word: "exam", matchPrefix: true)
207+
XCTAssertEqual(words2, true)
208+
let noWords = trie.contains(word: "tee", matchPrefix: true)
209+
XCTAssertEqual(noWords, false)
210+
let unicodeWord = "😬😎"
211+
trie.insert(word: unicodeWord)
212+
let wordsUnicode = trie.contains(word: "😬", matchPrefix: true)
213+
XCTAssertEqual(wordsUnicode, true)
214+
trie.insert(word: "Team")
215+
let wordsUpperCase = trie.contains(word: "Te", matchPrefix: true)
216+
XCTAssertEqual(wordsUpperCase, true)
217+
}
193218
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.