ObjFW  Check-in [64bc94cdb3]

Overview
Comment:Rename - objects / - items in OFArray / OFDataArray to - count.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 64bc94cdb3aa2906c18963c3bcf60509c5779a9c3c0bf6aaa072c5dbf81f5207
User & Date: js on 2009-05-05 17:59:49
Other Links: manifest | tags
Context
2009-05-07
11:55
There is no point in splitWithDelimiter: requiring an OFString.
Plus some code clean up in splitWithDelimiter:.
check-in: 1d44132d96 user: js tags: trunk
2009-05-05
17:59
Rename - objects / - items in OFArray / OFDataArray to - count. check-in: 64bc94cdb3 user: js tags: trunk
15:04
Add - splitWithDelimiter: to OFString. check-in: 029d4af91d user: js tags: trunk
Changes

Modified src/OFArray.h from [bc12ad5131] to [830e9546bb].

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 * \return A new autoreleased OFArray
 */
+ array;

/**
 * \return The number of objects in the OFArray
 */
- (size_t)objects;

/**
 * Clones the OFArray, creating a new one.
 *
 * \return A new autoreleased copy of the OFArray
 */
- (id)copy;







|







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 * \return A new autoreleased OFArray
 */
+ array;

/**
 * \return The number of objects in the OFArray
 */
- (size_t)count;

/**
 * Clones the OFArray, creating a new one.
 *
 * \return A new autoreleased copy of the OFArray
 */
- (id)copy;

Modified src/OFArray.m from [340e813c87] to [76eafae349].

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
		[self free];
		@throw e;
	}

	return self;
}

- (size_t)objects
{
	return [array items];
}

- (id)copy
{
	OFArray *new = [OFArray array];
	OFObject **objs;
	size_t len, i;

	objs = [array data];
	len = [array items];

	[new->array addNItems: len
		   fromCArray: objs];

	for (i = 0; i < len; i++)
		[objs[i] retain];








|

|









|







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
		[self free];
		@throw e;
	}

	return self;
}

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

- (id)copy
{
	OFArray *new = [OFArray array];
	OFObject **objs;
	size_t len, i;

	objs = [array data];
	len = [array count];

	[new->array addNItems: len
		   fromCArray: objs];

	for (i = 0; i < len; i++)
		[objs[i] retain];

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

- removeNObjects: (size_t)nobjects
{
	OFObject **objs;
	size_t len, i;

	objs = [array data];
	len = [array items];

	if (nobjects > len)
		@throw [OFOutOfRangeException newWithClass: isa];

	for (i = len - nobjects; i < len; i++)
		[objs[i] release];

	[array removeNItems: nobjects];

	return self;
}

- free
{
	OFObject **objs;
	size_t len, i;

	if (array != nil) {
		objs = [array data];
		len = [array items];

		for (i = 0; i < len; i++)
			[objs[i] release];

		[array release];
	}

	return [super free];
}
@end







|



















|










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

- removeNObjects: (size_t)nobjects
{
	OFObject **objs;
	size_t len, i;

	objs = [array data];
	len = [array count];

	if (nobjects > len)
		@throw [OFOutOfRangeException newWithClass: isa];

	for (i = len - nobjects; i < len; i++)
		[objs[i] release];

	[array removeNItems: nobjects];

	return self;
}

- free
{
	OFObject **objs;
	size_t len, i;

	if (array != nil) {
		objs = [array data];
		len = [array count];

		for (i = 0; i < len; i++)
			[objs[i] release];

		[array release];
	}

	return [super free];
}
@end

Modified src/OFDataArray.h from [2719b40a69] to [042b313b36].

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 * If you plan to store large hunks of data, you should consider using
 * OFBigDataArray, which allocates the memory in pages rather than in bytes.
 */
@interface OFDataArray: OFObject
{
	char   *data;
	size_t itemsize;
	size_t items;
}

/**
 * Creates a new OFDataArray whose items all have the same size.
 *
 * \param is The size of each element in the OFDataArray
 * \return A new autoreleased OFDataArray







|







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 * If you plan to store large hunks of data, you should consider using
 * OFBigDataArray, which allocates the memory in pages rather than in bytes.
 */
@interface OFDataArray: OFObject
{
	char   *data;
	size_t itemsize;
	size_t count;
}

/**
 * Creates a new OFDataArray whose items all have the same size.
 *
 * \param is The size of each element in the OFDataArray
 * \return A new autoreleased OFDataArray
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 * \return An initialized OFDataArray
 */
- initWithItemSize: (size_t)is;

/**
 * \return The number of items in the OFDataArray
 */
- (size_t)items;

/**
 * \return The size of each item in the OFDataArray in bytes
 */
- (size_t)itemsize;

/**







|







50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 * \return An initialized OFDataArray
 */
- initWithItemSize: (size_t)is;

/**
 * \return The number of items in the OFDataArray
 */
- (size_t)count;

/**
 * \return The size of each item in the OFDataArray in bytes
 */
- (size_t)itemsize;

/**

Modified src/OFDataArray.m from [0ce36bee3f] to [8ce162a9da].

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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
		c = isa;
		[super free];
		@throw [OFInvalidArgumentException newWithClass: c];
	}

	data = NULL;
	itemsize = is;
	items = 0;

	return self;
}

- (size_t)items
{
	return items;
}

- (size_t)itemsize
{
	return itemsize;
}

- (void*)data
{
	return data;
}

- (void*)item: (size_t)index
{
	if (index >= items)
		@throw [OFOutOfRangeException newWithClass: isa];

	return data + index * itemsize;
}

- (void*)last
{
	return data + (items - 1) * itemsize;
}

- add: (void*)item
{
	if (SIZE_MAX - items < 1)
		@throw [OFOutOfRangeException newWithClass: isa];

	data = [self resizeMem: data
		      toNItems: items + 1
		      withSize: itemsize];

	memcpy(data + items++ * itemsize, item, itemsize);

	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{
	if (nitems > SIZE_MAX - items)
		@throw [OFOutOfRangeException newWithClass: isa];

	data = [self resizeMem: data
		      toNItems: items + nitems
		      withSize: itemsize];

	memcpy(data + items * itemsize, carray, nitems * itemsize);
	items += nitems;

	return self;
}

- removeNItems: (size_t)nitems
{
	if (nitems > items)
		@throw [OFOutOfRangeException newWithClass: isa];

	data = [self resizeMem: data
		      toNItems: items - nitems
		      withSize: itemsize];

	items -= nitems;

	return self;
}

- (id)copy
{
	OFDataArray *new = [OFDataArray dataArrayWithItemSize: itemsize];
	[new addNItems: items
	    fromCArray: data];

	return new;
}

- (BOOL)isEqual: (id)obj
{
	if (![obj isKindOf: [OFDataArray class]])
		return NO;
	if ([obj items] != items || [obj itemsize] != itemsize)
		return NO;
	if (memcmp([obj data], data, items * itemsize))
		return NO;

	return YES;
}

- (int)compare: (id)obj
{
	int ret;

	if (![obj isKindOf: [OFDataArray class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];
	if ([obj itemsize] != itemsize)
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];

	if ([obj items] == items)
		return memcmp(data, [obj data], items * itemsize);

	if (items > [obj items]) {
		if ((ret = memcmp(data, [obj data], [obj items] * itemsize)))
			return ret;

		return *(char*)[self item: [obj items]];
	} else {
		if ((ret = memcmp(data, [obj data], items * itemsize)))
			return ret;

		return *(char*)[obj item: [self items]] * -1;
	}
}

- (uint32_t)hash
{
	uint32_t hash;
	size_t i;

	OF_HASH_INIT(hash);
	for (i = 0; i < items * itemsize; i++)
		OF_HASH_ADD(hash, ((char*)data)[i]);
	OF_HASH_FINALIZE(hash);

	return hash;
}
@end








|




|

|














|







|




|



|


|







|



|


|
|






|



|


|







|









|

|
















|
|

|
|


|

|


|









|







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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
		c = isa;
		[super free];
		@throw [OFInvalidArgumentException newWithClass: c];
	}

	data = NULL;
	itemsize = is;
	count = 0;

	return self;
}

- (size_t)count
{
	return count;
}

- (size_t)itemsize
{
	return itemsize;
}

- (void*)data
{
	return data;
}

- (void*)item: (size_t)index
{
	if (index >= count)
		@throw [OFOutOfRangeException newWithClass: isa];

	return data + index * itemsize;
}

- (void*)last
{
	return data + (count - 1) * itemsize;
}

- add: (void*)item
{
	if (SIZE_MAX - count < 1)
		@throw [OFOutOfRangeException newWithClass: isa];

	data = [self resizeMem: data
		      toNItems: count + 1
		      withSize: itemsize];

	memcpy(data + count++ * itemsize, item, itemsize);

	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{
	if (nitems > SIZE_MAX - count)
		@throw [OFOutOfRangeException newWithClass: isa];

	data = [self resizeMem: data
		      toNItems: count + nitems
		      withSize: itemsize];

	memcpy(data + count * itemsize, carray, nitems * itemsize);
	count += nitems;

	return self;
}

- removeNItems: (size_t)nitems
{
	if (nitems > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	data = [self resizeMem: data
		      toNItems: count - nitems
		      withSize: itemsize];

	count -= nitems;

	return self;
}

- (id)copy
{
	OFDataArray *new = [OFDataArray dataArrayWithItemSize: itemsize];
	[new addNItems: count
	    fromCArray: data];

	return new;
}

- (BOOL)isEqual: (id)obj
{
	if (![obj isKindOf: [OFDataArray class]])
		return NO;
	if ([obj count] != count || [obj itemsize] != itemsize)
		return NO;
	if (memcmp([obj data], data, count * itemsize))
		return NO;

	return YES;
}

- (int)compare: (id)obj
{
	int ret;

	if (![obj isKindOf: [OFDataArray class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];
	if ([obj itemsize] != itemsize)
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];

	if ([obj count] == count)
		return memcmp(data, [obj data], count * itemsize);

	if (count > [obj count]) {
		if ((ret = memcmp(data, [obj data], [obj count] * itemsize)))
			return ret;

		return *(char*)[self item: [obj count]];
	} else {
		if ((ret = memcmp(data, [obj data], count * itemsize)))
			return ret;

		return *(char*)[obj item: count] * -1;
	}
}

- (uint32_t)hash
{
	uint32_t hash;
	size_t i;

	OF_HASH_INIT(hash);
	for (i = 0; i < count * itemsize; i++)
		OF_HASH_ADD(hash, ((char*)data)[i]);
	OF_HASH_FINALIZE(hash);

	return hash;
}
@end

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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
	return self;
}

- add: (void*)item
{
	size_t nsize;

	if (SIZE_MAX - items < 1 || items + 1 > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: isa];

	nsize = ((items + 1) * itemsize + lastpagebyte) & ~lastpagebyte;

	if (size != nsize)
		data = [self resizeMem: data
				toSize: nsize];

	memcpy(data + items++ * itemsize, item, itemsize);
	size = nsize;

	return self;
}

-  addNItems: (size_t)nitems
  fromCArray: (void*)carray
{
	size_t nsize;

	if (nitems > SIZE_MAX - items || items + nitems > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: isa];

	nsize = ((items + nitems) * itemsize + lastpagebyte) & ~lastpagebyte;

	if (size != nsize)
		data = [self resizeMem: data
				toSize: nsize];

	memcpy(data + items * itemsize, carray, nitems * itemsize);
	items += nitems;
	size = nsize;

	return self;
}

- removeNItems: (size_t)nitems
{
	size_t nsize;

	if (nitems > items)
		@throw [OFOutOfRangeException newWithClass: isa];

	nsize = ((items - nitems) * itemsize + lastpagebyte) & ~lastpagebyte;

	if (size != nsize)
		data = [self resizeMem: data
				toSize: nsize];

	items -= nitems;
	size = nsize;

	return self;
}

- (id)copy
{
	OFDataArray *new = [OFDataArray bigDataArrayWithItemSize: itemsize];

	[new addNItems: items
	    fromCArray: data];

	return new;
}
@end







|


|





|










|


|





|
|









|


|





|









|





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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
	return self;
}

- add: (void*)item
{
	size_t nsize;

	if (SIZE_MAX - count < 1 || count + 1 > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: isa];

	nsize = ((count + 1) * itemsize + lastpagebyte) & ~lastpagebyte;

	if (size != nsize)
		data = [self resizeMem: data
				toSize: nsize];

	memcpy(data + count++ * itemsize, item, itemsize);
	size = nsize;

	return self;
}

-  addNItems: (size_t)nitems
  fromCArray: (void*)carray
{
	size_t nsize;

	if (nitems > SIZE_MAX - count || count + nitems > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: isa];

	nsize = ((count + nitems) * itemsize + lastpagebyte) & ~lastpagebyte;

	if (size != nsize)
		data = [self resizeMem: data
				toSize: nsize];

	memcpy(data + count * itemsize, carray, nitems * itemsize);
	count += nitems;
	size = nsize;

	return self;
}

- removeNItems: (size_t)nitems
{
	size_t nsize;

	if (nitems > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	nsize = ((count - nitems) * itemsize + lastpagebyte) & ~lastpagebyte;

	if (size != nsize)
		data = [self resizeMem: data
				toSize: nsize];

	count -= nitems;
	size = nsize;

	return self;
}

- (id)copy
{
	OFDataArray *new = [OFDataArray bigDataArrayWithItemSize: itemsize];

	[new addNItems: count
	    fromCArray: data];

	return new;
}
@end

Modified tests/OFDataArray/OFDataArray.m from [63952be35a] to [9fd6b5cc0c].

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
	[a freeMem: q];							\
									\
	puts("Adding multiple items at once...");			\
	p = [a allocWithSize: 8192];					\
	memset(p, 64, 8192);						\
	[a addNItems: 2							\
	  fromCArray: p];						\
	if (!memcmp([a last], [a item: [a items] - 2], 4096) &&		\
	    !memcmp([a item: [a items] - 2], p, 4096))			\
		puts("[a last], [a item: [a items] - 2] and p match!");	\
	else {								\
		puts("[a last], [a item: [a items] - 2] and p did not match!");\
		abort();						\
	}								\
	[a freeMem: p];							\
									\
	i = [a items];							\
	puts("Removing 2 items...");					\
	[a removeNItems: 2];						\
	if ([a items] + 2 != i) {					\
		puts("[a items] + 2 != i!");				\
		abort();						\
	}								\
									\
	puts("Trying to remove more data than we added...");		\
	CATCH_EXCEPTION([a removeNItems: [a items] + 1],		\
	    OFOutOfRangeException);					\
									\
	puts("Trying to access an index that does not exist...");	\
	CATCH_EXCEPTION([a item: [a items]], OFOutOfRangeException);	\
									\
	[a release];							\
									\
	puts("Creating new array and using it to build a string...");	\
	a = [[type alloc] initWithItemSize: 1];				\
									\
	for (i = 0; i < strlen(str); i++)				\







|
|
|

|




|


|
|




|



|







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
	[a freeMem: q];							\
									\
	puts("Adding multiple items at once...");			\
	p = [a allocWithSize: 8192];					\
	memset(p, 64, 8192);						\
	[a addNItems: 2							\
	  fromCArray: p];						\
	if (!memcmp([a last], [a item: [a count] - 2], 4096) &&		\
	    !memcmp([a item: [a count] - 2], p, 4096))			\
		puts("[a last], [a item: [a count] - 2] and p match!");	\
	else {								\
		puts("[a last], [a item: [a count] - 2] and p did not match!");\
		abort();						\
	}								\
	[a freeMem: p];							\
									\
	i = [a count];							\
	puts("Removing 2 items...");					\
	[a removeNItems: 2];						\
	if ([a count] + 2 != i) {					\
		puts("[a count] + 2 != i!");				\
		abort();						\
	}								\
									\
	puts("Trying to remove more data than we added...");		\
	CATCH_EXCEPTION([a removeNItems: [a count] + 1],		\
	    OFOutOfRangeException);					\
									\
	puts("Trying to access an index that does not exist...");	\
	CATCH_EXCEPTION([a item: [a count]], OFOutOfRangeException);	\
									\
	[a release];							\
									\
	puts("Creating new array and using it to build a string...");	\
	a = [[type alloc] initWithItemSize: 1];				\
									\
	for (i = 0; i < strlen(str); i++)				\