ObjFW  Diff

Differences From Artifact [3c930c2cbc]:

To Artifact [83bd61c36b]:

  • File src/OFDoubleVector.m — part of check-in [e1e7ffa903] at 2011-09-22 23:25:42 on branch trunk — Exceptions are now autoreleased.

    This is safe as an "exception loop" can't happen, since if allocating
    an exception fails, it throws an OFAllocFailedException which is
    preallocated and can always be thrown.

    So, the worst case would be that an autorelease of an exception fails,
    triggering an OFOutOfMemoryException for which there is no memory,
    resulting in an OFAllocFailedException to be thrown. (user: js, size: 8447) [annotate] [blame] [check-ins using]


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
	return ret;
}

- init
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithDimension: (size_t)dimension_
{
	self = [super init];

	@try {
		dimension = dimension_;

		if (SIZE_MAX / dimension < sizeof(double))
			@throw [OFOutOfRangeException newWithClass: isa];

		if ((data = malloc(dimension * sizeof(double))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: dimension * sizeof(double)];

		memset(data, 0, dimension * sizeof(double));
	} @catch (id e) {
		[self release];
		@throw e;
	}








|
|










|



|
|







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
	return ret;
}

- init
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException exceptionWithClass: c
						    selector: _cmd];
}

- initWithDimension: (size_t)dimension_
{
	self = [super init];

	@try {
		dimension = dimension_;

		if (SIZE_MAX / dimension < sizeof(double))
			@throw [OFOutOfRangeException exceptionWithClass: isa];

		if ((data = malloc(dimension * sizeof(double))) == NULL)
			@throw [OFOutOfMemoryException
			     exceptionWithClass: isa
				  requestedSize: dimension * sizeof(double)];

		memset(data, 0, dimension * sizeof(double));
	} @catch (id e) {
		[self release];
		@throw e;
	}

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

	@try {
		size_t i;

		dimension = dimension_;

		if (SIZE_MAX / dimension < sizeof(double))
			@throw [OFOutOfRangeException newWithClass: isa];

		if ((data = malloc(dimension * sizeof(double))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: dimension * sizeof(double)];

		data[0] = data_;
		for (i = 1; i < dimension; i++)
			data[i] = va_arg(arguments, double);
	} @catch (id e) {
		[self release];
		@throw e;







|



|
|







115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

	@try {
		size_t i;

		dimension = dimension_;

		if (SIZE_MAX / dimension < sizeof(double))
			@throw [OFOutOfRangeException exceptionWithClass: isa];

		if ((data = malloc(dimension * sizeof(double))) == NULL)
			@throw [OFOutOfMemoryException
			     exceptionWithClass: isa
				  requestedSize: dimension * sizeof(double)];

		data[0] = data_;
		for (i = 1; i < dimension; i++)
			data[i] = va_arg(arguments, double);
	} @catch (id e) {
		[self release];
		@throw e;
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
	[super dealloc];
}

- (void)setValue: (double)value
	 atIndex: (size_t)index
{
	if (index >= dimension)
		@throw [OFOutOfRangeException newWithClass: isa];

	data[index] = value;
}

- (double)valueAtIndex: (size_t)index
{
	if (index >= dimension)
		@throw [OFOutOfRangeException newWithClass: isa];

	return data[index];
}

- (size_t)dimension
{
	return dimension;
}

- (void)setDimension: (size_t)dimension_
{
	double *newData;
	size_t i;

	if ((newData = realloc(data, dimension_ * sizeof(double))) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa

					      requestedSize: dimension_ *
							     sizeof(double)];

	data = newData;

	for (i = dimension; i < dimension_; i++)
		data[i] = 0;

	dimension = dimension_;







|







|















|
>
|
<







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
	[super dealloc];
}

- (void)setValue: (double)value
	 atIndex: (size_t)index
{
	if (index >= dimension)
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	data[index] = value;
}

- (double)valueAtIndex: (size_t)index
{
	if (index >= dimension)
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	return data[index];
}

- (size_t)dimension
{
	return dimension;
}

- (void)setDimension: (size_t)dimension_
{
	double *newData;
	size_t i;

	if ((newData = realloc(data, dimension_ * sizeof(double))) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithClass: isa
			 requestedSize: dimension_ * sizeof(double)];


	data = newData;

	for (i = dimension; i < dimension_; i++)
		data[i] = 0;

	dimension = dimension_;
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
}

- (void)addVector: (OFDoubleVector*)vector
{
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	for (i = 0; i < dimension; i++)
		data[i] += vector->data[i];
}

- (void)subtractVector: (OFDoubleVector*)vector
{
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	for (i = 0; i < dimension; i++)
		data[i] -= vector->data[i];
}

- (void)multiplyWithScalar: (double)scalar
{







|
|










|
|







293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
}

- (void)addVector: (OFDoubleVector*)vector
{
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	for (i = 0; i < dimension; i++)
		data[i] += vector->data[i];
}

- (void)subtractVector: (OFDoubleVector*)vector
{
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	for (i = 0; i < dimension; i++)
		data[i] -= vector->data[i];
}

- (void)multiplyWithScalar: (double)scalar
{
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
}

- (void)multiplyWithComponentsOfVector: (OFDoubleVector*)vector
{
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	for (i = 0; i < dimension; i++)
		data[i] *= vector->data[i];
}

- (void)divideByComponentsOfVector: (OFDoubleVector*)vector
{
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	for (i = 0; i < dimension; i++)
		data[i] /= vector->data[i];
}

- (double)dotProductWithVector: (OFDoubleVector*)vector
{
	double dotProduct;
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	dotProduct = 0.0;

	for (i = 0; i < dimension; i++)
		dotProduct += data[i] * vector->data[i];

	return dotProduct;
}

- (OFDoubleVector*)crossProductWithVector: (OFDoubleVector*)vector
{
	OFDoubleVector *crossProduct;

	if (dimension != 3)
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];

	if (vector->dimension != dimension)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	crossProduct = [OFDoubleVector vectorWithDimension: 3];

	crossProduct->data[0] =
	    data[1] * vector->data[2] - data[2] * vector->data[1];
	crossProduct->data[1] =
	    data[2] * vector->data[0] - data[0] * vector->data[2];
	crossProduct->data[2] =
	    data[0] * vector->data[1] - data[1] * vector->data[0];

	return crossProduct;
}

- (void)multiplyWithMatrix: (OFDoubleMatrix*)matrix
{
	double *newData;
	size_t i, j, k;

	if (matrix->isa != doubleMatrix || dimension != matrix->columns)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ((newData = malloc(matrix->rows * sizeof(double))) == NULL)
		@throw [OFOutOfMemoryException
		     newWithClass: isa
		    requestedSize: matrix->rows * sizeof(double)];

	memset(newData, 0, matrix->rows * sizeof(double));

	for (i = j = k = 0; i < matrix->rows * matrix->columns; i++) {
		newData[j] += matrix->data[i] * data[k];

		if (++j == matrix->rows) {







|
|










|
|











|
|














|
|


|
|



















|
|



|
|







333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
}

- (void)multiplyWithComponentsOfVector: (OFDoubleVector*)vector
{
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	for (i = 0; i < dimension; i++)
		data[i] *= vector->data[i];
}

- (void)divideByComponentsOfVector: (OFDoubleVector*)vector
{
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	for (i = 0; i < dimension; i++)
		data[i] /= vector->data[i];
}

- (double)dotProductWithVector: (OFDoubleVector*)vector
{
	double dotProduct;
	size_t i;

	if (vector->isa != isa || vector->dimension != dimension)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	dotProduct = 0.0;

	for (i = 0; i < dimension; i++)
		dotProduct += data[i] * vector->data[i];

	return dotProduct;
}

- (OFDoubleVector*)crossProductWithVector: (OFDoubleVector*)vector
{
	OFDoubleVector *crossProduct;

	if (dimension != 3)
		@throw [OFNotImplementedException exceptionWithClass: isa
							    selector: _cmd];

	if (vector->dimension != dimension)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	crossProduct = [OFDoubleVector vectorWithDimension: 3];

	crossProduct->data[0] =
	    data[1] * vector->data[2] - data[2] * vector->data[1];
	crossProduct->data[1] =
	    data[2] * vector->data[0] - data[0] * vector->data[2];
	crossProduct->data[2] =
	    data[0] * vector->data[1] - data[1] * vector->data[0];

	return crossProduct;
}

- (void)multiplyWithMatrix: (OFDoubleMatrix*)matrix
{
	double *newData;
	size_t i, j, k;

	if (matrix->isa != doubleMatrix || dimension != matrix->columns)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	if ((newData = malloc(matrix->rows * sizeof(double))) == NULL)
		@throw [OFOutOfMemoryException
		     exceptionWithClass: isa
			  requestedSize: matrix->rows * sizeof(double)];

	memset(newData, 0, matrix->rows * sizeof(double));

	for (i = j = k = 0; i < matrix->rows * matrix->columns; i++) {
		newData[j] += matrix->data[i] * data[k];

		if (++j == matrix->rows) {