forked from The-OpenROAD-Project/OpenSTA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet.hh
212 lines (190 loc) · 5.87 KB
/
Set.hh
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2023, Parallax Software, Inc.
//
// 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 3 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, see <https://www.gnu.org/licenses/>.
#pragma once
#include <set>
namespace sta {
// Add convenience functions around STL container.
template <class KEY, class CMP = std::less<KEY> >
class Set : public std::set<KEY, CMP>
{
public:
Set() : std::set<KEY, CMP>() {}
explicit Set(const CMP &cmp) : std::set<KEY, CMP>(cmp) {}
// Find the entry corresponding to key.
KEY findKey(const KEY key) const
{
auto find_iter = this->find(key);
if (find_iter != this->end())
return *find_iter;
else
return nullptr;
}
// Find out if key is in the set.
bool hasKey(const KEY key) const
{
auto find_iter = this->find(key);
return find_iter != this->end();
}
// Slowaris STL doesn't support operator== on sets.
static bool equal(const std::set<KEY, CMP> *set1,
const std::set<KEY, CMP> *set2);
// True if set2 is a subset of this set.
bool isSubset(const std::set<KEY, CMP> *set2);
void insertSet(const std::set<KEY, CMP> *set2);
void
deleteContents()
{
Iterator iter(this);
while (iter.hasNext())
delete iter.next();
}
void
deleteContentsClear()
{
deleteContents();
this->clear();
}
static bool
intersects(const std::set<KEY, CMP> *set1,
const std::set<KEY, CMP> *set2,
CMP key_less);
// Java style container itererator
// Set::Iterator<Key*> iter(set);
// while (iter.hasNext()) {
// Key *v = iter.next();
// }
class Iterator
{
public:
Iterator() : container_(nullptr) {}
explicit Iterator(std::set<KEY, CMP> *container) :
container_(container)
{ if (container_ != nullptr) iter_ = container_->begin(); }
explicit Iterator(std::set<KEY, CMP> &container) :
container_(&container)
{ if (container_ != nullptr) iter_ = container_->begin(); }
void init(std::set<KEY, CMP> *container)
{ container_ = container; if (container_ != nullptr) iter_=container_->begin();}
void init(std::set<KEY, CMP> &container)
{ container_ = &container; if (container_ != nullptr) iter_=container_->begin();}
bool hasNext() { return container_ != nullptr && iter_ != container_->end(); }
KEY next() { return *iter_++; }
std::set<KEY, CMP> *container() { return container_; }
private:
std::set<KEY, CMP> *container_;
typename std::set<KEY, CMP>::iterator iter_;
};
class ConstIterator
{
public:
ConstIterator() : container_(nullptr) {}
explicit ConstIterator(const std::set<KEY, CMP> *container) :
container_(container)
{ if (container_ != nullptr) iter_ = container_->begin(); }
explicit ConstIterator(const std::set<KEY, CMP> &container) :
container_(&container)
{ if (container_ != nullptr) iter_ = container_->begin(); }
void init(const std::set<KEY, CMP> *container)
{ container_ = container; if (container_ != nullptr) iter_=container_->begin();}
void init(const std::set<KEY, CMP> &container)
{ container_ = &container; if (container_ != nullptr) iter_=container_->begin();}
bool hasNext() { return container_ != nullptr && iter_ != container_->end(); }
KEY next() { return *iter_++; }
const std::set<KEY, CMP> *container() { return container_; }
private:
const std::set<KEY, CMP> *container_;
typename std::set<KEY, CMP>::const_iterator iter_;
};
};
template <class KEY, class CMP>
bool
Set<KEY, CMP>::equal(const std::set<KEY, CMP> *set1,
const std::set<KEY, CMP> *set2)
{
if ((set1 == nullptr || set1->empty())
&& (set2 == nullptr || set2->empty()))
return true;
else if (set1 && set2) {
if (set1->size() == set2->size()) {
typename Set<KEY, CMP>::ConstIterator iter1(set1);
typename Set<KEY, CMP>::ConstIterator iter2(set2);
while (iter1.hasNext() && iter2.hasNext()) {
if (iter1.next() != iter2.next())
return false;
}
return true;
}
else
return false;
}
else
return false;
}
template <class KEY, class CMP>
bool
Set<KEY, CMP>::isSubset(const std::set<KEY, CMP> *set2)
{
if (this->empty() && set2->empty())
return true;
else {
typename Set<KEY, CMP>::ConstIterator iter2(set2);
while (iter2.hasNext()) {
const KEY key2 = iter2.next();
if (!hasKey(key2))
return false;
}
return true;
}
}
template <class KEY, class CMP>
bool
Set<KEY, CMP>::intersects(const std::set<KEY, CMP> *set1,
const std::set<KEY, CMP> *set2,
CMP key_less)
{
if (set1 && set2) {
auto iter1 = set1->begin();
auto end1 = set1->end();
auto iter2 = set2->begin();
auto end2 = set2->end();
while (iter1 != end1 && iter2 != end2) {
if (key_less(*iter1, *iter2))
iter1++;
else if (key_less(*iter2, *iter1))
iter2++;
else
return true;
}
}
return false;
}
// A complicated way to call the base class operator<.
template <class KEY, class CMP>
bool
operator<(const Set<KEY, CMP> &set1, const Set<KEY, CMP> &set2)
{
const std::set<KEY, CMP> &set1_base = set1;
const std::set<KEY, CMP> &set2_base = set2;
return set1_base < set2_base;
}
template <class KEY, class CMP>
void
Set<KEY, CMP>::insertSet(const std::set<KEY, CMP> *set2)
{
if (set2)
this->insert(set2->begin(), set2->end());
}
} // namespace