/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | Copyright (C) 2016-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 . \*---------------------------------------------------------------------------*/ #ifndef HashSet_C #define HashSet_C #include "HashSet.H" #include "FixedList.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // template template inline Foam::label Foam::HashSet::insertMultiple ( const InputIter begIter, const InputIter endIter ) { label changed = 0; for (InputIter iter = begIter; iter != endIter; ++iter) { if (insert(*iter)) { ++changed; } } return changed; } template template inline Foam::label Foam::HashSet::assignMultiple ( const InputIter begIter, const InputIter endIter, const label sz ) { if (!this->capacity()) { // Could be zero-sized from a previous transfer()? this->resize(sz); } else { this->clear(); } return insertMultiple(begIter, endIter); } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template Foam::HashSet::HashSet(const UList& lst) : HashTable(2*lst.size()) { for (const auto& k : lst) { this->insert(k); } } template template Foam::HashSet::HashSet(const FixedList& lst) : HashTable(2*lst.size()) { for (const auto& k : lst) { this->insert(k); } } template Foam::HashSet::HashSet(std::initializer_list lst) : HashTable(2*lst.size()) { for (const auto& k : lst) { this->insert(k); } } template template Foam::HashSet::HashSet ( const HashTable& tbl ) : HashTable(tbl.capacity()) { using other_iter = typename HashTable::const_iterator; for (other_iter iter = tbl.cbegin(); iter != tbl.cend(); ++iter) { this->insert(iter.key()); } } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template Foam::label Foam::HashSet::insert(const UList& lst) { return insertMultiple(lst.begin(), lst.end()); } template template Foam::label Foam::HashSet::insert(const FixedList& lst) { return insertMultiple(lst.begin(), lst.end()); } template Foam::label Foam::HashSet::insert(std::initializer_list lst) { return insertMultiple(lst.begin(), lst.end()); } // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template void Foam::HashSet::operator=(const UList& lst) { assignMultiple(lst.begin(), lst.end(), 2*lst.size()); } template template void Foam::HashSet::operator=(const FixedList& lst) { assignMultiple(lst.begin(), lst.end(), 2*lst.size()); } template void Foam::HashSet::operator=(std::initializer_list lst) { assignMultiple(lst.begin(), lst.end(), 2*lst.size()); } template inline bool Foam::HashSet::operator[](const Key& key) const { return this->found(key); } template bool Foam::HashSet::operator==(const HashSet& rhs) const { // Sizes (number of keys) must match if (this->size() != rhs.size()) { return false; } for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) { if (!this->found(iter.key())) { return false; } } return true; } template bool Foam::HashSet::operator!=(const HashSet& rhs) const { return !operator==(rhs); } template void Foam::HashSet::operator|=(const HashSet& rhs) { // Add rhs elements into lhs for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) { this->insert(iter.key()); } } template inline void Foam::HashSet::operator&=(const HashSet& rhs) { this->parent_type::retain(rhs); } template void Foam::HashSet::operator^=(const HashSet& rhs) { // Add missed rhs elements, remove duplicate elements for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) { if (this->found(iter.key())) { this->erase(iter.key()); } else { this->insert(iter.key()); } } } template inline void Foam::HashSet::operator-=(const HashSet& rhs) { this->parent_type::erase(rhs); } // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // template Foam::Ostream& Foam::operator<<(Ostream& os, const HashSet& tbl) { return tbl.writeList(os, 10); // 10=consistent with UList } /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */ template Foam::HashSet Foam::operator| ( const HashSet& hash1, const HashSet& hash2 ) { HashSet out(hash1); out |= hash2; return out; } template Foam::HashSet Foam::operator& ( const HashSet& hash1, const HashSet& hash2 ) { HashSet out(hash1); out &= hash2; return out; } template Foam::HashSet Foam::operator^ ( const HashSet& hash1, const HashSet& hash2 ) { HashSet out(hash1); out ^= hash2; return out; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // template inline typename Foam::HashSet::iterator Foam::HashSet::begin() { return HashTableCore::iterator_begin ( static_cast(*this) ); } template inline typename Foam::HashSet::const_iterator Foam::HashSet::begin() const { return HashTableCore::iterator_begin ( static_cast(*this) ); } template inline typename Foam::HashSet::const_iterator Foam::HashSet::cbegin() const { return HashTableCore::iterator_begin ( static_cast(*this) ); } template inline const typename Foam::HashSet::iterator& Foam::HashSet::end() { return HashTableCore::iterator_end(); } template inline const typename Foam::HashSet::const_iterator& Foam::HashSet::end() const { return HashTableCore::iterator_end(); } template inline const typename Foam::HashSet::const_iterator& Foam::HashSet::cend() const { return HashTableCore::iterator_cend(); } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif // ************************************************************************* //