list.h
Go to the documentation of this file.
1 /* vim: set ts=2 expandtab: */
2 /**
3  * @file list.h
4  * @brief Commotion double linked-list implementation
5  *
6  * @author Josh King (jheretic), jking@chambana.net
7  *
8  * @internal
9  * Created 11/14/2013 11:22:20 AM
10  * Compiler gcc/g++
11  * Organization The Open Technology Institute
12  * Copyright Copyright (c) 2013, Josh King
13  *
14  * This file is part of Commotion, Copyright (c) 2013, Josh King
15  *
16  * Commotion is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as
18  * published by the Free Software Foundation, either version 3 of the
19  * License, or (at your option) any later version.
20  *
21  * Commotion is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with Commotion. If not, see <http://www.gnu.org/licenses/>.
28  *
29  * =====================================================================================
30  */
31 #ifndef _LIST_H
32 #define _LIST_H
33 #include <stdlib.h>
34 #include <stddef.h>
35 #include <stdint.h>
36 #include "debug.h"
37 #include "extern/halloc.h"
38 
39 typedef struct _listnode_t _listnode_t;
40 
41 /* Type "list" declaration macros */
42 #define _DECLARE_LIST(L) typedef struct __attribute__((packed)) \
43  { co_obj_t _header; uint##L##_t _len; _listnode_t *_first; _listnode_t *_last; } co_list##L##_t; \
44  int co_list##L##_alloc(co_obj_t *output); co_obj_t *\
45  co_list##L##_create(void);
46 
47 _DECLARE_LIST(16);
48 _DECLARE_LIST(32);
49 
50 /**
51  * @brief return length (number of objects) of given list
52  * @param list list object
53  */
54 size_t co_list_length(co_obj_t *list);
55 
56 /**
57  * @brief return first element of given list
58  * @param list list object
59  */
60 co_obj_t * co_list_get_first(const co_obj_t *list);
61 
62 /**
63  * @brief return last element of given list
64  * @param list list object
65  */
66 co_obj_t * co_list_get_last(const co_obj_t *list);
67 
68 /**
69  * @brief process list with given iterator function
70  * @param list list object to process
71  * @param iter iterator function
72  * @param context additional arguments to iterator
73  */
74 co_obj_t * co_list_parse(co_obj_t *list, co_iter_t iter, void *context);
75 
76 /**
77  * @brief determine if list contains specified item
78  * @param list list object to process
79  * @param item item to search for
80  */
81 int co_list_contains(co_obj_t *list, co_obj_t *item);
82 
83 /**
84  * @brief insert new item in list before specified item
85  * @param list list object to process
86  * @param new_obj item to insert
87  * @param this_obj item to insert before
88  */
89 int co_list_insert_before(co_obj_t *list, co_obj_t *new_obj, \
90  co_obj_t *this_obj);
91 
92 /**
93  * @brief insert new item in list before specified item without the list managing the item's memory
94  * @param list list object to process
95  * @param new_obj item to insert
96  * @param this_obj item to insert before
97  */
98 int co_list_insert_before_unsafe(co_obj_t *list, co_obj_t *new_obj, \
99  co_obj_t *this_obj);
100 
101 /**
102  * @brief insert new item in list after specified item
103  * @param list list object to process
104  * @param new_obj item to insert
105  * @param this_obj item to insert after
106  */
107 int co_list_insert_after(co_obj_t *list, co_obj_t *new_obj, co_obj_t *this_obj);
108 
109 /**
110  * @brief insert new item in list after specified item without the list managing the item's memory
111  * @param list list object to process
112  * @param new_obj item to insert
113  * @param this_obj item to insert after
114  */
115 int co_list_insert_after_unsafe(co_obj_t *list, co_obj_t *new_obj, co_obj_t *this_obj);
116 
117 /**
118  * @brief insert new item at beginning of list
119  * @param list list object to process
120  * @param new_obj item to insert
121  */
122 int co_list_prepend(co_obj_t *list, co_obj_t *new_obj);
123 
124 /**
125  * @brief insert new item at beginning of list without the list managing the item's memory
126  * @param list list object to process
127  * @param new_obj item to insert
128  */
129 int co_list_prepend_unsafe(co_obj_t *list, co_obj_t *new_obj);
130 
131 /**
132  * @brief insert new item at end of list
133  * @param list list object to process
134  * @param new_obj item to insert
135  */
136 int co_list_append(co_obj_t *list, co_obj_t *new_obj);
137 
138 /**
139  * @brief insert new item at end of list without the list managing the item's memory
140  * @param list list object to process
141  * @param new_obj item to insert
142  */
143 int co_list_append_unsafe(co_obj_t *list, co_obj_t *new_obj);
144 
145 /**
146  * @brief delete specified item from list
147  * @param list list object to process
148  * @param item item to delete
149  */
151 
152 /**
153  * @brief return item at specified position in list
154  * @param list list object to process
155  * @param index number of item to return
156  */
157 co_obj_t *co_list_element(co_obj_t *list, const unsigned int index);
158 
159 /**
160  * @brief dump raw representation of list
161  * @param output output buffer
162  * @param olen length of output buffer
163  * @param list list object to process
164  */
165 size_t co_list_raw(char *output, const size_t olen, const co_obj_t *list);
166 
167 /**
168  * @brief import list from raw representation
169  * @param list target pointer to new list object
170  * @param input input buffer
171  * @param ilen length of input buffer
172  */
173 size_t co_list_import(co_obj_t **list, const char *input, const size_t ilen);
174 
175 /**
176  * @brief print list with indent
177  * @param list list object to print
178  * @param indent level of indent
179  */
180 void co_list_print_indent(co_obj_t *list, int indent);
181 
182 /**
183  * @brief print list
184  * @param list list object to print
185  */
186 int co_list_print(co_obj_t *list);
187 
188 #endif
int co_list_prepend_unsafe(co_obj_t *list, co_obj_t *new_obj)
insert new item at beginning of list without the list managing the item's memory
Definition: list.c:409
co_obj_t * co_list_parse(co_obj_t *list, co_iter_t iter, void *context)
process list with given iterator function
Definition: list.c:233
debug macros for the Commotion daemon
int co_list_insert_before(co_obj_t *list, co_obj_t *new_obj, co_obj_t *this_obj)
insert new item in list before specified item
Definition: list.c:273
size_t co_list_length(co_obj_t *list)
return length (number of objects) of given list
Definition: list.c:214
size_t co_list_import(co_obj_t **list, const char *input, const size_t ilen)
import list from raw representation
Definition: list.c:562
void co_list_print_indent(co_obj_t *list, int indent)
print list with indent
Definition: list.c:667
int co_list_contains(co_obj_t *list, co_obj_t *item)
determine if list contains specified item
Definition: list.c:258
int co_list_insert_before_unsafe(co_obj_t *list, co_obj_t *new_obj, co_obj_t *this_obj)
insert new item in list before specified item without the list managing the item's memory ...
Definition: list.c:303
size_t co_list_raw(char *output, const size_t olen, const co_obj_t *list)
dump raw representation of list
Definition: list.c:503
Definition: obj.h:131
co_obj_t * co_list_element(co_obj_t *list, const unsigned int index)
return item at specified position in list
Definition: list.c:483
int co_list_append_unsafe(co_obj_t *list, co_obj_t *new_obj)
insert new item at end of list without the list managing the item's memory
Definition: list.c:441
co_obj_t * co_list_delete(co_obj_t *list, co_obj_t *item)
delete specified item from list
Definition: list.c:457
int co_list_insert_after_unsafe(co_obj_t *list, co_obj_t *new_obj, co_obj_t *this_obj)
insert new item in list after specified item without the list managing the item's memory ...
Definition: list.c:363
int co_list_prepend(co_obj_t *list, co_obj_t *new_obj)
insert new item at beginning of list
Definition: list.c:393
co_obj_t * co_list_get_last(const co_obj_t *list)
return last element of given list
Definition: list.c:162
int co_list_insert_after(co_obj_t *list, co_obj_t *new_obj, co_obj_t *this_obj)
insert new item in list after specified item
Definition: list.c:333
int co_list_append(co_obj_t *list, co_obj_t *new_obj)
insert new item at end of list
Definition: list.c:425
Definition: list.c:47
co_obj_t * co_list_get_first(const co_obj_t *list)
return first element of given list
Definition: list.c:117
int co_list_print(co_obj_t *list)
print list
Definition: list.c:680