|
Blender
V2.59
|
00001 /* 00002 * $Id: BLI_linklist.c 35246 2011-02-27 20:37:56Z jesterking $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00021 * All rights reserved. 00022 * 00023 * The Original Code is: all of this file. 00024 * 00025 * Contributor(s): none yet. 00026 * 00027 * ***** END GPL LICENSE BLOCK ***** 00028 * Support for linked lists. 00029 */ 00030 00036 #include "MEM_guardedalloc.h" 00037 #include "BLI_linklist.h" 00038 #include "BLI_memarena.h" 00039 00040 int BLI_linklist_length(LinkNode *list) { 00041 if (0) { 00042 return list?(1+BLI_linklist_length(list->next)):0; 00043 } else { 00044 int len; 00045 00046 for (len=0; list; list= list->next) 00047 len++; 00048 00049 return len; 00050 } 00051 } 00052 00053 int BLI_linklist_index(LinkNode *list, void *ptr) 00054 { 00055 int index; 00056 00057 for (index = 0; list; list= list->next, index++) 00058 if (list->link == ptr) 00059 return index; 00060 00061 return -1; 00062 } 00063 00064 LinkNode *BLI_linklist_find(LinkNode *list, int index) 00065 { 00066 int i; 00067 00068 for (i = 0; list; list= list->next, i++) 00069 if (i == index) 00070 return list; 00071 00072 return NULL; 00073 } 00074 00075 void BLI_linklist_reverse(LinkNode **listp) { 00076 LinkNode *rhead= NULL, *cur= *listp; 00077 00078 while (cur) { 00079 LinkNode *next= cur->next; 00080 00081 cur->next= rhead; 00082 rhead= cur; 00083 00084 cur= next; 00085 } 00086 00087 *listp= rhead; 00088 } 00089 00090 void BLI_linklist_prepend(LinkNode **listp, void *ptr) { 00091 LinkNode *nlink= MEM_mallocN(sizeof(*nlink), "nlink"); 00092 nlink->link= ptr; 00093 00094 nlink->next= *listp; 00095 *listp= nlink; 00096 } 00097 00098 void BLI_linklist_append(LinkNode **listp, void *ptr) { 00099 LinkNode *nlink= MEM_mallocN(sizeof(*nlink), "nlink"); 00100 LinkNode *node = *listp; 00101 00102 nlink->link = ptr; 00103 nlink->next = NULL; 00104 00105 if(node == NULL){ 00106 *listp = nlink; 00107 } else { 00108 while(node->next != NULL){ 00109 node = node->next; 00110 } 00111 node->next = nlink; 00112 } 00113 } 00114 00115 void BLI_linklist_prepend_arena(LinkNode **listp, void *ptr, MemArena *ma) { 00116 LinkNode *nlink= BLI_memarena_alloc(ma, sizeof(*nlink)); 00117 nlink->link= ptr; 00118 00119 nlink->next= *listp; 00120 *listp= nlink; 00121 } 00122 00123 void BLI_linklist_insert_after(LinkNode **listp, void *ptr) { 00124 LinkNode *nlink= MEM_mallocN(sizeof(*nlink), "nlink"); 00125 LinkNode *node = *listp; 00126 00127 nlink->link = ptr; 00128 00129 if(node) { 00130 nlink->next = node->next; 00131 node->next = nlink; 00132 } 00133 else { 00134 nlink->next = NULL; 00135 *listp = nlink; 00136 } 00137 } 00138 00139 void BLI_linklist_free(LinkNode *list, LinkNodeFreeFP freefunc) { 00140 while (list) { 00141 LinkNode *next= list->next; 00142 00143 if (freefunc) 00144 freefunc(list->link); 00145 MEM_freeN(list); 00146 00147 list= next; 00148 } 00149 } 00150 00151 void BLI_linklist_apply(LinkNode *list, LinkNodeApplyFP applyfunc, void *userdata) { 00152 for (; list; list= list->next) 00153 applyfunc(list->link, userdata); 00154 }