Revert "remove unnecessary map"

This reverts commit 9af9bcf0f5.
This commit is contained in:
john30
2021-11-05 16:30:27 +01:00
parent 9af9bcf0f5
commit 2fcf2256d1
2 changed files with 30 additions and 24 deletions
+10 -6
View File
@@ -1119,7 +1119,9 @@ DataTypeList* DataTypeList::getInstance() {
void DataTypeList::dump(OutputFormat outputFormat, bool appendDivisor, ostream* output) const { void DataTypeList::dump(OutputFormat outputFormat, bool appendDivisor, ostream* output) const {
bool json = outputFormat & OF_JSON; bool json = outputFormat & OF_JSON;
string sep = "\n"; string sep = "\n";
for (const auto &it: m_typesByIdLength) { for (int withLength=0; withLength<2; withLength++) {
const map<string, const DataType*>* types = withLength==0 ? &m_typesById : &m_typesByIdLength;
for (const auto &it: *types) {
const DataType *dataType = it.second; const DataType *dataType = it.second;
if (json) { if (json) {
*output << sep << " {"; *output << sep << " {";
@@ -1137,6 +1139,7 @@ void DataTypeList::dump(OutputFormat outputFormat, bool appendDivisor, ostream*
} }
} }
} }
}
void DataTypeList::clear() { void DataTypeList::clear() {
for (auto& it : m_cleanupTypes) { for (auto& it : m_cleanupTypes) {
@@ -1144,6 +1147,7 @@ void DataTypeList::clear() {
} }
m_cleanupTypes.clear(); m_cleanupTypes.clear();
m_typesByIdLength.clear(); m_typesByIdLength.clear();
m_typesById.clear();
} }
result_t DataTypeList::add(const DataType* dataType) { result_t DataTypeList::add(const DataType* dataType) {
@@ -1155,14 +1159,14 @@ result_t DataTypeList::add(const DataType* dataType) {
return RESULT_ERR_DUPLICATE_NAME; // duplicate key return RESULT_ERR_DUPLICATE_NAME; // duplicate key
} }
m_typesByIdLength[str.str()] = dataType; m_typesByIdLength[str.str()] = dataType;
if (dataType->hasFlag(WLS) || m_typesByIdLength.find(dataType->getId()) != m_typesByIdLength.end()) { if (dataType->hasFlag(WLS) || m_typesById.find(dataType->getId()) != m_typesById.end()) {
m_cleanupTypes.push_back(dataType); m_cleanupTypes.push_back(dataType);
return RESULT_OK; // only store first one without WLS flag as default return RESULT_OK; // only store first one without WLS flag as default
} }
} else if (m_typesByIdLength.find(dataType->getId()) != m_typesByIdLength.end()) { } else if (m_typesById.find(dataType->getId()) != m_typesById.end()) {
return RESULT_ERR_DUPLICATE_NAME; // duplicate key return RESULT_ERR_DUPLICATE_NAME; // duplicate key
} }
m_typesByIdLength[dataType->getId()] = dataType; m_typesById[dataType->getId()] = dataType;
m_cleanupTypes.push_back(dataType); m_cleanupTypes.push_back(dataType);
return RESULT_OK; return RESULT_OK;
} }
@@ -1176,8 +1180,8 @@ const DataType* DataTypeList::get(const string& id, size_t length) const {
return it->second; return it->second;
} }
} }
auto it = m_typesByIdLength.find(id); auto it = m_typesById.find(id);
if (it == m_typesByIdLength.end()) { if (it == m_typesById.end()) {
return nullptr; return nullptr;
} }
if (length > 0 && !it->second->isAdjustableLength()) { if (length > 0 && !it->second->isAdjustableLength()) {
+6 -4
View File
@@ -615,17 +615,19 @@ class DataTypeList {
* Returns an iterator pointing to the first ID/@a DataType pair. * Returns an iterator pointing to the first ID/@a DataType pair.
* @return an iterator pointing to the first ID/@a DataType pair. * @return an iterator pointing to the first ID/@a DataType pair.
*/ */
map<string, const DataType*>::const_iterator begin() const { return m_typesByIdLength.begin(); } map<string, const DataType*>::const_iterator begin() const { return m_typesById.begin(); }
/** /**
* Returns an iterator pointing one past the last ID/@a DataType pair. * Returns an iterator pointing one past the last ID/@a DataType pair.
* @return an iterator pointing one past the last ID/@a DataType pair. * @return an iterator pointing one past the last ID/@a DataType pair.
*/ */
map<string, const DataType*>::const_iterator end() const { return m_typesByIdLength.end(); } map<string, const DataType*>::const_iterator end() const { return m_typesById.end(); }
private: private:
/** the known @a DataType instances by ID and length (i.e. "ID:BITS") as well /** the known @a DataType instances by ID only. */
* as without length for those without @a WLS flag. map<string, const DataType*> m_typesById;
/** the known @a DataType instances by ID and length (i.e. "ID:BITS").
* Note: adjustable length types are stored by ID only. */ * Note: adjustable length types are stored by ID only. */
map<string, const DataType*> m_typesByIdLength; map<string, const DataType*> m_typesByIdLength;