Using / saving an array of floats in Cocoa

In c, if I needed a float array (for example), I would just define a fixed size and allocate it, and I could access each element for mathematical operations. However, I want my arrays to be volatile, as they will constantly grow in size (as the application works, and the array can easily exceed 10,000+ elements), and the idea of ​​NSMutableArray sounds great. However (if I understand correctly) it only stores objects, I will have to collapse my numbers into NSNumber and then put them in an array. This is like a ridiculous amount of overhead to just store an array of floats (or double or integers).

On the other hand, I see that there are flout attributes for Core Data, but I do not see how I could access this in the same way (array [index]). Am I missing something? Should all weightlifting be performed in arrays with a fixed size c, are there ways to use foundation classes that I have not come across, or are there ways to access basic data objects, for example, access to an array?

+3
source share
2 answers

nothing is missing here; there is no formal objc interface for arrays of c-scalar types.

A simple way (as mentioned above) is to use std::vectorand then serialize / deserialize using a mechanism such as CF / NS-Data.

std::vector objc, :

/* MONDoubleArray.h */

/* by using pimpl, i'm assuming you are not building everything as objc++ */
struct t_MONDoubleArray_data;

@interface MONDoubleArray : NSObject < NSCoding, NSCopying, NSMutableCopying >
{
    t_MONDoubleArray_data* data;
}

- (double)doubleAtIndex;
- (void)setDoubleAtiIndex:(double)index;
- (NSUInteger)count;

/*...*/

@end

/* MONDoubleArray.mm */

struct t_MONDoubleArray_data {
    std::vector<double> array;
};

@implementation MONDoubleBuffer

- (id)init
{
    self = [super init];
    if (0 != self) {
    /* remember your c++ error handling (e.g., handle exceptions here) */
        array = new t_MONDoubleArray_data;
        if (0 == array) {
            [self release];
            return 0;
        }
    }
    return self;
}

/*...more variants...*/

- (void)dealloc
{
    delete array;
    [super dealloc];
}

- (NSData *)dataRepresentationOfDoubleData { /*...*/ }
- (void)setDoubleDataFromDataRepresentation:(NSData *)data { /*...*/ }

/*...*/

@end

objc .

CF/NS_MutableArray , ( ):

@interface MONFloatBuffer : NSObject
{
    NSMutableArray * floats;
}

@end

@implementation MONFloatBuffer

- (id)init
{
    self = [super init];
    if (0 != self) {
        CFAllocatorRef allocator = 0; /* default */
        CFIndex capacity = 0; /* resizable */
        /* you could implement some of this, if you wanted */
        const CFArrayCallBacks callBacks = { 0 /* version */ , 0 /* retain */ , 0 /* release */ , 0 /* copyDescription */ , 0 /* equal */ };
        floats = (NSMutableArray*)CFArrayCreateMutable(allocator, capacity, &callBacks);
        // now we can read/write pointer sized values to `floats`,
        // and the values won't be passed to CFRetain/CFRelease.
    }
    return self;
}

@end

, . ... NSPointerArray ... - , . . , .

+1

NSNumbers NSMutableArrays. , .

++ (STL). vector .

STL Objective-C Objective-C ++... .m .mm, #import , std::vector.

NSMutableData, , , (float).

+1

Source: https://habr.com/ru/post/1793130/


All Articles