// OpenSTA, Static Timing Analyzer // Copyright (c) 2020, 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 . #pragma once #include #include #include namespace sta { // Add convenience functions around STL container. template > class Set : public std::set { public: Set() : std::set() {} explicit Set(const CMP &cmp) : std::set(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 *set1, const std::set *set2); // True if set2 is a subset of this set. bool isSubset(const std::set *set2); void insertSet(const std::set *set2); void deleteContents() { Iterator iter(this); while (iter.hasNext()) delete iter.next(); } void deleteContentsClear() { deleteContents(); this->clear(); } static bool intersects(std::set &set1, std::set &set2); static bool intersects(std::set *set1, std::set *set2); // Java style container itererator // Set::Iterator iter(set); // while (iter.hasNext()) { // Key *v = iter.next(); // } class Iterator { public: Iterator() : container_(nullptr) {} explicit Iterator(std::set *container) : container_(container) { if (container_ != nullptr) iter_ = container_->begin(); } explicit Iterator(std::set &container) : container_(&container) { if (container_ != nullptr) iter_ = container_->begin(); } void init(std::set *container) { container_ = container; if (container_ != nullptr) iter_=container_->begin();} void init(std::set &container) { container_ = &container; if (container_ != nullptr) iter_=container_->begin();} bool hasNext() { return container_ != nullptr && iter_ != container_->end(); } KEY next() { return *iter_++; } std::set *container() { return container_; } private: std::set *container_; typename std::set::iterator iter_; }; class ConstIterator { public: ConstIterator() : container_(nullptr) {} explicit ConstIterator(const std::set *container) : container_(container) { if (container_ != nullptr) iter_ = container_->begin(); } explicit ConstIterator(const std::set &container) : container_(&container) { if (container_ != nullptr) iter_ = container_->begin(); } void init(const std::set *container) { container_ = container; if (container_ != nullptr) iter_=container_->begin();} void init(const std::set &container) { container_ = &container; if (container_ != nullptr) iter_=container_->begin();} bool hasNext() { return container_ != nullptr && iter_ != container_->end(); } KEY next() { return *iter_++; } const std::set *container() { return container_; } private: const std::set *container_; typename std::set::const_iterator iter_; }; }; template bool Set::equal(const std::set *set1, const std::set *set2) { if ((set1 == nullptr || set1->empty()) && (set2 == nullptr || set2->empty())) return true; else if (set1 && set2) { if (set1->size() == set2->size()) { typename Set::ConstIterator iter1(set1); typename Set::ConstIterator iter2(set2); while (iter1.hasNext() && iter2.hasNext()) { if (iter1.next() != iter2.next()) return false; } return true; } else return false; } else return false; } template bool Set::isSubset(const std::set *set2) { if (this->empty() && set2->empty()) return true; else { typename Set::ConstIterator iter2(set2); while (iter2.hasNext()) { const KEY key2 = iter2.next(); if (!hasKey(key2)) return false; } return true; } } template bool Set::intersects(std::set &set1, std::set &set2) { return intersects(&set1, &set2); } template bool Set::intersects(std::set *set1, std::set *set2) { if (set1 && !set1->empty() && set2 && !set2->empty()) { const std::set *small = set1; const std::set *big = set2; if (small->size() > big->size()) { small = set2; big = set1; } auto iter1 = big->begin(); auto last1 = big->end(); auto iter2 = small->begin(); auto last2 = small->end(); if (static_cast(small->size() + big->size()) < (small->size() * log(static_cast(big->size())))) { while (iter1 != last1 && iter2 != last2) { if (*iter1 < *iter2) ++iter1; else if (*iter2 < *iter1) ++iter2; else return true; } } else { for (/* empty */; iter2 != last2; ++iter2) { const KEY key2 = *iter2; if (big->find(key2) != last1) return true; } } } return false; } // A complicated way to call the base class operator<. template bool operator<(const Set &set1, const Set &set2) { const std::set &set1_base = set1; const std::set &set2_base = set2; return set1_base < set2_base; } template void Set::insertSet(const std::set *set2) { if (set2) this->insert(set2->begin(), set2->end()); } } // namespace