-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEPTwittererChain.m
130 lines (100 loc) · 2.8 KB
/
EPTwittererChain.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
//
// EPTweetChain.m
// Degrees of Tweetdom
//
// Created by Simone Manganelli on 2008-04-09.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "EPTwittererChain.h"
@implementation EPTwittererChain
- (id)init;
{
if (self = [super init]) {
chainStepsArray = [[NSMutableArray alloc] init];
pathLength = 1;
}
return self;
}
- (void)dealloc;
{
[chainStepsArray release];
[super dealloc];
}
- (id)initWithInitialTweetChain:(EPTwittererChain *)initialChain;
{
if (self = [super init]) {
chainStepsArray = [[NSMutableArray alloc] initWithArray:[initialChain chainStepsArray]];
pathLength = 1 - [chainStepsArray count];
}
return self;
}
- (id)initWithInitialChainStepsArray:(NSArray *)initialChainStepsArray;
{
if (self = [super init]) {
chainStepsArray = [initialChainStepsArray copy];
pathLength = 1 - [chainStepsArray count];
}
return self;
}
- (NSString *)description;
{
NSMutableString *descriptionString = [[NSMutableString alloc] init];
NSString *nextTwitterer = nil;
for (nextTwitterer in chainStepsArray) {
[descriptionString appendString:[NSString stringWithFormat:@"%@ --> ",nextTwitterer]];
}
[descriptionString appendString:@"end."];
return [descriptionString autorelease];
}
- (void)setRootTwitterer:(NSString *)startTwitterer;
{
[chainStepsArray release];
chainStepsArray = [[NSArray alloc] initWithObjects:startTwitterer,nil];
pathLength = 0;
}
- (void)addTwittererToChain:(NSString *)twittererHandle;
{
[chainStepsArray addObject:twittererHandle];
pathLength--;
}
- (void)addPartialChainToChain:(EPTwittererChain *)partialChain;
{
if ([[chainStepsArray lastObject] isEqualToString:[[partialChain chainStepsArray] objectAtIndex:0]]) {
// the chain to add starts with the same twitterer as the current end twitterer,
// so we don't want to end up with a twitterer being duplicated in the same chain
[chainStepsArray addObjectsFromArray:[[partialChain chainStepsArray]
subarrayWithRange:NSMakeRange(1,[[partialChain chainStepsArray] count] - 1)]
];
pathLength -= ([[partialChain chainStepsArray] count] - 1);
} else {
[chainStepsArray addObjectsFromArray:[partialChain chainStepsArray]];
pathLength -= [[partialChain chainStepsArray] count];
}
}
- (NSString *)lastTwittererInChain;
{
return [chainStepsArray lastObject];
}
- (NSString *)firstTwittererInChain;
{
return [chainStepsArray objectAtIndex:0];
}
- (BOOL)containsTwitterer:(NSString *)twittererHandle;
{
return [chainStepsArray containsObject:twittererHandle];
}
- (int)pathLength;
{
return pathLength;
}
- (NSArray *)chainStepsArray;
{
return chainStepsArray;
}
// this should *never* be used except for testing purposes;
// add twitterers to the chain to increase the path length
- (void)setPathLength:(int)newPathLength;
{
pathLength = newPathLength;
}
@end