Use ununordered_map instead of map

Should result in a performance boost when reading large files.
This commit is contained in:
Roman Telezhynskyi 2021-11-23 11:15:11 +02:00
parent c51d9d5b30
commit 642fbe815d
5 changed files with 16 additions and 19 deletions

View File

@ -119,7 +119,7 @@ void dx_iface::writeHeader(DRW_Header &data){
//complete copy of header vars: //complete copy of header vars:
data = cData.headerC; data = cData.headerC;
//or copy one by one: //or copy one by one:
// for (std::map<std::string,DRW_Variant*>::iterator it=cData->headerC.vars.begin(); it != cData->headerC.vars.end(); ++it) // for (auto it=cData->headerC.vars.begin(); it != cData->headerC.vars.end(); ++it)
// data.vars[it->first] = new DRW_Variant( *(it->second) ); // data.vars[it->first] = new DRW_Variant( *(it->second) );
} }

View File

@ -1662,8 +1662,7 @@ void DRW_Header::write(dxfWriter *writer, DRW::Version ver){
} }
#ifdef DRW_DBG #ifdef DRW_DBG
std::map<std::string,DRW_Variant *>::const_iterator it; for ( auto it=vars.begin() ; it != vars.end(); ++it ){
for ( it=vars.begin() ; it != vars.end(); ++it ){
DRW_DBG((*it).first); DRW_DBG("\n"); DRW_DBG((*it).first); DRW_DBG("\n");
} }
#endif #endif
@ -1703,8 +1702,7 @@ void DRW_Header::addCoord(std::string key, DRW_Coord value, int code){
bool DRW_Header::getDouble(std::string key, double *varDouble){ bool DRW_Header::getDouble(std::string key, double *varDouble){
bool result = false; bool result = false;
std::map<std::string,DRW_Variant *>::iterator it; auto it=vars.find( key);
it=vars.find( key);
if (it != vars.end()) { if (it != vars.end()) {
DRW_Variant *var = (*it).second; DRW_Variant *var = (*it).second;
if (var->type == DRW_Variant::DOUBLE) { if (var->type == DRW_Variant::DOUBLE) {
@ -1719,8 +1717,7 @@ bool DRW_Header::getDouble(std::string key, double *varDouble){
bool DRW_Header::getInt(std::string key, int *varInt){ bool DRW_Header::getInt(std::string key, int *varInt){
bool result = false; bool result = false;
std::map<std::string,DRW_Variant *>::iterator it; auto it=vars.find( key);
it=vars.find( key);
if (it != vars.end()) { if (it != vars.end()) {
DRW_Variant *var = (*it).second; DRW_Variant *var = (*it).second;
if (var->type == DRW_Variant::INTEGER) { if (var->type == DRW_Variant::INTEGER) {
@ -1735,8 +1732,7 @@ bool DRW_Header::getInt(std::string key, int *varInt){
bool DRW_Header::getStr(std::string key, std::string *varStr){ bool DRW_Header::getStr(std::string key, std::string *varStr){
bool result = false; bool result = false;
std::map<std::string,DRW_Variant *>::iterator it; auto it=vars.find( key);
it=vars.find( key);
if (it != vars.end()) { if (it != vars.end()) {
DRW_Variant *var = (*it).second; DRW_Variant *var = (*it).second;
if (var->type == DRW_Variant::STRING) { if (var->type == DRW_Variant::STRING) {
@ -1751,8 +1747,7 @@ bool DRW_Header::getStr(std::string key, std::string *varStr){
bool DRW_Header::getCoord(std::string key, DRW_Coord *varCoord){ bool DRW_Header::getCoord(std::string key, DRW_Coord *varCoord){
bool result = false; bool result = false;
std::map<std::string,DRW_Variant *>::iterator it; auto it=vars.find( key);
it=vars.find( key);
if (it != vars.end()) { if (it != vars.end()) {
DRW_Variant *var = (*it).second; DRW_Variant *var = (*it).second;
if (var->type == DRW_Variant::COORD) { if (var->type == DRW_Variant::COORD) {

View File

@ -55,7 +55,7 @@ public:
vportCtrl(), vportCtrl(),
vpEntHeaderCtrl() vpEntHeaderCtrl()
{ {
for (std::map<std::string,DRW_Variant*>::const_iterator it=h.vars.begin(); it!=h.vars.end(); ++it) for (auto it=h.vars.begin(); it!=h.vars.end(); ++it)
{ {
this->vars[it->first] = new DRW_Variant( *(it->second) ); this->vars[it->first] = new DRW_Variant( *(it->second) );
} }
@ -67,7 +67,7 @@ public:
clearVars(); clearVars();
this->version = h.version; this->version = h.version;
this->comments = h.comments; this->comments = h.comments;
for (std::map<std::string,DRW_Variant*>::const_iterator it=h.vars.begin(); it!=h.vars.end(); ++it) for (auto it=h.vars.begin(); it!=h.vars.end(); ++it)
{ {
this->vars[it->first] = new DRW_Variant( *(it->second) ); this->vars[it->first] = new DRW_Variant( *(it->second) );
} }
@ -92,14 +92,16 @@ private:
bool getCoord(std::string key, DRW_Coord *varCoord); bool getCoord(std::string key, DRW_Coord *varCoord);
void clearVars() void clearVars()
{ {
for (std::map<std::string,DRW_Variant*>::iterator it=vars.begin(); it!=vars.end(); ++it) for (auto it=vars.begin(); it!=vars.end(); ++it)
{
delete it->second; delete it->second;
}
vars.clear(); vars.clear();
} }
public: public:
std::map<std::string,DRW_Variant*> vars; std::unordered_map<std::string,DRW_Variant*> vars;
private: private:
std::string comments; std::string comments;
std::string name; std::string name;

View File

@ -1761,8 +1761,7 @@ bool dxfRW::writeObjects() {
//write IMAGEDEF_REACTOR //write IMAGEDEF_REACTOR
for (unsigned int i=0; i<imageDef.size(); i++) { for (unsigned int i=0; i<imageDef.size(); i++) {
DRW_ImageDef *id = imageDef.at(i); DRW_ImageDef *id = imageDef.at(i);
std::map<std::string, std::string>::iterator it; for (auto it=id->reactors.begin() ; it != id->reactors.end(); ++it ) {
for ( it=id->reactors.begin() ; it != id->reactors.end(); ++it ) {
writer->writeString(0, "IMAGEDEF_REACTOR"); writer->writeString(0, "IMAGEDEF_REACTOR");
writer->writeString(5, (*it).first); writer->writeString(5, (*it).first);
writer->writeString(330, (*it).second); writer->writeString(330, (*it).second);
@ -1795,7 +1794,7 @@ bool dxfRW::writeObjects() {
} }
writer->writeString(102, "{ACAD_REACTORS"); writer->writeString(102, "{ACAD_REACTORS");
std::map<std::string, std::string>::iterator it; std::map<std::string, std::string>::iterator it;
for ( it=id->reactors.begin() ; it != id->reactors.end(); ++it ) { for (auto it=id->reactors.begin() ; it != id->reactors.end(); ++it ) {
writer->writeString(330, (*it).first); writer->writeString(330, (*it).first);
} }
writer->writeString(102, "}"); writer->writeString(102, "}");

View File

@ -14,6 +14,7 @@
#define LIBDXFRW_H #define LIBDXFRW_H
#include <string> #include <string>
#include <unordered_map>
#include "drw_entities.h" #include "drw_entities.h"
#include "drw_objects.h" #include "drw_objects.h"
#include "drw_header.h" #include "drw_header.h"
@ -141,7 +142,7 @@ private:
bool applyExt; bool applyExt;
bool writingBlock; bool writingBlock;
int elParts; /*!< parts munber when convert ellipse to polyline */ int elParts; /*!< parts munber when convert ellipse to polyline */
std::map<std::string,int> blockMap; std::unordered_map<std::string,int> blockMap;
std::vector<DRW_ImageDef*> imageDef; /*!< imageDef list */ std::vector<DRW_ImageDef*> imageDef; /*!< imageDef list */
int currHandle; int currHandle;