Files
firebase-ios-sdk/FirebaseDatabase/Sources/Core/Utilities/FTree.m
T

193 lines
5.2 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2020-06-22 07:58:52 -07:00
#import "FirebaseDatabase/Sources/Core/Utilities/FTree.h"
#import "FirebaseDatabase/Sources/Core/Utilities/FPath.h"
#import "FirebaseDatabase/Sources/Core/Utilities/FTreeNode.h"
#import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
2017-05-15 12:27:07 -07:00
@implementation FTree
@synthesize name;
@synthesize parent;
@synthesize node;
2019-08-05 13:27:47 -07:00
- (id)init {
2017-05-15 12:27:07 -07:00
self = [super init];
if (self) {
self.name = @"";
self.parent = nil;
self.node = [[FTreeNode alloc] init];
}
return self;
}
2019-08-05 13:27:47 -07:00
- (id)initWithName:(NSString *)aName
withParent:(FTree *)aParent
withNode:(FTreeNode *)aNode {
2017-05-15 12:27:07 -07:00
self = [super init];
if (self) {
self.name = aName != nil ? aName : @"";
self.parent = aParent != nil ? aParent : nil;
self.node = aNode != nil ? aNode : [[FTreeNode alloc] init];
}
return self;
}
2019-08-05 13:27:47 -07:00
- (FTree *)subTree:(FPath *)path {
FTree *child = self;
NSString *next = [path getFront];
while (next != nil) {
FTreeNode *childNode = child.node.children[next];
2017-05-15 12:27:07 -07:00
if (childNode == nil) {
childNode = [[FTreeNode alloc] init];
}
2019-08-05 13:27:47 -07:00
child = [[FTree alloc] initWithName:next
withParent:child
withNode:childNode];
2017-05-15 12:27:07 -07:00
path = [path popFront];
next = [path getFront];
}
return child;
}
- (id)getValue {
return self.node.value;
}
- (void)setValue:(id)value {
self.node.value = value;
[self updateParents];
}
2019-08-05 13:27:47 -07:00
- (void)clear {
2017-05-15 12:27:07 -07:00
self.node.value = nil;
[self.node.children removeAllObjects];
self.node.childCount = 0;
[self updateParents];
}
2019-08-05 13:27:47 -07:00
- (BOOL)hasChildren {
2017-05-15 12:27:07 -07:00
return self.node.childCount > 0;
}
2019-08-05 13:27:47 -07:00
- (BOOL)isEmpty {
2017-05-15 12:27:07 -07:00
return [self getValue] == nil && ![self hasChildren];
}
2019-08-05 13:27:47 -07:00
- (void)forEachChild:(void (^)(FTree *))action {
for (NSString *key in self.node.children) {
action([[FTree alloc]
initWithName:key
withParent:self
withNode:[self.node.children objectForKey:key]]);
2017-05-15 12:27:07 -07:00
}
}
2019-08-05 13:27:47 -07:00
- (void)forEachChildMutationSafe:(void (^)(FTree *))action {
for (NSString *key in [self.node.children copy]) {
action([[FTree alloc]
initWithName:key
withParent:self
withNode:[self.node.children objectForKey:key]]);
2017-05-15 12:27:07 -07:00
}
}
2019-08-05 13:27:47 -07:00
- (void)forEachDescendant:(void (^)(FTree *))action {
2017-05-15 12:27:07 -07:00
[self forEachDescendant:action includeSelf:NO childrenFirst:NO];
}
2019-08-05 13:27:47 -07:00
- (void)forEachDescendant:(void (^)(FTree *))action
includeSelf:(BOOL)incSelf
childrenFirst:(BOOL)childFirst {
if (incSelf && !childFirst) {
2017-05-15 12:27:07 -07:00
action(self);
}
2017-06-30 10:47:37 -07:00
2019-08-05 13:27:47 -07:00
[self forEachChild:^(FTree *child) {
[child forEachDescendant:action includeSelf:YES childrenFirst:childFirst];
2017-05-15 12:27:07 -07:00
}];
2017-06-30 10:47:37 -07:00
2019-08-05 13:27:47 -07:00
if (incSelf && childFirst) {
2017-05-15 12:27:07 -07:00
action(self);
}
}
2019-08-05 13:27:47 -07:00
- (BOOL)forEachAncestor:(BOOL (^)(FTree *))action {
2017-05-15 12:27:07 -07:00
return [self forEachAncestor:action includeSelf:NO];
}
2019-08-05 13:27:47 -07:00
- (BOOL)forEachAncestor:(BOOL (^)(FTree *))action includeSelf:(BOOL)incSelf {
FTree *aNode = (incSelf) ? self : self.parent;
while (aNode != nil) {
if (action(aNode)) {
2017-05-15 12:27:07 -07:00
return YES;
}
aNode = aNode.parent;
}
return NO;
}
2019-08-05 13:27:47 -07:00
- (void)forEachImmediateDescendantWithValue:(void (^)(FTree *))action {
[self forEachChild:^(FTree *child) {
if ([child getValue] != nil) {
action(child);
} else {
[child forEachImmediateDescendantWithValue:action];
}
2017-05-15 12:27:07 -07:00
}];
}
2019-08-05 13:27:47 -07:00
- (BOOL)valueExistsAtOrAbove:(FPath *)path {
FTreeNode *aNode = self.node;
while (aNode != nil) {
if (aNode.value != nil) {
2017-05-15 12:27:07 -07:00
return YES;
}
aNode = [aNode.children objectForKey:path.getFront];
path = [path popFront];
}
// XXX Check with Michael if this is correct; deviates from JS.
return NO;
}
- (FPath *)path {
2019-08-05 13:27:47 -07:00
return [[FPath alloc]
initWith:(self.parent == nil)
? self.name
: [NSString stringWithFormat:@"%@/%@", [self.parent path],
self.name]];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)updateParents {
2017-05-15 12:27:07 -07:00
[self.parent updateChild:self.name withNode:self];
}
2019-08-05 13:27:47 -07:00
- (void)updateChild:(NSString *)childName withNode:(FTree *)child {
2017-05-15 12:27:07 -07:00
BOOL childEmpty = [child isEmpty];
BOOL childExists = self.node.children[childName] != nil;
2019-08-05 13:27:47 -07:00
if (childEmpty && childExists) {
2017-05-15 12:27:07 -07:00
[self.node.children removeObjectForKey:childName];
self.node.childCount = self.node.childCount - 1;
[self updateParents];
2019-08-05 13:27:47 -07:00
} else if (!childEmpty && !childExists) {
2017-05-15 12:27:07 -07:00
[self.node.children setObject:child.node forKey:childName];
self.node.childCount = self.node.childCount + 1;
[self updateParents];
}
}
@end