1e3ae2c7548fd45216eef6bb46b15c2fff3c3f6c
[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    $Id: worklist.c$
30
31 */
32
33 #include "mm/memory.h"
34 #include "toolbox/worklist.h"
35
36
37 /******************************************************************************
38
39 Worklist Implementation
40
41 New Elements (integers) are pushed on a stack and remembered in a
42 bitvector, to ensure efficitently uniqueness.
43
44 ******************************************************************************/
45
46 /*******************************************************************************
47 wl_new
48
49 IN:     int size    size of worklist
50
51 RETURN: worklist *  new worklist
52 *******************************************************************************/
53 worklist *wl_new(int size) {
54         worklist *w;
55
56         w = DNEW(worklist);
57         w->W_stack = DMNEW(int, size);
58         w->W_top = 0;
59         w->W_bv = bv_new(size);
60 #ifdef WL_DEBUG_CHECK
61         w->size = size;
62 #endif
63         return w;
64 }
65
66 /*******************************************************************************
67 wl_add
68
69 Adds the integer element to the worklist, if this value is not already
70 in the worklist.
71
72 IN:     worklist *w    pointer to worklist created with wl_new
73         int element    integer element to be added
74 *******************************************************************************/
75 void wl_add(worklist *w, int element) {
76         _WL_CHECK_BOUNDS(element, 0, w->size);
77         if (!bv_get_bit(w->W_bv, element)) {
78                 _BV_ASSERT((w->W_top < w->size));
79                 w->W_stack[(w->W_top)++] = element;
80                 bv_set_bit(w->W_bv, element);
81         }
82 }
83
84 /*******************************************************************************
85 wl_get
86
87 Returns and removes an element from the worklist.
88
89 IN:     worklist *w    pointer to worklist created with wl_new
90
91 RETURN  int            an element removed from the worklist
92 *******************************************************************************/
93 int wl_get(worklist *w) {
94         int element;
95
96         _WL_ASSERT((w->W_top > 0));
97         element = w->W_stack[--(w->W_top)];
98         bv_reset_bit(w->W_bv, element);
99         return element;
100 }
101
102 /*******************************************************************************
103 wl_is_empty
104
105 Checks if the worklist is empty.
106
107 IN:     worklist *w    pointer to worklist created with wl_new
108
109 RETURN  bool           true if w is empty, false otherwise
110 *******************************************************************************/
111 bool wl_is_empty(worklist *w) {
112         return (w->W_top == 0);
113 }
114
115 /*******************************************************************************
116 wl_reset
117
118 Empties the worklist.
119
120 IN:     worklist *w    pointer to worklist created with wl_new
121 *******************************************************************************/
122 void wl_reset(worklist *w, int size) {
123         w->W_top = 0;
124         bv_reset(w->W_bv, size);
125 }
126
127 /*
128  * These are local overrides for various environment variables in Emacs.
129  * Please do not remove this and leave it at the end of the file, where
130  * Emacs will automagically detect them.
131  * ---------------------------------------------------------------------
132  * Local variables:
133  * mode: c
134  * indent-tabs-mode: t
135  * c-basic-offset: 4
136  * tab-width: 4
137  * End:
138  */