1 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
\r
2 * This file is part of clib library
\r
3 * Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
6 * of this software and associated documentation files (the "Software"), to deal
\r
7 * in the Software without restriction, including without limitation the rights
\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
9 * copies of the Software, and to permit persons to whom the Software is
\r
10 * furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
22 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
\r
27 new_clib_slist(clib_destroy fn_d, clib_compare fn_c){
\r
28 struct clib_slist *pSlist = (struct clib_slist*)malloc(sizeof(struct clib_slist));
\r
29 pSlist->head = (struct clib_slist_node*)0;
\r
30 pSlist->destruct_fn = fn_d;
\r
31 pSlist->compare_fn = fn_c;
\r
36 delete_clib_slist( struct clib_slist *pSlist){
\r
37 while(pSlist->size != 0) {
\r
38 remove_clib_slist ( pSlist, 0 );
\r
44 push_back_clib_slist( struct clib_slist *pSlist, void *elem, size_t elem_size){
\r
46 struct clib_slist_node* current = (struct clib_slist_node*)0;
\r
47 struct clib_slist_node* new_node = (struct clib_slist_node*)0;
\r
49 new_node = (struct clib_slist_node*)malloc(sizeof(struct clib_slist_node));
\r
51 new_node->elem = new_clib_object ( elem, elem_size );
\r
52 if ( ! new_node->elem )
\r
53 return CLIB_SLIST_INSERT_FAILED;
\r
54 new_node->next = (struct clib_slist_node*)0;
\r
56 if ( pSlist->head == (struct clib_slist_node*)0 ) {
\r
57 pSlist->head = new_node;
\r
59 return CLIB_ERROR_SUCCESS;
\r
61 current = pSlist->head;
\r
62 while ( current->next != (struct clib_slist_node*)0 )
\r
63 current = current->next;
\r
64 current->next = new_node;
\r
67 return CLIB_ERROR_SUCCESS;
\r
70 pvt_remove_clib_list ( struct clib_slist *pSlist, struct clib_slist_node* pSlistNode ) {
\r
72 get_raw_clib_object(pSlistNode->elem, &elem);
\r
73 if ( pSlist->destruct_fn) {
\r
74 (pSlist->destruct_fn)(elem);
\r
75 delete_clib_object ( pSlistNode->elem );
\r
78 delete_clib_object ( pSlistNode->elem );
\r
83 remove_clib_slist( struct clib_slist *pSlist, int pos ) {
\r
86 struct clib_slist_node* current = pSlist->head;
\r
87 struct clib_slist_node* temp = (struct clib_slist_node*)0;
\r
89 if ( pos > pSlist->size ) return;
\r
92 pSlist->head = current->next;
\r
93 pvt_remove_clib_list(pSlist, current);
\r
97 for ( i = 1; i < pos - 1; i++)
\r
98 current = current->next;
\r
100 temp = current->next;
\r
101 current->next = current->next->next;
\r
102 pvt_remove_clib_list ( pSlist, temp );
\r
107 insert_clib_slist(struct clib_slist *pSlist, int pos, void *elem, size_t elem_size) {
\r
109 struct clib_slist_node* current = pSlist->head;
\r
110 struct clib_slist_node* new_node = (struct clib_slist_node*)0;
\r
113 new_node = (struct clib_slist_node*)malloc(sizeof(struct clib_slist_node));
\r
114 new_node->elem = new_clib_object ( elem, elem_size );
\r
115 if ( ! new_node->elem ) {
\r
117 return CLIB_SLIST_INSERT_FAILED;
\r
119 new_node->next = pSlist->head;
\r
120 pSlist->head = new_node;
\r
122 return CLIB_ERROR_SUCCESS;
\r
125 if ( pos >= pSlist->size + 1 ) {
\r
126 return push_back_clib_slist ( pSlist, elem, elem_size );
\r
129 for ( i = 1; i < pos - 1; i++) {
\r
130 current = current->next;
\r
132 new_node = (struct clib_slist_node*)malloc(sizeof(struct clib_slist_node));
\r
133 new_node->elem = new_clib_object ( elem, elem_size );
\r
134 if ( ! new_node->elem ) {
\r
136 return CLIB_SLIST_INSERT_FAILED;
\r
139 new_node->next = current->next;
\r
140 current->next = new_node;
\r
143 return CLIB_ERROR_SUCCESS;
\r
146 for_each_clib_slist (struct clib_slist *pSlist, void (*fn)(void* )) {
\r
148 struct clib_slist_node* current = pSlist->head;
\r
149 while ( current != (struct clib_slist_node*)0 ) {
\r
150 get_raw_clib_object(current->elem, &elem);
\r
153 current = current->next;
\r
157 find_clib_slist (struct clib_slist *pSlist, void* find_value, void**out_value) {
\r
158 struct clib_slist_node* current = pSlist->head;
\r
159 while ( current != (struct clib_slist_node*)0 ) {
\r
160 get_raw_clib_object(current->elem, out_value);
\r
161 if ((pSlist->compare_fn)(find_value,*out_value) != 0){
\r
164 free ( *out_value );
\r
165 current = current->next;
\r