FTree.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "FTree.h"
  17. #import "FPath.h"
  18. #import "FTreeNode.h"
  19. #import "FUtilities.h"
  20. @implementation FTree
  21. @synthesize name;
  22. @synthesize parent;
  23. @synthesize node;
  24. - (id)init {
  25. self = [super init];
  26. if (self) {
  27. self.name = @"";
  28. self.parent = nil;
  29. self.node = [[FTreeNode alloc] init];
  30. }
  31. return self;
  32. }
  33. - (id)initWithName:(NSString *)aName
  34. withParent:(FTree *)aParent
  35. withNode:(FTreeNode *)aNode {
  36. self = [super init];
  37. if (self) {
  38. self.name = aName != nil ? aName : @"";
  39. self.parent = aParent != nil ? aParent : nil;
  40. self.node = aNode != nil ? aNode : [[FTreeNode alloc] init];
  41. }
  42. return self;
  43. }
  44. - (FTree *)subTree:(FPath *)path {
  45. FTree *child = self;
  46. NSString *next = [path getFront];
  47. while (next != nil) {
  48. FTreeNode *childNode = child.node.children[next];
  49. if (childNode == nil) {
  50. childNode = [[FTreeNode alloc] init];
  51. }
  52. child = [[FTree alloc] initWithName:next
  53. withParent:child
  54. withNode:childNode];
  55. path = [path popFront];
  56. next = [path getFront];
  57. }
  58. return child;
  59. }
  60. - (id)getValue {
  61. return self.node.value;
  62. }
  63. - (void)setValue:(id)value {
  64. self.node.value = value;
  65. [self updateParents];
  66. }
  67. - (void)clear {
  68. self.node.value = nil;
  69. [self.node.children removeAllObjects];
  70. self.node.childCount = 0;
  71. [self updateParents];
  72. }
  73. - (BOOL)hasChildren {
  74. return self.node.childCount > 0;
  75. }
  76. - (BOOL)isEmpty {
  77. return [self getValue] == nil && ![self hasChildren];
  78. }
  79. - (void)forEachChild:(void (^)(FTree *))action {
  80. for (NSString *key in self.node.children) {
  81. action([[FTree alloc]
  82. initWithName:key
  83. withParent:self
  84. withNode:[self.node.children objectForKey:key]]);
  85. }
  86. }
  87. - (void)forEachChildMutationSafe:(void (^)(FTree *))action {
  88. for (NSString *key in [self.node.children copy]) {
  89. action([[FTree alloc]
  90. initWithName:key
  91. withParent:self
  92. withNode:[self.node.children objectForKey:key]]);
  93. }
  94. }
  95. - (void)forEachDescendant:(void (^)(FTree *))action {
  96. [self forEachDescendant:action includeSelf:NO childrenFirst:NO];
  97. }
  98. - (void)forEachDescendant:(void (^)(FTree *))action
  99. includeSelf:(BOOL)incSelf
  100. childrenFirst:(BOOL)childFirst {
  101. if (incSelf && !childFirst) {
  102. action(self);
  103. }
  104. [self forEachChild:^(FTree *child) {
  105. [child forEachDescendant:action includeSelf:YES childrenFirst:childFirst];
  106. }];
  107. if (incSelf && childFirst) {
  108. action(self);
  109. }
  110. }
  111. - (BOOL)forEachAncestor:(BOOL (^)(FTree *))action {
  112. return [self forEachAncestor:action includeSelf:NO];
  113. }
  114. - (BOOL)forEachAncestor:(BOOL (^)(FTree *))action includeSelf:(BOOL)incSelf {
  115. FTree *aNode = (incSelf) ? self : self.parent;
  116. while (aNode != nil) {
  117. if (action(aNode)) {
  118. return YES;
  119. }
  120. aNode = aNode.parent;
  121. }
  122. return NO;
  123. }
  124. - (void)forEachImmediateDescendantWithValue:(void (^)(FTree *))action {
  125. [self forEachChild:^(FTree *child) {
  126. if ([child getValue] != nil) {
  127. action(child);
  128. } else {
  129. [child forEachImmediateDescendantWithValue:action];
  130. }
  131. }];
  132. }
  133. - (BOOL)valueExistsAtOrAbove:(FPath *)path {
  134. FTreeNode *aNode = self.node;
  135. while (aNode != nil) {
  136. if (aNode.value != nil) {
  137. return YES;
  138. }
  139. aNode = [aNode.children objectForKey:path.getFront];
  140. path = [path popFront];
  141. }
  142. // XXX Check with Michael if this is correct; deviates from JS.
  143. return NO;
  144. }
  145. - (FPath *)path {
  146. return [[FPath alloc]
  147. initWith:(self.parent == nil)
  148. ? self.name
  149. : [NSString stringWithFormat:@"%@/%@", [self.parent path],
  150. self.name]];
  151. }
  152. - (void)updateParents {
  153. [self.parent updateChild:self.name withNode:self];
  154. }
  155. - (void)updateChild:(NSString *)childName withNode:(FTree *)child {
  156. BOOL childEmpty = [child isEmpty];
  157. BOOL childExists = self.node.children[childName] != nil;
  158. if (childEmpty && childExists) {
  159. [self.node.children removeObjectForKey:childName];
  160. self.node.childCount = self.node.childCount - 1;
  161. [self updateParents];
  162. } else if (!childEmpty && !childExists) {
  163. [self.node.children setObject:child.node forKey:childName];
  164. self.node.childCount = self.node.childCount + 1;
  165. [self updateParents];
  166. }
  167. }
  168. @end