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
+24 -20
View File
@@ -1119,21 +1119,24 @@ DataTypeList* DataTypeList::getInstance() {
void DataTypeList::dump(OutputFormat outputFormat, bool appendDivisor, ostream* output) const {
bool json = outputFormat & OF_JSON;
string sep = "\n";
for (const auto &it: m_typesByIdLength) {
const DataType *dataType = it.second;
if (json) {
*output << sep << " {";
}
if ((dataType->getBitCount() % 8) != 0) {
dataType->dump(outputFormat, dataType->getBitCount(), appendDivisor, output);
} else {
dataType->dump(outputFormat, dataType->getBitCount() / 8, appendDivisor, output);
}
if (json) {
*output << "}";
sep = ",\n";
} else {
*output << "\n";
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;
if (json) {
*output << sep << " {";
}
if ((dataType->getBitCount() % 8) != 0) {
dataType->dump(outputFormat, dataType->getBitCount(), appendDivisor, output);
} else {
dataType->dump(outputFormat, dataType->getBitCount() / 8, appendDivisor, output);
}
if (json) {
*output << "}";
sep = ",\n";
} else {
*output << "\n";
}
}
}
}
@@ -1144,6 +1147,7 @@ void DataTypeList::clear() {
}
m_cleanupTypes.clear();
m_typesByIdLength.clear();
m_typesById.clear();
}
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
}
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);
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
}
m_typesByIdLength[dataType->getId()] = dataType;
m_typesById[dataType->getId()] = dataType;
m_cleanupTypes.push_back(dataType);
return RESULT_OK;
}
@@ -1176,8 +1180,8 @@ const DataType* DataTypeList::get(const string& id, size_t length) const {
return it->second;
}
}
auto it = m_typesByIdLength.find(id);
if (it == m_typesByIdLength.end()) {
auto it = m_typesById.find(id);
if (it == m_typesById.end()) {
return nullptr;
}
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.
* @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.
* @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:
/** the known @a DataType instances by ID and length (i.e. "ID:BITS") as well
* as without length for those without @a WLS flag.
/** the known @a DataType instances by ID only. */
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. */
map<string, const DataType*> m_typesByIdLength;