Zen API

- Home
- Help Topics
- Types by Category
- All Types
- Files

All Classes Files Functions Variables Typedefs Macros Groups Pages
Public Member Functions
kArrayList Class Reference
kApi/Data

## Description

Represents a list implemented with a dynamic array.

kArrayList represents a dynamic, array-based list of objects or values. The kArrayList constructor accepts a kType value that determines the type of items that will be stored in the list. The list will automatically grow as new items are added.

kStatus ArrayListExample()
{
kArrayList list = kNULL;
k32s values[] = { 1, 2, 3, 5, 7, 9 };
kSize i;
kTry
{
//create a list that can store 32-bit integers
kTest(kArrayList_Construct(&list, kTypeOf(k32s), 0, kNULL));
//add some initial items to the list
for (i = 0; i < kCountOf(values); ++i)
{
kTest(kArrayList_Add(list, &values[i]);
}
//print some information about the list and its items
printf("Item type: %s\n", kType_Name(kArrayList_ItemType(list)));
printf("Count: %u\n", (k32u) kArrayList_Count(list));
for (i = 0; i < kArrayList_Count(list); ++i)
{
//the kArrayList_As_ macro can be used to get a list item and cast it to the desired type;
//this is equivalent to *(k32s*)kArrayList_At(list, i);
k32s value = kArrayList_As_(list, i, k32s);
printf("Item %u: %d\n", (k32u)i, value);
}
}
kFinally
{
kObject_Destroy(list);
kEndFinally();
}
return kOK;
}

For lists that contain objects (e.g. kImage) as opposed to values (e.g. k32s), the objects are not automatically destroyed when the list is destroyed. To recursively destroy both the list and the list items, use kObject_Dispose.

Inheritance diagram for kArrayList:

## Public Member Functions
kStatus | kArrayList_Add (kArrayList list, const void *item)
| Adds the specified item to the end of the list. More...
kStatus | kArrayList_AddCount (kArrayList list, kSize count)
| Increases the list count by the specified amount. More...
kStatus | kArrayList_Allocate (kArrayList list, kType itemType, kSize initialCapacity)
| Reallocates the list item buffer. More...
kStatus | kArrayList_Append (kArrayList list, const void *items, kSize count)
| Appends the specified items to the list. More...
kStatus | kArrayList_Assign (kArrayList list, kArrayList source)
| Performs a shallow copy of the source list. More...
void * | kArrayList_At (kArrayList list, kSize index)
| Returns a pointer to the specified item in the list buffer. More...
kStatus | kArrayList_Attach (kArrayList list, void *items, kType itemType, kSize capacity)
| Attaches the list object to an external buffer. More...
kSize | kArrayList_Capacity (kArrayList list)
| Returns the number of elements for which space has been allocated. More...
kStatus | kArrayList_Clear (kArrayList list)
| Sets the count of list items to zero. More...
kStatus | kArrayList_Construct (kArrayList *list, kType itemType, kSize initialCapacity, kAlloc allocator)
| Constructs a kArrayList object. More...
kSize | kArrayList_Count (kArrayList list)
| Returns the current count of items in the list. More...
void * | kArrayList_Data (kArrayList list)
| Returns a pointer to the list item buffer. More...
kSize | kArrayList_DataSize (kArrayList list)
| Returns the total size of list data (Count x ItemSize), in bytes. More...
kStatus | kArrayList_Import (kArrayList list, const void *items, kType itemType, kSize count)
| Copies the specified items into the list, replacing existing contents. More...
kStatus | kArrayList_Insert (kArrayList list, kSize before, const void *item)
| Inserts an item into the list at the specified position. More...
kStatus | kArrayList_Item (kArrayList list, kSize index, void *item)
| Gets the value of an item. More...
kSize | kArrayList_ItemSize (kArrayList list)
| Returns the list element size. More...
kType | kArrayList_ItemType (kArrayList list)
| Returns the list element type. More...
kStatus | kArrayList_Purge (kArrayList list)
| Disposes any elements in the list and sets the count of list items to zero. More...
kStatus | kArrayList_Remove (kArrayList list, kSize index, void *item)
| Removes an item from the list at the specified index. More...
kStatus | kArrayList_RemoveCount (kArrayList list, kSize count)
| Decreases the list count by the specified amount. More...
kStatus | kArrayList_Reserve (kArrayList list, kSize capacity)
| Ensures that capacity is reserved for at least the specified number of list items. More...
kStatus | kArrayList_Resize (kArrayList list, kSize count)
| Sets the current count of list items to the specified value. More...
kStatus | kArrayList_SetItem (kArrayList list, kSize index, const void *item)
| Sets the value of an item. More...
kStatus | kArrayList_Zero (kArrayList list)
| Sets the memory for all list elements to zero. More...
kSize | kCollection_Count (kCollection collection)
| Gets the collection element count. More...
kIterator | kCollection_GetIterator (kCollection collection)
| Returns an iterator to the first element in the collection. More...
kBool | kCollection_HasNext (kCollection collection, kIterator iterator)
| Determines whether a collection has another item. More...
kType | kCollection_ItemType (kCollection collection)
| Gets the collection element type. More...
void * | kCollection_Next (kCollection collection, kIterator *iterator)
| Gets the next collection element and then advances the iterator. More...
kAlloc | kObject_Alloc (kObject object)
| Gets the memory allocator associated with this object. More...
kStatus | kObject_Clone (kObject *object, kObject source, kAlloc allocator)
| Constructs a new object by copying an existing object, including any aggregated child elements. More...
kStatus | kObject_Destroy (kObject object)
| Destroys the object. More...
kStatus | kObject_Dispose (kObject object)
| Destroys the object and any aggregated child elements. More...
kBool | kObject_Equals (kObject object, kObject other)
| Determines whether the object is equal to another object. More...
kSize | kObject_HashCode (kObject object)
| Gets a hash code representing the state of this object. More...
kBool | kObject_Is (kObject object, kType type)
| Determines whether this object is an instance of the specified type. More...
kBool | kObject_IsShared (kObject object)
| Reports whether the object is currently shared (reference count greater than one). More...
kStatus | kObject_SetPool (kObject object, kObjectPool pool)
| Sets the object pool associated with this object. More...
kStatus | kObject_Share (kObject object)
| Increments the reference count associated with this object. More...
kSize | kObject_Size (kObject object)
| Estimates the memory consumed by this object, including any aggregated child elements. More...
kType | kObject_Type (kObject object)
| Returns the type of the object. More...

## Member Function Documentation

kStatus kArrayList_Add | ( | kArrayList | list,
|  | const void * | item
| ) |  |

Adds the specified item to the end of the list.

Parameters

  list | List object.
  item | Pointer to item that will be copied (by value) into the list.

Returns
  Operation status.

kStatus kArrayList_AddCount | ( | kArrayList | list,
|  | kSize | count
| ) |  |

Increases the list count by the specified amount.

Increases list capacity if necessary; existing list items are preserved. New items are not initialized.

Parameters

  list | List object.
  count | Amount to add to the existing count.

Returns
  Operation status.

kStatus kArrayList_Allocate | ( | kArrayList | list,
|  | kType | itemType,
|  | kSize | initialCapacity
| ) |  |

Reallocates the list item buffer.

Parameters

  list | List object.
  itemType | Type of list element.
  initialCapacity | Capacity initially reserved for list items.

Returns
  Operation status.

kStatus kArrayList_Append | ( | kArrayList | list,
|  | const void * | items,
|  | kSize | count
| ) |  |

Appends the specified items to the list.

Parameters

  list | List object.
  items | Item buffer.
  count | Count of list items.

Returns
  Operation status.

kStatus kArrayList_Assign | ( | kArrayList | list,
|  | kArrayList | source
| ) |  |

Performs a shallow copy of the source list.

Source items are copied by value; if the source list contains objects, the object handles are copied but the objects are not cloned.

Parameters

  list | List object.
  source | Source list to be copied.

Returns
  Operation status.

void * kArrayList_At | ( | kArrayList | list,
|  | kSize | index
| ) |  |

Returns a pointer to the specified item in the list buffer.

Parameters

  list | List object.
  index | Item index.

Returns
  Pointer to list element.

kStatus kArrayList_Attach | ( | kArrayList | list,
|  | void * | items,
|  | kType | itemType,
|  | kSize | capacity
| ) |  |

Attaches the list object to an external buffer.

The list count is set to the same value as the list capacity argument.

Parameters

  list | List object.
  items | Item buffer.
  itemType | Type of list element.
  capacity | List capacity.

Returns
  Operation status.

kSize kArrayList_Capacity | ( | kArrayList | list | ) |

Returns the number of elements for which space has been allocated.

Parameters

  list | List object.

Returns
  List capacity.

kStatus kArrayList_Clear | ( | kArrayList | list | ) |

Sets the count of list items to zero.

Parameters

  list | List object.

Returns
  Operation status.

kStatus kArrayList_Construct | ( | kArrayList * | list,
|  | kType | itemType,
|  | kSize | initialCapacity,
|  | kAlloc | allocator
| ) |  |

Constructs a kArrayList object.

Parameters

  list | Receives constructed list object.
  itemType | Type of list element.
  initialCapacity | Capacity initially reserved for list items.
  allocator | Memory allocator (or kNULL for default).

Returns
  Operation status.

kSize kArrayList_Count | ( | kArrayList | list | ) |

Returns the current count of items in the list.

Parameters

  list | List object.

Returns
  Current count of items.

void * kArrayList_Data | ( | kArrayList | list | ) |

Returns a pointer to the list item buffer.

Parameters

  list | List object.

Returns
  Pointer to list items.

kSize kArrayList_DataSize | ( | kArrayList | list | ) |

Returns the total size of list data (Count x ItemSize), in bytes.

Parameters

  list | List object.

Returns
  Data size, in bytes.

kStatus kArrayList_Import | ( | kArrayList | list,
|  | const void * | items,
|  | kType | itemType,
|  | kSize | count
| ) |  |

Copies the specified items into the list, replacing existing contents.

The list count is set to the value of the count argument.

Parameters

  list | List object.
  items | Item buffer.
  itemType | Type of list element.
  count | Count of list items.

Returns
  Operation status.

kStatus kArrayList_Insert | ( | kArrayList | list,
|  | kSize | before,
|  | const void * | item
| ) |  |

Inserts an item into the list at the specified position.

Increases list capacity, if necessary.

Parameters

  list | List object.
  before | Item will be inserted before the item at this index.
  item | Pointer to item that will be copied (by value) into the list.

Returns
  Operation status.

kStatus kArrayList_Item | ( | kArrayList | list,
|  | kSize | index,
|  | void * | item
| ) |  |

Gets the value of an item.

Parameters

  list | List object.
  index | Item index.
  item | Destination for item that will be copied (by value) from the list.

Returns
  Operation status.

kSize kArrayList_ItemSize | ( | kArrayList | list | ) |

Returns the list element size.

Parameters

  list | List object.

Returns
  Item size, in bytes.

kType kArrayList_ItemType | ( | kArrayList | list | ) |

Returns the list element type.

Parameters

  list | List object.

Returns
  Item type.

kStatus kArrayList_Purge | ( | kArrayList | list | ) |

Disposes any elements in the list and sets the count of list items to zero.

Parameters

  list | List object.

Returns
  Operation status.

kStatus kArrayList_Remove | ( | kArrayList | list,
|  | kSize | index,
|  | void * | item
| ) |  |

Removes an item from the list at the specified index.

Parameters

  list | List object.
  index | Item at this index will be removed from the list.
  item | Destination for the removed item (copied by value, can be null).

Returns
  Operation status.

kStatus kArrayList_RemoveCount | ( | kArrayList | list,
|  | kSize | count
| ) |  |

Decreases the list count by the specified amount.

Parameters

  list | List object.
  count | Amount to remove from the existing count.

Returns
  Operation status.

kStatus kArrayList_Reserve | ( | kArrayList | list,
|  | kSize | capacity
| ) |  |

Ensures that capacity is reserved for at least the specified number of list items.

Existing list items are preserved.

Parameters

  list | List object.
  capacity | List capacity, in items.

Returns
  Operation status.

kStatus kArrayList_Resize | ( | kArrayList | list,
|  | kSize | count
| ) |  |

Sets the current count of list items to the specified value.

Increases list capacity if necessary; existing list items are preserved.

Parameters

  list | List object.
  count | List size, in items.

Returns
  Operation status.

kStatus kArrayList_SetItem | ( | kArrayList | list,
|  | kSize | index,
|  | const void * | item
| ) |  |

Sets the value of an item.

Parameters

  list | List object.
  index | Item index.
  item | Item that will be copied into the list.

Returns
  Operation status.

kStatus kArrayList_Zero | ( | kArrayList | list | ) |

Sets the memory for all list elements to zero.

Parameters

  list | List object.

Returns
  Operation status.

kSize kCollection_Count | ( | kCollection | collection | ) |
| inherited

Gets the collection element count.

Parameters

  collection | Collection object.

Returns
  Item count.

kIterator kCollection_GetIterator | ( | kCollection | collection | ) |
| inherited

Returns an iterator to the first element in the collection.

Parameters

  collection | Collection object.

Returns
  Iterator.

kBool kCollection_HasNext | ( | kCollection | collection,
|  | kIterator | iterator
| ) |  |
| inherited

Determines whether a collection has another item.

Parameters

  collection | Collection object.
  iterator | Collection iterator.

Returns
  kTRUE if the collection has a next element.

kType kCollection_ItemType | ( | kCollection | collection | ) |
| inherited

Gets the collection element type.

Parameters

  collection | Collection object.

Returns
  Item type.

void * kCollection_Next | ( | kCollection | collection,
|  | kIterator * | iterator
| ) |  |
| inherited

Gets the next collection element and then advances the iterator.

Parameters

  collection | Collection object.
  iterator | Pointer to collection iterator.

Returns
  Pointer to next collection element.

kAlloc kObject_Alloc | ( | kObject | object | ) |
| inherited

Gets the memory allocator associated with this object.

Most objects are constructed with an allocator, which is used to allocate the memory required by the object. Objects retain a reference to this allocator to enable further allocations and to free memory when the object is destroyed.

Parameters

  object | Object.

Returns
  Memory allocator.

See also
  kAlloc, kObject_Alloc_

kStatus kObject_Clone | ( | kObject * | object,
|  | kObject | source,
|  | kAlloc | allocator
| ) |  |
| inherited

Constructs a new object by copying an existing object, including any aggregated child elements.

If the source object is an object collection (e.g. kArrayList<kString>), any aggregated child objects are also cloned. In this case, the kObject_Dispose method can be used to free the cloned collection and its associated elements.

This method will fail if the source object (or an aggregated child element) does not support cloning.

Parameters

  object | Receives the constructed object.
  source | Source object.
  allocator | Memory allocator (or kNULL for default).

Returns
  Operation status.

See also
  kObject_Dispose

kStatus kObject_Destroy | ( | kObject | object | ) |
| inherited

Destroys the object.

The kObject_Destroy method destroys the object itself and any resources that are owned by the object. See Object Destruction for more information.

When an object is destroyed (or disposed), its reference count is decremented. The object is only truly destroyed when the reference count reaches zero. See Reference Counting for more information.

Parameters

  object | Object (or kNULL).

Returns
  Operation status.

See also
  Object Destruction, Reference Counting

kStatus kObject_Dispose | ( | kObject | object | ) |
| inherited

Destroys the object and any aggregated child elements.

The kObject_Dispose method destroys the object itself, any resources that are owned by the object, and if the object represents a collection of objects, any child objects in the collection. See Object Destruction for more information.

When an object is destroyed (or disposed), its reference count is decremented. The object is only truly destroyed when the reference count reaches zero. See Reference Counting for more information.

Parameters

  object | Object (or kNULL).

Returns
  Operation status.

See also
  Object Destruction, Reference Counting

kBool kObject_Equals | ( | kObject | object,
|  | kObject | other
| ) |  |
| inherited

Determines whether the object is equal to another object.

By default, objects are compared by reference; objects are considered equal if the given handles refer to the same object instance. However, some classes override the Equals method to provide a more meaningful comparison (e.g. kString).

Parameters

  object | Object.
  other | Object for comparison.

Returns
  kTRUE if the objects are equal.

kSize kObject_HashCode | ( | kObject | object | ) |
| inherited

Gets a hash code representing the state of this object.

By default, objects return a hash code based on the object handle value. However, some classes override the HashCode method to provide a more useful hash (e.g. kString).

Parameters

  object | Object.

Returns
  Hash code.

kBool kObject_Is | ( | kObject | object,
|  | kType | type
| ) |  |
| inherited

Determines whether this object is an instance of the specified type.

This function compares the type of this object with the given type. An object is considered to be an instance of a given type if a) the type represents a class and this object inherits from (or instantiates) that class, or b) the type represents an interface and this object implements the interface.

Parameters

  object | Object.
  type | Type.

Returns
  kTRUE if the object is of the specified type; otherwise kFALSE.

See also
  kObject_Is_, kType_Is

kBool kObject_IsShared | ( | kObject | object | ) |
| inherited

Reports whether the object is currently shared (reference count greater than one).

Objects are initialized with a reference count of one. The kObject_Share method can be used to increment the reference count. The kObject_Destroy and kObject_Dispose methods decrease the reference count, and when the reference count reaches zero, the object is actually destroyed/disposed.

This method can be used to determine if the reference count of an object is currently greater than one.

This method is thread-safe.

Parameters

  object | Object.

Returns
  kTRUE if the object is shared; kFALSE otherwise.

See also
  Reference Counting, kObject_Share

kStatus kObject_SetPool | ( | kObject | object,
|  | kObjectPool | pool
| ) |  |
| inherited

Sets the object pool associated with this object.

Object pools can be used to implement custom lifecycle management. If an object has an assigned pool, then the kObjectPool_Reclaim method will be called just prior to destruction, to provide an opportunity for the object to be reclaimed.

Parameters

  object | Object.
  pool | Pool object (or kNULL to clear the pool assignment).

Returns
  Operation status.

kStatus kObject_Share | ( | kObject | object | ) |
| inherited

Increments the reference count associated with this object.

This method is thread-safe.

Parameters

  object | Object.

Returns
  Operation status.

See also
  reference-counting

kSize kObject_Size | ( | kObject | object | ) |
| inherited

Estimates the memory consumed by this object, including any aggregated child elements.

This method can be optionally overridden by kObject-derived classes to report the amount of memory consumed by an object. The default implementation reports only the size of the class instance (additional allocations performed by the class are excluded).

Parameters

  object | Object.

Returns
  Object size, in bytes.

kType kObject_Type | ( | kObject | object | ) |
| inherited

Returns the type of the object.

Each object is an instance of a specific class type. The type handle returned by this function can be used to learn about the class.

Parameters

  object | Object.

Returns
  Type.

See also
  kType, kObject_Type_

The documentation for this class was generated from the following file:

- Data/kArrayList.h