]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/hashcomp.cpp
Updated to keep lowermap const within 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 // from helperfuncs.cpp
34 extern const char lowermap[255];
35
36 /******************************************************
37  *
38  * The hash functions of InspIRCd are the centrepoint
39  * of the entire system. If these functions are
40  * inefficient or wasteful, the whole program suffers
41  * as a result. A lot of C programmers in the ircd
42  * scene spend a lot of time debating (arguing) about
43  * the best way to write hash functions to hash irc
44  * nicknames, channels etc.
45  * We are lucky as C++ developers as hash_map does
46  * a lot of this for us. It does intellegent memory
47  * requests, bucketing, search functions, insertion
48  * and deletion etc. All we have to do is write some
49  * overloaded comparison and hash value operators which
50  * cause it to act in an irc-like way. The features we
51  * add to the standard hash_map are:
52  *
53  * Case insensitivity: The hash_map will be case
54  * insensitive.
55  *
56  * Scandanavian Comparisons: The characters [, ], \ will
57  * be considered the lowercase of {, } and |.
58  *
59  * This file also contains hashing methods for hashing
60  * in_addr structs, we use this if we want to cache IP
61  * addresses.
62  *
63  ******************************************************/
64
65 using namespace std;
66
67 size_t nspace::hash<in_addr>::operator()(const struct in_addr &a) const
68 {
69         size_t q;
70         memcpy(&q,&a,sizeof(size_t));
71         return q;
72 }
73
74 size_t nspace::hash<string>::operator()(const string &s) const
75 {
76         char a[MAXBUF];
77         static struct hash<const char *> strhash;
78         strlcpy(a,s.c_str(),MAXBUF);
79         strlower(a);
80         return strhash(a);
81 }
82
83 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
84 {
85         irc::string a = s1.c_str();
86         irc::string b = s2.c_str();
87         return (a == b);
88 }
89
90 bool irc::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
101 /******************************************************
102  *
103  * This is the implementation of our special irc::string
104  * class which is a case-insensitive equivalent to
105  * std::string which is not only case-insensitive but
106  * can also do scandanavian comparisons, e.g. { = [, etc.
107  *
108  * This class depends on the global 'lowermap' which is
109  * initialized at startup by inspircd.cpp, and contains
110  * the 'scandanavian' casemappings for fast irc compare.
111  *
112  ******************************************************/
113
114 bool irc::irc_char_traits::eq(char c1st, char c2nd)
115 {
116         return lowermap[c1st] == lowermap[c2nd];
117 }
118
119 bool irc::irc_char_traits::ne(char c1st, char c2nd)
120 {
121         return lowermap[c1st] != lowermap[c2nd];
122 }
123
124 bool irc::irc_char_traits::lt(char c1st, char c2nd)
125 {
126         return lowermap[c1st] < lowermap[c2nd];
127 }
128
129 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
130 {
131         for(int i = 0; i < n; i++)
132         {
133                 if(lowermap[*str1] > lowermap[*str2])
134                         return 1;
135
136                 if(lowermap[*str1] < lowermap[*str2])
137                         return -1;
138
139                 if(*str1 == 0 || *str2 == 0)
140                         return 0;
141
142                 str1++;
143                 str2++;
144         }
145         return 0;
146 }
147
148 const char* irc::irc_char_traits::find(const char* s1, int  n, char c)
149 {
150         while(n-- > 0 && lowermap[*s1] != lowermap[c])
151                 s1++;
152         return s1;
153 }