/* Copyright (c) 1997-2020
   Ewgenij Gawrilow, Michael Joswig, and the polymake team
   Technische Universität Berlin, Germany
   https://polymake.org

   This program 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 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program 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.
--------------------------------------------------------------------------------
*/

#ifndef POLYMAKE_HASH_MAP_
#define POLYMAKE_HASH_MAP_

#include <unordered_map>

#include "polymake/internal/assoc.h"
#include "polymake/internal/hash_iterators.h"

namespace pm {

template <typename Key, typename Value, typename... Params>
using hash_map_base_t = std::conditional_t<tagged_list_extract_integral<typename mlist_wrap<Params...>::type, MultiTag>(false),
                                           std::unordered_multimap<Key, Value, hash_func<Key>, typename hash_table_cmp_adapter<Key, Params...>::type>,
                                           std::unordered_map<Key, Value, hash_func<Key>, typename hash_table_cmp_adapter<Key, Params...>::type>>;

template <typename Key, typename Value, typename... Params>
class hash_map
   : public hash_map_base_t<Key, Value, Params...> {
   using base_t = hash_map_base_t<Key, Value, Params...>;

   typedef typename mlist_wrap<Params...>::type params;
   typedef typename mtagged_list_extract<params, DefaultValueTag, operations::clear<Value>>::type default_value_supplier;
   default_value_supplier dflt;

public:
   static_assert(!std::is_same<Key, int>::value && !std::is_same<Value, int>::value, "use Int instead");

   using key_type = Key;
   using mapped_type = Value;
   using key_comparator_type = operations::cmp;
   static constexpr bool is_multimap = tagged_list_extract_integral<params, MultiTag>(false);

   hash_map() {}
   explicit hash_map(size_t start_cap) : base_t(start_cap) {}

   explicit hash_map(const default_value_supplier& dflt_arg) : dflt(dflt_arg) {}

   hash_map(const default_value_supplier& dflt_arg, size_t start_cap) : base_t(start_cap), dflt(dflt_arg) {}

   template <typename Iterator>
   hash_map(Iterator first, Iterator last) : base_t(first,last) {}

   template <typename Iterator>
   hash_map(Iterator first, Iterator last, const default_value_supplier& dflt_arg) : base_t(first,last), dflt(dflt_arg) {}

   bool exists(const Key& k) const
   {
      return this->find(k) != this->end();
   }

   typename base_t::iterator
   insert(const Key& k)
   {
      return this->emplace(k, dflt()).first;
   }

   template <typename ValueArg, typename... MoreArgs>
   typename base_t::iterator
   insert(const Key& k, ValueArg&& v, MoreArgs&& ...more_args)
   {
      auto ret=this->emplace(k, std::forward<ValueArg>(v), std::forward<MoreArgs>(more_args)...);
      if (!ret.second) ret.first->second=Value(std::forward<ValueArg>(v), std::forward<MoreArgs>(more_args)...);
      return ret.first;
   }

   auto find_or_insert(const Key& k)
   {
      return this->emplace(k, dflt());
   }

   auto insert(const typename base_t::value_type& p)
   {
      return base_t::insert(p);
   }

   template <typename Iterator>
   void insert(Iterator first, Iterator last, typename std::enable_if<!(std::is_same<Iterator, Key>::value && std::is_same<Iterator, Value>::value), void**>::type=nullptr)
   {
      base_t::insert(first, last);
   }

   /** @brief Associative search.

       Find the data element associated with the given key. If it doesn't exist so far, it will be created with the default constructor.

       Note that the type of the search key is not necessarily the same as of the map entries.
       It suffices that both are comparable with each other.

       k can also be a container with keys; the result will be a sequence of corresponding values.
   */
   template <typename TKeys>
   typename assoc_helper<hash_map, TKeys>::result_type operator[] (const TKeys& k)
   {
      return assoc_helper<hash_map, TKeys>()(*this, k);
   }

   /** @brief Associative search (const).
       Find the data element associated with the given key. If it doesn't exist so far, it will raise an exception.

       Note that the type of the search key is not necessarily the same as of the map entries.
       It suffices that both are comparable with each other.

       k can also be a container with keys; the result will be a sequence of corresponding values.
   */
   template <typename TKeys>
   typename assoc_helper<const hash_map, TKeys>::result_type operator[] (const TKeys& k) const
   {
      return assoc_helper<const hash_map, TKeys>()(*this, k);
   }

   // synonym for compatibility
   template <typename TKeys>
   typename assoc_helper<const hash_map, TKeys>::result_type
   at(const TKeys& k) const
   {
      return assoc_helper<const hash_map, TKeys>()(*this, k);
   }

   class filler {
   public:
      filler(hash_map& me_arg) : me(me_arg) {};

      void operator() (const typename base_t::value_type& p) const { me.insert(p); }

      template <typename... ValueArg>
      void operator() (const Key& k, ValueArg&& ... v) const
      {
         me.insert(k, std::forward<ValueArg>(v)...);
      }
   private:
      hash_map& me;
   };

   filler make_filler() { return filler(*this); }
};

template <typename Key, typename Value, typename... Params>
struct spec_object_traits< hash_map<Key, Value, Params...> >
   : spec_object_traits<is_container> {
   static const IO_separator_kind IO_separator=IO_sep_inherit;
};

template <typename Key, typename Value, typename... Params>
struct choose_generic_object_traits<hash_map<Key, Value, Params...>, false, false>
   : spec_object_traits<hash_map<Key, Value, Params...>> {
   typedef void generic_type;
   typedef is_map generic_tag;
   typedef hash_map<Key, Value, Params...> persistent_type;
};

template <typename Key, typename Value, typename... Params>
struct is_ordered< hash_map<Key, Value, Params...> >
   : std::false_type { };


template <typename TKeys, typename TValues>
class assoc_helper<const hash_map<TKeys,TValues>, TKeys, false, true> {
public:
   typedef const hash_map<TKeys,TValues> TMap;
   typedef typename inherit_const<typename TMap::mapped_type, TMap>::type& result_type;
   result_type operator()(TMap& map, const TKeys& k) const
   {
      auto e=map.find(k);
      if (e == map.end()) throw no_match();
      return e->second;
   }
};

template <typename TKeys, typename TValues>
class assoc_helper<hash_map<TKeys,TValues>, TKeys, false, true> {
public:
   typedef hash_map<TKeys,TValues> TMap;
   typedef typename inherit_const<typename TMap::mapped_type, TMap>::type& result_type;
   result_type operator()(TMap& map, const TKeys& k)
   {
      return map.insert(k)->second;
   }
};
   
} // end namespace pm

namespace polymake {
   using pm::hash_map;
}

#endif // POLYMAKE_HASH_MAP_

// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
