2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / hash-map-traits.h
blobce9c4a74b4031ec79d1957c12b44ff5a68aec0a3
1 /* A hash map traits.
2 Copyright (C) 2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #ifndef HASH_MAP_TRAITS_H
21 #define HASH_MAP_TRAITS_H
23 /* Bacause mem-stats.h uses default hashmap traits, we have to
24 put the class to this separate header file. */
26 /* implement default behavior for traits when types allow it. */
28 struct default_hashmap_traits
30 /* Hashes the passed in key. */
32 template<typename T>
33 static hashval_t
34 hash (T *p)
36 return uintptr_t (p) >> 3;
39 /* If the value converts to hashval_t just use it. */
41 template<typename T> static hashval_t hash (T v) { return v; }
43 /* Return true if the two keys passed as arguments are equal. */
45 template<typename T>
46 static bool
47 equal_keys (const T &a, const T &b)
49 return a == b;
52 /* Called to dispose of the key and value before marking the entry as
53 deleted. */
55 template<typename T> static void remove (T &v) { v.~T (); }
57 /* Mark the passed in entry as being deleted. */
59 template<typename T>
60 static void
61 mark_deleted (T &e)
63 mark_key_deleted (e.m_key);
66 /* Mark the passed in entry as being empty. */
68 template<typename T>
69 static void
70 mark_empty (T &e)
72 mark_key_empty (e.m_key);
75 /* Return true if the passed in entry is marked as deleted. */
77 template<typename T>
78 static bool
79 is_deleted (T &e)
81 return e.m_key == (void *)1;
84 /* Return true if the passed in entry is marked as empty. */
86 template<typename T> static bool is_empty (T &e) { return e.m_key == NULL; }
88 private:
89 template<typename T>
90 static void
91 mark_key_deleted (T *&k)
93 k = reinterpret_cast<T *> (1);
96 template<typename T>
97 static void
98 mark_key_empty (T *&k)
100 k = static_cast<T *> (0);
104 #endif // HASH_MAP_TRAITS_H