diff --git a/applications/test/hashedWordList/Make/files b/applications/test/hashedWordList/Make/files new file mode 100644 index 0000000000..e92564415e --- /dev/null +++ b/applications/test/hashedWordList/Make/files @@ -0,0 +1,3 @@ +Test-hashedWordList.C + +EXE = $(FOAM_USER_APPBIN)/Test-hashedWordList diff --git a/applications/test/hashedWordList/Make/options b/applications/test/hashedWordList/Make/options new file mode 100644 index 0000000000..6a9e9810b3 --- /dev/null +++ b/applications/test/hashedWordList/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */ +/* EXE_LIBS = -lfiniteVolume */ diff --git a/applications/test/hashedWordList/Test-hashedWordList.C b/applications/test/hashedWordList/Test-hashedWordList.C new file mode 100644 index 0000000000..e7126e6a8e --- /dev/null +++ b/applications/test/hashedWordList/Test-hashedWordList.C @@ -0,0 +1,181 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +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 + +\*---------------------------------------------------------------------------*/ + +#include "IOstreams.H" +#include "ITstream.H" +#include "FlatOutput.H" +#include "hashedWordList.H" + +using namespace Foam; + +Ostream& printInfo(const hashedWordList& list, bool withAddr=false) +{ + Info<< flatOutput(list) << nl << list.lookup() << nl; + if (withAddr) + { + Info<< "addr=" << long(list.cdata()) << nl; + } + + return Info; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + Info<< "Test hashedWordList" << nl; + + hashedWordList list1 + { + "this", + "is", + "a", + "list", + "of", + "words", + }; + + Info<> list4; + Info<(count) + wordList(len) { - forAll(*this, i) + for (label i=0; i < len; ++i) { - List::operator[](i) = lst[i]; + wordList::operator[](i) = array[i]; } - rehash(removeDuplicates); + rehash(unique); } -Foam::hashedWordList::hashedWordList -( - const char** lst, - const bool removeDuplicates -) -{ - // Determine the number of entries - label count = 0; - for (unsigned i = 0; lst[i] && *(lst[i]); ++i) - { - ++count; - } - - List::setSize(count); - forAll(*this, i) - { - List::operator[](i) = lst[i]; - } - - rehash(removeDuplicates); -} +Foam::hashedWordList::hashedWordList(const char** array, bool unique) +: + hashedWordList(CStringList::count(array), array, unique) +{} -Foam::hashedWordList::hashedWordList(Istream& is) -{ - is >> *this; -} - - -// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // - -void Foam::hashedWordList::transfer -( - List& lst, - const bool removeDuplicates -) -{ - List::transfer(lst); - rehash(removeDuplicates); -} - +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // void Foam::hashedWordList::rehash() const { - indices_.clear(); + lookup_.clear(); - forAll(*this, i) + const wordUList& list = *this; + const label len = list.size(); + + for (label i=0; i < len; ++i) { - indices_.insert(List::operator[](i), i); + lookup_.insert(list[i], i); } } void Foam::hashedWordList::uniq() { - indices_.clear(); + lookup_.clear(); - label nElem = 0; - forAll(*this, i) + wordList& list = *this; + const label len = list.size(); + + label count = 0; + for (label i=0; i < len; ++i) { - const word& item = List::operator[](i); + word& item = list[i]; - if (indices_.insert(item, nElem)) + if (lookup_.insert(item, i)) { - if (nElem != i) + if (count != i) { - List::operator[](nElem) = item; + list[count] = std::move(item); } - ++nElem; + ++count; } } - List::setSize(nElem); -} - - -// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // - -Foam::Istream& Foam::operator>>(Istream& is, hashedWordList& lst) -{ - is >> static_cast&>(lst); - lst.rehash(); - - return is; -} - - -Foam::Ostream& Foam::operator<<(Ostream& os, const hashedWordList& lst) -{ - os << static_cast&>(lst); - return os; + list.resize(count); } diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H index a2e68df9df..c7a451942e 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H @@ -25,7 +25,8 @@ Class Foam::hashedWordList Description - A wordList with hashed indices for additional fast lookup by name. + A wordList with hashed named lookup, which can be faster in some + situations than using the normal list find/found methods. SourceFiles hashedWordListI.H @@ -44,11 +45,9 @@ SourceFiles namespace Foam { +// Forward declarations class hashedWordList; - -// Forward declaration of friend functions and operators -Istream& operator>>(Istream& is, hashedWordList& lst); -Ostream& operator<<(Ostream& os, const hashedWordList& lst); +inline Istream& operator>>(Istream& is, hashedWordList& lst); /*---------------------------------------------------------------------------*\ @@ -57,44 +56,34 @@ Ostream& operator<<(Ostream& os, const hashedWordList& lst); class hashedWordList : - public List + public wordList { - // Private data + // Private Data - //- Hash of words/indices - mutable HashTable