none
How to optimize the code snippet which has several calls to find method using map? RRS feed

  • Question

  • Hello All,

    I wrote a functionality to read files and load them into the map (m_fileParams).

    Then I am searching the specific keys in the map container using "find". Here using "find" I am searching different keys. 

    My code is working fine. But I wanted to know how best I can optimize the code?

    In several places I have used "find" to search the key in the map. Is there any way I can write generic method?

    "m_fileParams" is declared like as shown below in the .h file:

    std::map<std::wstring, std::wstring> m_fileParams;

    below is the .cpp file:

    bool McEMRMgr::ReadFile()
    {
    IOperation* pFileReader = new McFileReader();
    std::map<std::wstring, std::wstring> mapFileReaderResponse;
    if(pFileReader)
    {
    std::wstring strVendor, wstrFileLocation, wstrRegLocation;
    std::wstring FileType, regType;

    auto manufacSearchValue = m_fileParams.find(VENDOR);
    if (manufacSearchValue != m_fileParams.end())
    {
    strVendor.assign(manufacSearchValue->second);
    transform(
    strVendor.begin(), strVendor.end(),
    strVendor.begin(),
    towlower);

    }
    else
    {
    strVendor.assign(L"default");
    FileType.assign(L".File");
    regType.assign(L".registry");
    }
    auto emailSearchValue = m_fileParams.find(L"email_locations." + strVendor + L".type" + FileType);
    if (emailSearchValue != m_fileParams.end())

    if (emailSearchValue->second.compare(L"File") == 0)
    {
    auto FileSearchPath = m_fileParams.find(L"email_locations." + strVendor + L".paths" + FileType);
    wstrFileLocation = FileSearchPath->second;
    }
    }


    emailSearchValue = m_fileParams.find(L"email_locations." + strVendor + L".type" + regType);
    if (emailSearchValue->second.compare(L"registry") == 0)
    {
    auto regSearchPath = m_fileParams.find(L"email_locations." + strVendor + L".paths" + regType);
    wstrRegLocation = regSearchPath->second;
    }
    }
    if (wstrFileLocation.length() != 0)
    {
    std::wstring oemFolderPath;
    auto oemSearchType = m_fileParams.find(L"oem_info_folder");
    if (oemSearchType != m_fileParams.end())
    {
    oemFolderPath.assign(oemSearchType->second);
    }
    else
    {
    oemFolderPath.assign(OEM_FOLDER_DEFAULT_PATH);
    }
    std::wstring oemPathPublicKey(oemFolderPath), oemPathSessionKey(oemFolderPath), oemPathUserChoices(oemFolderPath);
    oemPathPublicKey.append(PUBLIC_KEY_File);
    oemPathSessionKey.append(SESSION_KEY_File);
    oemPathUserChoices.append(USERCHOICES_File);
    pFileReader->SetParams((wchar_t*)oemPathPublicKey.c_str(), L"File");
    pFileReader->Execute();
    pFileReader->SetParams((wchar_t*)oemPathSessionKey.c_str(), L"File");
    pFileReader->Execute();
    pFileReader->SetParams((wchar_t*)oemPathUserChoices.c_str(), L"File");
    pFileReader->Execute();

    wchar_t* ptrWStr;
    wchar_t* buffer;
    ptrWStr = wcstok_s((wchar_t*)wstrFileLocation.c_str(), L" ", &buffer);
    while (ptrWStr != NULL)
    {
    pFileReader->SetParams(ptrWStr, L"File");
    pFileReader->Execute();
    mapFileReaderResponse = pFileReader->GetResponse();
    for (std::map<std::wstring, std::wstring>::iterator it = mapFileReaderResponse.begin(); it != mapFileReaderResponse.end(); ++it)
    {
    m_fileParams.insert({ it->first, it->second });
    }
    ptrWStr = wcstok_s(nullptr, L" ", &buffer);
    }

    if (mapFileReaderResponse.size() != 0)
    {
    m_cJobState.eFileStatus = OperationState::Success;
    }
    else
    {
    m_cJobState.eFileStatus = OperationState::Failed;
    }
    }

    if (wstrRegLocation.length() != 0)

    pFileReader->SetParams((wchar_t*)wstrRegLocation.c_str(), L"registry");
    pFileReader->Execute();
    mapFileReaderResponse = pFileReader->GetResponse();
    for (std::map<std::wstring, std::wstring>::iterator it = mapFileReaderResponse.begin(); it != mapFileReaderResponse.end(); ++it)
    {
    m_fileParams.insert({ it->first, it->second });
    }
    }
    auto emailSearchType = m_fileParams.find(L"email");
    std::wstring emailValue = emailSearchType->second;
    if (emailValue.length() > 0)
    {
    MakeCopyofEmailToHKLM(emailValue);
    }
    else
    {
    }

    delete pFileReader;
    }

    return true;
    }

    Someone please help me by providing your review comments.

    Thanks in advance.

    Friday, August 16, 2019 4:10 PM

Answers

  • Hi,

    Thank you for posting here.

    >>How to optimize the code snippet which has several calls to find method using map? 

    I suggest you could try to use a template class, because the functionality it provides is generic and independent of element or key type. The data types used for elements and keys are specified as parameters in the class template together with the comparison function and allocator.

    I suggest that you base the choice of container type on the kind of searching and inserting that is required by the application. Associative containers are optimized for the operations of lookup, insertion, and removal. The member functions that explicitly support these operations perform them in a worst-case time that is proportional to the logarithm of the number of elements in the container. Inserting elements invalidates no iterators, and removing elements invalidates only those iterators that specifically pointed to the removed elements.

    I suggest you could refer to the links:
    https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=vs-2019
    https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=vs-2019#find

    Best Regards,

    Jeanine Zhang

    Tuesday, August 20, 2019 6:08 AM
    Moderator