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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
// -*-c++-*-
/* $Id: keyfunc.h,v 1.5 2001/07/18 03:02:17 dm Exp $ */
/*
*
* Copyright (C) 1998 David Mazieres (dm@uun.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
#ifndef _KEYFUNC_H_
#define _KEYFUNC_H_ 1
template<class T> struct unref_t {
typedef T base_type;
typedef T unref_type;
typedef const T &ref_type;
typedef T &ncref_type;
};
template<class T> struct unref_t<const T> {
typedef T base_type;
typedef const T unref_type;
typedef const T &ref_type;
typedef T &ncref_type;
};
template<class T> struct unref_t<T &> {
typedef T base_type;
typedef T unref_type;
typedef const T &ref_type;
typedef T &ncref_type;
};
template<class T> struct unref_t<const T &> {
typedef T base_type;
typedef const T unref_type;
typedef const T &ref_type;
typedef T &ncref_type;
};
#define CREF(T) unref_t<T>::ref_type
#define UNREF(T) unref_t<T>::unref_type
#define UNCREF(T) unref_t<T>::base_type
#define NCREF(T) unref_t<T>::ncref_type
#define HASHSEED 5381
inline u_int
hash_bytes (const void *_key, int len, u_int seed = HASHSEED)
{
const u_char *key = (const u_char *) _key;
const u_char *end;
for (end = key + len; key < end; key++)
seed = ((seed << 5) + seed) ^ *key;
return seed;
}
inline u_int
hash_string (const void *_p, u_int v = HASHSEED)
{
const char *p = (const char *) _p;
while (*p)
v = (33 * v) ^ (unsigned char) *p++;
return v;
}
inline u_int
hash_rotate (u_int val, u_int rot)
{
rot %= 8 * sizeof (val);
return (val << rot) | (val >> (8 * sizeof (val) - rot));
}
class hash_t {
u_int val;
public:
hash_t () {}
hash_t (u_int v) : val (v) {}
operator u_int() const { return val; }
};
template<class T>
struct compare {
compare () {}
int operator() (CREF (T) a, CREF (T) b) const
{ return a < b ? -1 : b < a; }
};
template<class T>
struct compare<const T> : compare<T>
{
compare () {}
};
template<class T>
struct equals {
equals () {}
bool operator() (CREF (T) a, CREF (T) b) const
{ return a == b; }
};
template<class T>
struct equals<const T> : equals<T>
{
equals () {}
};
template<class T>
struct hashfn {
hashfn () {}
hash_t operator() (CREF (T) a) const
{ return a; }
};
template<class T>
struct hashfn<const T> : hashfn<T>
{
hashfn () {}
};
template<class T1, class T2>
struct hash2fn {
hashfn<T1> h1;
hashfn<T2> h2;
hash2fn () {}
hash_t operator() (CREF (T1) t1, CREF (T2) t2) const
{ return h1 (t1) ^ h2 (t2); }
};
/* Don't compare pointers by accident */
template<class T> struct compare<T *>;
template<class T> struct hashfn<T *>;
/* Specializations for (char *) */
#define _CHAR_PTR(T, u) \
template<> \
struct compare<T> { \
compare () {} \
int operator() (const u char *a, const u char *b) const \
{ return strcmp ((char *) a, (char *) b); } \
}; \
template<> \
struct equals<T> { \
equals () {} \
bool operator() (const u char *a, const u char *b) const \
{ return !strcmp ((char *) a, (char *) b); } \
}; \
template<> \
struct hashfn<T> { \
hashfn () {} \
hash_t operator() (const u char *a) const \
{ return hash_string (a); } \
};
#define CHAR_PTR(T, u) _CHAR_PTR(T, u) _CHAR_PTR(T const, u)
CHAR_PTR(char *,)
CHAR_PTR(const char *,)
CHAR_PTR(signed char *, signed)
CHAR_PTR(const signed char *, signed)
CHAR_PTR(unsigned char *, unsigned)
CHAR_PTR(const unsigned char *, unsigned)
template<class R, class V, class K, K V::*key, class F>
class keyfunc_1 {
public:
const F kf;
keyfunc_1 () {}
keyfunc_1 (const F &f) : kf (f) {}
R operator() (CREF (V) a) const
{ return kf (a.*key); }
};
template<class R, class V, class K, K V::*key, class F>
class keyfunc_2 {
public:
const F kf;
keyfunc_2 () {}
keyfunc_2 (const F &f) : kf (f) {}
R operator() (CREF (V) a, CREF (V) b) const
{ return kf (a.*key, b.*key); }
};
#endif /* !KEYFUNC_H_ */
|