]> git.ozlabs.org Git - ccan/blobdiff - junkcode/dongre.avinash@gmail.com-clibutils/src/c_slist.c
junkcode: upload via website.
[ccan] / junkcode / dongre.avinash@gmail.com-clibutils / src / c_slist.c
diff --git a/junkcode/dongre.avinash@gmail.com-clibutils/src/c_slist.c b/junkcode/dongre.avinash@gmail.com-clibutils/src/c_slist.c
new file mode 100644 (file)
index 0000000..fc8dfce
--- /dev/null
@@ -0,0 +1,173 @@
+/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\r
+ *  This file is part of clib library\r
+ *  Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )\r
+ *\r
+ *  Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ *  of this software and associated documentation files (the "Software"), to deal\r
+ *  in the Software without restriction, including without limitation the rights\r
+ *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ *  copies of the Software, and to permit persons to whom the Software is\r
+ *  furnished to do so, subject to the following conditions:\r
+ * \r
+ *  The above copyright notice and this permission notice shall be included in\r
+ *  all copies or substantial portions of the Software.\r
+ * \r
+ *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+ *  THE SOFTWARE.\r
+ ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/\r
+\r
+#include "c_lib.h"\r
+\r
+struct clib_slist* \r
+new_clib_slist(clib_destroy fn_d, clib_compare fn_c){\r
+    struct clib_slist *pSlist  = (struct clib_slist*)malloc(sizeof(struct clib_slist));\r
+    pSlist->head           = (struct clib_slist_node*)0;\r
+    pSlist->destruct_fn    = fn_d;\r
+    pSlist->compare_fn = fn_c;\r
+    pSlist->size           = 0;\r
+    return pSlist;\r
+}\r
+void           \r
+delete_clib_slist( struct clib_slist *pSlist){\r
+    while(pSlist->size != 0) {\r
+        remove_clib_slist ( pSlist, 0 );\r
+    }\r
+    free ( pSlist );\r
+}\r
+\r
+clib_error           \r
+push_back_clib_slist( struct clib_slist *pSlist, void *elem, size_t elem_size){\r
+\r
+    struct clib_slist_node* current  = (struct clib_slist_node*)0;\r
+    struct clib_slist_node* new_node = (struct clib_slist_node*)0;\r
+\r
+    new_node = (struct clib_slist_node*)malloc(sizeof(struct clib_slist_node));\r
+\r
+    new_node->elem = new_clib_object ( elem, elem_size );\r
+    if ( ! new_node->elem )\r
+        return CLIB_SLIST_INSERT_FAILED;\r
+    new_node->next = (struct clib_slist_node*)0;\r
+\r
+    if ( pSlist->head == (struct clib_slist_node*)0 ) {\r
+        pSlist->head = new_node;\r
+        pSlist->size++;\r
+        return CLIB_ERROR_SUCCESS;\r
+    }\r
+    current = pSlist->head;\r
+    while ( current->next != (struct clib_slist_node*)0 )\r
+        current  = current->next;    \r
+    current->next = new_node;\r
+    pSlist->size++;\r
+\r
+    return CLIB_ERROR_SUCCESS;\r
+}\r
+static void \r
+pvt_remove_clib_list ( struct clib_slist *pSlist, struct clib_slist_node* pSlistNode ) {\r
+    void *elem;\r
+    get_raw_clib_object(pSlistNode->elem, &elem);\r
+    if ( pSlist->destruct_fn) {             \r
+        (pSlist->destruct_fn)(elem);\r
+        delete_clib_object ( pSlistNode->elem );\r
+    }else {\r
+        free ( elem );\r
+        delete_clib_object ( pSlistNode->elem );\r
+    }        \r
+    free ( pSlistNode);\r
+}\r
+void           \r
+remove_clib_slist( struct clib_slist *pSlist, int pos ) {\r
+    int i = 0;\r
+\r
+    struct clib_slist_node* current = pSlist->head;\r
+    struct clib_slist_node* temp    = (struct clib_slist_node*)0;\r
+\r
+    if ( pos > pSlist->size ) return;\r
+\r
+    if ( pos == 0 ) {                \r
+        pSlist->head = current->next;    \r
+        pvt_remove_clib_list(pSlist, current);    \r
+        pSlist->size--;\r
+        return;\r
+    }\r
+    for ( i = 1; i < pos - 1; i++)\r
+        current = current->next;\r
+\r
+    temp          = current->next;\r
+    current->next = current->next->next;\r
+    pvt_remove_clib_list ( pSlist, temp );\r
+\r
+    pSlist->size--;\r
+}\r
+clib_error           \r
+insert_clib_slist(struct clib_slist *pSlist, int pos, void *elem, size_t elem_size) {\r
+    int i = 0;\r
+    struct clib_slist_node* current  = pSlist->head;\r
+    struct clib_slist_node* new_node = (struct clib_slist_node*)0;\r
+   \r
+    if ( pos == 1 ) {\r
+        new_node       = (struct clib_slist_node*)malloc(sizeof(struct clib_slist_node));\r
+        new_node->elem = new_clib_object ( elem, elem_size );\r
+        if ( ! new_node->elem ) {\r
+            free ( new_node );\r
+            return CLIB_SLIST_INSERT_FAILED;\r
+        }\r
+        new_node->next = pSlist->head;\r
+        pSlist->head       = new_node;\r
+        pSlist->size++;\r
+        return CLIB_ERROR_SUCCESS;\r
+    }\r
+\r
+    if ( pos >= pSlist->size + 1 ) {\r
+        return push_back_clib_slist ( pSlist, elem, elem_size );\r
+    }\r
+\r
+    for ( i = 1; i < pos - 1; i++) {\r
+        current = current->next;\r
+    }\r
+    new_node       = (struct clib_slist_node*)malloc(sizeof(struct clib_slist_node));\r
+    new_node->elem = new_clib_object ( elem, elem_size );\r
+    if ( ! new_node->elem ) {\r
+        free ( new_node );\r
+        return CLIB_SLIST_INSERT_FAILED;\r
+    }\r
+\r
+    new_node->next = current->next;\r
+    current->next  = new_node;\r
+    pSlist->size++;\r
+\r
+    return CLIB_ERROR_SUCCESS;\r
+}\r
+void           \r
+for_each_clib_slist (struct clib_slist *pSlist, void (*fn)(void* )) {\r
+    void *elem;\r
+    struct clib_slist_node* current  = pSlist->head;\r
+    while ( current != (struct clib_slist_node*)0 ) {\r
+        get_raw_clib_object(current->elem, &elem);\r
+        (fn)(elem);\r
+        free ( elem );\r
+        current = current->next;\r
+    }    \r
+}\r
+clib_bool\r
+find_clib_slist (struct clib_slist *pSlist, void* find_value, void**out_value) {\r
+    struct clib_slist_node* current  = pSlist->head;  \r
+    while ( current != (struct clib_slist_node*)0 ) {        \r
+        get_raw_clib_object(current->elem, out_value);\r
+        if ((pSlist->compare_fn)(find_value,*out_value) != 0){\r
+            break;\r
+        }\r
+        free ( *out_value );\r
+        current = current->next;\r
+    }\r
+    if ( current ) {\r
+        return clib_true;\r
+    }\r
+    return clib_false;\r
+}\r
+\r
+\r