|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* List object
|
|
Packit |
f574b8 |
*
|
|
Packit |
f574b8 |
* The list object is a generic container for storing collections
|
|
Packit |
f574b8 |
* of things in order.
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
#ifndef HTLIST_H
|
|
Packit |
f574b8 |
#define HTLIST_H
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
#ifndef HTUTILS_H
|
|
Packit |
f574b8 |
#include <HTUtils.h>
|
|
Packit |
f574b8 |
#endif
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
#ifdef __cplusplus
|
|
Packit |
f574b8 |
extern "C" {
|
|
Packit |
f574b8 |
#endif
|
|
Packit |
f574b8 |
typedef struct _HTList HTList;
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
struct _HTList {
|
|
Packit |
f574b8 |
void *object;
|
|
Packit |
f574b8 |
HTList *next;
|
|
Packit |
f574b8 |
};
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Fast macro to traverse a list. Call it first with copy of the list
|
|
Packit |
f574b8 |
* header. It returns the first object and increments the passed list
|
|
Packit |
f574b8 |
* pointer. Call it with the same variable until it returns NULL.
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
#define HTList_nextObject(me) \
|
|
Packit |
f574b8 |
((me) && ((me) = (me)->next) ? (me)->object : NULL)
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Macro to find object pointed to by the head (returns NULL
|
|
Packit |
f574b8 |
* if list is empty, OR if it doesn't exist - Yuk!)
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
#define HTList_lastObject(me) \
|
|
Packit |
f574b8 |
((me) && (me)->next ? (me)->next->object : NULL)
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Macro to check if a list is empty (or doesn't exist - Yuk!)
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
#define HTList_isEmpty(me) ((me) ? ((me)->next == NULL) : YES)
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Create list.
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern HTList *HTList_new(void);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Delete list.
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern void HTList_delete(HTList *me);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Reverse a list.
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern HTList *HTList_reverse(HTList *start);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Append two lists, making second list empty.
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern HTList *HTList_appendList(HTList *start,
|
|
Packit |
f574b8 |
HTList *tail);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Add object to START of list (so it is pointed to by the head).
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern void HTList_addObject(HTList *me,
|
|
Packit |
f574b8 |
void *newObject);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Append object to END of list (furthest from the head).
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern void HTList_appendObject(HTList *me,
|
|
Packit |
f574b8 |
void *newObject);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Insert an object into the list at a specified position.
|
|
Packit |
f574b8 |
* If position is 0, this places the object at the head of the list
|
|
Packit |
f574b8 |
* and is equivalent to HTList_addObject().
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern void HTList_insertObjectAt(HTList *me,
|
|
Packit |
f574b8 |
void *newObject,
|
|
Packit |
f574b8 |
int pos);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Remove specified object from list.
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern BOOL HTList_removeObject(HTList *me,
|
|
Packit |
f574b8 |
void *oldObject);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Remove object at a given position in the list, where 0 is the
|
|
Packit |
f574b8 |
* object pointed to by the head (returns a pointer to the element
|
|
Packit |
f574b8 |
* (->object) for the object, and NULL if the list is empty, or
|
|
Packit |
f574b8 |
* if it doesn't exist - Yuk!).
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern void *HTList_removeObjectAt(HTList *me,
|
|
Packit |
f574b8 |
int position);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Remove object from START of list (the Last one inserted
|
|
Packit |
f574b8 |
* via HTList_addObject(), and pointed to by the head).
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern void *HTList_removeLastObject(HTList *me);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Remove object from END of list (the First one inserted
|
|
Packit |
f574b8 |
* via HTList_addObject(), and furthest from the head).
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern void *HTList_removeFirstObject(HTList *me);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Determine total number of objects in the list,
|
|
Packit |
f574b8 |
* not counting the head.
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern int HTList_count(HTList *me);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Determine position of an object in the list (a value of 0
|
|
Packit |
f574b8 |
* means it is pointed to by the head; returns -1 if not found).
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern int HTList_indexOf(HTList *me,
|
|
Packit |
f574b8 |
void *object);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Return pointer to the object at a specified position in the list,
|
|
Packit |
f574b8 |
* where 0 is the object pointed to by the head (returns NULL if
|
|
Packit |
f574b8 |
* the list is empty, or if it doesn't exist - Yuk!).
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern void *HTList_objectAt(HTList *me,
|
|
Packit |
f574b8 |
int position);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Link object to START of list (so it is pointed to by the head).
|
|
Packit |
f574b8 |
*
|
|
Packit |
f574b8 |
* Unlike HTList_addObject(), it does not malloc memory for HTList entry,
|
|
Packit |
f574b8 |
* it use already allocated memory which should not be free'd by any
|
|
Packit |
f574b8 |
* list operations (optimization).
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern void HTList_linkObject(HTList *me,
|
|
Packit |
f574b8 |
void *newObject,
|
|
Packit |
f574b8 |
HTList *newNode);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Unlink object from START of list (the Last one inserted
|
|
Packit |
f574b8 |
* via HTList_linkObject(), and pointed to by the head).
|
|
Packit |
f574b8 |
* It does not free memory.
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern void *HTList_unlinkLastObject(HTList *me);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
/* Unlink specified object from list.
|
|
Packit |
f574b8 |
* It does not free memory.
|
|
Packit |
f574b8 |
*/
|
|
Packit |
f574b8 |
extern BOOL HTList_unlinkObject(HTList *me,
|
|
Packit |
f574b8 |
void *oldObject);
|
|
Packit |
f574b8 |
|
|
Packit |
f574b8 |
#ifdef __cplusplus
|
|
Packit |
f574b8 |
}
|
|
Packit |
f574b8 |
#endif
|
|
Packit |
f574b8 |
#endif /* HTLIST_H */
|