summaryrefslogtreecommitdiffstats
path: root/WWW/Library/Implementation/HTList.h
blob: 93f91473212a2e7aa39d34199c86d9c44f25cb87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*              List object
 *
 *      The list object is a generic container for storing collections
 *      of things in order.
 */
#ifndef HTLIST_H
#define HTLIST_H

#ifndef HTUTILS_H
#include <HTUtils.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif
    typedef struct _HTList HTList;

    struct _HTList {
	void *object;
	HTList *next;
    };

/*	Fast macro to traverse a list.  Call it first with copy of the list
 *	header.  It returns the first object and increments the passed list
 *	pointer.  Call it with the same variable until it returns NULL.
 */
#define HTList_nextObject(me) \
	((me) && ((me) = (me)->next) ? (me)->object : NULL)

/*	Macro to find object pointed to by the head (returns NULL
 *	if list is empty, OR if it doesn't exist - Yuk!)
 */
#define HTList_lastObject(me) \
	((me) && (me)->next ? (me)->next->object : NULL)

/*	Macro to check if a list is empty (or doesn't exist - Yuk!)
*/
#define HTList_isEmpty(me) ((me) ? ((me)->next == NULL) : YES)

/*	Create list.
*/
    extern HTList *HTList_new(void);

/*	Delete list.
*/
    extern void HTList_delete(HTList *me);

/*	Reverse a list.
*/
    extern HTList *HTList_reverse(HTList *start);

/*	Append two lists, making second list empty.
*/
    extern HTList *HTList_appendList(HTList *start,
				     HTList *tail);

/*      Add object to START of list (so it is pointed to by the head).
*/
    extern void HTList_addObject(HTList *me,
				 void *newObject);

/*      Append object to END of list (furthest from the head).
*/
    extern void HTList_appendObject(HTList *me,
				    void *newObject);

/*	Insert an object into the list at a specified position.
 *      If position is 0, this places the object at the head of the list
 *      and is equivalent to HTList_addObject().
 */
    extern void HTList_insertObjectAt(HTList *me,
				      void *newObject,
				      int pos);

/*	Remove specified object from list.
*/
    extern BOOL HTList_removeObject(HTList *me,
				    void *oldObject);

/*	Remove object at a given position in the list, where 0 is the
 *	object pointed to by the head (returns a pointer to the element
 *	(->object) for the object, and NULL if the list is empty, or
 *	if it doesn't exist - Yuk!).
 */
    extern void *HTList_removeObjectAt(HTList *me,
				       int position);

/*	Remove object from START of list (the Last one inserted
 *	via HTList_addObject(), and pointed to by the head).
 */
    extern void *HTList_removeLastObject(HTList *me);

/*	Remove object from END of list (the First one inserted
 *	via HTList_addObject(), and furthest from the head).
 */
    extern void *HTList_removeFirstObject(HTList *me);

/*	Determine total number of objects in the list,
 *	not counting the head.
 */
    extern int HTList_count(HTList *me);

/*	Determine position of an object in the list (a value of 0
 *	means it is pointed to by the head; returns -1 if not found).
 */
    extern int HTList_indexOf(HTList *me,
			      void *object);

/*	Return pointer to the object at a specified position in the list,
 *	where 0 is the object pointed to by the head (returns NULL if
 *	the list is empty, or if it doesn't exist - Yuk!).
 */
    extern void *HTList_objectAt(HTList *me,
				 int position);

/*      Link object to START of list (so it is pointed to by the head).
 *
 *      Unlike HTList_addObject(), it does not malloc memory for HTList entry,
 *	it use already allocated memory which should not be free'd by any
 *	list operations (optimization).
 */
    extern void HTList_linkObject(HTList *me,
				  void *newObject,
				  HTList *newNode);

/*	Unlink object from START of list (the Last one inserted
 *	via HTList_linkObject(), and pointed to by the head).
 *	It does not free memory.
 */
    extern void *HTList_unlinkLastObject(HTList *me);

/*	Unlink specified object from list.
 *	It does not free memory.
 */
    extern BOOL HTList_unlinkObject(HTList *me,
				    void *oldObject);

#ifdef __cplusplus
}
#endif
#endif				/* HTLIST_H */