1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "FRangeMerge.h"
#import "FEmptyNode.h"
@interface FRangeMerge ()
@property (nonatomic, strong) FPath *optExclusiveStart;
@property (nonatomic, strong) FPath *optInclusiveEnd;
@property (nonatomic, strong) id<FNode> updates;
@end
@implementation FRangeMerge
- (instancetype)initWithStart:(FPath *)start end:(FPath *)end updates:(id<FNode>)updates {
self = [super init];
if (self != nil) {
self->_optExclusiveStart = start;
self->_optInclusiveEnd = end;
self->_updates = updates;
}
return self;
}
- (id<FNode>)applyToNode:(id<FNode>)node {
return [self updateRangeInNode:[FPath empty] node:node updates:self.updates];
}
- (id<FNode>)updateRangeInNode:(FPath *)currentPath node:(id<FNode>)node updates:(id<FNode>)updates {
NSComparisonResult startComparison = (self.optExclusiveStart == nil) ? NSOrderedDescending : [currentPath compare:self.optExclusiveStart];
NSComparisonResult endComparison = (self.optInclusiveEnd == nil) ? NSOrderedAscending : [currentPath compare:self.optInclusiveEnd];
BOOL startInNode = self.optExclusiveStart != nil && [currentPath contains:self.optExclusiveStart];
BOOL endInNode = self.optInclusiveEnd != nil && [currentPath contains:self.optInclusiveEnd];
if (startComparison == NSOrderedDescending && endComparison == NSOrderedAscending && !endInNode) {
// child is completly contained
return updates;
} else if (startComparison == NSOrderedDescending && endInNode && [updates isLeafNode]) {
return updates;
} else if (startComparison == NSOrderedDescending && endComparison == NSOrderedSame) {
NSAssert(endInNode, @"End not in node");
NSAssert(![updates isLeafNode], @"Found leaf node update, this case should have been handled above.");
if ([node isLeafNode]) {
// Update node was not a leaf node, so we can delete it
return [FEmptyNode emptyNode];
} else {
// Unaffected by range, ignore
return node;
}
} else if (startInNode || endInNode) {
// There is a partial update we need to do, so collect all relevant children
NSMutableSet *allChildren = [NSMutableSet set];
[node enumerateChildrenUsingBlock:^(NSString *key, id<FNode> node, BOOL *stop) {
[allChildren addObject:key];
}];
[updates enumerateChildrenUsingBlock:^(NSString *key, id<FNode> node, BOOL *stop) {
[allChildren addObject:key];
}];
__block id<FNode> newNode = node;
void (^action)(id, BOOL *) = ^void(NSString *key, BOOL *stop) {
id<FNode> currentChild = [node getImmediateChild:key];
id<FNode> updatedChild = [self updateRangeInNode:[currentPath childFromString:key]
node:currentChild
updates:[updates getImmediateChild:key]];
// Only need to update if the node changed
if (updatedChild != currentChild) {
newNode = [newNode updateImmediateChild:key withNewChild:updatedChild];
}
};
[allChildren enumerateObjectsUsingBlock:action];
// Add priority last, so the node is not empty when applying
if (!updates.getPriority.isEmpty || !node.getPriority.isEmpty) {
BOOL stop = NO;
action(@".priority", &stop);
}
return newNode;
} else {
// Unaffected by this range
NSAssert(endComparison == NSOrderedDescending || startComparison <= NSOrderedSame, @"Invalid range for update");
return node;
}
}
- (NSString *)description {
return [NSString stringWithFormat:@"RangeMerge (optExclusiveStart = %@, optExclusiveEng = %@, updates = %@)",
self.optExclusiveStart, self.optInclusiveEnd, self.updates];
}
@end
|