-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBCObjectMatrix.m
219 lines (153 loc) · 6.73 KB
/
BCObjectMatrix.m
1
2
3
4
5
6
7
8
9
10
11
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
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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// BCObjectMatrix.m
// Xynk
//
// Created by Tom Houpt on 13/5/27.
//
//
#import "BCObjectMatrix.h"
#include "BCArrayUtilities.h"
@implementation BCObjectMatrix
// parallel implementation of BCMatrix but using objects in an NSMutableArray instead of elements in void *buffer
// [myMatrix elementAtIndices:i,j,k]
// matrix object [i][j][k] is at bufferArray [ c[0] * i + c[1] * j + c[3] * k]
// internally: matrix object [i][j][k] is at bufferArray [ (c[0] * index[0] + c[1] * index[1] + c[3] * index[2]) ]
@synthesize dimension;
@synthesize buffer;
@synthesize c;
@synthesize index;
@synthesize dimensionSizes;
-(id)initWithDimension:(NSInteger)d andMaxCount:(NSArray *)sizes; {
assert( d == [sizes count]);
// must have specificed a maximum size for each dimension of the matrix
self = [super init];
if (self) {
c= NULL;
dimensionSizes = NULL;
index = NULL;
dimension = d;
dimensionSizes = calloc(dimension, sizeof(NSInteger));
// copy the maximum size for each dimension into the coeffecient array
for (int i=0;i<[sizes count];i++) {
dimensionSizes[i] = [[sizes objectAtIndex:i] intValue];
}
// generate the coeffecient array as a product of the lower dimensions
c = calloc(dimension, sizeof(NSInteger) );
c[0] = 1;
for (int i=1;i<dimension;i++) {
c[i] = dimensionSizes[i-1] * c[i-1];
}
NSUInteger buffer_size = [self count];
buffer = [NSMutableArray arrayWithCapacity:buffer_size];
// initialize the content of the matrix with [NSNumber numberWithInteger:0]
// this will make sure the buffer actually has all array elements allocated and available for replacement
NSNumber *zeroNumber = [NSNumber numberWithInteger:0];
for (int i=0; i< buffer_size; i++) {
[buffer addObject:zeroNumber];
}
// make an array to hold the indices when dereferencing an element
index = calloc(dimension, sizeof(NSInteger));
}
return self;
}
-(void)dealloc; {
// free the malloc'd buffers
if (NULL != dimensionSizes ) { free(dimensionSizes); }
if (NULL != c) { free(c); }
if (NULL != index) { free(index); }
}
-(NSInteger)count; {
// total number of elements in matrix
return (productOfIntArray(dimensionSizes, dimension));
}
-(id)objectAtIndices:(NSInteger) firstIndex,...; {
// variadic method
// returns object at matrix[firstIndex,...]
// note that this returns object reference, not a copy of the object
va_list argumentList;
index[0] = firstIndex;
va_start(argumentList, firstIndex); // Start scanning for arguments after firstObject.
NSInteger currentIndex = 1;
while (currentIndex < dimension) {
// As many times as we can get an argument of type "NSInteger"
// that isn't nil, add it to self's contents.
index[currentIndex] = va_arg(argumentList, NSInteger);
if (index[currentIndex] >= dimensionSizes[currentIndex]){
NSLog(@"BCObjectMatrix currentIndex:%ld index[] = %ld dimensionSizes[] = %ld", (long)currentIndex, (long)index[currentIndex], (long)dimensionSizes[currentIndex]);
}
assert( index[currentIndex] < dimensionSizes[currentIndex]);
if (index[currentIndex] >= dimensionSizes[currentIndex]) {
NSLog(@"BCObjectMatrix objectAtIndices out of bounds index: %zd", (long)currentIndex);
return NULL;
}
currentIndex++;
}
va_end(argumentList);
NSUInteger offset_into_buffer = 0;
NSInteger i;
for (i=0;i<dimension;i++) {
offset_into_buffer += c[i] * index[i];
}
assert(offset_into_buffer < [self count]);
// NSInteger myCount = [self count];
// NSLog(@"dimension = %zd",dimension);
// NSLog(@"dimensionSizes[0] = %zd",dimensionSizes[0]);
// NSLog(@"dimensionSizes[1] = %zd",dimensionSizes[0]);
// NSLog(@"count = %zd",myCount);
// NSLog(@"offset_into_buffer = %zd",offset_into_buffer);
// NSLog(@"BCObjectMatrix objectAtIndices [%zd,%zd] offset: %zd",(long)index[0],(long)index[1],offset_into_buffer);
return [buffer objectAtIndex:offset_into_buffer];
}
-(void)setObject:(id)object atIndices:(NSInteger) firstIndex,...; {
// variadic method
// sets object into matrix[firstIndex,...]
// of course, # indices should == dimension
// but we can't check this ourselves
va_list argumentList;
index[0] = firstIndex;
va_start(argumentList, firstIndex); // Start scanning for arguments after firstObject.
NSInteger currentIndex = 1;
while (currentIndex < dimension) {
// As many times as we can get an argument of type "NSInteger"
// that isn't nil, add it to self's contents.
index[currentIndex] = va_arg(argumentList, NSInteger);
assert( index[currentIndex] < dimensionSizes[currentIndex]);
if (index[currentIndex] >= dimensionSizes[currentIndex]) {
NSLog(@"BCObjectMatrix setObject:AtIndices out of bounds index: %zd", (long)currentIndex);
}
currentIndex++;
}
va_end(argumentList);
NSUInteger offset_into_buffer = 0;
NSInteger i;
for (i=0;i<dimension;i++) {
offset_into_buffer += c[i] * index[i];
}
assert(offset_into_buffer < [self count]);
// NSInteger myCount = [self count];
// NSLog(@"dimension = %zd",dimension);
// NSLog(@"dimensionSizes[0] = %zd",dimensionSizes[0]);
// NSLog(@"dimensionSizes[1] = %zd",dimensionSizes[0]);
// NSLog(@"count = %zd",myCount);
// NSLog(@"offset_into_buffer = %zd",offset_into_buffer);
// NSLog(@"BCObjectMatrix setObject:atIndices: [%zd,%zd] offset: %zd",(long)index[0],(long)index[1],offset_into_buffer);
[buffer replaceObjectAtIndex:offset_into_buffer withObject:object];
}
/**
transpose the object matrix, so that m x n matrix becomes n x m
*/
-(BCObjectMatrix *)transpose; {
assert(2 == self.dimension); // can only transpose a 2D matrix
NSArray *transposeSizes = [[NSArray alloc] initWithObjects:
[NSNumber numberWithInteger:self.dimensionSizes[1]],
[NSNumber numberWithInteger:self.dimensionSizes[0]],
nil];
BCObjectMatrix *transposed = [[BCObjectMatrix alloc] initWithDimension:self.dimension andMaxCount:transposeSizes];
for (NSInteger i=0;i<self.dimensionSizes[0];i++) {
for (NSInteger j=0;j<self.dimensionSizes[1];j++) {
[transposed setObject:[self objectAtIndices:i,j] atIndices:j,i];
}
}
return transposed;
}
@end