-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDefaultEqualityComparerS.h
81 lines (70 loc) · 1.46 KB
/
DefaultEqualityComparerS.h
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
#pragma once
//class DefaultEqualityComparerS
//{
//public:
//
// DefaultEqualityComparerS()
// {
// }
//
// ~DefaultEqualityComparerS()
// {
// }
//};
#include <tchar.h>
#include <iostream>
#include <fstream>
#include "Dictionary.h"
template<>
class DefaultEqualityComparer<std::string>
{
public:
static bool Equals(const std::string &value1, const std::string &value2)
{
return value1 == value2;
}
static size_t GetHashCode(const std::string &value)
{
size_t length = value.length();
if (length < sizeof(size_t))
{
if (length == 0)
return 0;
const char *cString = value.c_str();
size_t result = 0;
for (size_t i = 0; i<length; i++)
{
result <<= 8;
result |= *cString;
cString++;
}
return result;
}
const char *cString = value.c_str();
size_t *asSizeTPointer = (size_t *)cString;
size_t result = *asSizeTPointer;
size_t lastCharactersToUseCount = length - sizeof(size_t);
if (lastCharactersToUseCount > sizeof(size_t))
lastCharactersToUseCount = sizeof(size_t);
if (lastCharactersToUseCount > 0)
{
size_t otherResult;
if (lastCharactersToUseCount == sizeof(size_t))
otherResult = *((size_t *)(cString + length - sizeof(size_t)));
else
{
otherResult = 0;
cString += length;
for (size_t i = 0; i<lastCharactersToUseCount; i++)
{
cString--;
otherResult <<= 8;
otherResult |= *cString;
}
}
result ^= otherResult;
result ^= length;
}
return result;
}
};