summaryrefslogtreecommitdiff
path: root/map.c
blob: bf02b0751eed21108e2ffffcc48752344519ade8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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 */