]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/hashcomp.cpp
Added comments to hashcomp.cpp
[user/henk/code/inspircd.git] / src / hashcomp.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include <string>
18 #include "inspircd.h"
19 #include "hashcomp.h"
20 #include "helperfuncs.h"
21 #ifdef GCC3
22 #include <ext/hash_map>
23 #else
24 #include <hash_map>
25 #endif
26
27 #ifdef GCC3
28 #define nspace __gnu_cxx
29 #else
30 #define nspace std
31 #endif
32
33 /******************************************************
34  *
35  * The hash functions of InspIRCd are the centrepoint
36  * of the entire system. If these functions are
37  * inefficient or wasteful, the whole program suffers
38  * as a result. A lot of C programmers in the ircd
39  * scene spend a lot of time debating (arguing) about
40  * the best way to write hash functions to hash irc
41  * nicknames, channels etc.
42  * We are lucky as C++ developers as hash_map does
43  * a lot of this for us. It does intellegent memory
44  * requests, bucketing, search functions, insertion
45  * and deletion etc. All we have to do is write some
46  * overloaded comparison and hash value operators which
47  * cause it to act in an irc-like way. The features we
48  * add to the standard hash_map are:
49  *
50  * Case insensitivity: The hash_map will be case
51  * insensitive.
52  *
53  * Scandanavian Comparisons: The characters [, ], \ will
54  * be considered the lowercase of {, } and |.
55  *
56  * This file also contains hashing methods for hashing
57  * in_addr structs, we use this if we want to cache IP
58  * addresses.
59  *
60  ******************************************************/
61
62 using namespace std;
63
64 size_t nspace::hash<in_addr>::operator()(const struct in_addr &a) const
65 {
66         size_t q;
67         memcpy(&q,&a,sizeof(size_t));
68         return q;
69 }
70
71 size_t nspace::hash<string>::operator()(const string &s) const
72 {
73         char a[MAXBUF];
74         static struct hash<const char *> strhash;
75         strlcpy(a,s.c_str(),MAXBUF);
76         strlower(a);
77         return strhash(a);
78 }
79
80 bool StrHashComp::operator()(const string& s1, const string& s2) const
81 {
82         char a[MAXBUF],b[MAXBUF];
83         strlcpy(a,s1.c_str(),MAXBUF);
84         strlcpy(b,s2.c_str(),MAXBUF);
85         strlower(a);
86         strlower(b);
87         return (strcasecmp(a,b) == 0);
88 }
89
90 bool InAddr_HashComp::operator()(const in_addr &s1, const in_addr &s2) const
91 {
92         size_t q;
93         size_t p;
94
95         memcpy(&q,&s1,sizeof(size_t));
96         memcpy(&p,&s2,sizeof(size_t));
97
98         return (q == p);
99 }
100