-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.m
357 lines (288 loc) · 9.79 KB
/
Tree.m
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// -*- mode:objc -*-
// $Id: Tree.m,v 1.8 2008-10-01 03:57:40 yfabian Exp $
//
/*
** Tree.m
**
** Copyright (c) 2002-2004
**
** Author: Ujwal S. Setlur
**
** Project: iTerm
**
** Description: Implements a tree structure for bookmarks.
** Adapted from Apple's example code.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#import <iTerm/Tree.h>
#define KEY_NAME @"Name"
#define KEY_DESCRIPTION @"Description"
#define KEY_IS_GROUP @"Group Node"
#define KEY_ENTRIES @"Entries"
#define KEY_DATA @"Data"
@implementation NSArray (MyExtensions)
- (BOOL) containsObjectIdenticalTo: (id)obj {
return [self indexOfObjectIdenticalTo: obj]!=NSNotFound;
}
@end
@implementation NSMutableArray (MyExtensions)
- (void) insertObjectsFromArray:(NSArray *)array atIndex:(int)index {
NSObject *entry = nil;
NSEnumerator *enumerator = [array objectEnumerator];
while ((entry=[enumerator nextObject])) {
[self insertObject:entry atIndex:index++];
}
}
@end
@implementation TreeNode
+ (id) treeFromDictionary:(NSDictionary*)dict {
return [[[TreeNode alloc] initFromDictionary:dict] autorelease];
}
- (id)initWithData:(NSDictionary *)data parent:(TreeNode*)parent children:(NSArray*)children {
self = [super init];
if (self==nil) return nil;
nodeData = [[NSMutableDictionary alloc] initWithDictionary: data];
nodeChildren = [[NSMutableArray arrayWithArray:children] retain];
nodeParent = parent;
return self;
}
- (id) initFromDictionary:(NSDictionary*)dict {
// This is a convenience init method to return a tree root of a tree derived from an input dictionary.
// The input dictionary for this example app is InitInfo.dict. Look at that file to understand the format.
NSDictionary *data = [dict objectForKey: KEY_DATA];
NSEnumerator *entryEnum = [[dict objectForKey: KEY_ENTRIES] objectEnumerator];
id entry;
TreeNode *child = nil;
self = [self initWithData:data parent:nil children:[NSArray array]];
if (self==nil) return nil;
if([dict objectForKey: KEY_IS_GROUP])
[self setIsLeaf: NO];
else
[self setIsLeaf: YES];
while ((entry=[entryEnum nextObject]))
{
// if child is another group, recursively add the branch
if ([[entry objectForKey: KEY_IS_GROUP] isEqualToString: @"Yes"])
{
child = [TreeNode treeFromDictionary: entry];
}
else
{
data = [entry objectForKey: KEY_DATA];
child = [[[TreeNode alloc] initWithData: data parent:nil children: [NSArray array]] autorelease];
[child setIsLeaf: YES];
}
[self insertChild: child atIndex: [self numberOfChildren]];
}
return self;
}
// return a dictionary representation of the node and its children
- (NSDictionary *) dictionary
{
NSMutableDictionary *aDict;
NSEnumerator *entryEnum;
TreeNode *child;
NSMutableArray *aMutableArray;
aDict = [[NSMutableDictionary alloc] init];
if(nodeData)
[aDict setObject: nodeData forKey: KEY_DATA];
if(!isLeaf)
[aDict setObject: @"Yes" forKey: KEY_IS_GROUP];
// recursively encode the children
aMutableArray = [NSMutableArray array];
entryEnum = [nodeChildren objectEnumerator];
while ((child = [entryEnum nextObject]))
{
[aMutableArray addObject: [child dictionary]];
}
[aDict setObject: aMutableArray forKey: KEY_ENTRIES];
return ([aDict autorelease]);
}
// return an array of all nodes
- (NSArray *) array
{
NSEnumerator *entryEnum;
TreeNode *child;
NSMutableArray *aMutableArray;
// recursively encode the children
aMutableArray = [NSMutableArray array];
entryEnum = [nodeChildren objectEnumerator];
while ((child = [entryEnum nextObject]))
{
if ([child isLeaf])
[aMutableArray addObject: child];
else
[aMutableArray addObjectsFromArray: [child array]];
}
return (aMutableArray);
}
- (void)dealloc {
[nodeData release];
[nodeChildren release];
nodeData = nil;
nodeChildren = nil;
[super dealloc];
}
// ================================================================
// Methods used to manage a node and its children.
// ================================================================
- (void)setNodeData:(NSDictionary *)data {
[nodeData release];
nodeData = [[NSMutableDictionary alloc] initWithDictionary: data];
}
- (NSDictionary *)nodeData {
return nodeData;
}
- (void)setNodeParent:(TreeNode*)parent {
nodeParent = parent;
}
- (TreeNode*)nodeParent {
return nodeParent;
}
- (BOOL)isGroup
{
return (!isLeaf);
}
- (BOOL) isLeaf
{
return (isLeaf);
}
- (void) setIsLeaf: (BOOL) flag
{
isLeaf = flag;
}
- (void)insertChild:(TreeNode*)child atIndex:(int)index
{
[nodeChildren insertObject:child atIndex:index];
[child setNodeParent: self];
//[nodeChildren sortUsingSelector:@selector(compare:)];
}
- (void)insertChildren:(NSArray*)children atIndex:(int)index {
[nodeChildren insertObjectsFromArray: children atIndex: index];
[children makeObjectsPerformSelector:@selector(setNodeParent:) withObject:self];
//[nodeChildren sortUsingSelector:@selector(compare:)];
}
- (void)_removeChildrenIdenticalTo:(NSArray*)children {
TreeNode *child;
NSEnumerator *childEnumerator = [children objectEnumerator];
[children makeObjectsPerformSelector:@selector(setNodeParent:) withObject:nil];
while ((child=[childEnumerator nextObject])) {
[nodeChildren removeObjectIdenticalTo:child];
}
}
- (void)removeChild:(TreeNode*)child {
int index = [self indexOfChild: child];
if (index!=NSNotFound) {
[self _removeChildrenIdenticalTo: [NSArray arrayWithObject: [self childAtIndex:index]]];
}
}
- (void)removeFromParent {
[[self nodeParent] removeChild:self];
}
- (int)indexOfChild:(TreeNode*)child {
return [nodeChildren indexOfObject:child];
}
- (int)indexOfChildIdenticalTo:(TreeNode*)child {
return [nodeChildren indexOfObjectIdenticalTo:child];
}
- (int)numberOfChildren {
return [nodeChildren count];
}
- (NSArray*)children {
return [NSArray arrayWithArray: nodeChildren];
}
- (TreeNode*)firstChild {
return [nodeChildren objectAtIndex:0];
}
- (TreeNode*)lastChild {
return [nodeChildren lastObject];
}
- (TreeNode*)childAtIndex:(int)index {
return [nodeChildren objectAtIndex:index];
}
- (BOOL)isDescendantOfNode:(TreeNode*)node {
// returns YES if 'node' is an ancestor.
// Walk up the tree, to see if any of our ancestors is 'node'.
TreeNode *parent = self;
while(parent) {
if(parent==node) return YES;
parent = [parent nodeParent];
}
return NO;
}
- (BOOL)isDescendantOfNodeInArray:(NSArray*)nodes {
// returns YES if any 'node' in the array 'nodes' is an ancestor of ours.
// For each node in nodes, if node is an ancestor return YES. If none is an
// ancestor, return NO.
NSEnumerator *nodeEnum = [nodes objectEnumerator];
TreeNode *node = nil;
while((node=[nodeEnum nextObject])) {
if([self isDescendantOfNode:node]) return YES;
}
return NO;
}
- (void)recursiveSortChildren {
if ([self isGroup]) {
[nodeChildren sortUsingSelector:@selector(compare:)];
[nodeChildren makeObjectsPerformSelector: @selector(recursiveSortChildren)];
}
}
- (int) indexForNode: (id) node {
return ([[self array] indexOfObject:node]);
}
- (id) nodeForIndex: (int) index {
return ([[self array] objectAtIndex:index]);
}
- (NSString*)description {
// Return something that will be useful for debugging.
return [NSString stringWithFormat: @"{%@}", nodeData];
}
// Returns the minimum nodes from 'allNodes' required to cover the nodes in 'allNodes'.
// This methods returns an array containing nodes from 'allNodes' such that no node in
// the returned array has an ancestor in the returned array.
// There are better ways to compute this, but this implementation should be efficient for our app.
+ (NSArray *) minimumNodeCoverFromNodesInArray: (NSArray *)allNodes {
NSMutableArray *minimumCover = [NSMutableArray array];
NSMutableArray *nodeQueue = [NSMutableArray arrayWithArray:allNodes];
TreeNode *node = nil;
while ([nodeQueue count]) {
node = [nodeQueue objectAtIndex:0];
[nodeQueue removeObjectAtIndex:0];
while ( [node nodeParent] && [nodeQueue containsObjectIdenticalTo:[node nodeParent]] ) {
[nodeQueue removeObjectIdenticalTo: node];
node = [node nodeParent];
}
if (![node isDescendantOfNodeInArray: minimumCover]) [minimumCover addObject: node];
[nodeQueue removeObjectIdenticalTo: node];
}
return minimumCover;
}
- (id)initWithCoder:(NSCoder *)coder {
self = [[TreeNode alloc] initFromDictionary:[coder decodeObject]];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
if (self) {
[coder encodeObject: [self dictionary]];
}
}
- (NSComparisonResult) compare: (id) comparator
{
if ([(NSString *)[[self nodeData] objectForKey:KEY_NAME] isEqualToString:@"Bonjour"]) return NSOrderedDescending;
if ([(NSString *)[[comparator nodeData] objectForKey:KEY_NAME] isEqualToString:@"Bonjour"]) return NSOrderedAscending;
return [(NSString *)[[self nodeData] objectForKey:KEY_NAME] localizedCaseInsensitiveCompare:[[comparator nodeData] objectForKey:KEY_NAME]];
}
@end