1 /* A hash map traits.
2    Copyright (C) 2015-2016 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef HASH_MAP_TRAITS_H
21 #define HASH_MAP_TRAITS_H
22 
23 /* Bacause mem-stats.h uses default hashmap traits, we have to
24    put the class to this separate header file.  */
25 
26 #include "hash-traits.h"
27 
28 /* Implement hash_map traits for a key with hash traits H.  Empty and
29    deleted map entries are represented as empty and deleted keys.  */
30 
31 template <typename H, typename Value>
32 struct simple_hashmap_traits
33 {
34   typedef typename H::value_type key_type;
35   static inline hashval_t hash (const key_type &);
36   static inline bool equal_keys (const key_type &, const key_type &);
37   template <typename T> static inline void remove (T &);
38   template <typename T> static inline bool is_empty (const T &);
39   template <typename T> static inline bool is_deleted (const T &);
40   template <typename T> static inline void mark_empty (T &);
41   template <typename T> static inline void mark_deleted (T &);
42 };
43 
44 template <typename H, typename Value>
45 inline hashval_t
hash(const key_type & h)46 simple_hashmap_traits <H, Value>::hash (const key_type &h)
47 {
48   return H::hash (h);
49 }
50 
51 template <typename H, typename Value>
52 inline bool
equal_keys(const key_type & k1,const key_type & k2)53 simple_hashmap_traits <H, Value>::equal_keys (const key_type &k1,
54 					      const key_type &k2)
55 {
56   return H::equal (k1, k2);
57 }
58 
59 template <typename H, typename Value>
60 template <typename T>
61 inline void
remove(T & entry)62 simple_hashmap_traits <H, Value>::remove (T &entry)
63 {
64   H::remove (entry.m_key);
65   entry.m_value.~Value ();
66 }
67 
68 template <typename H, typename Value>
69 template <typename T>
70 inline bool
is_empty(const T & entry)71 simple_hashmap_traits <H, Value>::is_empty (const T &entry)
72 {
73   return H::is_empty (entry.m_key);
74 }
75 
76 template <typename H, typename Value>
77 template <typename T>
78 inline bool
is_deleted(const T & entry)79 simple_hashmap_traits <H, Value>::is_deleted (const T &entry)
80 {
81   return H::is_deleted (entry.m_key);
82 }
83 
84 template <typename H, typename Value>
85 template <typename T>
86 inline void
mark_empty(T & entry)87 simple_hashmap_traits <H, Value>::mark_empty (T &entry)
88 {
89   H::mark_empty (entry.m_key);
90 }
91 
92 template <typename H, typename Value>
93 template <typename T>
94 inline void
mark_deleted(T & entry)95 simple_hashmap_traits <H, Value>::mark_deleted (T &entry)
96 {
97   H::mark_deleted (entry.m_key);
98 }
99 
100 /* Implement traits for a hash_map with values of type Value for cases
101    in which the key cannot represent empty and deleted slots.  Instead
102    record empty and deleted entries in Value.  Derived classes must
103    implement the hash and equal_keys functions.  */
104 
105 template <typename Value>
106 struct unbounded_hashmap_traits
107 {
108   template <typename T> static inline void remove (T &);
109   template <typename T> static inline bool is_empty (const T &);
110   template <typename T> static inline bool is_deleted (const T &);
111   template <typename T> static inline void mark_empty (T &);
112   template <typename T> static inline void mark_deleted (T &);
113 };
114 
115 template <typename Value>
116 template <typename T>
117 inline void
remove(T & entry)118 unbounded_hashmap_traits <Value>::remove (T &entry)
119 {
120   default_hash_traits <Value>::remove (entry.m_value);
121 }
122 
123 template <typename Value>
124 template <typename T>
125 inline bool
is_empty(const T & entry)126 unbounded_hashmap_traits <Value>::is_empty (const T &entry)
127 {
128   return default_hash_traits <Value>::is_empty (entry.m_value);
129 }
130 
131 template <typename Value>
132 template <typename T>
133 inline bool
is_deleted(const T & entry)134 unbounded_hashmap_traits <Value>::is_deleted (const T &entry)
135 {
136   return default_hash_traits <Value>::is_deleted (entry.m_value);
137 }
138 
139 template <typename Value>
140 template <typename T>
141 inline void
mark_empty(T & entry)142 unbounded_hashmap_traits <Value>::mark_empty (T &entry)
143 {
144   default_hash_traits <Value>::mark_empty (entry.m_value);
145 }
146 
147 template <typename Value>
148 template <typename T>
149 inline void
mark_deleted(T & entry)150 unbounded_hashmap_traits <Value>::mark_deleted (T &entry)
151 {
152   default_hash_traits <Value>::mark_deleted (entry.m_value);
153 }
154 
155 /* Implement traits for a hash_map from integer type Key to Value in
156    cases where Key has no spare values for recording empty and deleted
157    slots.  */
158 
159 template <typename Key, typename Value>
160 struct unbounded_int_hashmap_traits : unbounded_hashmap_traits <Value>
161 {
162   typedef Key key_type;
163   static inline hashval_t hash (Key);
164   static inline bool equal_keys (Key, Key);
165 };
166 
167 template <typename Key, typename Value>
168 inline hashval_t
hash(Key k)169 unbounded_int_hashmap_traits <Key, Value>::hash (Key k)
170 {
171   return k;
172 }
173 
174 template <typename Key, typename Value>
175 inline bool
equal_keys(Key k1,Key k2)176 unbounded_int_hashmap_traits <Key, Value>::equal_keys (Key k1, Key k2)
177 {
178   return k1 == k2;
179 }
180 
181 #endif // HASH_MAP_TRAITS_H
182