pasteit4me

C

l_list.h.c

  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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 *  l_list.h  - crw
   a simple linked list implementation

conventions:
   l_  all functions here have the prefix
   any variable named l is a list
   any variable named o is a pointer to your object

 */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>

#ifndef NULL
#define NULL 0L
#endif

#ifndef L_LIST
#define L_LIST

typedef struct l_node {
	struct l_node *next;
	struct l_node *prev;
	void *o;
} l_node, *l_nodep;

typedef struct l_list {
	struct l_node *first;
	struct l_node *last;
} l_list;



//create a new list
l_list *l_alloc(void);

//dealloc the list
void l_dealloc(void *l2);

//create a new list with n new items. (NULL TERMINATE THIS!)
l_list *l_nu(void *o2, ...);

//return first element of list
void *l_head(void *l);
//return last element of list
void *l_tail(void *l);

//address of pointer to element i in list
void **l_index(void *l, long);

//enqueue item into list
void l_enqueue(void *l, void *d);
//enqueue n items. (NULL TERMINATE THIS!)
void l_enqn(l_list *l, ...);

//pop an element
void *l_pop(void *l);
//push an element
void l_push(void *l, void *d);

//returns 1 if o is in the list
int l_contains(void *l, void *o);

// # of nodes in list
int l_count(void *l);

//returns a duplicate of the list
l_list *l_copy(void *l);

//deallocate all nodes in the list.  list will still be valid, with 0 elements
void l_forget(void *l);

//concatenate two lists.  
// destroys the second list so requires you to reference your passed variable as a reminder
l_list *l_cat(l_list *a, l_list **b2);


// insert at 0 is equivalent to a push
// insert at 2 adds an element between the second and third elements in the list
// etc
// returns 0 if your index was greater than the list's length
int l_insert_at(struct l_list *l, void *o, long i);


// search list and remove all instances of o.  return # of occurrences
int l_remove(struct l_list *l, void *o);

// remove recurrences of element
int l_uniquify(struct l_list *l, void *o);
// remove all recurrences of all elements (slow)
int l_uniquify_all(l_list *l);


// crash if list is messed up
int l_integrity(struct l_list *l);




/*  internal */void l_map_removeNode(l_list *l, l_node *n);
/*  internal */void l_map_precedeNode(l_list *l, l_node *before, l_node *after);
/*  internal */struct l_node *l_map_n_o_alloc(void *o);





//map macro.
//insert code for f
//iterates over all elements
//use the variable o to refer to the current element
#define l_map(l, f)\
do {\
if (l){\
	struct l_node *l_map_nodevar = ((l_list *)l)->first;\
		l_integrity((l_list *)l);\
		while (l_map_nodevar)\
		{\
			struct l_node *l_map_nextvar = l_map_nodevar->next;\
				void *o = l_map_nodevar->o;\
				{f;}\
					l_map_nodevar = l_map_nextvar;\
		}\
}\
} while(0)


//mapt macro.  just like map macro but with a typecast
//insert code for f
//variable t is the type of pointer your elements are using
//iterates over all elements
//use the variable o to refer to the current element
#define l_mapt(l, t, f)\
do {\
if (l){\
	struct l_node *l_map_nodevar = ((l_list *)l)->first;\
		l_integrity((l_list *)l);\
		while (l_map_nodevar)\
		{\
			struct l_node *l_map_nextvar = l_map_nodevar->next;\
				t *o = (t *)l_map_nodevar->o;\
				{f;}\
        l_map_nodevar = l_map_nextvar;\
		}\
}\
} while(0)


//map remove macro
//put this inside your map macro to insert a new item before/after the current element(o) being examined
//l should be the current list you're mapping over
#define l_map_insert(a, after)\
do {\
  if (after) \
    l_map_precedeNode(l, l_map_n_o_alloc(a), l_map_nextvar); \
  else \
    l_map_precedeNode(l, l_map_n_o_alloc(a), l_map_nodevar); \
} while(0)

//map remove macro
//put this inside your map macro to remove the current item(o) being examined
//l should be the current 
#define l_map_remove(l)\
do {\
  l_map_removeNode(l, l_map_nodevar); \
} while(0)


#endif

Logo by Benalene | Syntax coloring by Pygments