FTree.m 4.8 KB

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