/
CWTree.h
168 lines (128 loc) · 5.54 KB
/
CWTree.h
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
/*
// CWTree.h
// Zangetsu
//
// Created by Colin Wheeler on 7/12/11.
// Copyright 2012. All rights reserved.
//
Copyright (c) 2012 Colin Wheeler
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import <Foundation/Foundation.h>
@interface CWTreeNode : NSObject
/**
Node value
*/
@property(retain) id value;
/**
Parent Object which is also a CWTreeNode
*/
@property(weak) id parent;
/**
Children Node Objects
*/
@property(readonly, retain) NSMutableArray *children;
/**
Key to set if a node allows duplicate children
*/
@property(assign) BOOL allowsDuplicates;
/**
Initializes and create a new CWTreeNode Object initialized with aValue
This is the prefeered initializer for CWTreeNode.
@param aValue an Objective-C object that the CWTreeNode will retain
@return a new CWTreeNode with aValue for the nodes data value and no children
*/
-(id)initWithValue:(id)aValue;
/**
Adds node to the receivers children
If the receiver allows duplicates it simply adds node to the receivers children
and sets itself as the nodes parent. If the receiver does not allow duplicates then
the receiver checks if the node isn't already in its children. If it is not then it
checks the node values of its children to make sure there isn't already a node with
the same value there if there isn't then it proceeds and adds the node to the receivers
chilren and sets itself as the nodes parent.
@param node a CWTreeNode object
*/
-(void)addChild:(CWTreeNode *)node;
/**
Removes the object from the receivers children
The receiver checks to make sure the node is in its children and if node is,
then it removes itself as a parent and removes the node from its chilren.
@param a CWTreeNode object
*/
-(void)removeChild:(CWTreeNode *)node;
/**
Returns a bool value if the receivers value & node pointers are all equal to node
Returns a BOOL value if nodes value is equal to the receivers and if its parent poiners
are equal as well as its children contents.
@param node a valid CWTreeNode object
@return a BOOL indicatign if the value and children/parent pointers all equal to nodes value & pointers
*/
-(BOOL)isEqualToNode:(CWTreeNode *)node;
/**
Returns a bool value indicating if nodes value is equal to the receivers
@param node a valid CWTreeNode object
@return a BOOL with yes if the node values are equal, otherwise no.
*/
-(BOOL)isNodeValueEqualTo:(CWTreeNode *)node;
/**
Returns the depth level of the node in the tree it is in
@return a NSUInteger with the depth level of the node in its graph of nodes
*/
-(NSUInteger)nodeLevel;
@end
@interface CWTree : NSObject
/**
Initializes and returns a new CWTree object with a root CWTreeNode value containing value
@param value any valid Objective-C object to initialize a CWTreeNode node with
@return a CWTree object with its rootnode pointer pointing at a newly created CWTreeNode object with value as its node value
*/
-(id)initWithRootNodeValue:(id)value;
/**
Pointer to the root node of the tree of nodes
*/
@property(retain) CWTreeNode *rootNode;
/**
Enumerates a CWTree object object on a level by level basis.
Enumerates a CWTree object by starting with the root node and then visiting
the children. It visits eash level going from left to right and then visiting
the next level until it has visited all nodes in the tree or until the BOOL
stop pointer in the block has been set to YES.
Block values passed back to you are as follows
@param nodeValue a convenience to accessing (CWTreeNode *)[node nodeValue]
@param node a pointer to the node being enumerated over
@param stop a BOOL pointer which you can set to YES to stop enumeration, otherwise it will continue until all nodes have been enumerated over
*/
-(void)enumerateTreeWithBlock:(void (^)(id nodeValue, id node, BOOL *stop))block;
/**
Returns a bool indicating if the tree object is equal to the receiver tree object
@return a BOOL if the receivers children objects are equal to tree's children objects
*/
-(BOOL)isEqualToTree:(CWTree *)tree;
/**
Returns a BOOL indicating if the object argument is contained in the receiver
@param object the object you wish to see if its contained in the receiver
@return a BOOL variable with YES if object is contained in the block or NO otherwise
*/
-(BOOL)containsObject:(id)object;
/**
Returns a BOOL indicating if the object argument is contained in the receiver
@param block a block that returns a bool indicating if the object passed in is a match to object
@return a BOOL variable with YES if object is contained in the block or NO otherwise
*/
-(BOOL)containsObjectWithBlock:(BOOL(^)(id obj))block;
@end