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