Files
openfoam/src/OpenFOAM/containers/HashTables/HashTable/HashTableCore.C
Mark Olesen 03d180724b ENH: improve HashTable iterator access and management
- provide key_iterator/const_key_iterator for all hashes,
  reuse directly for HashSet as iterator/const_iterator, respectively.

- additional keys() method for HashTable that returns a wrapped to
  a pair of begin/end const_iterators with additional size/empty
  information that allows these to be used directly by anything else
  expecting things with begin/end/size. Unfortunately does not yet
  work with std::distance().

  Example,
     for (auto& k : labelHashTable.keys())
     {
        ...
     }
2017-05-04 10:17:18 +02:00

87 lines
2.6 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\/ 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "HashTable.H"
#include "uLabel.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(HashTableCore, 0);
}
// Approximately labelMax/4
const Foam::label Foam::HashTableCore::maxTableSize(1 << (sizeof(label)*8-3));
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
Foam::label Foam::HashTableCore::canonicalSize(const label requested_size)
{
if (requested_size < 1)
{
return 0;
}
// Enforce power of two - makes for a vey fast modulus etc.
// Use unsigned for these calculations.
//
// - The lower limit (8) is somewhat arbitrary, but if the hash table
// is too small, there will be many direct table collisions.
// - The uper limit (approx. labelMax/4) must be a power of two,
// need not be extremely large for hashing.
uLabel powerOfTwo = 8; // lower-limit
const uLabel size = requested_size;
if (size < powerOfTwo)
{
return powerOfTwo;
}
else if (requested_size >= maxTableSize)
{
return maxTableSize;
}
else if (size & (size-1)) // <- Modulus of i^2
{
// Determine power-of-two. Brute-force is fast enough.
while (powerOfTwo < size)
{
powerOfTwo <<= 1;
}
return powerOfTwo;
}
else
{
return size;
}
}
// ************************************************************************* //