vegastrike  0.5.1.r1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hashtable.h
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Alan Shieh
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #ifndef _HASHTABLE_H_
23 #define _HASHTABLE_H_
24 #include "config.h"
25 #include "gnuhash.h"
26 #include <math.h>
27 #include <string>
28 #include <vector>
29 #include <algorithm>
30 #include <utility>
31 #define HASH_INTSIZE (sizeof (int)*8)
32 #define HASH_SALT_0 0x7EF92C3B
33 #define HASH_SALT_1 0x9B
34 class Unit;
35 //const int hashsize = 1001;
36 //Hashtable doesn't grow
37 template < class KEY, class VALUE, int SIZ >
38 class Hashtable : public vsUMap< KEY, VALUE* >
39 {
40  typedef std::pair< KEY, VALUE* >HashElement;
41  typedef vsUMap< KEY, VALUE* > supertype;
42 public:
43  static int hash( const int key )
44  {
45  unsigned int k = key;
46  k %= SIZ;
47  return k;
48  }
49  static int hash( const char *key )
50  {
51  unsigned int k = 0;
52  for (const char *start = key; *start != '\0'; ++start) {
53  k ^= (*start&HASH_SALT_1);
54  k ^= HASH_SALT_0;
55  k = ( ( (k>>4)&0xF )|( k<<(HASH_INTSIZE-4) ) );
56  k ^= *start;
57  }
58  k %= SIZ;
59  return k;
60  }
61  static int hash( const std::string &key )
62  {
63  unsigned int k = 0;
64  for (typename std::string::const_iterator start = key.begin(); start != key.end(); ++start) {
65  k ^= (*start&HASH_SALT_1);
66  k ^= HASH_SALT_0;
67  k = ( ( (k>>4)&0xF )|( k<<(HASH_INTSIZE-4) ) );
68  k ^= *start;
69  }
70  k %= SIZ;
71  return k;
72  }
73  std::vector< VALUE* >GetAll() const
74  {
75  std::vector< VALUE* >retval( this->size() );
76  typename supertype::const_iterator iter = this->begin();
77  typename supertype::const_iterator end = this->end();
78  size_t i = 0;
79  for (; iter != end; ++iter, ++i)
80  retval[i] = iter->second;
81  return retval;
82  }
83 
84  VALUE * Get( const KEY &key ) const
85  {
86  typename supertype::const_iterator iter = this->find( key );
87  typename supertype::const_iterator end = this->end();
88  if (iter != end) return iter->second;
89  return NULL;
90  }
91 
92  void Put( const KEY &key, VALUE *value )
93  {
94  (*this)[key] = value;
95  }
96 
97  void Delete( const KEY &key )
98  {
99  typename supertype::iterator iter = this->find( key );
100  if ( iter == this->end() )
101 //fprintf(stderr,"failed to remove item in hash_map\n");
102  return;
103  this->erase( iter );
104  }
105 };
106 
107 #endif
108