FRangeMerge.m 4.4 KB

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