aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/nextpnr.cc35
-rw-r--r--common/nextpnr.h57
2 files changed, 80 insertions, 12 deletions
diff --git a/common/nextpnr.cc b/common/nextpnr.cc
new file mode 100644
index 00000000..ce2cef63
--- /dev/null
+++ b/common/nextpnr.cc
@@ -0,0 +1,35 @@
+/*
+ * nextpnr -- Next Generation Place and Route
+ *
+ * Copyright (C) 2018 Clifford Wolf <clifford@clifford.at>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+
+#include "nextpnr.h"
+
+NEXTPNR_NAMESPACE_BEGIN
+
+std::unordered_map<std::string, int> *IdString::database_str_to_idx = nullptr;
+std::vector<const std::string*> *IdString::database_idx_to_str = nullptr;
+
+void IdString::initialize()
+{
+ database_str_to_idx = new std::unordered_map<std::string, int>;
+ database_idx_to_str = new std::vector<const std::string*>;
+ auto insert_rc = database_str_to_idx->insert({std::string(), 0});
+ database_idx_to_str->push_back(&insert_rc.first->first);
+}
+
+NEXTPNR_NAMESPACE_END
diff --git a/common/nextpnr.h b/common/nextpnr.h
index bc92719f..dad6239f 100644
--- a/common/nextpnr.h
+++ b/common/nextpnr.h
@@ -43,25 +43,58 @@ NEXTPNR_NAMESPACE_BEGIN
struct IdString
{
- std::string data;
+ int index = 0;
+
+ static std::unordered_map<std::string, int> *database_str_to_idx;
+ static std::vector<const std::string*> *database_idx_to_str;
+
+ void initialize();
IdString() {}
- IdString(std::string s) : data(s) {}
- IdString(const char *s) : data(s) {}
- const char *c_str() const { return data.c_str(); }
- const std::string &str() const { return data; }
+ IdString(const std::string &s)
+ {
+ if (database_str_to_idx == nullptr)
+ initialize();
+
+ auto it = database_str_to_idx->find(s);
+ if (it == database_str_to_idx->end()) {
+ index = database_idx_to_str->size();
+ auto insert_rc = database_str_to_idx->insert({s, index});
+ database_idx_to_str->push_back(&insert_rc.first->first);
+ } else {
+ index = it->second;
+ }
+ }
+
+ IdString(const char *s)
+ {
+ if (database_str_to_idx == nullptr)
+ initialize();
+
+ auto it = database_str_to_idx->find(s);
+ if (it == database_str_to_idx->end()) {
+ index = database_idx_to_str->size();
+ auto insert_rc = database_str_to_idx->insert({s, index});
+ database_idx_to_str->push_back(&insert_rc.first->first);
+ } else {
+ index = it->second;
+ }
+ }
+
+ const std::string &str() const { return *database_idx_to_str->at(index); }
+ const char *c_str() const { return str().c_str(); }
operator const char *() const { return c_str(); }
operator const std::string &() const { return str(); }
- bool operator<(const IdString &other) const { return data < other.data; }
- bool operator==(const IdString &other) const { return data == other.data; }
- bool operator==(const std::string &s) const { return data == s; }
- bool operator==(const char *s) const { return data == s; }
+ bool operator<(const IdString &other) const { return index < other.index; }
+ bool operator==(const IdString &other) const { return index == other.index; }
+ bool operator==(const std::string &s) const { return str() == s; }
+ bool operator==(const char *s) const { return str() == s; }
- size_t size() const { return data.size(); }
- bool empty() const { return data.empty(); }
+ size_t size() const { return str().size(); }
+ bool empty() const { return index == 0; }
};
NEXTPNR_NAMESPACE_END
@@ -72,7 +105,7 @@ template <> struct hash<NEXTPNR_NAMESPACE_PREFIX IdString>
std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX IdString &obj) const
noexcept
{
- return std::hash<std::string>()(obj.data);
+ return obj.index;
}
};
}