ObjFW  Check-in [4fb0f4bf66]

Overview
Comment:Add -[OFMutableDictionary initWithCapacity:].

This was already implemented in OFDictionary_hashtable for internal
usage and is now publicly available.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4fb0f4bf66a09a408383f40639ffbbce4f81b56fbb3e9d86cfa58295507550d3
User & Date: js on 2013-02-18 11:07:29
Other Links: manifest | tags
Context
2013-02-18
11:31
A few small fixes for OS X/PPC. check-in: adaf5a3e6e user: js tags: trunk
11:07
Add -[OFMutableDictionary initWithCapacity:]. check-in: 4fb0f4bf66 user: js tags: trunk
10:38
Add -[OFDataArray initWithItemSize:capacity:]. check-in: e403ca05cb user: js tags: trunk
Changes

Modified src/OFMapTable.h from [c74d98dfe6] to [98630c9c0b].

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 * @brief A class similar to OFDictionary, but providing more options how keys
 *	  and values should be retained, released, compared and hashed.
 */
@interface OFMapTable: OFObject <OFCopying, OFFastEnumeration>
{
	of_map_table_functions_t _keyFunctions, _valueFunctions;
	struct of_map_table_bucket **_buckets;
	uint32_t _minCapacity, _capacity, _count;
	uint8_t _rotate;
	unsigned long _mutations;
}

/*!
 * @brief Creates a new OFMapTable with the specified key and value functions.
 *







|







44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 * @brief A class similar to OFDictionary, but providing more options how keys
 *	  and values should be retained, released, compared and hashed.
 */
@interface OFMapTable: OFObject <OFCopying, OFFastEnumeration>
{
	of_map_table_functions_t _keyFunctions, _valueFunctions;
	struct of_map_table_bucket **_buckets;
	uint32_t _count, _capacity;
	uint8_t _rotate;
	unsigned long _mutations;
}

/*!
 * @brief Creates a new OFMapTable with the specified key and value functions.
 *

Modified src/OFMapTable.m from [212223cd50] to [1f61a324ed].

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
		for (_capacity = 1; _capacity < capacity; _capacity <<= 1);
		if (capacity * 8 / _capacity >= 6)
			_capacity <<= 1;

		if (_capacity < MIN_CAPACITY)
			_capacity = MIN_CAPACITY;

		_minCapacity = _capacity;

		_buckets = [self allocMemoryWithSize: sizeof(*_buckets)
					       count: _capacity];

		memset(_buckets, 0, _capacity * sizeof(*_buckets));

		if (of_hash_seed != 0)
#if defined(HAVE_ARC4RANDOM)







<
<







143
144
145
146
147
148
149


150
151
152
153
154
155
156
		for (_capacity = 1; _capacity < capacity; _capacity <<= 1);
		if (capacity * 8 / _capacity >= 6)
			_capacity <<= 1;

		if (_capacity < MIN_CAPACITY)
			_capacity = MIN_CAPACITY;



		_buckets = [self allocMemoryWithSize: sizeof(*_buckets)
					       count: _capacity];

		memset(_buckets, 0, _capacity * sizeof(*_buckets));

		if (of_hash_seed != 0)
#if defined(HAVE_ARC4RANDOM)
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
					     hash: OF_ROR(_buckets[i]->hash,
						       _rotate)];
	} @catch (id e) {
		[copy release];
		@throw e;
	}

	copy->_minCapacity = MIN_CAPACITY;

	return copy;
}

- (size_t)count
{
	return _count;
}







<
<







240
241
242
243
244
245
246


247
248
249
250
251
252
253
					     hash: OF_ROR(_buckets[i]->hash,
						       _rotate)];
	} @catch (id e) {
		[copy release];
		@throw e;
	}



	return copy;
}

- (size_t)count
{
	return _count;
}
307
308
309
310
311
312
313




314
315
316
317
318
319
320
321
	if (fullness >= 6)
		capacity = _capacity << 1;
	else if (fullness <= 1)
		capacity = _capacity >> 1;
	else
		return;





	if (capacity < _capacity && capacity < _minCapacity)
		return;

	buckets = [self allocMemoryWithSize: sizeof(*buckets)
				      count: capacity];

	for (i = 0; i < capacity; i++)
		buckets[i] = NULL;







>
>
>
>
|







303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
	if (fullness >= 6)
		capacity = _capacity << 1;
	else if (fullness <= 1)
		capacity = _capacity >> 1;
	else
		return;

	/*
	 * Don't downsize if we have an initial capacity or if we would fall
	 * below the minimum capacity.
	 */
	if ((capacity < _capacity && count > _count) || capacity < MIN_CAPACITY)
		return;

	buckets = [self allocMemoryWithSize: sizeof(*buckets)
				      count: capacity];

	for (i = 0; i < capacity; i++)
		buckets[i] = NULL;

Modified src/OFMutableDictionary.h from [0910db56e1] to [2e2922b971].

20
21
22
23
24
25
26


















27
28
29
30
31
32
33
typedef id (^of_dictionary_replace_block_t)(id key, id object, BOOL *stop);
#endif

/*!
 * @brief An abstract class for storing and changing objects in a dictionary.
 */
@interface OFMutableDictionary: OFDictionary


















/*!
 * @brief Sets an object for a key.
 *
 * A key can be any object that conforms to the OFCopying protocol.
 *
 * @param key The key to set
 * @param object The object to set the key to







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
typedef id (^of_dictionary_replace_block_t)(id key, id object, BOOL *stop);
#endif

/*!
 * @brief An abstract class for storing and changing objects in a dictionary.
 */
@interface OFMutableDictionary: OFDictionary
/*!
 * @brief Creates a new OFMutableDictionary with enough memory to hold the
 *	  specified number of objects.
 *
 * @param capacity The initial capacity for the OFMutableDictionary
 * @return A new autoreleased OFMutableDictionary
 */
+ (instancetype)dictionaryWithCapacity: (size_t)capacity;

/*!
 * @brief Initializes an already allocated OFMutableDictionary with enough
 *	  memory to hold the specified number of objects.
 *
 * @param capacity The initial capacity for the OFMutableDictionary
 * @return A new initialized OFMutableDictionary
 */
- initWithCapacity: (size_t)capacity;

/*!
 * @brief Sets an object for a key.
 *
 * A key can be any object that conforms to the OFCopying protocol.
 *
 * @param key The key to set
 * @param object The object to set the key to

Modified src/OFMutableDictionary.m from [7687a7a311] to [3f243b3679].

87
88
89
90
91
92
93






94
95
96
97
98
99
100
}

- initWithSerialization: (OFXMLElement*)element
{
	return (id)[[OFMutableDictionary_hashtable alloc]
	    initWithSerialization: element];
}







- retain
{
	return self;
}

- autorelease







>
>
>
>
>
>







87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
}

- initWithSerialization: (OFXMLElement*)element
{
	return (id)[[OFMutableDictionary_hashtable alloc]
	    initWithSerialization: element];
}

- initWithCapacity: (size_t)capacity
{
	return (id)[[OFMutableDictionary_hashtable alloc]
	    initWithCapacity: capacity];
}

- retain
{
	return self;
}

- autorelease
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
+ alloc
{
	if (self == [OFMutableDictionary class])
		return (id)&placeholder;

	return [super alloc];
}






- init
{
	if (object_getClass(self) == [OFMutableDictionary class]) {
		@try {
			[self doesNotRecognizeSelector: _cmd];
			abort();
		} @catch (id e) {
			[self release];
			@throw e;
		}
	}

	return [super init];
}












- (void)setObject: (id)object
	   forKey: (id)key
{
	[self doesNotRecognizeSelector: _cmd];
	abort();
}







>
>
>
>
>















>
>
>
>
>
>
>
>
>
>
>







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
169
170
171
172
173
174
175
176
+ alloc
{
	if (self == [OFMutableDictionary class])
		return (id)&placeholder;

	return [super alloc];
}

+ dictionaryWithCapacity: (size_t)capacity
{
	return [[[self alloc] initWithCapacity: capacity] autorelease];
}

- init
{
	if (object_getClass(self) == [OFMutableDictionary class]) {
		@try {
			[self doesNotRecognizeSelector: _cmd];
			abort();
		} @catch (id e) {
			[self release];
			@throw e;
		}
	}

	return [super init];
}

- initWithCapacity: (size_t)capacity
{
	@try {
		[self doesNotRecognizeSelector: _cmd];
		abort();
	} @catch (id e) {
		[self release];
		@throw e;
	}
}

- (void)setObject: (id)object
	   forKey: (id)key
{
	[self doesNotRecognizeSelector: _cmd];
	abort();
}