• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSData and Nested Arrays
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSData and Nested Arrays


  • Subject: Re: NSData and Nested Arrays
  • From: Gordon Apple <email@hidden>
  • Date: Tue, 06 Mar 2007 15:00:14 -0600

    I'm currently using NSMutableData as a dynamic array in a class called
EDDynamicArray.  "elSize" is the size of the struct that make up the
elements of the array.

-(id)initWithSizeOfElement:(int)elementSize
{
    [self init];
    elSize = elementSize;
    array = [NSMutableData dataWithCapacity:256];    //    Well, it had to
be something
    [array retain];
    return self;
}

-(void)append:(void*)element
{
    [array appendBytes:element
               length:elSize];
}

-(void)element:(void*)element
             atIndex:(int)index
{
    //    Make a circular buffer out of it by using a (correct) modulo
operator on the index.
    int i = index % [self numEl];
    if(i < 0) i += [self numEl];    //    Stupid % operator has to be
retrained to be a real modulo.
    NSRange range = NSMakeRange(elSize * i, elSize);

    [array getBytes:element range:range];
}



> I'm using RubyCocoa as a file parser. It returns an NSArray of NSArrays each
> containing NSDecimalNumbers in the following format:
> ((1,2,3), (4,5,6), (7,8,9))
>
> This obviously has quite a footprint and iteration is expensive. I have
> previously converted it into a primitive c array, but really need it in
> NSData format for archiving purposes and smaller memory footprint.
>
> I've looked at the documentation, but am unclear as to how I should make the
> conversion. I'd like to index and enumerate through the NSData object as
> though each sequence of bytes were a struct:
>
> struct vertex {
>   GLfloat x, y, z;
> };
>
> equivalent to this fictional accessor-type:
>
> Vector *v = [NSData bytesAtIndex:index*sizeof(vertex)];
>
>
>
> Any advice is much appreciated.
>

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: "You can create a window on a secondary thread."
  • Next by Date: Cooperative threading questions
  • Previous by thread: NSData and Nested Arrays
  • Next by thread: Change password on DMG
  • Index(es):
    • Date
    • Thread