forked from djw/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTMutableNumericData.m
More file actions
239 lines (190 loc) · 7.69 KB
/
Copy pathCPTMutableNumericData.m
File metadata and controls
239 lines (190 loc) · 7.69 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#import "CPTMutableNumericData.h"
#import "CPTExceptions.h"
/// @cond
@interface CPTMutableNumericData()
-(void)commonInitWithData:(NSData *)newData dataType:(CPTNumericDataType)newDataType shape:(NSArray *)shapeArray;
@end
/// @endcond
#pragma mark -
/** @brief An annotated NSMutableData type.
*
* CPTNumericData combines a mutable data buffer with information
* about the data (shape, data type, size, etc.).
* The data is assumed to be an array of one or more dimensions
* of a single type of numeric data. Each numeric value in the array,
* which can be more than one byte in size, is referred to as a "sample".
* The structure of this object is similar to the NumPy ndarray
* object.
**/
@implementation CPTMutableNumericData
/** @property mutableBytes
* @brief Returns a pointer to the data buffer’s contents.
**/
@dynamic mutableBytes;
/** @property shape
* @brief The shape of the data buffer array. Set a new shape to change the size of the data buffer.
*
* The shape describes the dimensions of the sample array stored in
* the data buffer. Each entry in the shape array represents the
* size of the corresponding array dimension and should be an unsigned
* integer encoded in an instance of NSNumber.
**/
@dynamic shape;
#pragma mark -
#pragma mark Factory Methods
/** @brief Creates and returns a new CPTMutableNumericData instance.
* @param newData The data buffer.
* @param newDataType The type of data stored in the buffer.
* @param shapeArray The shape of the data buffer array.
* @return A new CPTMutableNumericData instance.
**/
+(CPTMutableNumericData *)numericDataWithData:(NSData *)newData
dataType:(CPTNumericDataType)newDataType
shape:(NSArray *)shapeArray
{
return [[[CPTMutableNumericData alloc] initWithData:newData
dataType:newDataType
shape:shapeArray]
autorelease];
}
/** @brief Creates and returns a new CPTMutableNumericData instance.
* @param newData The data buffer.
* @param newDataTypeString The type of data stored in the buffer.
* @param shapeArray The shape of the data buffer array.
* @return A new CPTMutableNumericData instance.
**/
+(CPTMutableNumericData *)numericDataWithData:(NSData *)newData
dataTypeString:(NSString *)newDataTypeString
shape:(NSArray *)shapeArray
{
return [[[CPTMutableNumericData alloc] initWithData:newData
dataType:CPTDataTypeWithDataTypeString(newDataTypeString)
shape:shapeArray]
autorelease];
}
#pragma mark -
#pragma mark Init/Dealloc
/** @brief Initializes a newly allocated CPTMutableNumericData object with the provided data. This is the designated initializer.
* @param newData The data buffer.
* @param newDataType The type of data stored in the buffer.
* @param shapeArray The shape of the data buffer array.
* @return The initialized CPTMutableNumericData instance.
**/
-(id)initWithData:(NSData *)newData
dataType:(CPTNumericDataType)newDataType
shape:(NSArray *)shapeArray
{
if ( (self = [super init]) ) {
[self commonInitWithData:newData
dataType:newDataType
shape:shapeArray];
}
return self;
}
/// @cond
-(void)commonInitWithData:(NSData *)newData
dataType:(CPTNumericDataType)newDataType
shape:(NSArray *)shapeArray
{
NSParameterAssert( CPTDataTypeIsSupported(newDataType) );
data = [newData mutableCopy];
dataType = newDataType;
if ( shapeArray == nil ) {
shape = [[NSArray arrayWithObject:[NSNumber numberWithUnsignedInteger:self.numberOfSamples]] retain];
}
else {
NSUInteger prod = 1;
for ( NSNumber *cNum in shapeArray ) {
prod *= [cNum unsignedIntegerValue];
}
if ( prod != self.numberOfSamples ) {
[NSException raise:CPTNumericDataException
format:@"Shape product (%lu) does not match data size (%lu)", (unsigned long)prod, (unsigned long)self.numberOfSamples];
}
shape = [shapeArray copy];
}
}
/// @endcond
#pragma mark -
#pragma mark Accessors
/// @cond
-(void *)mutableBytes
{
return [(NSMutableData *)self.data mutableBytes];
}
-(void)setShape:(NSArray *)newShape
{
if ( newShape != shape ) {
[shape release];
shape = [newShape copy];
NSUInteger sampleCount = 1;
for ( NSNumber *num in shape ) {
sampleCount *= [num unsignedIntegerValue];
}
( (NSMutableData *)data ).length = sampleCount * self.sampleBytes;
}
}
/// @endcond
#pragma mark -
#pragma mark NSMutableCopying
-(id)mutableCopyWithZone:(NSZone *)zone
{
return [[CPTMutableNumericData allocWithZone:zone] initWithData:self.data
dataType:self.dataType
shape:self.shape];
}
#pragma mark -
#pragma mark NSCopying
-(id)copyWithZone:(NSZone *)zone
{
return [[[self class] allocWithZone:zone] initWithData:self.data
dataType:self.dataType
shape:self.shape];
}
#pragma mark -
#pragma mark NSCoding
-(void)encodeWithCoder:(NSCoder *)encoder
{
//[super encodeWithCoder:encoder];
if ( [encoder allowsKeyedCoding] ) {
[encoder encodeObject:self.data forKey:@"data"];
CPTNumericDataType selfDataType = self.dataType;
[encoder encodeInteger:selfDataType.dataTypeFormat forKey:@"CPTMutableNumericData.dataType.dataTypeFormat"];
[encoder encodeInteger:selfDataType.sampleBytes forKey:@"CPTMutableNumericData.dataType.sampleBytes"];
[encoder encodeInteger:selfDataType.byteOrder forKey:@"CPTMutableNumericData.dataType.byteOrder"];
[encoder encodeObject:self.shape forKey:@"shape"];
}
else {
[encoder encodeObject:self.data];
CPTNumericDataType selfDataType = self.dataType;
[encoder encodeValueOfObjCType:@encode(CPTDataTypeFormat) at:&(selfDataType.dataTypeFormat)];
[encoder encodeValueOfObjCType:@encode(NSUInteger) at:&(selfDataType.sampleBytes)];
[encoder encodeValueOfObjCType:@encode(CFByteOrder) at:&(selfDataType.byteOrder)];
[encoder encodeObject:self.shape];
}
}
-(id)initWithCoder:(NSCoder *)decoder
{
if ( (self = [super init]) ) {
NSData *newData;
CPTNumericDataType newDataType;
NSArray *shapeArray;
if ( [decoder allowsKeyedCoding] ) {
newData = [decoder decodeObjectForKey:@"data"];
newDataType = CPTDataType([decoder decodeIntegerForKey:@"CPTMutableNumericData.dataType.dataTypeFormat"],
[decoder decodeIntegerForKey:@"CPTMutableNumericData.dataType.sampleBytes"],
[decoder decodeIntegerForKey:@"CPTMutableNumericData.dataType.byteOrder"]);
shapeArray = [decoder decodeObjectForKey:@"shape"];
}
else {
newData = [decoder decodeObject];
[decoder decodeValueOfObjCType:@encode(CPTDataTypeFormat) at:&(newDataType.dataTypeFormat)];
[decoder decodeValueOfObjCType:@encode(NSUInteger) at:&(newDataType.sampleBytes)];
[decoder decodeValueOfObjCType:@encode(CFByteOrder) at:&(newDataType.byteOrder)];
shapeArray = [decoder decodeObject];
}
[self commonInitWithData:newData dataType:newDataType shape:shapeArray];
}
return self;
}
@end