Static Value-Flow Analysis
WorkList.h
Go to the documentation of this file.
1 //===- WorkList.h -- Internal worklist used in SVF---------------------------//
2 //
3 // SVF: Static Value-Flow Analysis
4 //
5 // Copyright (C) <2013-2017> <Yulei Sui>
6 //
7 
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
17 
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 /*
24  * @file: WorkList.h
25  * @author: yesen
26  * @date: 06/12/2013
27  * @version: 1.0
28  *
29  * @section LICENSE
30  *
31  * @section DESCRIPTION
32  *
33  */
34 
35 
36 #ifndef WORKLIST_H_
37 #define WORKLIST_H_
38 
39 #include "SVFIR/SVFValue.h"
40 
41 #include <assert.h>
42 #include <cstdlib>
43 #include <vector>
44 #include <deque>
45 #include <set>
46 
47 namespace SVF
48 {
49 
55 template<class Data>
56 class List
57 {
58  class ListNode
59  {
60  public:
61  ListNode(const Data &d)
62  {
63  data = d;
64  next = nullptr;
65  }
66 
67  ~ListNode() {}
68 
69  Data data;
71  };
72 
73  typedef Set<Data> DataSet;
74  typedef ListNode Node;
75 
76 public:
77  List()
78  {
79  head = nullptr;
80  tail = nullptr;
81  }
82 
83  ~List() {}
84 
85  inline bool empty() const
86  {
87  return (head == nullptr);
88  }
89 
90  inline bool find(const Data &data) const
91  {
92  return nodeSet.find(data) != nodeSet.end();
93  }
94 
95  void push(const Data &data)
96  {
97  if (nodeSet.find(data) == nodeSet.end())
98  {
99  Node *new_node = new Node(data);
100  if (head == nullptr)
101  head = new_node;// the list is empty
102  else
103  tail->next = new_node;
104  tail = new_node;
105  }
106  }
107 
108  Data pop()
109  {
110  assert(head != nullptr && "list is empty");
112  Node *head_node = head;
113 
115  head = head->next;
116  if (head == nullptr)
117  tail = nullptr;
118 
119  Data data = head_node->data;
120  nodeSet.erase(data);
121  delete head_node;
122  return data;
123  }
124 
125 private:
129 };
130 
136 template<class Data>
138 {
140  typedef std::deque<Data> DataDeque;
141 public:
143 
145 
146  inline bool empty() const
147  {
148  return data_list.empty();
149  }
150 
151  inline u32_t size() const
152  {
153  assert(data_list.size() == data_set.size() && "list and set must be the same size!");
154  return data_list.size();
155  }
156 
157  inline bool find(const Data &data) const
158  {
159  return data_set.find(data) != data_set.end();
160  }
161 
165  inline bool push(const Data &data)
166  {
167  if (!find(data))
168  {
169  data_list.push_back(data);
170  data_set.insert(data);
171  return true;
172  }
173  else
174  return false;
175  }
176 
180  inline void removeFront()
181  {
182  assert(!empty() && "work list is empty");
183  data_set.erase(front());
184  data_list.pop_front();
185  }
186 
190  inline Data &front()
191  {
192  assert(!empty() && "work list is empty");
193  Data &data = data_list.front();
194  return data;
195  }
196 
200  inline Data pop()
201  {
202  assert(!empty() && "work list is empty");
203  Data data = data_list.front();
204  data_list.pop_front();
205  data_set.erase(data);
206  return data;
207  }
208 
212  inline void clear()
213  {
214  data_list.clear();
215  data_set.clear();
216  }
217 
218 private:
221 };
222 
228 template<class Data>
230 {
232  typedef std::vector<Data> DataVector;
233 public:
235 
237 
238  inline bool empty() const
239  {
240  return data_list.empty();
241  }
242 
243  inline u32_t size() const
244  {
245  assert(data_list.size() == data_set.size() && "list and set must be the same size!");
246  return data_list.size();
247  }
248 
249  inline bool find(const Data &data) const
250  {
251  return data_set.find(data) != data_set.end();;
252  }
253 
257  inline bool push(const Data &data)
258  {
259  if (!find(data))
260  {
261  data_list.push_back(data);
262  data_set.insert(data);
263  return true;
264  }
265  else
266  return false;
267  }
268 
272  inline Data pop()
273  {
274  assert(!empty() && "work list is empty");
275  Data data = data_list.back();
276  data_list.pop_back();
277  data_set.erase(data);
278  return data;
279  }
280 
284  inline void removeBack()
285  {
286  assert(!empty() && "work list is empty");
287  data_set.erase(back());
288  data_list.pop_back();
289  }
290 
294  inline Data &back()
295  {
296  assert(!empty() && "work list is empty");
297  Data &data = data_list.back();
298  return data;
299  }
300 
304  inline void clear()
305  {
306  data_list.clear();
307  data_set.clear();
308  }
309 
310 private:
313 };
314 
315 } // End namespace SVF
316 
317 #endif /* WORKLIST_H_ */
bool push(const Data &data)
Definition: WorkList.h:165
DataDeque data_list
work list using std::vector.
Definition: WorkList.h:220
std::deque< Data > DataDeque
Definition: WorkList.h:140
Set< Data > DataSet
Definition: WorkList.h:139
DataSet data_set
store all data in the work list.
Definition: WorkList.h:219
void removeFront()
Definition: WorkList.h:180
bool empty() const
Definition: WorkList.h:146
bool find(const Data &data) const
Definition: WorkList.h:157
u32_t size() const
Definition: WorkList.h:151
Data & front()
Definition: WorkList.h:190
bool empty() const
Definition: WorkList.h:238
void removeBack()
Definition: WorkList.h:284
std::vector< Data > DataVector
Definition: WorkList.h:232
DataSet data_set
store all data in the work list.
Definition: WorkList.h:311
Data & back()
Definition: WorkList.h:294
Set< Data > DataSet
Definition: WorkList.h:231
bool find(const Data &data) const
Definition: WorkList.h:249
DataVector data_list
work list using std::vector.
Definition: WorkList.h:312
bool push(const Data &data)
Definition: WorkList.h:257
u32_t size() const
Definition: WorkList.h:243
ListNode * next
Definition: WorkList.h:70
ListNode(const Data &d)
Definition: WorkList.h:61
bool empty() const
Definition: WorkList.h:85
Node * head
Definition: WorkList.h:127
ListNode Node
Definition: WorkList.h:74
bool find(const Data &data) const
Definition: WorkList.h:90
Data pop()
Definition: WorkList.h:108
Node * tail
Definition: WorkList.h:128
DataSet nodeSet
Definition: WorkList.h:126
Set< Data > DataSet
Definition: WorkList.h:73
void push(const Data &data)
Definition: WorkList.h:95
for isBitcode
Definition: BasicTypes.h:68
unsigned u32_t
Definition: GeneralType.h:46
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: GeneralType.h:96