Skip to content

Commit a941e75

Browse files
committed
Rename BTreeMatchStrategy to BTreeMatchingStrategy; add docs.
1 parent 3e81002 commit a941e75

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

Sources/BTreeComparisons.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ extension BTree {
109109
/// - Complexity:
110110
/// - O(min(`self.count`, `tree.count`)) in general.
111111
/// - O(log(`self.count` + `tree.count`)) if there are only a constant amount of interleaving element runs.
112-
public func isSubset(of tree: BTree, by strategy: BTreeMatchStrategy) -> Bool {
112+
public func isSubset(of tree: BTree, by strategy: BTreeMatchingStrategy) -> Bool {
113113
return isSubset(of: tree, by: strategy, strict: false)
114114
}
115115

@@ -119,7 +119,7 @@ extension BTree {
119119
/// - Complexity:
120120
/// - O(min(`self.count`, `tree.count`)) in general.
121121
/// - O(log(`self.count` + `tree.count`)) if there are only a constant amount of interleaving element runs.
122-
public func isStrictSubset(of tree: BTree, by strategy: BTreeMatchStrategy) -> Bool {
122+
public func isStrictSubset(of tree: BTree, by strategy: BTreeMatchingStrategy) -> Bool {
123123
return isSubset(of: tree, by: strategy, strict: true)
124124
}
125125

@@ -128,7 +128,7 @@ extension BTree {
128128
/// - Complexity:
129129
/// - O(min(`self.count`, `tree.count`)) in general.
130130
/// - O(log(`self.count` + `tree.count`)) if there are only a constant amount of interleaving element runs.
131-
public func isSuperset(of tree: BTree, by strategy: BTreeMatchStrategy) -> Bool {
131+
public func isSuperset(of tree: BTree, by strategy: BTreeMatchingStrategy) -> Bool {
132132
return tree.isSubset(of: self, by: strategy, strict: false)
133133
}
134134

@@ -138,11 +138,11 @@ extension BTree {
138138
/// - Complexity:
139139
/// - O(min(`self.count`, `tree.count`)) in general.
140140
/// - O(log(`self.count` + `tree.count`)) if there are only a constant amount of interleaving element runs.
141-
public func isStrictSuperset(of tree: BTree, by strategy: BTreeMatchStrategy) -> Bool {
141+
public func isStrictSuperset(of tree: BTree, by strategy: BTreeMatchingStrategy) -> Bool {
142142
return tree.isSubset(of: self, by: strategy, strict: true)
143143
}
144144

145-
internal func isSubset(of tree: BTree, by strategy: BTreeMatchStrategy, strict: Bool) -> Bool {
145+
internal func isSubset(of tree: BTree, by strategy: BTreeMatchingStrategy, strict: Bool) -> Bool {
146146
var a = BTreeStrongPath(startOf: self.root)
147147
var b = BTreeStrongPath(startOf: tree.root)
148148
var knownStrict = false

Sources/BTreeMerger.swift

+21-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,22 @@
66
// Copyright © 2016 Károly Lőrentey.
77
//
88

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.
1017
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.
1125
case countingMatches
1226
}
1327

@@ -40,7 +54,7 @@ extension BTree {
4054
/// - Complexity:
4155
/// - O(min(`self.count`, `other.count`)) in general.
4256
/// - 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 {
4458
var m = BTreeMerger(first: self, second: other)
4559
switch strategy {
4660
case .groupingMatches:
@@ -84,7 +98,7 @@ extension BTree {
8498
/// - Complexity:
8599
/// - O(min(`self.count`, `tree.count`)) in general.
86100
/// - 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 {
88102
var m = BTreeMerger(first: self, second: other)
89103
while !m.done {
90104
m.copyFromFirst(.excludingOtherKey)
@@ -127,7 +141,7 @@ extension BTree {
127141
/// - Complexity:
128142
/// - O(min(`self.count`, `other.count`)) in general.
129143
/// - 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 {
131145
var m = BTreeMerger(first: self, second: other)
132146
while !m.done {
133147
m.copyFromFirst(.excludingOtherKey)
@@ -171,7 +185,7 @@ extension BTree {
171185
/// - Complexity:
172186
/// - O(min(`self.count`, `other.count`)) in general.
173187
/// - 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 {
175189
var m = BTreeMerger(first: self, second: other)
176190
while !m.done {
177191
m.skipFromFirst(.excludingOtherKey)
@@ -195,7 +209,7 @@ extension BTree {
195209
///
196210
/// - Requires: `sortedKeys` is sorted in ascending order.
197211
/// - 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 {
199213
if self.isEmpty { return self }
200214

201215
var b = BTreeBuilder<Key, Value>(order: self.order)
@@ -237,7 +251,7 @@ extension BTree {
237251
///
238252
/// - Requires: `sortedKeys` is sorted in ascending order.
239253
/// - 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 {
241255
if self.isEmpty { return self }
242256

243257
var b = BTreeBuilder<Key, Value>(order: self.order)

Tests/BTreeComparisonTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class BTreeComparisonTests: XCTestCase {
169169
let e = makeTree(Array(0 ..< 50) + Array(51 ..< 100))
170170
let f = makeTree([50])
171171

172-
for strategy in [BTreeMatchStrategy.groupingMatches, .countingMatches] {
172+
for strategy in [BTreeMatchingStrategy.groupingMatches, .countingMatches] {
173173
XCTAssertTrue(a.isSubset(of: b, by: strategy))
174174
XCTAssertFalse(a.isSubset(of: c, by: strategy))
175175
XCTAssertTrue(a.isSubset(of: d, by: strategy))
@@ -216,7 +216,7 @@ class BTreeComparisonTests: XCTestCase {
216216
let e = makeTree(Array(0 ..< 50) + Array(51 ..< 100))
217217
let f = makeTree([50])
218218

219-
for strategy in [BTreeMatchStrategy.groupingMatches, .countingMatches] {
219+
for strategy in [BTreeMatchingStrategy.groupingMatches, .countingMatches] {
220220
XCTAssertFalse(a.isStrictSubset(of: b, by: strategy))
221221
XCTAssertFalse(a.isStrictSubset(of: c, by: strategy))
222222
XCTAssertTrue(a.isStrictSubset(of: d, by: strategy))
@@ -263,7 +263,7 @@ class BTreeComparisonTests: XCTestCase {
263263
let e = makeTree(Array(0 ..< 50) + Array(51 ..< 100))
264264
let f = makeTree([50])
265265

266-
for strategy in [BTreeMatchStrategy.groupingMatches, .countingMatches] {
266+
for strategy in [BTreeMatchingStrategy.groupingMatches, .countingMatches] {
267267
XCTAssertTrue(a.isSuperset(of: b, by: strategy))
268268
XCTAssertTrue(a.isSuperset(of: c, by: strategy))
269269
XCTAssertFalse(a.isSuperset(of: d, by: strategy))
@@ -310,7 +310,7 @@ class BTreeComparisonTests: XCTestCase {
310310
let e = makeTree(Array(0 ..< 50) + Array(51 ..< 100))
311311
let f = makeTree([50])
312312

313-
for strategy in [BTreeMatchStrategy.groupingMatches, .countingMatches] {
313+
for strategy in [BTreeMatchingStrategy.groupingMatches, .countingMatches] {
314314
XCTAssertFalse(b.isStrictSuperset(of: a, by: strategy))
315315
XCTAssertFalse(c.isStrictSuperset(of: a, by: strategy))
316316
XCTAssertTrue(d.isStrictSuperset(of: a, by: strategy))
@@ -356,7 +356,7 @@ class BTreeComparisonTests: XCTestCase {
356356
var z = x
357357
z.removeFirst()
358358

359-
for strategy in [BTreeMatchStrategy.groupingMatches, .countingMatches] {
359+
for strategy in [BTreeMatchingStrategy.groupingMatches, .countingMatches] {
360360
XCTAssertTrue(x.isSubset(of: x, by: strategy))
361361
XCTAssertTrue(y.isSubset(of: x, by: strategy))
362362
XCTAssertTrue(z.isSubset(of: x, by: strategy))

0 commit comments

Comments
 (0)