Skip to content
Snippets Groups Projects
HashSet.C 10.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • mattijs's avatar
    mattijs committed
    /*---------------------------------------------------------------------------*\
      =========                 |
      \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
       \\    /   O peration     |
    
    OpenFOAM bot's avatar
    OpenFOAM bot committed
        \\  /    A nd           | www.openfoam.com
    
         \\/     M anipulation  |
    -------------------------------------------------------------------------------
    
    OpenFOAM bot's avatar
    OpenFOAM bot committed
        Copyright (C) 2011-2016 OpenFOAM Foundation
    
        Copyright (C) 2016-2020 OpenCFD Ltd.
    
    mattijs's avatar
    mattijs committed
    -------------------------------------------------------------------------------
    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.
    
    mattijs's avatar
    mattijs committed
    
        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/>.
    
    mattijs's avatar
    mattijs committed
    
    \*---------------------------------------------------------------------------*/
    
    #ifndef HashSet_C
    #define HashSet_C
    
    #include "HashSet.H"
    
    #include "FixedList.H"
    
    // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
    
    template<class Key, class Hash>
    template<class InputIter>
    
    inline Foam::label Foam::HashSet<Key, Hash>::assignMany
    
        const label nItems,
        InputIter first,
        InputIter last
    
            // Zero-sized from a previous transfer()?
    
    mattijs's avatar
    mattijs committed
    
    
    // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
    
    
    Mark Olesen's avatar
    Mark Olesen committed
    template<class Key, class Hash>
    
    template<unsigned N>
    Foam::HashSet<Key, Hash>::HashSet(const FixedList<Key, N>& list)
    
    Mark Olesen's avatar
    Mark Olesen committed
    :
    
    Mark Olesen's avatar
    Mark Olesen committed
    {
    
        insert(list.begin(), list.end());
    
    }
    
    
    template<class Key, class Hash>
    
    Foam::HashSet<Key, Hash>::HashSet(const UList<Key>& list)
    
        insert(list.begin(), list.end());
    
    template<class Key, class Hash>
    
    template<class Addr>
    Foam::HashSet<Key, Hash>::HashSet(const IndirectListBase<Key, Addr>& list)
    
        insert(list.begin(), list.end());
    
    }
    
    
    template<class Key, class Hash>
    Foam::HashSet<Key, Hash>::HashSet(std::initializer_list<Key> list)
    :
        parent_type(2*list.size())
    {
    
        insert(list.begin(), list.end());
    
    template<class Key, class Hash>
    
    template<class AnyType, class AnyHash>
    Foam::HashSet<Key, Hash>::HashSet
    (
    
        const HashTable<AnyType, Key, AnyHash>& tbl
    
        parent_type(tbl.capacity())
    
        for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
    
    Mark Olesen's avatar
    Mark Olesen committed
    // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
    
    template<class Key, class Hash>
    
    template<class InputIter>
    inline Foam::label Foam::HashSet<Key, Hash>::insert
    (
        InputIter first,
        InputIter last
    )
    {
        label changed = 0;
    
        while (first != last)
    
        }
        return changed;
    }
    
    
    template<class Key, class Hash>
    inline Foam::label Foam::HashSet<Key, Hash>::insert
    (
        std::initializer_list<Key> list
    )
    
    Mark Olesen's avatar
    Mark Olesen committed
    {
    
        return insert(list.begin(), list.end());
    
    Mark Olesen's avatar
    Mark Olesen committed
    
    
    template<class Key, class Hash>
    
    inline Foam::label Foam::HashSet<Key, Hash>::insert
    
        return insert(list.begin(), list.end());
    
    template<class Key, class Hash>
    
    inline Foam::label Foam::HashSet<Key, Hash>::insert
    
        return insert(list.begin(), list.end());
    }
    
    
    template<class Key, class Hash>
    
    inline  Foam::label Foam::HashSet<Key, Hash>::insert
    
        const IndirectListBase<Key, Addr>& list
    
    )
    {
        return insert(list.begin(), list.end());
    }
    
    
    template<class Key, class Hash>
    template<class InputIter>
    inline Foam::label Foam::HashSet<Key, Hash>::unset
    (
        InputIter first,
        InputIter last
    )
    {
        return this->parent_type::erase(first, last);
    }
    
    
    template<class Key, class Hash>
    inline Foam::label Foam::HashSet<Key, Hash>::unset
    (
        std::initializer_list<Key> list
    )
    {
        return unset(list.begin(), list.end());
    }
    
    
    template<class Key, class Hash>
    
    inline Foam::label Foam::HashSet<Key, Hash>::unset
    
    )
    {
        return unset(list.begin(), list.end());
    }
    
    
    template<class Key, class Hash>
    
    inline Foam::label Foam::HashSet<Key, Hash>::unset
    
    (
        const UList<Key>& list
    )
    {
        return unset(list.begin(), list.end());
    }
    
    
    template<class Key, class Hash>
    
    inline Foam::label Foam::HashSet<Key, Hash>::unset
    
        const IndirectListBase<Key, Addr>& list
    
    Mark Olesen's avatar
    Mark Olesen committed
    
    
    mattijs's avatar
    mattijs committed
    // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
    
    
    template<class Key, class Hash>
    
    inline bool Foam::HashSet<Key, Hash>::operator()(const Key& key) const noexcept
    
    {
        return this->found(key);
    }
    
    
    template<class Key, class Hash>
    
    inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const noexcept
    
    template<class Key, class Hash>
    
    template<unsigned N>
    void Foam::HashSet<Key, Hash>::operator=(const FixedList<Key, N>& rhs)
    
        assignMany(rhs.size(), rhs.begin(), rhs.end());
    
    }
    
    
    template<class Key, class Hash>
    
    void Foam::HashSet<Key, Hash>::operator=(const UList<Key>& rhs)
    
        assignMany(rhs.size(), rhs.begin(), rhs.end());
    
    }
    
    
    template<class Key, class Hash>
    
    void Foam::HashSet<Key, Hash>::operator=(std::initializer_list<Key> rhs)
    
        assignMany(rhs.size(), rhs.begin(), rhs.end());
    
    mattijs's avatar
    mattijs committed
    template<class Key, class Hash>
    
    bool Foam::HashSet<Key, Hash>::operator==(const HashSet<Key, Hash>& rhs) const
    
    mattijs's avatar
    mattijs committed
    {
    
        // Sizes (number of keys) must match
        if (this->size() != rhs.size())
    
    mattijs's avatar
    mattijs committed
        {
    
            return false;
    
    mattijs's avatar
    mattijs committed
        }
    
    
        for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
    
    mattijs's avatar
    mattijs committed
        {
    
            if (!this->found(iter.key()))
    
    mattijs's avatar
    mattijs committed
            {
                return false;
            }
        }
    
    mattijs's avatar
    mattijs committed
        return true;
    }
    
    
    template<class Key, class Hash>
    
    bool Foam::HashSet<Key, Hash>::operator!=(const HashSet<Key, Hash>& rhs) const
    
    mattijs's avatar
    mattijs committed
    {
    
        return !operator==(rhs);
    
    template<class Key, class Hash>
    
    Foam::HashSet<Key, Hash>&
    Foam::HashSet<Key, Hash>::operator|=(const HashSet<Key, Hash>& rhs)
    
        // Add rhs elements into lhs
    
        for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
    
            this->insert(iter.key());
    
    }
    
    
    template<class Key, class Hash>
    
    inline Foam::HashSet<Key, Hash>&
    Foam::HashSet<Key, Hash>::operator&=(const HashSet<Key, Hash>& rhs)
    
        this->parent_type::retain(rhs);
    
    }
    
    
    template<class Key, class Hash>
    
    Foam::HashSet<Key, Hash>&
    Foam::HashSet<Key, Hash>::operator^=(const HashSet<Key, Hash>& 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());
    
                this->insert(iter.key());
    
    mattijs's avatar
    mattijs committed
    
    
    
    template<class Key, class Hash>
    inline Foam::HashSet<Key, Hash>&
    Foam::HashSet<Key, Hash>::operator+=(const HashSet<Key, Hash>& rhs)
    {
        return this->operator|=(rhs);
    }
    
    
    
    template<class Key, class Hash>
    
    inline Foam::HashSet<Key, Hash>&
    Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
    
        this->parent_type::erase(rhs);
    
    // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
    
    template<class Key, class Hash>
    
    Foam::Ostream& Foam::operator<<(Ostream& os, const HashSet<Key, Hash>& rhs)
    
        return rhs.writeKeys(os, Detail::ListPolicy::short_length<Key>::value);
    
    /* * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * */
    
    
    template<class Key, class Hash>
    
    Foam::HashSet<Key, Hash> Foam::operator|
    
        const HashSet<Key, Hash>& a,
        const HashSet<Key, Hash>& b
    
        HashSet<Key, Hash> result(a);
        result |= b;
        return result;
    
    }
    
    
    template<class Key, class Hash>
    
    Foam::HashSet<Key, Hash> Foam::operator&
    
        const HashSet<Key, Hash>& a,
        const HashSet<Key, Hash>& b
    
        HashSet<Key, Hash> result(a.capacity());
    
        for (const Key& k : a)
        {
            if (b.found(k))
            {
                result.insert(k);
            }
        }
    
        return result;
    
    }
    
    
    template<class Key, class Hash>
    
    Foam::HashSet<Key, Hash> Foam::operator^
    
        const HashSet<Key, Hash>& a,
        const HashSet<Key, Hash>& b
    
        HashSet<Key, Hash> result(a);
        result ^= b;
        return result;
    }
    
    
    template<class Key, class Hash>
    Foam::HashSet<Key, Hash> Foam::operator-
    (
        const HashSet<Key, Hash>& a,
        const HashSet<Key, Hash>& b
    )
    {
        HashSet<Key, Hash> result(a.capacity());
    
        for (const Key& k : a)
        {
            if (!b.found(k))
            {
                result.insert(k);
            }
        }
    
        return result;
    
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    
    template<class Key, class Hash>
    inline typename Foam::HashSet<Key, Hash>::iterator
    Foam::HashSet<Key, Hash>::begin()
    {
    
            static_cast<parent_type&>(*this).begin()
    
    template<class Key, class Hash>
    inline typename Foam::HashSet<Key, Hash>::const_iterator
    Foam::HashSet<Key, Hash>::begin() const
    {
    
            static_cast<const parent_type&>(*this).begin()
    
    template<class Key, class Hash>
    inline typename Foam::HashSet<Key, Hash>::const_iterator
    Foam::HashSet<Key, Hash>::cbegin() const
    {
    
            static_cast<const parent_type&>(*this).cbegin()
    
    template<class Key, class Hash>
    
    inline typename Foam::HashSet<Key, Hash>::iterator
    Foam::HashSet<Key, Hash>::end() noexcept
    
    template<class Key, class Hash>
    
    inline typename Foam::HashSet<Key, Hash>::const_iterator
    Foam::HashSet<Key, Hash>::end() const noexcept
    
    template<class Key, class Hash>
    
    inline constexpr typename Foam::HashSet<Key, Hash>::const_iterator
    Foam::HashSet<Key, Hash>::cend() const noexcept
    
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    
    
    mattijs's avatar
    mattijs committed
    #endif
    
    // ************************************************************************* //