PXLinkedList Class Reference
| Inherits from | NSObject |
| Conforms to | NSCoding NSCopying NSFastEnumeration PXPooledObject |
| Declared in | PXLinkedList.h PXLinkedList.m |
Overview
A collection data structure which can hold any number of NSObject instances.
Like all collection classes, the PXLinkedList class
increases an object’s retain count when it is added, and decreases the
object’s retain count when it is removed. This behavior can be disabled,
however it is not recommended.
On the surface the PXLinkedList class is structured very similarly to the
native NSArray class, but under the hood it uses a linked list
structure.
The PXLinkedList class is designed to be fast and efficient. It has been
tested and found to be considerably faster than NSArray when
performing the following tasks:
- Adding and removing objects to and from the ends of the list
- Adding and removing objects to and from the middle of the list
- Looping through the list (And much more so when using
PXLinkedListForEachor its counterpartPXLinkedListForEachReverse)
Iterating through a linked list
There are 3 (count ‘em) ways to loop through a linked list:
1) (Recommended) Using Objective-C’s fast enumeration. It is the encouraged way to loop through linked lists. Here’s a code example:
PXLinkedList *list = ...
for (NSObject *item in list)
{
NSLog("Item = %@", item);
}
2) (For optimization only) This is the fastest way to loop through linked lists. (According to tests it could be as fast as looping through a plain C array). The downside is that it’s not as clean as the recommended fast enumeration method and requires you to write a bit more code. Here’s a code example:
PXLinkedList *list = ...
// It's essential that this variable be declared before the loop
NSObject *item = nil;
PXLinkedListForEach(list, item)
{
NSLog("Item = %@", item);
}
We recommend only using this method of iteration for very large lists and/or lists that require one or more iterations every frame (such as a list of all the entities in the world). For short lists, or one-time operations you should stick to the fast enumeration method (#2 above).
3) (Not recommended) The n00bish way. It’s the most obvious way to go but also the slowest. It’s strongly discouraged to loop through a list this way. Here’s an example to show you what not to do:
PXLinkedList *list = ...
NSObject *item = nil;
for (int i = 0; i < list.count; ++i)
{
item = [list objectAtIndex:i];
NSLog("Item %i = %@", i, item);
}
Tasks
-
countThe number of items in the list.
property -
firstObjectThe first object in the list.
property -
lastObjectThe last object in the list.
property -
weakReferences
propertyYESif the list does not retain its elements; otherwiseNO. Default value isNO, as it is advised to keep a retain on the added elements. -
– initWithPooledNodes:Creates a linked list that uses pooled nodes if specified.
-
– initWithWeakReferences:Creates a new linked list that uses pooled nodes and only retains added objects if weakReferences is set to NO.
-
– initWithWeakReferences:usePooledNodes:Creates a new linked list that uses pooled nodes if specified and only retains added objects if weakReferences is set to NO.
-
– addObject:Adds the specified object to the end of list. If weakReferences is set to
NO(default), the object’s retain count is incremented; otherwise the object’s retain count stays the same. -
– insertObject:atIndex:Adds the specified object to the list at the specified index.
-
– addObjectsFromList:Adds all of the objects from the provided list to this list.
-
– setIndex:ofObject:Sets the index of the object in the list, to the index provided. This shifts the data over properly in the process.
-
– removeObject:Removes the specified object from the list.
-
– removeObjectAtIndex:Removes the object at the specified index from the list.
-
– removeLastObjectRemoves the last object in the list (object at index count
- 1). If the list is empty the call is ignored. -
– removeFirstObjectRemoves the first object in the list (object at index
0). If the list is empty the call is ignored. -
– removeAllObjectsRemoves all of the objects in the list, restoring it to its initial state.
-
– removeObjectsInList:Removes all of the objects in the list that are also in the provided list.
-
– swapObject:withObject:Swaps the location of two objects in the list. If either of the parameters aren’t contained in the list, a PXArgumentException is thrown.
-
– swapObjectAtIndex:withObjectAtIndex:Swaps the location of two objects specified their indices in the list. If either of the parameters aren’t contained in the list, or are out of bounds, a PXArgumentException is thrown.
-
– objectAtIndex:Finds and returns the object at the specified position in the list. If the index is out of bounds, a PXArgumentException is thrown.
-
– containsObject:Determines if an object is contained in the list.
-
– indexOfObject:Finds the position in the list of the specified object.
-
– copyReturns a new list containing the same objects as this list, and in the same order. The individual items in the list aren’t duplicated, only their reference is.
-
+ linkedListWithPooledNodes:Creates a linked list with strong references.
-
+ linkedWithWeakReferences:Creates a linked list without using pooled nodes.
-
+ linkedListWithWeakReferences:usePooledNodes:Creates a linked list.
Properties
count
The number of items in the list.
@property (nonatomic, readonly) unsigned countDeclared In
PXLinkedList.hfirstObject
The first object in the list.
@property (nonatomic, readonly) id firstObjectDiscussion
Complexity: O(1)
Declared In
PXLinkedList.hlastObject
The last object in the list.
@property (nonatomic, readonly) id lastObjectDiscussion
Complexity: O(1)
Declared In
PXLinkedList.hClass Methods
linkedListWithPooledNodes:
Creates a linked list with strong references.
+ (PXLinkedList *)linkedListWithPooledNodes:(BOOL)pooledNodesParameters
- pooledNodes
Whether or not too use pooled nodes internally. It’s recommended that this value always be set to
YES.
Return Value
The created linked list.
Example:
PXLinkedList *list = [PXLinkedList linkedListWithPooledNodes:YES];
// list will use pooled nodes
list = [PXLinkedList linkedListWithPooledNodes:NO];
// list will not use pooled nodes
Declared In
PXLinkedList.mlinkedListWithWeakReferences:usePooledNodes:
Creates a linked list.
+ (PXLinkedList *)linkedListWithWeakReferences:(BOOL)weakReferences usePooledNodes:(BOOL)pooledNodesParameters
- weakReferences
YESif the list should not retain added elements;NOif it should. Setting this toYESis only useful in very rare circumstances and should be used with caution. The default value isNO.
- pooledNodes
Whether or not too use pooled nodes internally. It’s recommended that this value always be set to
YES.
Return Value
The created linked list.
Example:
PXLinkedList *list = [PXLinkedList linkedWithWeakReferences:YES usePooledNodes:YES];
// list will use weak references (will not retain objects added to it) and will use pooled nodes.
Declared In
PXLinkedList.mlinkedWithWeakReferences:
Creates a linked list without using pooled nodes.
+ (PXLinkedList *)linkedWithWeakReferences:(BOOL)weakReferencesParameters
- weakReferences
YESif the list should not retain added elements;NOif it should. Setting this toYESis only useful in very rare circumstances and should be used with caution. The default value isNO.
Return Value
The created linked list.
Example:
PXLinkedList *list = [PXLinkedList linkedWithWeakReferences:YES];
// list will use weak references (will not retain objects added to it)
Declared In
PXLinkedList.mInstance Methods
addObject:
Adds the specified object to the end of list. If
weakReferences is set to NO (default), the
object’s retain count is incremented; otherwise the object’s retain count
stays the same.
- (void)addObject:(id)objectParameters
- object
The object to add to the end of the list. Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4]; // add1's retain count is 1 PXLinkedList *list = [PXLinkedList new]; // list will use pooled nodes, and keeps a retain on added objects. [list addObject:add1]; // add1's retain count is 2 [list release]; // add1's retain count is 1 list = [[PXLinkedList alloc] initWithWeakReferences:YES]; // list will use pooled nodes, and will not keep a retain on added objects. [list addObject:add1]; // add1's retain count is 1
Discussion
Complexity: O(1)
See Also
Declared In
PXLinkedList.maddObjectsFromList:
Adds all of the objects from the provided list to this list.
- (void)addObjectsFromList:(PXLinkedList *)otherListDiscussion
If weakReferences is set to NO (default), the
objects' retain counts are incremented; otherwise the object’s retain count
stays the same.
Complexity: O(n)
Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4];
PXPoint *add2 = [[PXPoint alloc] initWithX:2 y:5];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
PXLinkedList *list = [PXLinkedList new];
PXLinkedList *otherList = [PXLinkedList new];
// list will use pooled nodes, and keeps a retain on added objects.
[list addObject:add1];
[otherList addObject:add2];
// add1 has a retain count of 2, and an index of 0 in list
// add2 has a retain count of 2, and an index of 0 in otherList
[list addObjectsFromList:otherList];
// add1 has a retain count of 2, and an index of 0 in list
// add2 has a retain count of 3, and an index of 1 in list, and 0 in other list
[list release];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 2, and an index of 0 in otherList
[otherList release];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
list = [[PXLinkedList alloc] initWithWeakReferences:YES];
otherList = [[PXLinkedList alloc] initWithWeakReferences:YES];
// list will use pooled nodes, and will not keep a retain on added objects.
[list addObject:add1];
[otherList addObject:add2];
// add1 has a retain count of 1, and an index of 0 in list
// add2 has a retain count of 1, and an index of 0 in otherList
[list addObjectsFromList:otherList];
// add1 has a retain count of 1, and an index of 0 in list
// add2 has a retain count of 1, and an index of 1 in list, and 0 in other list
See Also
Declared In
PXLinkedList.mcontainsObject:
Determines if an object is contained in the list.
- (BOOL)containsObject:(id)objectParameters
- object
The object for which to check existence in the list.
Return Value
YES If the object exists in the list; otherwise
NO.
Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4];
// add1 has a retain count of 1, and no index
PXLinkedList *list = [PXLinkedList new];
// list will use pooled nodes, and keeps a retain on added objects.
[list addObject:add1];
// add1 has a retain count of 2, and an index of 0
BOOL doesContain = [list containsObject:add1];
// doesContain == YES
Discussion
Complexity: O(n)
Declared In
PXLinkedList.mcopy
Returns a new list containing the same objects as this list, and in the same order. The individual items in the list aren’t duplicated, only their reference is.
- (id)copyDiscussion
Note that the new list also retains each of the objects as long as
weakReferences is set to NO (default).
Declared In
PXLinkedList.mindexOfObject:
Finds the position in the list of the specified object.
- (int)indexOfObject:(id)objectParameters
- object
The object for which to check existence in the list.
Return Value
If the object is contained in the list, its index; otherwise
-1.
Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4];
// add1 has a retain count of 1, and no index
PXLinkedList *list = [PXLinkedList new];
// list will use pooled nodes, and keeps a retain on added objects.
[list addObject:add1];
// add1 has a retain count of 2, and an index of 0
int index = [list indexOfObject:add1];
// index == 0
Discussion
Complexity: O(n)
Declared In
PXLinkedList.minitWithPooledNodes:
Creates a linked list that uses pooled nodes if specified.
- (id)initWithPooledNodes:(BOOL)pooledNodesParameters
- pooledNodes
Whether or not too use pooled nodes internally. It’s recommended that this value always be set to
YES.Example:
PXLinkedList *list = [[PXLinkedList alloc] initWithPooledNodes:YES]; // list will use pooled nodes.
Discussion
Equivalent to calling:
[linkedList initWithWeakReferences:NO usePooledNodes:pooledNodes]
Declared In
PXLinkedList.minitWithWeakReferences:
Creates a new linked list that uses pooled nodes and only retains added objects if weakReferences is set to NO.
- (id)initWithWeakReferences:(BOOL)weakReferencesParameters
- weakReferences
YESif the list should not retain added elements;NOif it should. Setting this toYESis only useful in very rare circumstances and should be used with caution. The default value isNO.Example:
PXLinkedList *list = [[PXLinkedList alloc] initWithWeakReferences:NO]; // list will use pooled nodes, and keeps a retain on added objects.
Discussion
Equivalent to calling:
[linkedList initWithWeakReferences:weakReferences usePooledNodes:YES]
Declared In
PXLinkedList.minitWithWeakReferences:usePooledNodes:
Creates a new linked list that uses pooled nodes if specified and only retains added objects if weakReferences is set to NO.
- (id)initWithWeakReferences:(BOOL)weakReferences usePooledNodes:(BOOL)pooledNodesParameters
- weakReferences
YESif the list should not retain added elements;NOif it should. Setting this toYESis only useful in very rare circumstances and should be used with caution. The default value isNO.
- pooledNodes
Whether or not too use pooled nodes internally. It’s recommended that this value always be set to
YES.Example:
PXLinkedList *list = [[PXLinkedList alloc] initWithWeakReferences:NO usePooledNodes:YES]; // list will use pooled nodes, and keeps a retain on added objects.
Declared In
PXLinkedList.minsertObject:atIndex:
Adds the specified object to the list at the specified index.
- (void)insertObject:(id)object atIndex:(int)indexOfObjectParameters
- object
The object to add to the front of the. Must be a descendant of the
NSObjectclass.
- index
The index to add the object to. Must be a value between 0 and count.
Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4]; PXPoint *add2 = [[PXPoint alloc] initWithX:2 y:5]; // add1 has a retain count of 1, and no index // add2 has a retain count of 1, and no index PXLinkedList *list = [PXLinkedList new]; // list will use pooled nodes, and keeps a retain on added objects. [list insertObject:add1 atIndex:0]; [list insertObject:add2 atIndex:0]; // add1 has a retain count of 2, and an index of 1 // add2 has a retain count of 2, and an index of 0 [list release]; // add1 has a retain count of 1, and no index // add2 has a retain count of 1, and no index list = [[PXLinkedList alloc] initWithWeakReferences:YES]; // list will use pooled nodes, and will not keep a retain on added objects. [list insertObject:add1 atIndex:0]; [list insertObject:add2 atIndex:0]; // add1 has a retain count of 1, and an index of 1 // add2 has a retain count of 1, and an index of 0
Discussion
If weakReferences is set to NO (default), the
object’s retain count is incremented; otherwise the object’s retain count
stays the same.
If an object already exists at the specified index, all of the objects whose indices are greater then the specified, are shifted up by one position.
Complexity: O(n)
Declared In
PXLinkedList.mobjectAtIndex:
Finds and returns the object at the specified position in the list. If the index is out of bounds, a PXArgumentException is thrown.
- (id)objectAtIndex:(int)indexOfObjectParameters
- index
The index from which to look up the return object. Must be a value between
0and count- 1
Return Value
The object at the specified index.
Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4];
PXPoint *add2 = [[PXPoint alloc] initWithX:2 y:5];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
PXLinkedList *list = [PXLinkedList new];
// list will use pooled nodes, and keeps a retain on added objects.
[list addObject:add1];
[list addObject:add2];
// add1 has a retain count of 2, and an index of 0
// add2 has a retain count of 2, and an index of 1
PXPoint *foundObject = (PXPoint *)[list objectAtIndex:0];
// foundObject == add1
Discussion
Complexity: O(n)
Declared In
PXLinkedList.mremoveAllObjects
Removes all of the objects in the list, restoring it to its initial state.
- (void)removeAllObjectsDiscussion
If weakReferences is set to NO (default), the
objects' retain counts are decremented; otherwise the object’s retain count
stays the same.
Complexity: O(n)
Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4];
PXPoint *add2 = [[PXPoint alloc] initWithX:2 y:5];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
PXLinkedList *list = [PXLinkedList new];
// list will use pooled nodes, and keeps a retain on added objects.
[list addObject:add1];
[list addObject:add2];
// add1 has a retain count of 2, and an index of 0
// add2 has a retain count of 2, and an index of 1
[list removeAllObjects];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
[list release];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
list = [[PXLinkedList alloc] initWithWeakReferences:YES];
// list will use pooled nodes, and will not keep a retain on added objects.
[list addObject:add1];
[list addObject:add2];
// add1 has a retain count of 1, and an index of 0
// add2 has a retain count of 1, and an index of 1
[list removeAllObjects];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
See Also
Declared In
PXLinkedList.mremoveFirstObject
Removes the first object in the list (object at index 0). If
the list is empty the call is ignored.
- (void)removeFirstObjectDiscussion
If weakReferences is set to NO (default), the
object’s retain count is decremented; otherwise the object’s retain count
stays the same.
Complexity: O(1)
Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4];
PXPoint *add2 = [[PXPoint alloc] initWithX:2 y:5];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
PXLinkedList *list = [PXLinkedList new];
// list will use pooled nodes, and keeps a retain on added objects.
[list addObject:add1];
[list addObject:add2];
// add1 has a retain count of 2, and an index of 0
// add2 has a retain count of 2, and an index of 1
[list removeFirstObject];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 2, and an index of 0
[list release];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
list = [[PXLinkedList alloc] initWithWeakReferences:YES];
// list will use pooled nodes, and will not keep a retain on added objects.
[list addObject:add1];
[list addObject:add2];
// add1 has a retain count of 1, and an index of 0
// add2 has a retain count of 1, and an index of 1
[list removeFirstObject];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and an index of 0
See Also
Declared In
PXLinkedList.mremoveLastObject
Removes the last object in the list (object at index
count - 1). If the list is empty the call is ignored.
- (void)removeLastObjectDiscussion
If weakReferences is set to NO (default), the
object’s retain count is decremented; otherwise the object’s retain count
stays the same.
Complexity: O(1)
Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4];
PXPoint *add2 = [[PXPoint alloc] initWithX:2 y:5];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
PXLinkedList *list = [PXLinkedList new];
// list will use pooled nodes, and keeps a retain on added objects.
[list addObject:add1];
[list addObject:add2];
// add1 has a retain count of 2, and an index of 0
// add2 has a retain count of 2, and an index of 1
[list removeLastObject];
// add1 has a retain count of 2, and an index of 0
// add2 has a retain count of 1, and no index
[list release];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
list = [[PXLinkedList alloc] initWithWeakReferences:YES];
// list will use pooled nodes, and will not keep a retain on added objects.
[list addObject:add1];
[list addObject:add2];
// add1 has a retain count of 1, and an index of 0
// add2 has a retain count of 1, and an index of 1
[list removeLastObject];
// add1 has a retain count of 1, and an index of 0
// add2 has a retain count of 1, and no index
See Also
Declared In
PXLinkedList.mremoveObject:
Removes the specified object from the list.
- (void)removeObject:(id)objectParameters
- object
The object to remove from the list.
Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4]; PXPoint *add2 = [[PXPoint alloc] initWithX:2 y:5]; // add1 has a retain count of 1, and no index // add2 has a retain count of 1, and no index PXLinkedList *list = [PXLinkedList new]; // list will use pooled nodes, and keeps a retain on added objects. [list insertObject:add1 atIndex:0]; [list insertObject:add2 atIndex:0]; // add1 has a retain count of 2, and an index of 1 // add2 has a retain count of 2, and an index of 0 [list removeObject:add2]; // add1 has a retain count of 2, and an index of 0 // add2 has a retain count of 1, and no index [list release]; // add1 has a retain count of 1, and no index // add2 has a retain count of 1, and no index list = [[PXLinkedList alloc] initWithWeakReferences:YES]; // list will use pooled nodes, and will not keep a retain on added objects. [list insertObject:add1 atIndex:0]; [list insertObject:add2 atIndex:0]; // add1 has a retain count of 1, and an index of 1 // add2 has a retain count of 1, and an index of 0 [list removeObject:add2]; // add1 has a retain count of 1, and an index of 0 // add2 has a retain count of 1, and no index
Discussion
If the object isn’t contained in the list the call is simply ignored. otherwise all of the objects after the index of the specified object are shifted down by one to fill the gap.
If weakReferences is set to NO (default), the
object’s retain count is decremented; otherwise the object’s retain count
stays the same.
Complexity: O(n)
See Also
Declared In
PXLinkedList.mremoveObjectAtIndex:
Removes the object at the specified index from the list.
- (void)removeObjectAtIndex:(int)indexOfObjectParameters
- index
The index from which to remove the object.
indexmust be be a value between 0 and count – 1Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4]; PXPoint *add2 = [[PXPoint alloc] initWithX:2 y:5]; // add1 has a retain count of 1, and no index // add2 has a retain count of 1, and no index PXLinkedList *list = [PXLinkedList new]; // list will use pooled nodes, and keeps a retain on added objects. [list addObject:add1]; [list addObject:add2]; // add1 has a retain count of 2, and an index of 0 // add2 has a retain count of 2, and an index of 1 [list removeObjectAtIndex:0]; // add1 has a retain count of 1, and no index // add2 has a retain count of 2, and an index of 0 [list release]; // add1 has a retain count of 1, and no index // add2 has a retain count of 1, and no index list = [[PXLinkedList alloc] initWithWeakReferences:YES]; // list will use pooled nodes, and will not keep a retain on added objects. [list addObject:add1]; [list addObject:add2]; // add1 has a retain count of 1, and an index of 0 // add2 has a retain count of 1, and an index of 1 [list removeObjectAtIndex:0]; // add1 has a retain count of 1, and no index // add2 has a retain count of 1, and an index of 0
Discussion
If the object isn’t contained in the list the call is simply ignored;
otherwise all of the objects following the oject at index are
shifted down by one.
If weakReferences is set to NO (default), the
object’s retain count is decremented; otherwise the object’s retain count
stays the same.
Complexity: O(n)
See Also
Declared In
PXLinkedList.mremoveObjectsInList:
Removes all of the objects in the list that are also in the provided list.
- (void)removeObjectsInList:(PXLinkedList *)otherListDiscussion
If weakReferences is set to NO (default), the
objects' retain counts are decremented; otherwise the object’s retain count
stays the same.
Complexity: O(n * m)
Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4];
PXPoint *add2 = [[PXPoint alloc] initWithX:2 y:5];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
PXLinkedList *list = [PXLinkedList new];
PXLinkedList *otherList = [PXLinkedList new];
// list will use pooled nodes, and keeps a retain on added objects.
[list addObject:add1];
[list addObject:add2];
// add1 has a retain count of 2, and an index of 0
// add2 has a retain count of 2, and an index of 1
[otherList addObject:add2];
// add1 has a retain count of 2, and an index of 0
// add2 has a retain count of 3, and an index of 1
[list removeObjectsInList:otherList];
// add1 has a retain count of 2, and no index
// add2 has a retain count of 2, and no index
[list release];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
[otherList release];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
list = [[PXLinkedList alloc] initWithWeakReferences:YES];
otherList = [[PXLinkedList alloc] initWithWeakReferences:YES];
// list will use pooled nodes, and will not keep a retain on added objects.
[list addObject:add1];
[list addObject:add2];
[otherList addObject:add2];
// add1 has a retain count of 1, and an index of 0
// add2 has a retain count of 1, and an index of 1
[list removeObjectsInList:otherList];
// add1 has a retain count of 1, and no index
// add2 has a retain count of 1, and no index
See Also
Declared In
PXLinkedList.msetIndex:ofObject:
Sets the index of the object in the list, to the index provided. This shifts the data over properly in the process.
- (void)setIndex:(int)newIndexOfObject ofObject:(id)objectDiscussion
Example:
PXPoint *point0 = [PXPoint pointWithX:3 y:4];
PXPoint *point1 = [PXPoint pointWithX:2 y:5];
PXPoint *point2 = [PXPoint pointWithX:1 y:6];
PXPoint *point3 = [PXPoint pointWithX:0 y:7];
// point0 has no index
// point1 has no index
// point2 has no index
// point3 has no index
PXLinkedList *list = [[PXLinkedList alloc] init];
[list addObject:point0];
[list addObject:point1];
[list addObject:point2];
[list addObject:point3];
// point0 has an index of 0
// point1 has an index of 1
// point2 has an index of 2
// point3 has an index of 3
[list setIndex:2 ofObject:point0];
// point0 has an index of 2
// point1 has an index of 0
// point2 has an index of 1
// point3 has an index of 3
[list release];
See Also
Declared In
PXLinkedList.mswapObject:withObject:
Swaps the location of two objects in the list. If either of the parameters aren’t contained in the list, a PXArgumentException is thrown.
- (void)swapObject:(id)object1 withObject:(id)object2Parameters
- object1
The object to swap with
object2
- object2
The object to swap with
object1Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4]; PXPoint *add2 = [[PXPoint alloc] initWithX:2 y:5]; // add1 has a retain count of 1, and no index // add2 has a retain count of 1, and no index PXLinkedList *list = [PXLinkedList new]; // list will use pooled nodes, and keeps a retain on added objects. [list addObject:add1]; [list addObject:add2]; // add1 has a retain count of 2, and an index of 0 // add2 has a retain count of 2, and an index of 1 [list swapObject:add1 withObject:add2]; // add1 has a retain count of 2, and an index of 1 // add2 has a retain count of 2, and an index of 0
Declared In
PXLinkedList.mswapObjectAtIndex:withObjectAtIndex:
Swaps the location of two objects specified their indices in the list. If either of the parameters aren’t contained in the list, or are out of bounds, a PXArgumentException is thrown.
- (void)swapObjectAtIndex:(int)index1 withObjectAtIndex:(int)index2Parameters
- index1
The index of the object to swap with the object at
index2. Must be a value between0and count- 1.
- index2
The index of the object to swap with the object at
index1. Must be a value between0and count- 1.Example:
PXPoint *add1 = [[PXPoint alloc] initWithX:3 y:4]; PXPoint *add2 = [[PXPoint alloc] initWithX:2 y:5]; // add1 has a retain count of 1, and no index // add2 has a retain count of 1, and no index PXLinkedList *list = [PXLinkedList new]; // list will use pooled nodes, and keeps a retain on added objects. [list addObject:add1]; [list addObject:add2]; // add1 has a retain count of 2, and an index of 0 // add2 has a retain count of 2, and an index of 1 [list swapObjectAtIndex:0 withObjectAtIndex:1]; // add1 has a retain count of 2, and an index of 1 // add2 has a retain count of 2, and an index of 0
Declared In
PXLinkedList.m