CObject
CCArrayList Struct Reference

Array implementation of CIList. More...

#include <CCArrayList.h>

+ Inheritance diagram for CCArrayList:

Public Member Functions

const struct CCArrayList_VTableCCArrayList_GetVTable ()
 
CError CCArrayList (struct CCArrayList *self, size_t element_size, size_t max_size)
 
CError CCArrayListStatic (struct CCArrayList *self, size_t element_size, size_t max_size, void *memory)
 
CIListError CIList_Add (struct CIList *self, void *element)
 
CIListError CIList_AddAt (struct CIList *self, void *element, size_t index)
 
CIListError CIList_Get (struct CIList *self, void *element, size_t index)
 
CIListError CIList_Remove (struct CIList *self, void *element, size_t index)
 
void CIList_Clear (struct CIList *self)
 
size_t CIList_Size (struct CIList *self)
 
size_t CIList_MaxSize (struct CIList *self)
 

Data Fields

struct CObject cobject
 
struct CIList cilist
 
struct {
   char *   list_base
 
   unsigned char *   list_mask
 
   size_t   max_size
 
   size_t   current_size
 
   size_t   element_size
 
   size_t   add_index
 
   CBool   is_static
 
_
 
struct CInterface interface
 

Detailed Description

See also
CCListIterator

Implements a non resizable, copy by value, list using an array as the underlying data structure. Below is an example code showing adding items to a list which can hold chars, and has a length of 3.

struct CCArrayList list;
CCArrayList(&list, sizeof(unsigned char), 3);
unsigned char test_val = 0;
CIList_Add(&list.cIList, &test_val);
++test_val;
CIList_Add(&list.cIList, &test_val);
++test_val;
CIList_Add(&list.cIList, &test_val);
CIList_Get(&list.cIList, &test_val, 0);
printf("test val at index 0 is %d", test_val);
CIList_Get(&list.cIList, &test_val, 1);
printf("test val at index 1 is %d", test_val);
CIList_Get(&list.cIList, &test_val, 2);
printf("test val at index 2 is %d", test_val);
CDestroy(&list);

Valid indices into the array list are [0, 1, ..., n-1] where n is the length of the list.
Since this is a copy by value list, all inputs to the list point to the memory location where data will be copied from/to.
In the above example, the list was created to copy only one byte (the size of an unsigned char). When adding/removing elements, the address of the char was provided, because the input points to the memory location which will be copied from/to.

Attention
  • To call inherited methods from CIList, use as input to first argument:
    • CCArrayList::cIList

Constructor & Destructor Documentation

CError CCArrayList ( struct CCArrayList self,
size_t  element_size,
size_t  max_size 
)
Class Constructor

Creates an array list using CMalloc (defined in Class.h) to allocate space. The created list is not resizable and uses an array as the back end data structure to implement the list methods. This is a copy by value list, so all inputs to the list point to a memory location where data is copied from/to.

Parameters
selfThe list.
element_sizeThe size of elements being added/removed from the list.
max_sizeThe maximum number of elements which can be in the list at one time.
Returns
Error code.
COBJ_OK: on successful construction.
COBJ_ALLOC_FAIL: on failure to allocate memory for list.

Member Function Documentation

CError CCArrayListStatic ( struct CCArrayList self,
size_t  element_size,
size_t  max_size,
void *  memory 
)
Class Constructor
Creates an array list using the memory block provided as input. The size

of the memory block for different element_size and max_size lists can be resolved at compile time using the macro CCARRAY_LIST_SIZE(). For example, to make a list with the max size of three elements, and each element being the size of an int:

struct CCArrayList list;
char list_memory[CCARRAY_LIST_SIZE(sizeof(int), 3)];
CCArrayListStatic(&list, sizeof(int), 3, list_memory);

Also, take a look at CCArrayList().

Parameters
selfThe list.
element_sizeThe size of elements being copied into/out of the list.
max_sizeThe maximum number of elements in the list.
Returns
Always returns COBJ_OK.
CIListError CIList_Add ( struct CIList self,
void *  element 
)
inherited

Add parameter element, by copy, to the first empty spot in the list. If there are no empty spots, the list will be resized or an error returned.

Parameters
selfThe list.
elementA pointer to the data which will be copied into the list.
Returns
  • CILIST_OK: The element was inserted into the list
  • CILIST_ERR_FULL: No room in the list, and if a resize was attempted it failed. the element wasn't added.
CIListError CIList_AddAt ( struct CIList self,
void *  element,
size_t  index 
)
inherited

Insert the element into the specefied index of the list. If there is already something at that index in the list, it gets overwritten.

Parameters
selfThe list.
elementPointer to the data that is copied into the list.
indexThe index in the list to copy the data.
Returns
  • CILIST_OK: Element was inserted.
  • CILIST_ERR_INDEX: Index out of bounds.
void CIList_Clear ( struct CIList self)
inherited

Reset the list to a completely empty state. All data within the list is lost.

Parameters
selfThe list.
CIListError CIList_Get ( struct CIList self,
void *  element,
size_t  index 
)
inherited

Get an element from the list at the specified index. The element is not removed from the list.

Parameters
selfThe list.
elementData will be copied from the list into the location pointed to by this. If a NULL pointer is provided, no data is copied.
indexThe location in the list to get an item from.
Returns
  • CILIST_OK: Retrieved the element.
  • CILIST_ERR_EMPTY: Nothing at the location given by parameter index.
  • CILIST_ERR_INDEX: The index is out of bounds, not a valid index.
size_t CIList_MaxSize ( struct CIList self)
inherited

Poll the currently maximum available space in the list.

Parameters
selfThe list.
Returns
The maximum available space in the list.
CIListError CIList_Remove ( struct CIList self,
void *  element,
size_t  index 
)
inherited

Remove an element from the list at the specified index.

Parameters
selfThe list.
elementPointer to location where data removed from the list will be copied into. If this is a NULL pointer, no data is copied.
indexIndex in the list to remove data.
Returns
  • CILIST_OK: Element removed from given index.
  • CILIST_ERR_EMPTY: No element at the given index.
  • CILIST_ERR_INDEX: Index is out of bounds.
size_t CIList_Size ( struct CIList self)
inherited

Poll the current size of the list.

Parameters
selfThe list.
Returns
Number of items currently in the list.

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