ObjFW  Check-in [eea73f8d37]

Overview
Comment:-[setObject:forKey:] and -[removeObjectForKey:] now return void.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: eea73f8d379a93b0371893ff0d9f5915398f739ab91d87d067d8d2f3ae5725ff
User & Date: js on 2010-08-25 11:33:58
Other Links: manifest | tags
Context
2010-08-25
21:35
Assembly implementation for atomic operations (x86 and AMD64). check-in: bd2eef5fe1 user: js tags: trunk
11:33
-[setObject:forKey:] and -[removeObjectForKey:] now return void. check-in: eea73f8d37 user: js tags: trunk
11:22
Add warning to doc about collections not retaining and autoreleasing. check-in: 91ccd70c55 user: js tags: trunk
Changes

Modified src/OFMutableDictionary.h from [d7025a03e1] to [034e4119e3].

16
17
18
19
20
21
22

23

24
25
26
27
28
29
30


31
32
33
34
35
36
37
38

39
16
17
18
19
20
21
22
23

24
25
26
27

28


29
30
31
32
33
34
35

36

37
38







+
-
+



-

-
-
+
+





-

-
+

 */
@interface OFMutableDictionary: OFDictionary
{
	unsigned long mutations;
}

/**
 * Sets an object for a key.
 * Sets a key to an object. A key can be any object.
 * A key can be any object.
 *
 * \param key The key to set
 * \param obj The object to set the key to
 * \return The old object, autoreleased
 */
- (id)setObject: (OFObject*)obj
	 forKey: (OFObject <OFCopying>*)key;
- (void)setObject: (OFObject*)obj
	   forKey: (OFObject <OFCopying>*)key;

/**
 * Remove the object with the given key from the dictionary.
 *
 * \param key The key whose object should be removed
 * \return The object that was stored for the key, autoreleased
 */
- (id)removeObjectForKey: (OFObject*)key;
- (void)removeObjectForKey: (OFObject*)key;
@end

Modified src/OFMutableDictionary.m from [c74680827d] to [0791cedf2b].

75
76
77
78
79
80
81
82
83


84
85
86
87
88
89
90
75
76
77
78
79
80
81


82
83
84
85
86
87
88
89
90







-
-
+
+







	}

	[self freeMemory: data];
	data = newdata;
	size = newsize;
}

- (id)setObject: (OFObject*)obj
	 forKey: (OFObject <OFCopying>*)key
- (void)setObject: (OFObject*)obj
	   forKey: (OFObject <OFCopying>*)key
{
	uint32_t i, hash, last;
	id old;

	if (key == nil || obj == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];
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
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







-
+




-
-
+


-
+


-













-
-

+







-
+




-
+









-
-

+







-
+


-
-








		b->key = key;
		b->object = obj;
		b->hash = hash;
		data[i] = b;
		count++;

		return nil;
		return;
	}

	old = data[i]->object;
	data[i]->object = [obj retain];

	return [old autorelease];
	[old release];
}

- (id)removeObjectForKey: (OFObject*)key
- (void)removeObjectForKey: (OFObject*)key
{
	uint32_t i, hash, last;
	id old;

	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i] != NULL; i++) {
		if (data[i] == DELETED)
			continue;

		if ([data[i]->key isEqual: key]) {
			old = data[i]->object;

			[data[i]->key release];
			[data[i]->object release];
			[self freeMemory: data[i]];
			data[i] = DELETED;

			count--;
			mutations++;
			[self _resizeForCount: count];

			return [old autorelease];
			return;
		}
	}

	if (i < last)
		return nil;
		return;

	/* In case the last bucket is already used */
	last = hash & (size - 1);

	for (i = 0; i < last && data[i] != NULL; i++) {
		if (data[i] == DELETED)
			continue;

		if ([data[i]->key isEqual: key]) {
			old = data[i]->object;

			[data[i]->key release];
			[data[i]->object release];
			[self freeMemory: data[i]];
			data[i] = DELETED;

			count--;
			mutations++;
			[self _resizeForCount: count];

			return [old autorelease];
			return;
		}
	}

	return nil;
}

- copy
{
	return [[OFDictionary alloc] initWithDictionary: self];
}

Modified tests/OFDictionaryTests.m from [09fd546f0f] to [c3d8891ce2].

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
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







+
-
-
+
+



-
+





-
+

-
-
+
+





	    (dict = [[dict mutableCopy] autorelease]) &&
	    [dict count] == [dict2 count] &&
	    [[dict objectForKey: keys[0]] isEqual: values[0]] &&
	    [[dict objectForKey: keys[1]] isEqual: values[1]] &&
	    R([dict setObject: @"value3"
		       forKey: @"key3"]) &&
	    [[dict objectForKey: @"key3"] isEqual: @"value3"] &&
	    [[dict objectForKey: keys[0]] isEqual: values[0]] &&
	    [[dict setObject: @"foo"
		      forKey: keys[0]] isEqual: values[0]] &&
	    R([dict setObject: @"foo"
		       forKey: keys[0]]) &&
	    [[dict objectForKey: keys[0]] isEqual: @"foo"])

	TEST(@"-[removeObjectForKey:]",
	    [dict removeObjectForKey: keys[0]] &&
	    R([dict removeObjectForKey: keys[0]]) &&
	    [dict objectForKey: keys[0]] == nil)

	[dict setObject: @"foo"
		 forKey: keys[0]];
	TEST(@"-[isEqual:]", ![dict isEqual: dict2] &&
	    [dict removeObjectForKey: @"key3"] &&
	    R([dict removeObjectForKey: @"key3"]) &&
	    ![dict isEqual: dict2] &&
	    [dict setObject: values[0]
		     forKey: keys[0]] &&
	    R([dict setObject: values[0]
		       forKey: keys[0]]) &&
	    [dict isEqual: dict2])

	[pool drain];
}
@end