ObjFW  Diff

Differences From Artifact [0e7f420221]:

To Artifact [077edbfb6d]:

  • File src/OFFloatMatrix.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: 10120) [annotate] [blame] [check-ins using]


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

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

- initWithRows: (size_t)rows_
       columns: (size_t)columns_
{
	self = [super init];

	@try {
		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns ||
		    SIZE_MAX / rows * columns < sizeof(float))
			@throw [OFOutOfRangeException
			    newWithClass: isa];

		if ((data = malloc(rows * columns * sizeof(float))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: rows * columns * sizeof(float)];


		memset(data, 0, rows * columns * sizeof(float));

		if (rows == columns) {
			size_t i;

			for (i = 0; i < rows * columns; i += rows + 1)







|
|













|
<



|
|
>







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

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

- initWithRows: (size_t)rows_
       columns: (size_t)columns_
{
	self = [super init];

	@try {
		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns ||
		    SIZE_MAX / rows * columns < sizeof(float))
			@throw [OFOutOfRangeException exceptionWithClass: isa];


		if ((data = malloc(rows * columns * sizeof(float))) == NULL)
			@throw [OFOutOfMemoryException
			     exceptionWithClass: isa
				  requestedSize: rows * columns *
						 sizeof(float)];

		memset(data, 0, rows * columns * sizeof(float));

		if (rows == columns) {
			size_t i;

			for (i = 0; i < rows * columns; i += rows + 1)
135
136
137
138
139
140
141
142
143
144
145
146
147

148
149
150
151
152
153
154
		size_t i;

		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns ||
		    SIZE_MAX / rows * columns < sizeof(float))
			@throw [OFOutOfRangeException newWithClass: isa];

		if ((data = malloc(rows * columns * sizeof(float))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: rows * columns * sizeof(float)];


		for (i = 0; i < rows; i++) {
			size_t j;

			for (j = i; j < rows * columns; j += rows)
				data[j] = (j == 0
				    ? data_ : (float)va_arg(arguments, double));







|



|
|
>







135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
		size_t i;

		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns ||
		    SIZE_MAX / rows * columns < sizeof(float))
			@throw [OFOutOfRangeException exceptionWithClass: isa];

		if ((data = malloc(rows * columns * sizeof(float))) == NULL)
			@throw [OFOutOfMemoryException
			     exceptionWithClass: isa
				  requestedSize: rows * columns *
						 sizeof(float)];

		for (i = 0; i < rows; i++) {
			size_t j;

			for (j = i; j < rows * columns; j += rows)
				data[j] = (j == 0
				    ? data_ : (float)va_arg(arguments, double));
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
}

- (void)setValue: (float)value
	  forRow: (size_t)row
	  column: (size_t)column
{
	if (row >= rows || column >= columns)
		@throw [OFOutOfRangeException newWithClass: isa];

	data[row * columns + column] = value;
}

- (float)valueForRow: (size_t)row
	      column: (size_t)column
{
	if (row >= rows || column >= columns)
		@throw [OFOutOfRangeException newWithClass: isa];

	return data[row * columns + column];
}

- (size_t)rows
{
	return rows;







|








|







170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
}

- (void)setValue: (float)value
	  forRow: (size_t)row
	  column: (size_t)column
{
	if (row >= rows || column >= columns)
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	data[row * columns + column] = value;
}

- (float)valueForRow: (size_t)row
	      column: (size_t)column
{
	if (row >= rows || column >= columns)
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	return data[row * columns + column];
}

- (size_t)rows
{
	return rows;
290
291
292
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

- (void)addMatrix: (OFFloatMatrix*)matrix
{
	size_t i;

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

	for (i = 0; i < rows * columns; i++)
		data[i] += matrix->data[i];
}

- (void)subtractMatrix: (OFFloatMatrix*)matrix
{
	size_t i;

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

	for (i = 0; i < rows * columns; i++)
		data[i] -= matrix->data[i];
}


- (void)multiplyWithScalar: (float)scalar







|
|











|
|







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

- (void)addMatrix: (OFFloatMatrix*)matrix
{
	size_t i;

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

	for (i = 0; i < rows * columns; i++)
		data[i] += matrix->data[i];
}

- (void)subtractMatrix: (OFFloatMatrix*)matrix
{
	size_t i;

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

	for (i = 0; i < rows * columns; i++)
		data[i] -= matrix->data[i];
}


- (void)multiplyWithScalar: (float)scalar
333
334
335
336
337
338
339
340
341
342
343
344
345
346

347
348
349
350
351
352
353

- (void)multiplyWithMatrix: (OFFloatMatrix*)matrix
{
	float *newData;
	size_t i, base1, base2;

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

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


	base1 = 0;
	base2 = 0;

	for (i = 0; i < columns; i++) {
		size_t base3 = base2;
		size_t j;







|
|



|
|
>







334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

- (void)multiplyWithMatrix: (OFFloatMatrix*)matrix
{
	float *newData;
	size_t i, base1, base2;

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

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

	base1 = 0;
	base2 = 0;

	for (i = 0; i < columns; i++) {
		size_t base3 = base2;
		size_t j;
380
381
382
383
384
385
386
387

388
389
390
391
392
393
394
395
396

- (void)transpose
{
	float *newData;
	size_t i, k;

	if ((newData = malloc(rows * columns * sizeof(float))) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa

					      requestedSize: rows * columns *
							     sizeof(float)];

	rows ^= columns;
	columns ^= rows;
	rows ^= columns;

	for (i = k = 0; i < rows; i++) {
		size_t j;







|
>
|
<







382
383
384
385
386
387
388
389
390
391

392
393
394
395
396
397
398

- (void)transpose
{
	float *newData;
	size_t i, k;

	if ((newData = malloc(rows * columns * sizeof(float))) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithClass: isa
			 requestedSize: rows * columns * sizeof(float)];


	rows ^= columns;
	columns ^= rows;
	rows ^= columns;

	for (i = k = 0; i < rows; i++) {
		size_t j;
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
- (void)translateWithVector: (OFFloatVector*)vector
{
	OFFloatMatrix *translation;
	float *cArray;

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

	cArray = [vector cArray];
	translation = [[OFFloatMatrix alloc] initWithRows: rows
						  columns: columns];

	memcpy(translation->data + (columns - 1) * rows, cArray,
	    (rows - 1) * sizeof(float));







|
|







408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
- (void)translateWithVector: (OFFloatVector*)vector
{
	OFFloatMatrix *translation;
	float *cArray;

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

	cArray = [vector cArray];
	translation = [[OFFloatMatrix alloc] initWithRows: rows
						  columns: columns];

	memcpy(translation->data + (columns - 1) * rows, cArray,
	    (rows - 1) * sizeof(float));
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
		   angle: (float)angle
{
	OFFloatMatrix *rotation;
	float n[3], m, angleCos, angleSin;

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

	n[0] = vector->data[0];
	n[1] = vector->data[1];
	n[2] = vector->data[2];

	m = sqrtf(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);








|
|







433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
		   angle: (float)angle
{
	OFFloatMatrix *rotation;
	float n[3], m, angleCos, angleSin;

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

	n[0] = vector->data[0];
	n[1] = vector->data[1];
	n[2] = vector->data[2];

	m = sqrtf(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);

480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
{
	OFFloatMatrix *scale;
	float *cArray;
	size_t i, j;

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

	cArray = [vector cArray];
	scale = [[OFFloatMatrix alloc] initWithRows: rows
					    columns: columns];

	for (i = j = 0; i < ((rows - 1) * columns) - 1; i += rows + 1)
		scale->data[i] = cArray[j++];







|
|







482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
{
	OFFloatMatrix *scale;
	float *cArray;
	size_t i, j;

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

	cArray = [vector cArray];
	scale = [[OFFloatMatrix alloc] initWithRows: rows
					    columns: columns];

	for (i = j = 0; i < ((rows - 1) * columns) - 1; i += rows + 1)
		scale->data[i] = cArray[j++];