File: hash_fun.hpp

package info (click to toggle)
aspell 0.60.8.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,336 kB
  • sloc: cpp: 24,378; sh: 12,340; perl: 1,924; ansic: 1,661; makefile: 852; sed: 16
file content (53 lines) | stat: -rw-r--r-- 1,936 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2001
// Kevin Atkinson
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without
// fee, provided that the above copyright notice appear in all copies
// and that both that copyright notice and this permission notice
// appear in supporting documentation. Kevin Atkinson makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied
// warranty.

#ifndef acommon_hash_fun__hpp
#define acommon_hash_fun__hpp

namespace acommon {
  
  template <typename K> struct hash {};

  template <> struct hash<char>  {unsigned long operator()(char  v) const {return v;}};
  template <> struct hash<short> {unsigned long operator()(short v) const {return v;}};
  template <> struct hash<int>   {unsigned long operator()(int   v) const {return v;}};
  template <> struct hash<long>  {unsigned long operator()(long  v) const {return v;}};
  template <> struct hash<unsigned char>  {unsigned long operator()(unsigned char  v) const {return v;}};
  template <> struct hash<unsigned short> {unsigned long operator()(unsigned short v) const {return v;}};
  template <> struct hash<unsigned int>   {unsigned long operator()(unsigned int   v) const {return v;}};
  template <> struct hash<unsigned long>  {unsigned long operator()(unsigned long  v) const {return v;}};

  template <> struct hash<const char *> {
    inline unsigned long operator() (const char * s) const {
      unsigned long h = 0;
      for (; *s; ++s)
	h=5*h + *s;
      return h;
    }
  };

  template<class Str>
  struct HashString {
    inline unsigned long operator() (const Str &str) const {
      unsigned long h = 0;
      typename Str::const_iterator end = str.end();
      for (typename Str::const_iterator s = str.begin(); 
	   s != end; 
	   ++s)
	h=5*h + *s;
      return h;
    }
  };
  
}

#endif