vegastrike  0.5.1.r1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gnuhash.h
Go to the documentation of this file.
1 #ifndef _GNUHASH_H_
2 #define _GNUHASH_H_
3 #include "config.h"
4 //The following block is to only use tr1 from at least 4.3 since 4.2 apparently bugs out.
5 //Windows is untested at the moment.
6 #ifdef HAVE_TR1_UNORDERED_MAP
7 #ifndef WIN32
8 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
9 #undef HAVE_TR1_UNORDERED_MAP
10 #endif
11 #endif
12 #endif
13 
14 #ifdef HAVE_TR1_UNORDERED_MAP
15 #define vsUMap std::tr1::unordered_map
16 #define vsHashComp std::tr1::hash_compare
17 #define vsHash std::tr1::hash
18 #else
19 #define vsUMap stdext::hash_map
20 #define vsHashComp stdext::hash_compare
21 #define vsHash stdext::hash
22 #endif
23 
24 #ifdef _WIN32
25 #ifdef HAVE_TR1_UNORDERED_MAP
26 #if defined(_MSC_VER) && _MSC_VER >= 1600
27 #include <unordered_map> //MSVC doesn't use tr1 dirs
28 #else
29 #include <tr1/unordered_map>
30 #endif
31 #else
32 #include <hash_map>
33 #endif
34 #else //#ifdef _WIN32 { ... } else ...
35 #if __GNUC__ == 2
36 #include <map>
37 #define hash_map map
38 #define stdext std
39 namespace stdext
40 {
41 template < class Key, class Traits = std::less< Key > >
42 class hash_compare
43 {
44 public:
45  static const size_t bucket_size = 4;
46  static const size_t min_buckets = 8;
47 };
48 }
49 
50 #include "hashtable.h"
51 #else //if __GNUC__ == 2 { ... } else ...
52 #ifdef HAVE_TR1_UNORDERED_MAP
53 #include <tr1/unordered_map>
54 #include "hashtable.h"
55 class Unit;
56 namespace std
57 {
58 namespace tr1
59 {
60 #else //if HAVE_TR1_UNORDERED_MAP { ... } else ...
61 #include <ext/hash_map>
62 #define stdext __gnu_cxx
63 #include "hashtable.h"
64 
65 class Unit;
66 
67 namespace stdext
68 {
69 template < >
70 class hash< std::string >
71 {
72 public:
73  size_t operator()( const std::string &key ) const
74  {
75  size_t _HASH_INTSIZE = (sizeof (size_t)*8);
76  size_t _HASH_SALT_0 = 0x7EF92C3B;
77  size_t _HASH_SALT_1 = 0x9B;
78  size_t k = 0;
79  for (std::string::const_iterator start = key.begin(); start != key.end(); start++) {
80  k ^= (*start&_HASH_SALT_1);
81  k ^= _HASH_SALT_0;
82  k = ( ( (k>>4)&0xF )|( k<<(_HASH_INTSIZE-4) ) );
83  k ^= *start;
84  }
85  return k;
86  }
87 };
88 #endif //if HAVE_TR1_UNORDERED_MAP { ... } else ...
89 
90 template < >
91 class hash< void* >
92 {
93  hash< size_t >a;
94 public:
95  size_t operator()( const void *key ) const
96  {
97  return a( (size_t) key );
98  }
99 };
100 template < >
101 class hash< const void* >
102 {
103  hash< size_t >a;
104 public:
105  size_t operator()( const void*const &key ) const
106  {
107  return a( (size_t) key );
108  }
109 };
110 template < >
111 class hash< const Unit* >
112 {
113  hash< size_t >a;
114 public:
115  size_t operator()( const Unit*const &key ) const
116  {
117  return a( (size_t) key>>4 );
118  }
119 };
120 template < >
121 class hash< std::pair< Unit*, Unit* > >
122 {
123  hash< size_t >a;
124 public:
125  size_t operator()( const std::pair< Unit*, Unit* > &key ) const
126  {
127  return (size_t) (size_t) ( a( (int) ( ( (size_t) key.first )>>4 ) )
128  ^a( (int) ( ( (size_t) key.second )>>4 ) ) );
129  }
130 };
131 //Minimum declaration needed by SharedPool.h
132 template < class Key, class Traits = std::less< Key > >
134 {
135 public:
136  static const size_t bucket_size = 4;
137  static const size_t min_buckets = 8;
138 };
139 }
140 #ifdef HAVE_TR1_UNORDERED_MAP
141 }
142 #endif
143 
144 #endif //if __GNUC__ == 2 { ... } else ...
145 #endif //#ifdef _WIN32 { ... } else ...
146 #endif //def _GNUHASH_H_
147