OpenSTA/include/sta/UnorderedMap.hh

178 lines
5.1 KiB
C++
Raw Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
2020-03-07 03:50:37 +01:00
// Copyright (c) 2020, Parallax Software, Inc.
2018-09-28 17:54:21 +02:00
//
// 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/>.
2020-02-16 01:13:16 +01:00
#pragma once
2018-09-28 17:54:21 +02:00
#include <unordered_map>
#include <algorithm>
namespace sta {
// Add convenience functions around STL container.
template <class KEY, class VALUE, class HASH = std::hash<KEY>, class EQUAL = std::equal_to<KEY> >
class UnorderedMap : public std::unordered_map<KEY, VALUE, HASH, EQUAL>
{
public:
UnorderedMap() :
std::unordered_map<KEY, VALUE, HASH, EQUAL>()
{
}
explicit UnorderedMap(size_t size,
const HASH &hash,
const EQUAL &equal) :
std::unordered_map<KEY, VALUE, HASH, EQUAL>(size, hash, equal)
{
}
// Find out if key is in the set.
bool
hasKey(const KEY key) const
{
return this->find(key) != this->end();
}
// Find the value corresponding to key.
VALUE
findKey(const KEY key) const
{
2019-03-13 01:25:53 +01:00
auto find_iter = this->find(key);
2018-09-28 17:54:21 +02:00
if (find_iter != this->end())
return find_iter->second;
else
2019-03-13 01:25:53 +01:00
return nullptr;
2018-09-28 17:54:21 +02:00
}
void
findKey(const KEY key,
// Return Values.
VALUE &value,
bool &exists) const
{
2019-03-13 01:25:53 +01:00
auto find_iter = this->find(key);
2018-09-28 17:54:21 +02:00
if (find_iter != this->end()) {
value = find_iter->second;
exists = true;
}
else
exists = false;
}
void
findKey(const KEY &key,
// Return Values.
KEY &map_key,
VALUE &value,
bool &exists) const
{
2019-03-13 01:25:53 +01:00
auto find_iter = this->find(key);
2018-09-28 17:54:21 +02:00
if (find_iter != this->end()) {
map_key = find_iter->first;
value = find_iter->second;
exists = true;
}
else
exists = false;
}
void
insert(const KEY &key,
VALUE value)
{
this->operator[](key) = value;
}
void
deleteContents()
2018-09-28 17:54:21 +02:00
{
Iterator iter(this);
while (iter.hasNext())
delete iter.next();
2018-09-28 17:54:21 +02:00
}
void
deleteArrayContents()
2018-09-28 17:54:21 +02:00
{
Iterator iter(this);
while (iter.hasNext())
delete [] iter.next();
2018-09-28 17:54:21 +02:00
}
void
deleteContentsClear()
{
deleteContents();
std::unordered_map<KEY,VALUE,HASH,EQUAL>::clear();
}
// Java style container itererator
// Map::Iterator<string *, Value, stringLess> iter(map);
// while (iter.hasNext()) {
// Value *v = iter.next();
// }
class Iterator
{
public:
2019-03-13 01:25:53 +01:00
Iterator() : container_(nullptr) {}
2018-09-28 17:54:21 +02:00
explicit Iterator(std::unordered_map<KEY,VALUE,HASH,EQUAL> *container) :
container_(container)
2019-03-13 01:25:53 +01:00
{ if (container_ != nullptr) iter_ = container_->begin(); }
2018-09-28 17:54:21 +02:00
explicit Iterator(std::unordered_map<KEY,VALUE,HASH,EQUAL> &container) :
container_(&container)
2019-03-13 01:25:53 +01:00
{ if (container_ != nullptr) iter_ = container_->begin(); }
2018-09-28 17:54:21 +02:00
void init(std::unordered_map<KEY,VALUE,HASH,EQUAL> *container)
2019-03-13 01:25:53 +01:00
{ container_ = container; if (container_ != nullptr) iter_=container_->begin();}
2018-09-28 17:54:21 +02:00
void init(std::unordered_map<KEY,VALUE,HASH,EQUAL> &container)
2019-03-13 01:25:53 +01:00
{ container_ = &container; if (container_ != nullptr) iter_=container_->begin();}
bool hasNext() { return container_ != nullptr && iter_ != container_->end(); }
2018-09-28 17:54:21 +02:00
VALUE next() { return iter_++->second; }
void next(KEY &key,
VALUE &value)
{ key = iter_->first; value = iter_->second; iter_++; }
std::unordered_map<KEY,VALUE,HASH,EQUAL> *container() { return container_; }
private:
std::unordered_map<KEY,VALUE,HASH,EQUAL> *container_;
typename std::unordered_map<KEY,VALUE,HASH,EQUAL>::iterator iter_;
};
class ConstIterator
{
public:
2019-03-13 01:25:53 +01:00
ConstIterator() : container_(nullptr) {}
2018-09-28 17:54:21 +02:00
explicit ConstIterator(const std::unordered_map<KEY,VALUE,HASH,EQUAL> *container) :
container_(container)
2019-03-13 01:25:53 +01:00
{ if (container_ != nullptr) iter_ = container_->begin(); }
2018-09-28 17:54:21 +02:00
explicit ConstIterator(const std::unordered_map<KEY,VALUE,HASH,EQUAL> &container) :
container_(&container)
2019-03-13 01:25:53 +01:00
{ if (container_ != nullptr) iter_ = container_->begin(); }
2018-09-28 17:54:21 +02:00
void init(const std::unordered_map<KEY,VALUE,HASH,EQUAL> *container)
2019-03-13 01:25:53 +01:00
{ container_ = container; if (container_ != nullptr) iter_=container_->begin();}
2018-09-28 17:54:21 +02:00
void init(const std::unordered_map<KEY,VALUE,HASH,EQUAL> &container)
2019-03-13 01:25:53 +01:00
{ container_ = &container; if (container_ != nullptr) iter_=container_->begin();}
bool hasNext() { return container_ != nullptr && iter_ != container_->end(); }
2018-09-28 17:54:21 +02:00
VALUE next() { return iter_++->second; }
void next(KEY &key,
VALUE &value)
{ key = iter_->first; value = iter_->second; iter_++; }
const std::unordered_map<KEY,VALUE,HASH,EQUAL> *container() { return container_; }
private:
const std::unordered_map<KEY,VALUE,HASH,EQUAL> *container_;
typename std::unordered_map<KEY,VALUE,HASH,EQUAL>::const_iterator iter_;
};
};
} // namespace