* Removed all Id tags.
[cacao.git] / src / toolbox / worklist.c
1 /* src/toolbox/worklist.c - worklist implementation
2
3    Copyright (C) 2005, 2006 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Ullrich
28
29
30 */
31
32 #include "mm/memory.h"
33 #include "toolbox/worklist.h"
34
35
36 /******************************************************************************
37
38 Worklist Implementation
39
40 New Elements (integers) are pushed on a stack and remembered in a
41 bitvector, to ensure efficitently uniqueness.
42
43 ******************************************************************************/
44
45 /*******************************************************************************
46 wl_new
47
48 IN:     int size    size of worklist
49
50 RETURN: worklist *  new worklist
51 *******************************************************************************/
52 worklist *wl_new(int size) {
53         worklist *w;
54
55         w = DNEW(worklist);
56         w->W_stack = DMNEW(int, size);
57         w->W_top = 0;
58         w->W_bv = bv_new(size);
59 #ifdef WL_DEBUG_CHECK
60         w->size = size;
61 #endif
62         return w;
63 }
64
65 /*******************************************************************************
66 wl_add
67
68 Adds the integer element to the worklist, if this value is not already
69 in the worklist.
70
71 IN:     worklist *w    pointer to worklist created with wl_new
72         int element    integer element to be added
73 *******************************************************************************/
74 void wl_add(worklist *w, int element) {
75         _WL_CHECK_BOUNDS(element, 0, w->size);
76         if (!bv_get_bit(w->W_bv, element)) {
77                 _BV_ASSERT((w->W_top < w->size));
78                 w->W_stack[(w->W_top)++] = element;
79                 bv_set_bit(w->W_bv, element);
80         }
81 }
82
83 /*******************************************************************************
84 wl_get
85
86 Returns and removes an element from the worklist.
87
88 IN:     worklist *w    pointer to worklist created with wl_new
89
90 RETURN  int            an element removed from the worklist
91 *******************************************************************************/
92 int wl_get(worklist *w) {
93         int element;
94
95         _WL_ASSERT((w->W_top > 0));
96         element = w->W_stack[--(w->W_top)];
97         bv_reset_bit(w->W_bv, element);
98         return element;
99 }
100
101 /*******************************************************************************
102 wl_is_empty
103
104 Checks if the worklist is empty.
105
106 IN:     worklist *w    pointer to worklist created with wl_new
107
108 RETURN  bool           true if w is empty, false otherwise
109 *******************************************************************************/
110 bool wl_is_empty(worklist *w) {
111         return (w->W_top == 0);
112 }
113
114 /*******************************************************************************
115 wl_reset
116
117 Empties the worklist.
118
119 IN:     worklist *w    pointer to worklist created with wl_new
120 *******************************************************************************/
121 void wl_reset(worklist *w, int size) {
122         w->W_top = 0;
123         bv_reset(w->W_bv, size);
124 }
125
126 /*
127  * These are local overrides for various environment variables in Emacs.
128  * Please do not remove this and leave it at the end of the file, where
129  * Emacs will automagically detect them.
130  * ---------------------------------------------------------------------
131  * Local variables:
132  * mode: c
133  * indent-tabs-mode: t
134  * c-basic-offset: 4
135  * tab-width: 4
136  * End:
137  */