ArrayList functions for C developers
This minimal projects is aimed to give a simple procedural interface to manage dynamically allocated arraylists in C
Type arrlist
- int size: Number of items currently stored in the arraylist
- int ubound: Current size of allocated memory spaces to accomodate future items
- void items: Array of pointers to stored items
- INIT_SIZE: Number of initially allocated items when the array is instatiated
- GENERIC_ERROR: Generic error return code
- GENERIC_OK: Generic ok return code
- arrlist* arrnew()
- Creates a new, blank, arraylist and returns its pointer
- arrlist* arrnew_size(int initial_size)
- Creates a new, blank, arraylist and returns its pointer with initial size of initial_size
- int additem(arrlist *p, void *pitem)
- Adds a new pitem item to the p arrlist and returns the size after the insertion. Returns -1 if something went wrong
- int delitems(arrlist *p, int position)
- Deletes the items from the p arrlist at the position position until the end. Returns the arraylist's size after the deletion otherwise -1 if something went wrong
- int delitem(arrlist *p, int position)
- Deletes the item from the p arrlist at the position position and compacts the arraylist. Returns the arraylist's size after the deletion otherwise -1 if something went wrong
- int splice(arrlist *s, arrlist *p, int position)
- Splices the arraylist in two arraylists at the position position. s contains the pointer to the arraylist from item 0 to position position -1, p contains the arraylist from item at position position to the end. Returns -1 if something went wrong
- void clear(arrlist *p)
- Destroys the p instance and recreates it returning its new pointer.
arrlist *a = arrnew();
printf("Size %d\r\n", a->size);
arrlist *a = arrnew_size(10);
printf("Size %d\r\n", a->size);
arrlist *a = arrnew();
additem(a, "Item number 1");
arrlist *a = arrnew();
additem(a, "Item number 1");
additem(a, "Item number 2");
additem(a, "Item number 3");
additem(a, "Item number 4");
[...]
delitems(a, 9);
arrlist *a = arrnew();
additem(a, "Item number 1");
additem(a, "Item number 2");
additem(a, "Item number 3");
additem(a, "Item number 4");
[...]
delitem(a, 9);
arrlist *a = arrnew();
arrlist *b = arrnew();
additem(a, "Item number 1");
additem(a, "Item number 2");
additem(a, "Item number 3");
additem(a, "Item number 4");
[...]
splice(a, b, 5);
arrlist *a = arrnew();
additem(a, "Item number 1");
additem(a, "Item number 2");
additem(a, "Item number 3");
additem(a, "Item number 4");
[...]
clear(a);
printf("Size %d\r\n", a->size);