aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan3/set.cpp
blob: c5750c455c0471f9b87c7569735ec52d32759aca (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
163
164
165
166
167
168
169
170
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/alan3/set.h"
#include "glk/alan3/lists.h"
#include "glk/alan3/syserr.h"
#include "glk/alan3/memory.h"
#include "glk/alan3/instance.h"

namespace Glk {
namespace Alan3 {

#define EXTENT 5

/*======================================================================*/
Set *newSet(int allocation) {
	Set *theSet = NEW(Set);

	if (allocation) {
		theSet->members = (Aword *)allocate(allocation * sizeof(theSet->members[0]));
		theSet->size = 0;
		theSet->allocated = allocation;
	}
	return theSet;
}


/*======================================================================*/
void initSets(SetInitEntry *initTable) {
	SetInitEntry *init;
	int i;

	for (init = initTable; !isEndOfArray(init); init++) {
		Set *set = newSet(init->size);
		Aword *member = (Aword *)pointerTo(init->setAddress);
		for (i = 0; i < init->size; i++, member++)
			addToSet(set, *member);
		setInstanceAttribute(init->instanceCode, init->attributeCode, toAptr(set));
	}
}


/*======================================================================*/
int setSize(Set *theSet) {
	return theSet->size;
}


/*======================================================================*/
void clearSet(Set *theSet) {
	theSet->size = 0;
}


/*======================================================================*/
Set *copySet(Set *theSet) {
	Set *nset = newSet(theSet->size);
	int i;

	for (i = 1; i <= theSet->size; i++)
		addToSet(nset, getSetMember(theSet, i));
	return nset;
}


/*======================================================================*/
Aword getSetMember(Set *theSet, Aint theMember) {
	if (theMember > theSet->size || theMember < 1)
		apperr("Accessing nonexisting member in a set");
	return theSet->members[theMember - 1];
}


/*======================================================================*/
bool inSet(Set *theSet, Aword member) {
	int i;

	for (i = 1; i <= theSet->size; i++)
		if (getSetMember(theSet, i) == member)
			return TRUE;
	return FALSE;
}


/*=======================================================================*/
Set *setUnion(Set *set1, Set *set2) {
	Set *theUnion = newSet(set1->size + set2->size);
	int i;

	for (i = 0; i < set1->size; i++)
		addToSet(theUnion, set1->members[i]);
	for (i = 0; i < set2->size; i++)
		addToSet(theUnion, set2->members[i]);
	return theUnion;
}


/*=======================================================================*/
void addToSet(Set *theSet, Aword newMember) {
	if (inSet(theSet, newMember)) return;
	if (theSet->size == theSet->allocated) {
		theSet->allocated += EXTENT;
		theSet->members = (Aword *)realloc(theSet->members, theSet->allocated * sizeof(theSet->members[0]));
	}
	theSet->members[theSet->size] = newMember;
	theSet->size++;
}


/*=======================================================================*/
void removeFromSet(Set *theSet, Aword member) {
	int i, j;

	if (!inSet(theSet, member)) return;

	for (i = 0; i < theSet->size; i++) {
		if ((Aword)theSet->members[i] == member) {
			for (j = i; j < theSet->size - 1; j++)
				theSet->members[j] = theSet->members[j + 1];
			theSet->size--;
			break;
		}
	}
}


/*=======================================================================*/
bool equalSets(Set *set1, Set *set2) {
	int i;

	if (set1->size != set2->size) return FALSE;

	for (i = 0; i < set1->size; i++) {
		if (!inSet(set2, set1->members[i]))
			return FALSE;
	}
	return TRUE;
}


/*======================================================================*/
void freeSet(Set *theSet) {
	if (theSet != NULL) {
		if (theSet->members != NULL)
			deallocate(theSet->members);
		deallocate(theSet);
	}
}

} // End of namespace Alan3
} // End of namespace Glk