FIndexedNode.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "FIndexedNode.h"
  17. #import "FImmutableSortedSet.h"
  18. #import "FIndex.h"
  19. #import "FPriorityIndex.h"
  20. #import "FKeyIndex.h"
  21. #import "FChildrenNode.h"
  22. static FImmutableSortedSet *FALLBACK_INDEX;
  23. @interface FIndexedNode ()
  24. @property (nonatomic, strong) id<FNode> node;
  25. /**
  26. * The indexed set is initialized lazily to prevent creation when it is not needed
  27. */
  28. @property (nonatomic, strong) FImmutableSortedSet *indexed;
  29. @property (nonatomic, strong) id<FIndex> index;
  30. @end
  31. @implementation FIndexedNode
  32. + (FImmutableSortedSet *)fallbackIndex {
  33. static FImmutableSortedSet *fallbackIndex;
  34. static dispatch_once_t once;
  35. dispatch_once(&once, ^{
  36. fallbackIndex = [[FImmutableSortedSet alloc] init];
  37. });
  38. return fallbackIndex;
  39. }
  40. + (FIndexedNode *)indexedNodeWithNode:(id<FNode>)node
  41. {
  42. return [[FIndexedNode alloc] initWithNode:node index:[FPriorityIndex priorityIndex]];
  43. }
  44. + (FIndexedNode *)indexedNodeWithNode:(id<FNode>)node index:(id<FIndex>)index
  45. {
  46. return [[FIndexedNode alloc] initWithNode:node index:index];
  47. }
  48. - (id)initWithNode:(id<FNode>)node index:(id<FIndex>)index
  49. {
  50. // Initialize indexed lazily
  51. return [self initWithNode:node index:index indexed:nil];
  52. }
  53. - (id)initWithNode:(id<FNode>)node index:(id<FIndex>)index indexed:(FImmutableSortedSet *)indexed
  54. {
  55. self = [super init];
  56. if (self != nil) {
  57. self->_node = node;
  58. self->_index = index;
  59. self->_indexed = indexed;
  60. }
  61. return self;
  62. }
  63. - (void)ensureIndexed
  64. {
  65. if (!self.indexed) {
  66. if ([self.index isEqual:[FKeyIndex keyIndex]]) {
  67. self.indexed = [FIndexedNode fallbackIndex];
  68. } else {
  69. __block BOOL sawChild = NO;
  70. [self.node enumerateChildrenUsingBlock:^(NSString *key, id<FNode> node, BOOL *stop) {
  71. sawChild = sawChild || [self.index isDefinedOn:node];
  72. *stop = sawChild;
  73. }];
  74. if (sawChild) {
  75. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  76. [self.node enumerateChildrenUsingBlock:^(NSString *key, id<FNode> node, BOOL *stop) {
  77. FNamedNode *namedNode = [[FNamedNode alloc] initWithName:key andNode:node];
  78. dict[namedNode] = [NSNull null];
  79. }];
  80. // Make sure to assign index here, because the comparator will be retained and using self will cause a
  81. // cycle
  82. id<FIndex> index = self.index;
  83. self.indexed = [FImmutableSortedSet setWithKeysFromDictionary:dict
  84. comparator:^NSComparisonResult(FNamedNode *namedNode1, FNamedNode *namedNode2) {
  85. return [index compareNamedNode:namedNode1 toNamedNode:namedNode2];
  86. }];
  87. } else {
  88. self.indexed = [FIndexedNode fallbackIndex];
  89. }
  90. }
  91. }
  92. }
  93. - (BOOL)hasIndex:(id<FIndex>)index
  94. {
  95. return [self.index isEqual:index];
  96. }
  97. - (FIndexedNode *)updateChild:(NSString *)key withNewChild:(id<FNode>)newChildNode
  98. {
  99. id<FNode> newNode = [self.node updateImmediateChild:key withNewChild:newChildNode];
  100. if (self.indexed == [FIndexedNode fallbackIndex] && ![self.index isDefinedOn:newChildNode]) {
  101. // doesn't affect the index, no need to create an index
  102. return [[FIndexedNode alloc] initWithNode:newNode index:self.index indexed:[FIndexedNode fallbackIndex]];
  103. } else if (!self.indexed || self.indexed == [FIndexedNode fallbackIndex]) {
  104. // No need to index yet, index lazily
  105. return [[FIndexedNode alloc] initWithNode:newNode index:self.index];
  106. } else {
  107. id<FNode> oldChild = [self.node getImmediateChild:key];
  108. FImmutableSortedSet *newIndexed = [self.indexed removeObject:[FNamedNode nodeWithName:key node:oldChild]];
  109. if (![newChildNode isEmpty]) {
  110. newIndexed = [newIndexed addObject:[FNamedNode nodeWithName:key node:newChildNode]];
  111. }
  112. return [[FIndexedNode alloc] initWithNode:newNode index:self.index indexed:newIndexed];
  113. }
  114. }
  115. - (FIndexedNode *)updatePriority:(id<FNode>)priority
  116. {
  117. return [[FIndexedNode alloc] initWithNode:[self.node updatePriority:priority]
  118. index:self.index
  119. indexed:self.indexed];
  120. }
  121. - (FNamedNode *)firstChild
  122. {
  123. if (![self.node isKindOfClass:[FChildrenNode class]]) {
  124. return nil;
  125. } else {
  126. [self ensureIndexed];
  127. if (self.indexed == [FIndexedNode fallbackIndex]) {
  128. return [((FChildrenNode *)self.node) firstChild];
  129. } else {
  130. return self.indexed.firstObject;
  131. }
  132. }
  133. }
  134. - (FNamedNode *)lastChild
  135. {
  136. if (![self.node isKindOfClass:[FChildrenNode class]]) {
  137. return nil;
  138. } else {
  139. [self ensureIndexed];
  140. if (self.indexed == [FIndexedNode fallbackIndex]) {
  141. return [((FChildrenNode *)self.node) lastChild];
  142. } else {
  143. return self.indexed.lastObject;
  144. }
  145. }
  146. }
  147. - (NSString *)predecessorForChildKey:(NSString *)childKey childNode:(id<FNode>)childNode index:(id<FIndex>)index
  148. {
  149. if (![self.index isEqual:index]) {
  150. [NSException raise:NSInvalidArgumentException format:@"Index not available in IndexedNode!"];
  151. }
  152. [self ensureIndexed];
  153. if (self.indexed == [FIndexedNode fallbackIndex]) {
  154. return [self.node predecessorChildKey:childKey];
  155. } else {
  156. FNamedNode *node = [self.indexed predecessorEntry:[FNamedNode nodeWithName:childKey node:childNode]];
  157. return node.name;
  158. }
  159. }
  160. - (void)enumerateChildrenReverse:(BOOL)reverse usingBlock:(void (^)(NSString *, id<FNode>, BOOL *))block
  161. {
  162. [self ensureIndexed];
  163. if (self.indexed == [FIndexedNode fallbackIndex]) {
  164. [self.node enumerateChildrenReverse:reverse usingBlock:block];
  165. } else {
  166. [self.indexed enumerateObjectsReverse:reverse usingBlock:^(FNamedNode *namedNode, BOOL *stop) {
  167. block(namedNode.name, namedNode.node, stop);
  168. }];
  169. }
  170. }
  171. - (NSEnumerator *)childEnumerator
  172. {
  173. [self ensureIndexed];
  174. if (self.indexed == [FIndexedNode fallbackIndex]) {
  175. return [self.node childEnumerator];
  176. } else {
  177. return [self.indexed objectEnumerator];
  178. }
  179. }
  180. @end