/* Copyright (c) 1997-2018
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.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... TParams>
class hash_map
   : public std::unordered_map<Key, Value, hash_func<Key>, typename hash_table_cmp_adapter<Key, TParams...>::type> {
   typedef std::unordered_map<Key, Value, hash_func<Key>, typename hash_table_cmp_adapter<Key, TParams...>::type> base_t;

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

public:
   typedef Key key_type;
   typedef Value mapped_type;
   typedef operations::cmp key_comparator_type;

   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;
   }

   std::pair<typename base_t::iterator, bool>
   find_or_insert(const Key& k)
   {
      return this->emplace(k, dflt());
   }

   std::pair<typename base_t::iterator, bool>
   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... TParams>
struct spec_object_traits< hash_map<Key, Value, TParams...> >
   : spec_object_traits<is_container> {
   static const IO_separator_kind IO_separator=IO_sep_inherit;
};

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

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


template <typename TKeys, typename TValues>
class assoc_helper<const hash_map<TKeys,TValues>, TKeys, 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, 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:
