summaryrefslogtreecommitdiff
path: root/map.c
diff options
context:
space:
mode:
Diffstat (limited to 'map.c')
-rw-r--r--map.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/map.c b/map.c
new file mode 100644
index 0000000..bf02b07
--- /dev/null
+++ b/map.c
@@ -0,0 +1,162 @@
+/*
+
+ viDOOM - level editor for DOOM
+
+ Copyright (C) 2000 Ian Cowburn (ianc@noddybox.demon.co.uk)
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+ -------------------------------------------------------------------------
+
+ Provides a dynamic sort-of-array type
+
+*/
+static const char rcs_id[]="$Id$";
+
+#include "config.h"
+
+#include <string.h>
+
+#include "map.h"
+#include "mem.h"
+
+#define MAPCHUNK 100
+
+struct VIDOOM_Map
+ {
+ int top;
+ int alloc;
+ int size;
+ char *data;
+ };
+
+/* ----------------------------------------
+*/
+Map MapNew(int type_size)
+{
+ Map nm;
+
+ nm=Grab(sizeof(struct VIDOOM_Map));
+
+ nm->top=-1;
+ nm->size=type_size;
+ nm->alloc=0;
+ nm->data=NULL;
+
+ return(nm);
+}
+
+Map MapCopy(Map m)
+{
+ Map nm;
+
+ nm=NULL;
+
+ if (m)
+ {
+ nm=Copy(m,sizeof(struct VIDOOM_Map));
+ nm->data=Copy(m->data,m->alloc*m->size);
+ }
+
+ return(nm);
+}
+
+Map MapClear(Map m)
+{
+ if (m)
+ {
+ if (m->data)
+ Release(m->data);
+ Release(m);
+ }
+
+ return(NULL);
+}
+
+Map MapEmpty(Map m)
+{
+ if (m)
+ {
+ if (m->data)
+ Release(m->data);
+
+ m->top=-1;
+ m->alloc=0;
+ m->data=NULL;
+ }
+
+ return(m);
+}
+
+int MapSize(Map m)
+{
+ if (m)
+ return(m->top+1);
+ else
+ return(0);
+}
+
+void *MapElem(Map m,int no)
+{
+ if ((no<0)||(no>m->top))
+ return(NULL);
+ else
+ return(m->data+(no*m->size));
+}
+
+void MapAdd(Map m,int no,void *data)
+{
+ if (no==-1)
+ no=m->top+1;
+
+ if (m->alloc<no+1)
+ {
+ m->alloc=(no/MAPCHUNK+1)*MAPCHUNK;
+
+ if (m->data)
+ m->data=ReGrab(m->data,m->alloc*m->size);
+ else
+ m->data=Grab(m->alloc*m->size);
+ }
+
+ memcpy(m->data+(no*m->size),data,m->size);
+
+ if (m->top<no)
+ m->top=no;
+}
+
+void *MapFindElem(Map m,int (*pred)(void *, void *),void *data)
+{
+ int f;
+ void *ret,*e;
+
+ ret=NULL;
+ f=0;
+
+ while((f<=m->top)&&(!ret))
+ {
+ e=MapElem(m,f);
+
+ if ((*pred)(e,data))
+ ret=e;
+
+ f++;
+ }
+
+ return(ret);
+}
+
+
+/* END OF FILE */