ObjFW  Diff

Differences From Artifact [a7338c48b5]:

To Artifact [d37cee00b5]:


12
13
14
15
16
17
18

19
20
21
22
23
24
25
26
27

28
29
30
31
32
33
34
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36







+









+







 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <math.h>

#import "OFFloatMatrix.h"
#import "OFFloatVector.h"
#import "OFString.h"

#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

@implementation OFFloatMatrix
+ matrixWithRows: (size_t)rows
	 columns: (size_t)columns
65
66
67
68
69
70
71
72


73
74
75
76
77




78
79
80
81
82
83
84
67
68
69
70
71
72
73

74
75
76
77
78


79
80
81
82
83
84
85
86
87
88
89







-
+
+



-
-
+
+
+
+







{
	self = [super init];

	@try {
		rows = rows_;
		columns = columns_;

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

		data = [self allocMemoryForNItems: rows * columns
					 withSize: sizeof(float)];
		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)
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
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







-
-
+
+
+
-

-
-
+
+
+
+














+
+
+
+
+
+
+








	@try {
		size_t i;

		rows = rows_;
		columns = columns_;

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

		data = [self allocMemoryForNItems: rows * columns
					 withSize: sizeof(float)];
		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] = (float)va_arg(arguments, double);
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	free(data);

	[super dealloc];
}

- (void)setValue: (float)value
	  forRow: (size_t)row
	  column: (size_t)column
{
	if (row >= rows || column >= columns)
		@throw [OFOutOfRangeException newWithClass: isa];
310
311
312
313
314
315
316
317
318




319
320
321
322
323
324
325
324
325
326
327
328
329
330


331
332
333
334
335
336
337
338
339
340
341







-
-
+
+
+
+







	float *newData;
	size_t i, base1, base2;

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

	newData = [self allocMemoryForNItems: matrix->rows * columns
				    withSize: sizeof(float)];
	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;
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
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







-
+







-
+
-

+
+
+
+
+












-
+







			base3++;
		}

		base1 += rows;
		base2 += matrix->rows;
	}

	[self freeMemory: data];
	free(data);
	data = newData;

	rows = matrix->rows;
}

- (void)transpose
{
	float *newData = [self allocMemoryForNItems: rows * columns
	float *newData;
					   withSize: sizeof(float)];
	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;

		for (j = i; j < rows * columns; j += rows)
			newData[j] = data[k++];
	}

	[self freeMemory: data];
	free(data);
	data = newData;
}

- (void)translateWithVector: (OFFloatVector*)vector
{
	OFFloatMatrix *translation;
	float *cArray;