carb/container/RHUnorderedMultimap.h

File members: carb/container/RHUnorderedMultimap.h

// Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//

#pragma once

#include "RobinHoodImpl.h"

namespace carb
{
namespace container
{

template <class Key, class Value, class Hasher = std::hash<Key>, class Equals = std::equal_to<Key>, size_t LoadFactorMax100 = 80>
class RHUnorderedMultimap
    : public detail::
          RobinHood<LoadFactorMax100, Key, std::pair<const Key, Value>, detail::Select1st<Key, std::pair<const Key, Value>>, Hasher, Equals>
{
    using Base = detail::
        RobinHood<LoadFactorMax100, Key, std::pair<const Key, Value>, detail::Select1st<Key, std::pair<const Key, Value>>, Hasher, Equals>;

public:
    using key_type = typename Base::key_type;
    using mapped_type = Value;
    using value_type = typename Base::value_type;
    using size_type = typename Base::size_type;
    using difference_type = typename Base::difference_type;
    using hasher = typename Base::hasher;
    using key_equal = typename Base::key_equal;
    using reference = typename Base::reference;
    using const_reference = typename Base::const_reference;
    using pointer = typename Base::pointer;
    using const_pointer = typename Base::const_pointer;
    using iterator = typename Base::iterator;
    using const_iterator = typename Base::const_iterator;
    using find_iterator = typename Base::find_iterator;
    using const_find_iterator = typename Base::const_find_iterator;

    constexpr RHUnorderedMultimap() noexcept = default;

    RHUnorderedMultimap(const RHUnorderedMultimap& other) : Base(other)
    {
    }

    RHUnorderedMultimap(RHUnorderedMultimap&& other) : Base(std::move(other))
    {
    }

    ~RHUnorderedMultimap() = default;

    RHUnorderedMultimap& operator=(const RHUnorderedMultimap& other)
    {
        Base::operator=(other);
        return *this;
    }

    RHUnorderedMultimap& operator=(RHUnorderedMultimap&& other)
    {
        Base::operator=(std::move(other));
        return *this;
    }

    iterator insert(const value_type& value)
    {
        return this->insert_multi(value);
    }

    iterator insert(value_type&& value)
    {
        return this->insert_multi(std::move(value));
    }

    template <class P>
    iterator insert(std::enable_if_t<std::is_constructible<value_type, P&&>::value, P&&> value)
    {
        return insert(value_type{ std::forward<P>(value) });
    }

    template <class... Args>
    iterator emplace(Args&&... args)
    {
        // We need the key, so just construct the item here
        return insert(value_type{ std::forward<Args>(args)... });
    }

    size_type erase(const key_type& key)
    {
        size_t count = 0;
        auto vt = this->internal_find(key);
        decltype(vt) next;
        for (; vt; vt = next)
        {
            next = this->_findnext(vt);
            this->internal_erase(vt);
            ++count;
        }
        return count;
    }

    size_t count(const key_type& key) const
    {
        return this->internal_count_multi(key);
    }

#ifndef DOXYGEN_BUILD
    using Base::begin;
    using Base::cbegin;
    using Base::cend;
    using Base::end;

    using Base::capacity;
    using Base::empty;
    using Base::max_size;
    using Base::size;

    using Base::clear;
    using Base::erase;
    using Base::swap;

    using Base::contains;
    using Base::equal_range;
    using Base::find;

    using Base::rehash;
    using Base::reserve;
#endif
};

} // namespace container
} // namespace carb