Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals

lstbasic.c

Go to the documentation of this file.
00001 /************************************************************************* 00002 * $Id: lstbasic.c,v 1.1 2005/09/09 08:22:52 bellet Exp $ 00003 ************************************************************************** 00004 This software is governed by the CeCILL license under French law and 00005 abiding by the rules of distribution of free software. You can use, 00006 modify and/ or redistribute the software under the terms of the CeCILL 00007 license as circulated by CEA, CNRS and INRIA at the following URL 00008 "http://www.cecill.info". 00009 00010 As a counterpart to the access to the source code and rights to copy, 00011 modify and redistribute granted by the license, users are provided only 00012 with a limited warranty and the software's author, the holder of the 00013 economic rights, and the successive licensors have only limited 00014 liability. 00015 00016 In this respect, the user's attention is drawn to the risks associated 00017 with loading, using, modifying and/or developing or reproducing the 00018 software by the user in light of its specific status of free software, 00019 that may mean that it is complicated to manipulate, and that also 00020 therefore means that it is reserved for developers and experienced 00021 professionals having in-depth computer knowledge. Users are therefore 00022 encouraged to load and test the software's suitability as regards their 00023 requirements in conditions enabling the security of their systems and/or 00024 data to be ensured and, more generally, to use and operate it in the 00025 same conditions as regards security. 00026 00027 The fact that you are presently reading this means that you have had 00028 knowledge of the CeCILL license and that you accept its terms. 00029 00030 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de 00031 l'Image). All rights reserved. See License.txt for details. 00032 00033 Version 1.0 05/09/2005 00034 *************************************************************************/ 00035 00036 #include <stdlib.h> 00037 #include "idliste.h" 00038 #include "idprint.h" 00039 #include "iderr.h" 00040 00041 /************************************************************************* 00042 * 00043 * Description : Ajout et suppression d'un element dans une liste 00044 * 00045 **************************************************************************/ 00093 int 00094 IdLstAddElement(PLIST list, void *obj, int mode, PLIST_ELEMENT elem) 00095 { 00096 PLIST_ELEMENT e, p; 00097 00098 if (!list) { 00099 IdErrno = IDERR_POINTER_IS_NULL; 00100 return (0); 00101 } 00102 00103 e = calloc(1, sizeof(LIST_ELEMENT)); 00104 if (!e) { 00105 IdErrno = IDERR_ALLOC_ELEM; 00106 return (0); 00107 } 00108 00109 (_IdLstPrivate(list)->NbElems)++; 00110 e->Numero = IdLstNbElems(list); 00111 switch (mode & 0x0f) { 00112 case LST_BEG: 00113 if (mode & (LST_NEXT | LST_BIDIR)) { 00114 p = IdLstFirst(list); 00115 if (p == 0) 00116 IdLstLast(list) = e; 00117 e->Next = p; 00118 IdLstFirst(list) = e; 00119 e->Object = obj; 00120 if (mode & LST_BIDIR) 00121 if (e->Next) 00122 e->Next->Pred = e; 00123 } 00124 if (mode & LST_PRED) { 00125 e->Pred = IdLstFirst(list); 00126 IdLstFirst(list) = e; 00127 e->Object = obj; 00128 } 00129 break; 00130 case LST_END: 00131 if (mode & (LST_NEXT | LST_BIDIR)) { 00132 00133 p = IdLstFirst(list); 00134 if (p) { 00135 while (p->Next) 00136 p = p->Next; 00137 p->Next = e; 00138 if (mode & LST_BIDIR) 00139 e->Pred = p; 00140 } else 00141 IdLstFirst(list) = e; 00142 e->Object = obj; 00143 IdLstLast(list) = e; 00144 } 00145 if (mode & LST_PRED) { 00146 p = IdLstFirst(list); 00147 if (p) { 00148 while (p->Pred) 00149 p = p->Pred; 00150 p->Pred = e; 00151 } else 00152 IdLstFirst(list) = e; 00153 e->Object = obj; 00154 } 00155 break; 00156 case LST_ELEM: 00157 if (mode & (LST_NEXT | LST_BIDIR)) { 00158 if (elem) { 00159 if (elem == IdLstLast(list)) 00160 IdLstLast(list) = e; 00161 e->Next = elem->Next; 00162 elem->Next = e; 00163 if (mode & LST_BIDIR) { 00164 e->Pred = elem; 00165 if (e->Next) 00166 e->Next->Pred = e; 00167 } 00168 } 00169 e->Object = obj; 00170 } 00171 if (mode & LST_PRED) { 00172 if (elem) { 00173 e->Pred = elem->Pred; 00174 elem->Pred = e; 00175 } 00176 e->Object = obj; 00177 } 00178 break; 00179 00180 case LST_ELEM_AV: 00181 if (mode & (LST_NEXT | LST_BIDIR)) { 00182 if (elem) { 00183 if (elem == IdLstFirst(list)) 00184 IdLstFirst(list) = e; 00185 e->Pred = elem->Pred; 00186 elem->Pred = e; 00187 if (mode & LST_BIDIR) { 00188 e->Next = elem; 00189 if (e->Pred) 00190 e->Pred->Next = e; 00191 } 00192 } 00193 e->Object = obj; 00194 } 00195 if (mode & LST_PRED) { 00196 if (elem) { 00197 e->Next = elem->Next; 00198 elem->Next = e; 00199 } 00200 e->Object = obj; 00201 } 00202 break; 00203 } 00204 return 1; 00205 } 00206 00213 int 00214 IdLstRemoveFirst(PLIST l) 00215 { 00216 PLIST_ELEMENT e; 00217 if (IdLstNbElems(l) == 0) { 00218 /* 00219 * empty list 00220 */ 00221 IdErrno = IDERR_EMPTY_LIST; 00222 return (0); 00223 } 00224 (_IdLstPrivate(l)->NbElems)--; 00225 00226 e = IdLstFirst(l); 00227 IdLstFirst(l) = IdLstNext(e); 00228 00229 if (IdLstNext(e) == NULL) 00230 /* 00231 * si c'est le dernier 00232 */ 00233 IdLstLast(l) = NULL; 00234 else 00235 IdLstPrevious(IdLstNext(e)) = NULL; 00236 00237 /* 00238 * les liens de chainage ont deja ete supprimes 00239 */ 00240 free(e); 00241 return (1); 00242 } 00243 00250 int 00251 IdLstRemoveLast(PLIST l) 00252 { 00253 PLIST_ELEMENT e; 00254 if (IdLstNbElems(l) == 0) { 00255 /* 00256 * liste vide 00257 */ 00258 IdErrno = IDERR_EMPTY_LIST; 00259 return (0); 00260 } 00261 (_IdLstPrivate(l)->NbElems)--; 00262 00263 e = IdLstLast(l); 00264 IdLstLast(l) = IdLstPrevious(e); 00265 00266 if (IdLstPrevious(e) == NULL) 00267 /* 00268 * si c'est le Premier 00269 */ 00270 IdLstFirst(l) = NULL; 00271 else 00272 IdLstNext(IdLstPrevious(e)) = NULL; 00273 00274 /* 00275 * les liens de chainage ont deja ete supprimes 00276 */ 00277 free(e); 00278 return (1); 00279 } 00280 00288 int 00289 IdLstRemoveElem(PLIST l, PLIST_ELEMENT e) 00290 { 00291 PLIST_ELEMENT pr, sv; 00292 00293 if (IdLstNbElems(l) == 0) { 00294 /* 00295 * liste vide 00296 */ 00297 IdErrno = IDERR_EMPTY_LIST; 00298 return (0); 00299 } 00300 pr = IdLstPrevious(e); 00301 sv = IdLstNext(e); 00302 00303 if (pr == NULL) 00304 IdLstRemoveFirst(l); 00305 /* 00306 * si c est le Premier 00307 */ 00308 else if (sv == NULL) 00309 IdLstRemoveLast(l); 00310 /* 00311 * si c est le Dernier 00312 */ 00313 00314 else { 00315 /* 00316 * Element quelconque 00317 */ 00318 IdLstNext(pr) = IdLstNext(e); 00319 IdLstPrevious(sv) = IdLstPrevious(e); 00320 (_IdLstPrivate(l)->NbElems)--; 00321 00322 /* 00323 * les liens de chainage ont deja ete supprimes 00324 */ 00325 free(e); 00326 } 00327 return (1); 00328 } 00329 00339 int 00340 LstAddAfter(PLIST l, void *pobj, PLIST_ELEMENT e) 00341 { 00342 00343 PLIST_ELEMENT elem; 00344 00345 if (e == IdLstLast(l)) 00346 IdLstAddLast(l, pobj); 00347 else { 00348 /* 00349 * Si l'element n'est pas le dernier 00350 */ 00351 elem = calloc(1, sizeof(LIST_ELEMENT)); 00352 if (!elem) { 00353 IdErrPrintf("(Erreur allocation nouvel element\n"); 00354 IdErrno = IDERR_ALLOC_ELEM; 00355 return 0; 00356 } 00357 (_IdLstPrivate(l)->NbElems)++; 00358 elem->Numero = IdLstNbElems(l); 00359 elem->Object = pobj; 00360 IdLstNext(elem) = IdLstNext(e); 00361 IdLstPrevious(elem) = e; 00362 IdLstPrevious(IdLstNext(e)) = elem; 00363 IdLstNext(e) = elem; 00364 } 00365 return 1; 00366 } 00367 00377 int 00378 LstAddBefore(PLIST l, void *pobj, PLIST_ELEMENT e) 00379 { 00380 00381 PLIST_ELEMENT elem; 00382 00383 if (e == IdLstFirst(l)) 00384 IdLstAddFirst(l, pobj); 00385 else { 00386 /* 00387 * Si l'element n'est pas le premier 00388 */ 00389 elem = calloc(1, sizeof(LIST_ELEMENT)); 00390 if (!elem) { 00391 IdErrPrintf("Erreur allocation nouvel element\n"); 00392 IdErrno = IDERR_ALLOC_ELEM; 00393 return 0; 00394 } 00395 (_IdLstPrivate(l)->NbElems)++; 00396 00397 elem->Numero = IdLstNbElems(l); 00398 elem->Object = pobj; 00399 IdLstNext(elem) = e; 00400 IdLstPrevious(elem) = IdLstPrevious(e); 00401 IdLstNext(IdLstPrevious(e)) = elem; 00402 IdLstPrevious(e) = elem; 00403 } 00404 return 1; 00405 }

Generated on Wed Oct 19 09:28:34 2005 for SIMRI3D by doxygen 1.3.7