FSparseSnapshotTree.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "FSparseSnapshotTree.h"
  17. #import "FChildrenNode.h"
  18. @interface FSparseSnapshotTree () {
  19. id<FNode> value;
  20. NSMutableDictionary* children;
  21. }
  22. @end
  23. @implementation FSparseSnapshotTree
  24. - (id) init {
  25. self = [super init];
  26. if (self) {
  27. value = nil;
  28. children = nil;
  29. }
  30. return self;
  31. }
  32. - (id<FNode>) findPath:(FPath *)path {
  33. if (value != nil) {
  34. return [value getChild:path];
  35. } else if (![path isEmpty] && children != nil) {
  36. NSString* childKey = [path getFront];
  37. path = [path popFront];
  38. FSparseSnapshotTree* childTree = children[childKey];
  39. if (childTree != nil) {
  40. return [childTree findPath:path];
  41. } else {
  42. return nil;
  43. }
  44. } else {
  45. return nil;
  46. }
  47. }
  48. - (void) rememberData:(id<FNode>)data onPath:(FPath *)path {
  49. if ([path isEmpty]) {
  50. value = data;
  51. children = nil;
  52. } else if (value != nil) {
  53. value = [value updateChild:path withNewChild:data];
  54. } else {
  55. if (children == nil) {
  56. children = [[NSMutableDictionary alloc] init];
  57. }
  58. NSString* childKey = [path getFront];
  59. if (children[childKey] == nil) {
  60. children[childKey] = [[FSparseSnapshotTree alloc] init];
  61. }
  62. FSparseSnapshotTree* child = children[childKey];
  63. path = [path popFront];
  64. [child rememberData:data onPath:path];
  65. }
  66. }
  67. - (BOOL) forgetPath:(FPath *)path {
  68. if ([path isEmpty]) {
  69. value = nil;
  70. children = nil;
  71. return YES;
  72. } else {
  73. if (value != nil) {
  74. if ([value isLeafNode]) {
  75. // non-empty path at leaf. the path leads to nowhere
  76. return NO;
  77. } else {
  78. id<FNode> tmp = value;
  79. value = nil;
  80. [tmp enumerateChildrenUsingBlock:^(NSString *key, id<FNode> node, BOOL *stop) {
  81. [self rememberData:node onPath:[[FPath alloc] initWith:key]];
  82. }];
  83. // we've cleared out the value and set children. Call ourself again to hit the next case
  84. return [self forgetPath:path];
  85. }
  86. } else if (children != nil) {
  87. NSString* childKey = [path getFront];
  88. path = [path popFront];
  89. if (children[childKey] != nil) {
  90. FSparseSnapshotTree* child = children[childKey];
  91. BOOL safeToRemove = [child forgetPath:path];
  92. if (safeToRemove) {
  93. [children removeObjectForKey:childKey];
  94. }
  95. }
  96. if ([children count] == 0) {
  97. children = nil;
  98. return YES;
  99. } else {
  100. return NO;
  101. }
  102. } else {
  103. return YES;
  104. }
  105. }
  106. }
  107. - (void) forEachTreeAtPath:(FPath *)prefixPath do:(fbt_void_path_node)func {
  108. if (value != nil) {
  109. func(prefixPath, value);
  110. } else {
  111. [self forEachChild:^(NSString* key, FSparseSnapshotTree* tree) {
  112. FPath* path = [prefixPath childFromString:key];
  113. [tree forEachTreeAtPath:path do:func];
  114. }];
  115. }
  116. }
  117. - (void) forEachChild:(fbt_void_nsstring_sstree)func {
  118. if (children != nil) {
  119. for (NSString* key in children) {
  120. FSparseSnapshotTree* tree = [children objectForKey:key];
  121. func(key, tree);
  122. }
  123. }
  124. }
  125. @end