/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. OpenFOAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see . Description Test HashTable resizing \*---------------------------------------------------------------------------*/ #include "argList.H" #include "HashSet.H" #include "HashTable.H" #include "Map.H" #include "cpuTime.H" #include "memInfo.H" #include #include #include #include // #undef ORDERED // #define ORDERED using namespace Foam; template Ostream& printInfo(Ostream& os, const HashTable>& ht) { os << " (size " << ht.size() << " capacity " << ht.capacity() << ") "; return os; } template inline void insertElem ( #ifdef ORDERED std::set& container, #else std::unordered_set>& container, #endif K k, V v ) { container.insert(k); } template inline void insertElem ( #ifdef ORDERED std::map& container, #else std::unordered_map>& container, #endif K k, V v ) { container.insert(std::make_pair(k, v)); } template inline void insertElem ( HashSet>& container, K k, V v ) { container.insert(k); } template inline void insertElem ( HashTable>& container, K k, V v ) { container.insert(k, v); } template inline void loopInsert(Container& container, const label n) { for (label i = 0; i < n; i++) { insertElem(container, i, i); } } template inline unsigned long loopFind(const Container& container, const label n) { const auto endIter = container.end(); unsigned long sum = 0; for (label i = 0; i < n; i++) { if (container.find(i) != endIter) { ++sum; } } return sum; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: int main(int argc, char *argv[]) { const label nLoops = 200; const label nFind = 10; const label nElem = 1000000; argList::noBanner(); argList::addBoolOption("find", "test find"); argList::addBoolOption("set", "test HashSet"); argList::addBoolOption("std", "std::unordered_map or std::unordered_set"); argList args(argc, argv); const bool optFnd = args.found("find"); const bool optSet = args.found("set"); const bool optStd = args.found("std"); cpuTime timer; Info<< "insert " << nElem << " (int) elements"; if (optFnd) { Info<< ", then find " << (nFind*nLoops) << " times\n"; } else { Info<< " repeated " << nLoops << " times " << endl; } if (false) { // Verify that resizing around (0) doesn't fail HashTable> map(32); printInfo(Info, map) << endl; map.insert(10, 1000); map.resize(0); printInfo(Info, map) << endl; map.resize(10); printInfo(Info, map) << endl; map.clear(); printInfo(Info, map) << endl; map.resize(0); printInfo(Info, map) << endl; return 0; } if (optStd) { if (optFnd) { #ifdef ORDERED Info<< "using stl::set" << endl; std::set