![]() |
Helixis 1.0
Task Programming API
|
00001 /* 00002 ** Copyright 2009-2011, helixis.org 00003 ** All rights reserved. 00004 ** 00005 ** Redistribution and use in source and binary forms, with or without modification, are permitted provided 00006 ** that the following conditions are met: 00007 ** 00008 ** Redistributions of source code must retain the above copyright notice, this list of conditions and the 00009 ** following disclaimer. 00010 ** 00011 ** Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 00012 ** the following disclaimer in the documentation and/or other materials provided with the distribution. 00013 ** 00014 ** Neither the name of the helixis.org nor the names of its contributors may be used to endorse or promote 00015 ** products derived from this software without specific prior written permission. 00016 ** 00017 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 00018 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00019 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00020 ** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00021 ** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00022 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00023 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00024 ** POSSIBILITY OF SUCH DAMAGE. 00025 */ 00026 00027 /* 00028 ** INCLUDES 00029 */ 00030 00031 #include "hlx/core.h" 00032 #include "helixis_internal.h" 00033 00034 /* 00035 ** EXTERNALS 00036 */ 00037 00038 extern hlx_api gl_api; 00039 00040 /* 00041 ** FUNCTIONS 00042 */ 00043 00044 void hlx_list_construct(hlx_list* list) 00045 { 00046 list->first = 0; 00047 list->last = 0; 00048 list->size = 0; 00049 } 00050 00051 void hlx_list_destruct(hlx_list* list) 00052 { 00053 hlx_list_clear(list); 00054 } 00055 00056 hlx_list_node* hlx_list_push_front(hlx_list* list, void* data) 00057 { 00058 hlx_list_node* node; 00059 00060 node = gl_api.malloc_entry(sizeof(*node)); 00061 if (node == 0) 00062 return (0); 00063 node->data = data; 00064 node->next = list->first; 00065 node->parent = 0; 00066 if (!list->first) 00067 list->last = node; 00068 else 00069 list->first->parent = node; 00070 list->first = node; 00071 #ifdef HLX_BUILD_WITH_PARALLEL_THREADING 00072 gl_api.atomic_incr_entry(&list->size); 00073 #else 00074 ++list->size; 00075 #endif 00076 return (list->first); 00077 } 00078 00079 hlx_list_node* hlx_list_push_back(hlx_list* list, void* data) 00080 { 00081 hlx_list_node* node; 00082 hlx_list_node* tmp; 00083 00084 node = gl_api.malloc_entry(sizeof(*node)); 00085 if (node == 0) 00086 return (0); 00087 tmp = list->first; 00088 node->data = data; 00089 node->next = 0; 00090 if (!list->last) 00091 list->first = node; 00092 else 00093 list->last->next = node; 00094 node->parent = list->last; 00095 list->last = node; 00096 #ifdef HLX_BUILD_WITH_PARALLEL_THREADING 00097 gl_api.atomic_incr_entry(&list->size); 00098 #else 00099 ++list->size; 00100 #endif 00101 return (list->last); 00102 } 00103 00104 hlx_list_node* hlx_list_find(const hlx_list* list, void* data) 00105 { 00106 hlx_list_node* it; 00107 00108 for (it = list->first; it; it = it->next) 00109 if (it->data == data) 00110 break; 00111 return (it); 00112 } 00113 00114 hlx_list_node* hlx_list_get(const hlx_list* list, unsigned int pos) 00115 { 00116 hlx_list_node* it; 00117 unsigned int i; 00118 00119 for (i = 0, it = list->first; it; it = it->next, ++i) 00120 if (i == pos) 00121 return (it); 00122 return (0); 00123 } 00124 00125 void hlx_list_erase(hlx_list* list, hlx_list_node* node) 00126 { 00127 hlx_list_node* it; 00128 hlx_list_node* prev; 00129 00130 for (it = list->first, prev = 0; it; it = it->next) 00131 { 00132 if (it == node) 00133 { 00134 if (!prev) 00135 list->first = it->next; 00136 else 00137 prev->next = it->next; 00138 if (!it->next) 00139 list->last = prev; 00140 gl_api.free_entry(node); 00141 #ifdef HLX_BUILD_WITH_PARALLEL_THREADING 00142 gl_api.atomic_decr_entry(&list->size); 00143 #else 00144 --list->size; 00145 #endif 00146 return; 00147 } 00148 prev = it; 00149 } 00150 } 00151 00152 void hlx_list_clear(hlx_list* list) 00153 { 00154 hlx_list_node* it; 00155 hlx_list_node* tmp; 00156 00157 it = list->first; 00158 while (it) 00159 { 00160 tmp = it; 00161 it = it->next; 00162 gl_api.free_entry(tmp); 00163 #ifdef HLX_BUILD_WITH_PARALLEL_THREADING 00164 gl_api.atomic_decr_entry(&list->size); 00165 #else 00166 --list->size; 00167 #endif 00168 } 00169 list->first = 0; 00170 list->last = 0; 00171 } 00172 00173 void hlx_list_clear_data(hlx_list* list) 00174 { 00175 hlx_list_node* it; 00176 hlx_list_node* tmp; 00177 00178 it = list->first; 00179 while (it) 00180 { 00181 tmp = it; 00182 it = it->next; 00183 gl_api.free_entry(tmp->data); 00184 gl_api.free_entry(tmp); 00185 #ifdef HLX_BUILD_WITH_PARALLEL_THREADING 00186 gl_api.atomic_decr_entry(&list->size); 00187 #else 00188 --list->size; 00189 #endif 00190 } 00191 list->first = 0; 00192 list->last = 0; 00193 } 00194 00195 unsigned int hlx_list_empty(const hlx_list* list) 00196 { 00197 return (list->size == 0); 00198 } 00199 00200 unsigned int hlx_listsize(const hlx_list* list) 00201 { 00202 return (list->size); 00203 } 00204 00205 /* 00206 ** END OF FILE 00207 */