SDLGameEngine

src/sgearray.c

00001 /*
00002  * Copyright (c) 2007 Heiko Irrgang
00003  *
00004  * The license and distribution terms for this file may be
00005  * found in the file COPYING in this distribution or at
00006  * http://93-interactive.com/cms/products/software/sdl-game-engine/license/
00007  */
00008 
00009 #include <sge.h>
00010 
00011 SGEARRAY *sgeArrayNew() {
00012         SGEARRAY *ret;
00013         sgeNew(ret, SGEARRAY);
00014         ret->numberOfElements=0;
00015         ret->freeFunction=NULL;
00016         sgeMalloc(ret->element, void *, 1);
00017         return ret;
00018 }
00019 
00020 SGEARRAY *sgeAutoArrayNew(void (*function)(Uint32, void *)) {
00021         SGEARRAY *ret=sgeArrayNew();
00022         ret->freeFunction=function;
00023         return ret;
00024 }
00025 
00026 void sgeArrayDestroy(SGEARRAY *a) {
00027         if (a->freeFunction!=NULL) {
00028                 while (a->numberOfElements>0) {
00029                         sgeArrayRemove(a, 0);
00030                 }
00031         }
00032         sgeFree(a->element);
00033         sgeFree(a);
00034 }
00035 
00036 void sgeArrayAdd(SGEARRAY *a, void *e) {
00037         a->numberOfElements++;
00038         sgeRealloc(a->element, void *, a->numberOfElements);
00039         a->element[a->numberOfElements-1]=e;
00040 }
00041 
00042 void sgeArrayInsert(SGEARRAY *a, Uint32 offset, void *e) {
00043         if (offset>=a->numberOfElements) {
00044                 sgeArrayAdd(a, e);
00045                 return;
00046         }
00047 
00048         sgeRealloc(a->element, void *, ++a->numberOfElements);
00049         memmove(a->element+offset+1,a->element+offset,(a->numberOfElements-offset-1)*sizeof(void *));
00050         a->element[offset]=e;
00051 }
00052 
00053 void sgeArrayReplace(SGEARRAY *a, Uint32 offset, void *e) {
00054         if (offset>=a->numberOfElements) {
00055                 return;
00056         }
00057 
00058         a->element[offset]=e;
00059 }
00060 
00061 void *sgeArrayGet(SGEARRAY *a, Uint32 offset) {
00062         if (offset<a->numberOfElements) {
00063                 return a->element[offset];
00064         }
00065         return NULL;
00066 }
00067 
00068 void sgeArrayRemove(SGEARRAY *a, Uint32 offset) {
00069         if (a->numberOfElements>0) {
00070                 if (a->freeFunction!=NULL) {
00071                         (a->freeFunction)(offset, a->element[offset]);
00072                 }
00073                 memmove(a->element+offset,a->element+offset+1,(a->numberOfElements-offset-1)*sizeof(void *));
00074                 a->numberOfElements--;
00075         }
00076 }
00077 
00078 void sgeArrayForEach(SGEARRAY *a, void function(Uint32, void *)) {
00079         Uint32 i;
00080         for (i=0;i<a->numberOfElements;i++) {
00081                 function(i, a->element[i]);
00082         }
00083 }
00084 
00085 inline Uint32 sgeArraySize(SGEARRAY *a) {
00086         return a->numberOfElements;
00087 }
00088 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines