FPruneForest.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "FPruneForest.h"
  17. #import "FImmutableTree.h"
  18. @interface FPruneForest ()
  19. @property (nonatomic, strong) FImmutableTree *pruneForest;
  20. @end
  21. @implementation FPruneForest
  22. static BOOL (^kFPrunePredicate)(id) = ^BOOL(NSNumber *pruneValue) {
  23. return [pruneValue boolValue];
  24. };
  25. static BOOL (^kFKeepPredicate)(id) = ^BOOL(NSNumber *pruneValue) {
  26. return ![pruneValue boolValue];
  27. };
  28. + (FImmutableTree *)pruneTree {
  29. static dispatch_once_t onceToken;
  30. static FImmutableTree *pruneTree;
  31. dispatch_once(&onceToken, ^{
  32. pruneTree = [[FImmutableTree alloc] initWithValue:@YES];
  33. });
  34. return pruneTree;
  35. }
  36. + (FImmutableTree *)keepTree {
  37. static dispatch_once_t onceToken;
  38. static FImmutableTree *keepTree;
  39. dispatch_once(&onceToken, ^{
  40. keepTree = [[FImmutableTree alloc] initWithValue:@NO];
  41. });
  42. return keepTree;
  43. }
  44. - (id) initWithForest:(FImmutableTree *)tree {
  45. self = [super init];
  46. if (self != nil) {
  47. self->_pruneForest = tree;
  48. }
  49. return self;
  50. }
  51. + (FPruneForest *)empty {
  52. static dispatch_once_t onceToken;
  53. static FPruneForest *forest;
  54. dispatch_once(&onceToken, ^{
  55. forest = [[FPruneForest alloc] initWithForest:[FImmutableTree empty]];
  56. });
  57. return forest;
  58. }
  59. - (BOOL)prunesAnything {
  60. return [self.pruneForest containsValueMatching:kFPrunePredicate];
  61. }
  62. - (BOOL)shouldPruneUnkeptDescendantsAtPath:(FPath *)path {
  63. NSNumber *shouldPrune = [self.pruneForest leafMostValueOnPath:path];
  64. return shouldPrune != nil && [shouldPrune boolValue];
  65. }
  66. - (BOOL)shouldKeepPath:(FPath *)path {
  67. NSNumber *shouldPrune = [self.pruneForest leafMostValueOnPath:path];
  68. return shouldPrune != nil && ![shouldPrune boolValue];
  69. }
  70. - (BOOL)affectsPath:(FPath *)path {
  71. return [self.pruneForest rootMostValueOnPath:path] != nil || ![[self.pruneForest subtreeAtPath:path] isEmpty];
  72. }
  73. - (FPruneForest *)child:(NSString *)childKey {
  74. FImmutableTree *childPruneForest = [self.pruneForest.children get:childKey];
  75. if (childPruneForest == nil) {
  76. if (self.pruneForest.value != nil) {
  77. childPruneForest = [self.pruneForest.value boolValue] ? [FPruneForest pruneTree] : [FPruneForest keepTree];
  78. } else {
  79. childPruneForest = [FImmutableTree empty];
  80. }
  81. } else {
  82. if (childPruneForest.value == nil && self.pruneForest.value != nil) {
  83. childPruneForest = [childPruneForest setValue:self.pruneForest.value atPath:[FPath empty]];
  84. }
  85. }
  86. return [[FPruneForest alloc] initWithForest:childPruneForest];
  87. }
  88. - (FPruneForest *)childAtPath:(FPath *)path {
  89. if (path.isEmpty) {
  90. return self;
  91. } else {
  92. return [[self child:path.getFront] childAtPath:[path popFront]];
  93. }
  94. }
  95. - (FPruneForest *)prunePath:(FPath *)path {
  96. if ([self.pruneForest rootMostValueOnPath:path matching:kFKeepPredicate]) {
  97. [NSException raise:NSInvalidArgumentException format:@"Can't prune path that was kept previously!"];
  98. }
  99. if ([self.pruneForest rootMostValueOnPath:path matching:kFPrunePredicate]) {
  100. // This path will already be pruned
  101. return self;
  102. } else {
  103. FImmutableTree *newPruneForest = [self.pruneForest setTree:[FPruneForest pruneTree] atPath:path];
  104. return [[FPruneForest alloc] initWithForest:newPruneForest];
  105. }
  106. }
  107. - (FPruneForest *)keepPath:(FPath *)path {
  108. if ([self.pruneForest rootMostValueOnPath:path matching:kFKeepPredicate]) {
  109. // This path will already be kept
  110. return self;
  111. } else {
  112. FImmutableTree *newPruneForest = [self.pruneForest setTree:[FPruneForest keepTree] atPath:path];
  113. return [[FPruneForest alloc] initWithForest:newPruneForest];
  114. }
  115. }
  116. - (FPruneForest *)keepAll:(NSSet *)children atPath:(FPath *)path {
  117. if ([self.pruneForest rootMostValueOnPath:path matching:kFKeepPredicate]) {
  118. // This path will already be kept
  119. return self;
  120. } else {
  121. return [self setPruneValue:[FPruneForest keepTree] forAll:children atPath:path];
  122. }
  123. }
  124. - (FPruneForest *)pruneAll:(NSSet *)children atPath:(FPath *)path {
  125. if ([self.pruneForest rootMostValueOnPath:path matching:kFKeepPredicate]) {
  126. [NSException raise:NSInvalidArgumentException format:@"Can't prune path that was kept previously!"];
  127. }
  128. if ([self.pruneForest rootMostValueOnPath:path matching:kFPrunePredicate]) {
  129. // This path will already be pruned
  130. return self;
  131. } else {
  132. return [self setPruneValue:[FPruneForest pruneTree] forAll:children atPath:path];
  133. }
  134. }
  135. - (FPruneForest *)setPruneValue:(FImmutableTree *)pruneValue forAll:(NSSet *)children atPath:(FPath *)path {
  136. FImmutableTree *subtree = [self.pruneForest subtreeAtPath:path];
  137. __block FImmutableSortedDictionary *childrenDictionary = subtree.children;
  138. [children enumerateObjectsUsingBlock:^(NSString *childKey, BOOL *stop) {
  139. childrenDictionary = [childrenDictionary insertKey:childKey withValue:pruneValue];
  140. }];
  141. FImmutableTree *newSubtree = [[FImmutableTree alloc] initWithValue:subtree.value children:childrenDictionary];
  142. return [[FPruneForest alloc] initWithForest:[self.pruneForest setTree:newSubtree atPath:path]];
  143. }
  144. - (void)enumarateKeptNodesUsingBlock:(void (^)(FPath *))block {
  145. [self.pruneForest forEach:^(FPath *path, id value) {
  146. if (value != nil && ![value boolValue]) {
  147. block(path);
  148. }
  149. }];
  150. }
  151. @end