summaryrefslogtreecommitdiffstats
path: root/src/BlockState.cpp
blob: 8ee87c50fb7b628af9d68711212f6329599fa0fb (plain) (blame)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "Globals.h"
#include "BlockState.h"





BlockState::BlockState():
	mChecksum(initializeChecksum())
{
	// Nothing needed yet
}





BlockState::BlockState(const AString & aKey, const AString & aValue):
	mState({{aKey, aValue}}),
	mChecksum(initializeChecksum())
{
}





BlockState::BlockState(std::initializer_list<std::pair<const AString, AString>> aKeysAndValues):
	mState(aKeysAndValues),
	mChecksum(initializeChecksum())
{
}





BlockState::BlockState(const std::map<AString, AString> & aKeysAndValues):
	mState(aKeysAndValues),
	mChecksum(initializeChecksum())
{
}





BlockState::BlockState(std::map<AString, AString> && aKeysAndValues):
	mState(std::move(aKeysAndValues)),
	mChecksum(initializeChecksum())
{
}





BlockState::BlockState(const BlockState & aCopyFrom, std::initializer_list<std::pair<const AString, AString>> aAdditionalKeysAndValues):
	mState(aCopyFrom.mState)
{
	for (const auto & kav: aAdditionalKeysAndValues)
	{
		mState[kav.first] = kav.second;
	}
	mChecksum = initializeChecksum();
}





BlockState::BlockState(const BlockState & aCopyFrom, const std::map<AString, AString> & aAdditionalKeysAndValues):
	mState(aCopyFrom.mState)
{
	for (const auto & kav: aAdditionalKeysAndValues)
	{
		mState[kav.first] = kav.second;
	}
	mChecksum = initializeChecksum();
}





bool BlockState::operator <(const BlockState & aOther) const
{
	// Fast-return this using checksum
	if (mChecksum != aOther.mChecksum)
	{
		return (mChecksum < aOther.mChecksum);
	}

	// Can fast-return this due to how comparison works
	if (mState.size() != aOther.mState.size())
	{
		return (mState.size() < aOther.mState.size());
	}

	auto itA = mState.begin();
	auto itOther = aOther.mState.begin();

	// don't need to check itOther, size checks above ensure size(A) == size(O)
	while (itA != mState.end())
	{
		{
			const auto cmp = itA->first.compare(itOther->first);
			if (cmp != 0)
			{
				return (cmp < 0);
			}
		}
		{
			const auto cmp = itA->second.compare(itOther->second);
			if (cmp != 0)
			{
				return (cmp < 0);
			}
		}

		++itA;
		++itOther;
	}

	return false;
}





bool BlockState::operator ==(const BlockState & aOther) const
{
	// Fast-fail if the checksums differ or differrent counts:
	if ((mChecksum != aOther.mChecksum) || (mState.size() != aOther.mState.size()))
	{
		return false;
	}

	// Slow-check everything if the checksums match:
	return std::equal(mState.begin(), mState.end(), aOther.mState.begin());
}





const AString & BlockState::value(const AString & aKey) const
{
	auto itr = mState.find(aKey);
	if (itr == mState.end())
	{
		static AString empty;
		return empty;
	}
	return itr->second;
}





UInt32 BlockState::initializeChecksum()
{
	removeEmptyKeys();

	// Calculate the checksum as a XOR of all mState keys' and values' checksums
	// This way we don't depend on the std::map's ordering
	UInt32 res = 0;
	for (const auto & kv: mState)
	{
		auto partial = partialChecksum(kv.first) ^ partialChecksum(kv.second);
		res = res ^ partial;
	}
	return res;
}





void BlockState::removeEmptyKeys()
{
	for (auto itr = mState.begin(); itr != mState.end();)
	{
		if (itr->second.empty())
		{
			itr = mState.erase(itr);
		}
		else
		{
			++itr;
		}
	}
}





UInt32 BlockState::partialChecksum(const AString & aString)
{
	UInt32 shift = 0;
	UInt32 res = 0;
	for (auto ch: aString)
	{
		UInt32 v = static_cast<UInt8>(ch);
		v = v << shift;
		shift = (shift + 1) % 24;
		res = res ^ v;
	}
	return res;
}