Free Electron
AutoHashMap.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __core_AutoHashMap_h__
8 #define __core_AutoHashMap_h__
9 
10 namespace fe
11 {
12 
13 struct eq_string
14 {
15  bool operator()(const String &s1, const String &s2) const
16  {
17  return s1 == s2;
18  }
19 };
20 
21 struct hash_int
22 {
23  FE_UWORD operator()(const I32 &i) const
24  {
25  return I32(i);
26  }
27 };
28 
29 struct hash_string
30 {
31  FE_UWORD operator()(const String &s) const
32  {
33  FE_UWORD hash = 0;
34  const unsigned char *buffer = s.rawU8();
35 
36  for(FE_UWORD i = 0; i < s.length(); i++)
37  {
38  hash = (hash << 3) + buffer[i];
39  }
40 
41  return hash;
42  }
43 };
44 
45 template<typename T>
46 struct hash_auto
47 {
48  FE_UWORD operator()(const T t) const
49  { return reinterpret_cast<FE_UWORD>(t); }
50 };
51 template<typename T>
52 struct eq_auto
53 {
54  bool operator()(T t1, T t2) const
55  { return t1 == t2; }
56 };
57 
58 template<typename T, typename U>
59 class AutoHashMap:
60  public HashMap< T, U, hash_auto<T>, eq_auto<T> > {};
61 
62 template<typename U>
63 class AutoHashMap< I32, U >:
64  public HashMap< I32, U, hash_int, eq_string > {};
65 
66 template<typename U>
67 class AutoHashMap< String, U >:
68  public HashMap< String, U, hash_string, eq_string > {};
69 
70 } /* namespace */
71 
72 #endif /* __core_AutoHashMap_h__ */
kernel
Definition: namespace.dox:3