summaryrefslogtreecommitdiffstats
path: root/macosx/HBTreeNode.m
blob: ac45323c86f155cf7f1606fb31cf5312fe49f01b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*  HBTreeNode.m $

 This file is part of the HandBrake source code.
 Homepage: <http://handbrake.fr/>.
 It may be used under the terms of the GNU General Public License. */

#import "HBTreeNode.h"

@implementation HBTreeNode

- (instancetype)init
{
    self = [super init];
    if (self) {
        _children = [[NSMutableArray alloc] init];
        _isLeaf = YES;
    }
    return self;
}

- (id)copyWithZone:(NSZone *)zone
{
    HBTreeNode *node = [[self class] allocWithZone:zone];
    node->_children = [[NSMutableArray alloc] init];
    node->_isLeaf = self.isLeaf;

    for (HBTreeNode *children in self.children)
    {
        [node.children addObject:[children copy]];
    }

    return node;
}

- (void)setDelegate:(id<HBTreeNodeDelegate>)delegate
{
    _delegate = delegate;
    for (HBTreeNode *node in self.children)
    {
        node.delegate = delegate;
    }
}

- (NSUInteger)countOfChildren
{
    return self.children.count;
}

- (id)objectInChildrenAtIndex:(NSUInteger)index
{
    return self.children[index];
}

- (void)insertObject:(HBTreeNode *)object inChildrenAtIndex:(NSUInteger)index
{
    [self.children insertObject:object atIndex:index];
    object.delegate = self.delegate;
    [self.delegate nodeDidChange:self];
}

- (void)removeObjectFromChildrenAtIndex:(NSUInteger)index
{
    self.children[index].delegate = nil;
    [self.children removeObjectAtIndex:index];
    [self.delegate nodeDidChange:self];
}

- (void)replaceObjectInChildrenAtIndex:(NSUInteger)index withObject:(HBTreeNode *)object
{
    [self.children replaceObjectAtIndex:index withObject:object];
    object.delegate = self.delegate;
    [self.delegate nodeDidChange:self];
}

#pragma mark - Enumeration

- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSIndexPath *idx, BOOL *stop))block
{
    BOOL stop = NO;
    NSMutableArray *queue = [[NSMutableArray alloc] init];
    NSMutableArray *indexesQueue = [[NSMutableArray alloc] init];

    [queue addObject:self];
    [indexesQueue addObject:[[NSIndexPath alloc] init]];

    HBTreeNode *node = nil;
    while ((node = [queue lastObject]) != nil)
    {
        // Get the index path of the current object
        NSIndexPath *indexPath = [indexesQueue lastObject];

        // Call the block
        block(node, indexPath, &stop);

        if (stop)
        {
            break;
        }

        [indexesQueue removeLastObject];

        for (NSInteger i = node.children.count - 1; i >= 0; i--)
        {
            [indexesQueue addObject:[indexPath indexPathByAddingIndex:i]];
        }

        [queue removeLastObject];

        for (id childNode in [node.children reverseObjectEnumerator])
        {
            [queue addObject:childNode];
        }
    }
}

- (NSIndexPath *)indexPathOfObject:(id)obj
{
    __block NSIndexPath *retValue = nil;

    // Visit the whole tree to find the index path.
    [self enumerateObjectsUsingBlock:^(id obj2, NSIndexPath *idx, BOOL *stop)
    {
         if ([obj2 isEqualTo:obj])
         {
             retValue = idx;
             *stop = YES;
         }
     }];

    return retValue;
}

- (void)removeObjectAtIndexPath:(NSIndexPath *)idx
{
    HBTreeNode *parentNode = self;

    // Find the object parent array
    // and delete it.
    NSUInteger currIdx = 0;
    NSUInteger i = 0;
    for (i = 0; i < idx.length - 1; i++)
    {
        currIdx = [idx indexAtPosition:i];

        if (parentNode.children.count > currIdx)
        {
            parentNode = parentNode.children[currIdx];
        }
    }

    currIdx = [idx indexAtPosition:i];

    if (parentNode.children.count > currIdx)
    {
        id removedNode = parentNode.children[currIdx];

        [parentNode removeObjectFromChildrenAtIndex:currIdx];

        if ([self.delegate respondsToSelector:@selector(treeDidRemoveNode:)])
        {
            [self.delegate treeDidRemoveNode:removedNode];
        }
    }
}

- (void)replaceObjectAtIndexPath:(NSIndexPath *)idx withObject:(HBTreeNode *)object
{
    HBTreeNode *parentNode = self;

    // Find the object parent array
    NSUInteger currIdx = 0;
    NSUInteger i = 0;
    for (i = 0; i < idx.length - 1; i++)
    {
        currIdx = [idx indexAtPosition:i];

        if (parentNode.children.count > currIdx)
        {
            parentNode = parentNode.children[currIdx];
        }
    }

    NSUInteger insertionIndex = [idx indexAtPosition:idx.length - 1];
    [parentNode replaceObjectInChildrenAtIndex:insertionIndex withObject:object];
}

@end