summaryrefslogtreecommitdiffstats
path: root/Classes/HJWeakMutableArray.m
blob: ce5988f0a9200257f396fcdc86588a475e12188e (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
//
//  HJWeakMutableArray.m
//  hjlib
//
//  Copyright Hunter and Johnson 2009, 2010, 2011
//  HJCache may be used freely in any iOS or Mac application free or commercial.
//  May be redistributed as source code only if all the original files are included.
//  See http://www.markj.net/hjcache-iphone-image-cache/

#import "HJWeakMutableArray.h"


@implementation HJWeakMutableArray

+(HJWeakMutableArray*) arrayWithCapacity:(int)capacity {
	return [[[HJWeakMutableArray alloc] initWithCapacity:capacity] autorelease];
}

-(HJWeakMutableArray*) initWithCapacity:(int)capacity {
	[super init];
	array = [[NSMutableArray arrayWithCapacity:capacity] retain];
	return self;
}

-(void) dealloc {
	[array release];
	[super dealloc];
}
	
- (void)addObject:(id)anObject {
	[array addObject:[NSValue valueWithNonretainedObject:anObject]];
}

-(id)objectAtIndex:(int)i {
	return [(NSValue*)[array objectAtIndex:i] nonretainedObjectValue];
}

- (void) removeObject:(id)objToRemove {
	for (int i=0; i<[array count]; i++) {
		NSValue* val = [array objectAtIndex:i];
		id obj = [val nonretainedObjectValue];
		if ([obj isEqual:objToRemove]) {
			[array removeObjectAtIndex:i];
			return;
		}
	}
	return;
}

- (id) findObject:(id)obj {
	for (int i=0; i<[array count]; i++) {
		NSValue* val = [array objectAtIndex:i];
		id objI = [val nonretainedObjectValue];
		if ([objI isEqual:obj]) {
			return objI;
		}
	}
	return nil;
}

-(int)count {
	return [array count];
}

- (NSEnumerator*)objectEnumerator {
	return [[[HJWeakMutableArrayEnumerator alloc] initWithInternalArray:array] autorelease];
}

- (NSString*)description {
	NSMutableString* s = [NSMutableString stringWithCapacity:100];
	[s appendString:@"Weak array {\n"];
	for (NSValue* v in array) {
		[s appendString:[[v nonretainedObjectValue] description]];
		[s appendString:@"  "];
		[s appendFormat:@"%u",[[v nonretainedObjectValue] retainCount]];
		[s appendString:@"\n"];
	}
	[s appendString:@"}"];
	return s;
}

+(void)test {
	/*
	HJWeakMutableArray* array = [HJWeakMutableArray arrayWithCapacity:4];

	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
		
	NSString* s = [[NSString stringWithFormat:@"zero"] retain];
	NSUInteger i1 = [s retainCount];
	[array addObject:s];
	NSUInteger i2 = [s retainCount];
	[array addObject:[[NSString stringWithFormat:@"one"] retain]];
	[array addObject:[[NSString stringWithFormat:@"two"] retain]];
	[array addObject:[NSString stringWithFormat:@"three"]];
	
	for (id obj in [array objectEnumerator]) {
		NSLog(@"obj %@  retain count %u",obj,[obj retainCount]);
	}
	
	NSEnumerator* e = [array objectEnumerator];
	id obj = [e nextObject];
	id all = [e allObjects];
	
	[pool drain];
	*/
}


@end


@implementation HJWeakMutableArrayEnumerator 

-(HJWeakMutableArrayEnumerator*)initWithInternalArray:(NSArray*)array {
	arrayEnum = [array objectEnumerator];
	[arrayEnum retain];
	return self;
}

-(void)dealloc {
	[arrayEnum release];
	[super dealloc];
}

-(id)nextObject {
	NSValue* val = (NSValue*)[arrayEnum nextObject];
	return [val nonretainedObjectValue];
}

-(NSArray*)allObjects {
	NSMutableArray* all2 = [NSMutableArray arrayWithCapacity:10];
	for (NSValue* val in [arrayEnum allObjects]) {
		//the value _does_ get retained here
		[all2 addObject:[val nonretainedObjectValue]];
	}
	return all2;
}



@end