CocoaDev

Edit AllPages

KTLLMutableArray

This code creates a subclass of NSMutableArray implementing DesignDoublyXORLinkedList for constant-time head/tail operations, at the cost of two pointers per array element (cf one for simple C-array implementations) and O(n) times for general operations.

Aside from reverseObjects, which reverses the order of the array, this subclass has no methods not found in NSMutableArray, so no documentation is provided. http://goo.gl/OeSCu

Note that using an enumerator is the fastest way of traversing all the objects in this class, unlike the standard NSArray implementations.

Feel free to correct errors in this code, add comments, etc. (Any changes you make to the code on this page, however, will be placed under the license described below.) This code has been tested, but not exhaustively, so bugs may still lurk.

– KritTer


Are you sure this is even necessary? The standard NSMutableArray implementation is analogous to the STL deque (double-ended *queue), where head and tail operations are constant-time due to the beginning of the array being somewhere in the middle of an allocated chunk of memory*

Think of this as an intellectual exercise. Specifically, exercise 11.2-8 of Cormen, Leiserson and Rivest’s “Introduction to algorithms”. The interesting feature is the O(1) reverseObjects method.


// // KTLLMutableArray // Doubly-XOR-Linked List NSArray subclass // // The XOR Doubly Linked List takes twice the room of a C-array-using // NSArray subclass to allow very fast head/tail insertion and deletion // Head/tail access/insertion/deletion is O(1), but these operations are O(n) // in general // Reversing the list is an O(1) operation // // Copyright (c) 2002 Chris Purcell. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the “Software”), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. //

#import <Foundation/Foundation.h>

/************************’/ /’ KTLLMutableArray interface ‘/ /’************************/ @interface KTLLMutableArray : NSMutableArray { // Note: offsets range [1, capacity], not [0, capacity) // 0 offset means “no such object” void *memory; unsigned capacity; // Size of memory unsigned uninitialized; // First uninitialized offset unsigned count; // Number of objects stored unsigned head; // Offset of first used node unsigned tail; // Offset of last used node unsigned unused_head; // Offset of first empty node (singly linked) }

@end

/************************’/ /’ Element structure ‘/ /’************************/ struct element { id object; unsigned pn; // prev and next indices XOR-ed together } ;

/************************’/ /’ Object enumerator ‘/ /’************************/ @interface KTLLMutableArrayEnumerator : NSEnumerator { struct element *array; unsigned offset, prev; id collection; }

// initWithArray:firstOffset:forCollection:

@implementation KTLLMutableArrayEnumerator

/************************’/ /’ KTLLMutableArray implementation ‘/ /’************************/ @implementation KTLLMutableArray

// Querying the array

// Working with array elements

// Adding and replacing objects

// Removing objects

} @end


Just started playing with this today. I know that this has been around for a couple of years, but I never had the need for a made from scratch mutable array. I haven’t done any performance tests on this yet, but from what I can see, this should be a nice starting point. I did find a memory bug though. The “memory” for the array was released in the dealloc instance method, but the instance wasn’t. I subclassed this and couldn’t figure out why my subclass wasn’t being deallocated when the subclass’s dealloc method was being called. I guess the moral of the story is, when you can’t find a memory leak, check to make sure the parent class is doing the right thing.

– zootbobbalu