1 /*
2 Vertex.m
3 graph_cmd
4
5 The implemetation file for Vertex
6
7
8 */
9
10 //Version 1.0b6
11
12 #import "Vertex.h"
13
14
15 @implementation Vertex
16
17
18 #pragma mark ***init methods***
19
20
21 //init the vertex
22 -(id)initWithNumber:(int)thisNumber connections:(NSArray *)theseConnections
23 {
24 if (self =[super init])
25 {
26 number = thisNumber;
27 color=0;
28 //we make a copy here so that the original items may be released by the calling method
29 //especially important because of +vertexWithVertex:
30 connections=[[NSArray alloc] initWithArray:theseConnections copyItems:TRUE];
31 }
32 return self;
33 }
34
35 //the init method for loading from file
36 //this method is part of NSCoding
37 -(id)initWithCoder:(NSCoder *)coder
38 {
39 if (self =[super init])
40 {
41 connections = [[coder decodeObject] retain]; //decode the connections
42 [coder decodeValueOfObjCType:@encode(int) at:&color]; //decode by address
43 [coder decodeValueOfObjCType:@encode(int) at:&number]; //decode by address
44 }
45 return self;
46 }
47
48 //the init method for loading from a string representation
49 -(id)initWithString:(NSString *)stringVertex
50 {
51
52 NSLog(@"Making a vertex from string");
53 NSLog(stringVertex);
54
55 if (self = [super init])
56 {
57 //make a mutable copy
58 NSMutableString *thisString = [[NSMutableString alloc] initWithString:stringVertex];
59
60
61 //delete whitespace and line endings
62 int i;
63 NSRange delete;
64 delete.length = 1;
65 for (i=0; i<[thisString length]; i++)
66 {
67 delete.location = i;
68 if ([[thisString substringWithRange:delete] isEqualToString:@" "] ||
69 [[thisString substringWithRange:delete] isEqualToString:@"\n"])
70 {
71 [thisString deleteCharactersInRange:delete];
72 }
73 }
74
75
76 //deletethe parens
77 NSRange first,last;
78 first.location = 0;
79 first.length = 1;
80 last.location = [thisString length]-2;
81 last.length = 1;
82 [thisString deleteCharactersInRange:first];
83 [thisString deleteCharactersInRange:last];
84
85
86
87 NSLog(@"Clean vertex");
88 NSLog(thisString);
89
90 //this text parsing is not particularly clean or fast
91 //however, it is usually only used once since data files are used most of the time
92
93 //separate the number
94 NSArray *numberAndRest = [thisString componentsSeparatedByString:@":"];
95
96 //set the number
97 NSLog([numberAndRest objectAtIndex:0]);
98 number = [[numberAndRest objectAtIndex:0] intValue];
99
100 // NSLog(@"Number is: %d",number);
101 // NSLog([numberAndRest objectAtIndex:0]);
102 // NSLog([numberAndRest objectAtIndex:1]);
103
104 //separate the color
105 NSArray *connectionsAndColor = [[numberAndRest objectAtIndex:1] componentsSeparatedByString:@"-"];
106 color = [[connectionsAndColor objectAtIndex:1] intValue];
107
108 NSString *connectionString = [connectionsAndColor objectAtIndex:0];
109 // NSLog(connectionString);
110
111 int j;
112 NSArray *connectionStringList = [connectionString componentsSeparatedByString:@","];
113 NSMutableArray *connectionNumberList = [[NSMutableArray alloc] init];
114
115 //make the parsed connections into NSNumber for vertex storage
116 for(j=0;j<[connectionStringList count];j++)
117 {
118 [connectionNumberList addObject:[NSNumber numberWithInt:[[connectionStringList objectAtIndex:j] intValue]]];
119 }
120 connections = connectionNumberList;
121
122 }
123 NSLog([self stringVertex]);
124 return self;
125 }
126
127
128 #pragma mark ***copy methods***
129
130 //class method to create a new vertex from a vertex
131 +(Vertex *)vertexWithVertex:(Vertex *)vertex
132 {
133 //connections get copied by -initWithNumber:connections:
134 Vertex *newVertex = [[Vertex alloc] initWithNumber:[vertex number] connections:[vertex connections]];
135 [newVertex setThisColor:[vertex getColor]];
136 return newVertex;
137 }
138
139 //to implement the copying protocol
140 //note that the zone is ignored for now
141 -(id)copyWithZone:(NSZone *)zone
142 {
143 return [Vertex vertexWithVertex:self];
144 }
145
146
147
148
149 #pragma mark ***accessor methods***
150
151
152 -(int)getColor
153 {
154 return color;
155 }
156
157
158 -(NSArray *)connections
159 {
160 return connections;
161 }
162
163 -(int)number
164 {
165 return number;
166 }
167
168 -(int)numConnections
169 {
170 return [connections count];
171 }
172
173
174 //give a string representation of the Vertex
175 -(NSString *)stringVertex
176 {
177 int i;
178 NSMutableString *stringToBuild = [[NSMutableString alloc] init];
179
180 //append the basic stuff
181 [stringToBuild appendString:@"("];
182 [stringToBuild appendString:[[NSNumber numberWithInt:number] stringValue]];
183 [stringToBuild appendString:@":"];
184
185 //add connections
186
187 //to avoid a leading comma
188 if ([connections count]>0)
189 {
190 [stringToBuild appendString:[[connections objectAtIndex:0] stringValue]];
191 }
192 for (i=1; i<[connections count]; i++)
193 {
194 [stringToBuild appendString:@","];
195 [stringToBuild appendString:[[connections objectAtIndex:i] stringValue]];
196 }
197
198 //now add the color
199 [stringToBuild appendString:@"-"];
200 [stringToBuild appendString:[[NSNumber numberWithInt:color] stringValue]];
201
202 [stringToBuild appendString:@")"];
203
204
205 return stringToBuild;
206 }
207
208
209 #pragma mark ***mutator methods***
210
211
212 -(void)setThisColor:(int)newColor
213 {
214 color=newColor;
215 }
216
217 //set the connections
218 //send this an array of NSNumber
219 -(void)setConnections:(NSArray *)newConnections
220 {
221 [connections release];
222 connections = newConnections;
223 [connections retain];
224 }
225
226
227 -(void)setNumber:(int)newNumber
228
229 {
230 number = newNumber;
231 }
232
233 #pragma mark ***encoding***
234
235 //encoder method for saving
236 //for coding protocol
237 -(void)encodeWithCoder:(NSCoder *)coder
238 {
239 [coder encodeObject:connections];
240 [coder encodeValueOfObjCType:@encode(int) at:&color];
241 [coder encodeValueOfObjCType:@encode(int) at:&number];
242 }
243
244
245 @end
syntax highlighted by Code2HTML, v. 0.9.1