1
1
/*
2
2
This source file is part of the Swift.org open source project
3
- Copyright (c) 2020 Apple Inc. and the Swift project authors
3
+
4
+ Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
4
5
Licensed under Apache License v2.0 with Runtime Library Exception
6
+
5
7
See http://swift.org/LICENSE.txt for license information
6
8
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
7
9
*/
@@ -14,6 +16,7 @@ struct Trie<Document: Hashable> {
14
16
private typealias Node = TrieNode < Character , Document >
15
17
16
18
private let root : Node
19
+ private let lock = Lock ( )
17
20
18
21
init ( ) {
19
22
self . root = Node ( )
@@ -23,17 +26,19 @@ struct Trie<Document: Hashable> {
23
26
func insert( word: String , foundIn document: Document ) {
24
27
guard !word. isEmpty else { return }
25
28
26
- var currentNode = self . root
27
- // Check if word already exists otherwise creates the node path
28
- for character in word. lowercased ( ) {
29
- if let child = currentNode. children [ character] {
30
- currentNode = child
31
- } else {
32
- currentNode = currentNode. add ( value: character)
29
+ self . lock. withLock {
30
+ var currentNode = self . root
31
+ // Check if word already exists otherwise creates the node path
32
+ for character in word. lowercased ( ) {
33
+ if let child = currentNode. children [ character] {
34
+ currentNode = child
35
+ } else {
36
+ currentNode = currentNode. add ( value: character)
37
+ }
33
38
}
34
- }
35
39
36
- currentNode. add ( document: document)
40
+ currentNode. add ( document: document)
41
+ }
37
42
}
38
43
39
44
/// Removes word occurrences found in the given document.
@@ -57,7 +62,9 @@ struct Trie<Document: Hashable> {
57
62
}
58
63
}
59
64
60
- removeInSubTrie ( root: self . root, document: document)
65
+ self . lock. withLock {
66
+ removeInSubTrie ( root: self . root, document: document)
67
+ }
61
68
}
62
69
63
70
/// Removes word occurrences found in matching document(s).
@@ -81,7 +88,9 @@ struct Trie<Document: Hashable> {
81
88
}
82
89
}
83
90
84
- removeInSubTrie ( root: self . root, where: predicate)
91
+ self . lock. withLock {
92
+ removeInSubTrie ( root: self . root, where: predicate)
93
+ }
85
94
}
86
95
87
96
/// Checks if the trie contains the exact word or words with matching prefix.
@@ -149,15 +158,17 @@ struct Trie<Document: Hashable> {
149
158
private func findLastNodeOf( word: String ) -> Node ? {
150
159
guard !word. isEmpty else { return nil }
151
160
152
- var currentNode = self . root
153
- // Traverse down the trie as far as we can
154
- for character in word. lowercased ( ) {
155
- guard let child = currentNode. children [ character] else {
156
- return nil
161
+ return self . lock. withLock {
162
+ var currentNode = self . root
163
+ // Traverse down the trie as far as we can
164
+ for character in word. lowercased ( ) {
165
+ guard let child = currentNode. children [ character] else {
166
+ return nil
167
+ }
168
+ currentNode = child
157
169
}
158
- currentNode = child
170
+ return currentNode
159
171
}
160
- return currentNode
161
172
}
162
173
}
163
174
0 commit comments