6
6
// Copyright © 2016 Károly Lőrentey.
7
7
//
8
8
9
- public enum BTreeMatchStrategy {
9
+ /// The matching strategy to use when comparing elements from two trees with duplicate keys.
10
+ public enum BTreeMatchingStrategy {
11
+
12
+ /// Use a matching strategy appropriate for a set. I.e., match partition classes over key equality rather than individual keys.
13
+ ///
14
+ /// This strategy ignores the multiplicity of keys, and only consider whether a key is included in the two trees at all.
15
+ /// E.g., a single element from one tree will be considered a match for any positive number of elements with the same key in
16
+ /// the other tree.
10
17
case groupingMatches
18
+
19
+ /// Use a matching strategy appropriate for a multiset. I.e., try to establish a one-to-one correspondence between
20
+ /// elements from the two trees with equal keys.
21
+ ///
22
+ /// This strategy keeps track of the multiplicity of each key, and matches every element from one tree with
23
+ /// at most a single element with an equal key from the other tree. If a key has different multiplicities in
24
+ /// the two trees, duplicate elements above the lesser multiplicity will not be considered matching.
11
25
case countingMatches
12
26
}
13
27
@@ -40,7 +54,7 @@ extension BTree {
40
54
/// - Complexity:
41
55
/// - O(min(`self.count`, `other.count`)) in general.
42
56
/// - O(log(`self.count` + `other.count`)) if there are only a constant amount of interleaving element runs.
43
- public func union( _ other: BTree , by strategy: BTreeMatchStrategy ) -> BTree {
57
+ public func union( _ other: BTree , by strategy: BTreeMatchingStrategy ) -> BTree {
44
58
var m = BTreeMerger ( first: self , second: other)
45
59
switch strategy {
46
60
case . groupingMatches:
@@ -84,7 +98,7 @@ extension BTree {
84
98
/// - Complexity:
85
99
/// - O(min(`self.count`, `tree.count`)) in general.
86
100
/// - O(log(`self.count` + `tree.count`)) if there are only a constant amount of interleaving element runs.
87
- public func subtracting( _ other: BTree , by strategy: BTreeMatchStrategy ) -> BTree {
101
+ public func subtracting( _ other: BTree , by strategy: BTreeMatchingStrategy ) -> BTree {
88
102
var m = BTreeMerger ( first: self , second: other)
89
103
while !m. done {
90
104
m. copyFromFirst ( . excludingOtherKey)
@@ -127,7 +141,7 @@ extension BTree {
127
141
/// - Complexity:
128
142
/// - O(min(`self.count`, `other.count`)) in general.
129
143
/// - O(log(`self.count` + `other.count`)) if there are only a constant amount of interleaving element runs.
130
- public func symmetricDifference( _ other: BTree , by strategy: BTreeMatchStrategy ) -> BTree {
144
+ public func symmetricDifference( _ other: BTree , by strategy: BTreeMatchingStrategy ) -> BTree {
131
145
var m = BTreeMerger ( first: self , second: other)
132
146
while !m. done {
133
147
m. copyFromFirst ( . excludingOtherKey)
@@ -171,7 +185,7 @@ extension BTree {
171
185
/// - Complexity:
172
186
/// - O(min(`self.count`, `other.count`)) in general.
173
187
/// - O(log(`self.count` + `other.count`)) if there are only a constant amount of interleaving element runs.
174
- public func intersection( _ other: BTree , by strategy: BTreeMatchStrategy ) -> BTree {
188
+ public func intersection( _ other: BTree , by strategy: BTreeMatchingStrategy ) -> BTree {
175
189
var m = BTreeMerger ( first: self , second: other)
176
190
while !m. done {
177
191
m. skipFromFirst ( . excludingOtherKey)
@@ -195,7 +209,7 @@ extension BTree {
195
209
///
196
210
/// - Requires: `sortedKeys` is sorted in ascending order.
197
211
/// - Complexity: O(*n* + `self.count`), where *n* is the number of keys in `sortedKeys`.
198
- public func subtracting< S: Sequence > ( sortedKeys: S , by strategy: BTreeMatchStrategy ) -> BTree where S. Iterator. Element == Key {
212
+ public func subtracting< S: Sequence > ( sortedKeys: S , by strategy: BTreeMatchingStrategy ) -> BTree where S. Iterator. Element == Key {
199
213
if self . isEmpty { return self }
200
214
201
215
var b = BTreeBuilder < Key , Value > ( order: self . order)
@@ -237,7 +251,7 @@ extension BTree {
237
251
///
238
252
/// - Requires: `sortedKeys` is sorted in ascending order.
239
253
/// - Complexity: O(*n* + `self.count`), where *n* is the number of keys in `sortedKeys`.
240
- public func intersection< S: Sequence > ( sortedKeys: S , by strategy: BTreeMatchStrategy ) -> BTree where S. Iterator. Element == Key {
254
+ public func intersection< S: Sequence > ( sortedKeys: S , by strategy: BTreeMatchingStrategy ) -> BTree where S. Iterator. Element == Key {
241
255
if self . isEmpty { return self }
242
256
243
257
var b = BTreeBuilder < Key , Value > ( order: self . order)
0 commit comments