File: hash_map

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (209 lines) | stat: -rw-r--r-- 7,228 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* Copyright (c) 1997-2024
   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: