graphicscomposition/openwfcompositionengine/common/src/owflinkedlist.c
changeset 0 5d03bc08d59c
child 152 9f1c3fea0f87
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 /* Copyright (c) 2009 The Khronos Group Inc.
       
     2  *
       
     3  * Permission is hereby granted, free of charge, to any person obtaining a
       
     4  * copy of this software and/or associated documentation files (the
       
     5  * "Materials"), to deal in the Materials without restriction, including
       
     6  * without limitation the rights to use, copy, modify, merge, publish,
       
     7  * distribute, sublicense, and/or sell copies of the Materials, and to
       
     8  * permit persons to whom the Materials are furnished to do so, subject to
       
     9  * the following conditions:
       
    10  *
       
    11  * The above copyright notice and this permission notice shall be included
       
    12  * in all copies or substantial portions of the Materials.
       
    13  *
       
    14  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
       
    18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
       
    19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
       
    20  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    21  */
       
    22 
       
    23 
       
    24 #ifdef __cplusplus
       
    25 extern "C"
       
    26 {
       
    27 #endif
       
    28 
       
    29 
       
    30 #include <string.h>
       
    31 #include <stdlib.h>
       
    32 
       
    33 #include "owfmemory.h"
       
    34 #include "owflinkedlist.h"
       
    35 #include "owftypes.h"
       
    36 #include "owfpool.h"
       
    37 
       
    38 
       
    39 
       
    40 
       
    41 OWF_API_CALL OWF_NODE*
       
    42 OWF_Node_Create(OWF_POOL* pool, void* data)
       
    43 {
       
    44     OWF_NODE*               node;
       
    45 
       
    46     node = (OWF_NODE*)OWF_Pool_GetObject(pool);
       
    47     if (node)
       
    48     {
       
    49         node->data = data;
       
    50     }
       
    51     return node;
       
    52 }
       
    53 
       
    54 OWF_API_CALL void
       
    55 OWF_Node_Destroy(OWF_NODE* node)
       
    56 {
       
    57     OWF_Pool_PutObject(node);
       
    58 }
       
    59 
       
    60 OWF_API_CALL OWF_NODE*
       
    61 OWF_List_Tail(OWF_NODE* root)
       
    62 {
       
    63     if (root)
       
    64     {
       
    65         while (root->next)
       
    66         {
       
    67             root = root->next;
       
    68         }
       
    69     }
       
    70     return root;
       
    71 }
       
    72 
       
    73 OWF_API_CALL OWF_NODE*
       
    74 OWF_List_Append(OWF_NODE* root, OWF_NODE* node)
       
    75 {
       
    76     OWF_NODE*               tail;
       
    77 
       
    78     tail = OWF_List_Tail(root);
       
    79     if (tail)
       
    80     {
       
    81         tail->next = node;
       
    82     }
       
    83     else
       
    84     {
       
    85         root = node;
       
    86     }
       
    87     return root;
       
    88 }
       
    89 
       
    90 OWF_API_CALL OWF_NODE*
       
    91 OWF_List_Insert(OWF_NODE* root, OWF_NODE* node)
       
    92 {
       
    93     if (root)
       
    94     {
       
    95         node->next = root;
       
    96     }
       
    97     root = node;
       
    98     return root;
       
    99 }
       
   100 
       
   101 OWF_API_CALL void
       
   102 OWF_List_InsertAfter(OWF_NODE* pred, OWF_NODE* succ)
       
   103 {
       
   104     if (pred && succ)
       
   105     {
       
   106         succ->next = pred->next;
       
   107         pred->next = succ;
       
   108     }
       
   109 }
       
   110 
       
   111 OWF_API_CALL OWF_NODE*
       
   112 OWF_List_Contains(OWF_NODE* root, void* data)
       
   113 {
       
   114     while (root)
       
   115     {
       
   116         if (root->data == data)
       
   117         {
       
   118             break;
       
   119         }
       
   120         root = root->next;
       
   121     }
       
   122 
       
   123     return root;
       
   124 }
       
   125 
       
   126 OWF_API_CALL OWF_NODE*
       
   127 OWF_List_Remove(OWF_NODE* root, OWF_NODE* node)
       
   128 {
       
   129     OWF_NODE*                iter = NULL;
       
   130 
       
   131     if (root)
       
   132     {
       
   133         if (node != root)
       
   134         {
       
   135             iter = root;
       
   136             while (iter)
       
   137             {
       
   138                 if (iter->next == node)
       
   139                 {
       
   140                     iter->next = node->next;
       
   141                     break;
       
   142                 }
       
   143                 iter = iter->next;
       
   144             }
       
   145         }
       
   146         else
       
   147         {
       
   148             root = root->next;
       
   149         }
       
   150     }
       
   151 
       
   152     return root;
       
   153 }
       
   154 
       
   155 OWF_API_CALL OWF_NODE*
       
   156 OWF_List_Clear(OWF_NODE* root)
       
   157 {
       
   158     OWF_NODE* next = NULL;
       
   159     while (root)
       
   160     {
       
   161         next = root->next;
       
   162         OWF_Node_Destroy(root);
       
   163         root = next;
       
   164     }
       
   165     return root;
       
   166 }
       
   167 
       
   168 OWF_API_CALL void
       
   169 OWF_List_ForEach(OWF_NODE* root, NODEITERFUNC func, void* data)
       
   170 {
       
   171     while (root) {
       
   172         if (!func(root->data, data)) {
       
   173             return;
       
   174         }
       
   175         root = root->next;
       
   176     }
       
   177 }
       
   178 
       
   179 OWF_API_CALL OWF_NODE*
       
   180 OWF_List_Find(OWF_NODE* root, NODECMPFUNC func, void* data)
       
   181 {
       
   182     while (root) {
       
   183         if (func(root->data, data)) {
       
   184             break;
       
   185         }
       
   186         root = root->next;
       
   187     }
       
   188     return root;
       
   189 }
       
   190 
       
   191 
       
   192 #ifdef __cplusplus
       
   193 }
       
   194 #endif