1 /* src/toolbox/set.c - Set implementation.
4 CACAOVM - Verein zu Foerderung der freien virtuellen Machine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 This file implements a set of pointers.
25 The current implementation is naive and should be improved in the future,
26 so that the O(size) operations take O(log(size)) instead.
28 The representation of the set is an contingous unordered array of the
32 #include "toolbox/set.h"
36 #include "mm/memory.h"
38 /* struct set ******************************************************************
42 *******************************************************************************/
45 void **elements; /* An array of elements */
46 unsigned capacity; /* Size of elements */
47 unsigned size; /* Current number of elements */
50 /* set_new ********************************************************************
52 Creates an instance of a set on the dump area.
55 capacity: Maximal number of elements of elements the set can hold.
57 *******************************************************************************/
59 set *set_new(unsigned capacity) {
62 s->elements = DMNEW(void *, capacity);
63 MZERO(s->elements, void *, capacity);
64 s->capacity = capacity;
70 /* set_insert ******************************************************************
72 Inserts element e into set s
74 The current implementation takes O(size).
76 *******************************************************************************/
78 void set_insert(set *s, void *element) {
81 for (i = 0; i < s->size; ++i) {
82 if (s->elements[i] == element) {
87 assert(i < s->capacity);
90 s->elements[i] = element;
93 /* set_remove ******************************************************************
95 Removes element e into set s
97 The current implementation takes O(size).
99 *******************************************************************************/
101 void set_remove(set *s, void *element) {
103 for (i = 0; i < s->size; ++i) {
104 if (s->elements[i] == element) {
105 /* Do not creaet a "hole".
106 * Overwrite this element with the last element.
108 if (i == (s->size - 1)) { /* The last one */
109 s->elements[i] = NULL;
111 s->elements[i] = s->elements[s->size - 1];
112 s->elements[s->size - 1] = NULL;
119 /* set_size ********************************************************************
121 Returns the number of elements in the set s.
122 The complexity of the operation is O(1).
124 *******************************************************************************/
126 unsigned set_size(const set *s) {
130 /* set_empty *******************************************************************
132 Returns true, iif the set s is empty.
133 The complexity of the operation is O(1).
135 *******************************************************************************/
137 bool set_empty(const set *s) {
141 /* set_contains ****************************************************************
143 Returns true, iif the set s contains element element.
145 The current implementation takes O(size).
147 *******************************************************************************/
149 bool set_contains(const set *s, void *element) {
151 for (i = 0; i < s->size; ++i) {
152 if (s->elements[i] == element) {
159 /* set_pop *********************************************************************
161 Pics and removes some element from the set s and returns it.
162 Returns NULL if the set s is empty.
163 The complexity of the operation is O(1).
165 *******************************************************************************/
167 void *set_pop(set *s) {
171 ret = s->elements[s->size - 1];
172 s->elements[s->size - 1] = NULL;
180 * These are local overrides for various environment variables in Emacs.
181 * Please do not remove this and leave it at the end of the file, where
182 * Emacs will automagically detect them.
183 * ---------------------------------------------------------------------
186 * indent-tabs-mode: t
190 * vim:noexpandtab:sw=4:ts=4: