#ifndef UTILITY_H
#define UTILITY_H
inline bool
xfs_name_comp(const char* name1, size_t length1, const char* name2, size_t length2)
{
return length1 == length2 && memcmp(name1, name2, length1) == 0;
}
inline uint32
hashfunction(const char* name, int length)
{
uint32 hashVal = 0;
int lengthCovered = 0;
int index = 0;
if (length >= 4) {
for (; index < length && (length - index) >= 4; index += 4) {
lengthCovered += 4;
hashVal = (name[index] << 21) ^ (name[index + 1] << 14) ^ (name[index + 2] << 7)
^ (name[index + 3] << 0) ^ ((hashVal << 28) | (hashVal >> 4));
}
}
int leftToCover = length - lengthCovered;
if (leftToCover == 3) {
hashVal = (name[index] << 14) ^ (name[index + 1] << 7) ^ (name[index + 2] << 0)
^ ((hashVal << 21) | (hashVal >> 11));
}
if (leftToCover == 2) {
hashVal = (name[index] << 7) ^ (name[index + 1] << 0)
^ ((hashVal << 14) | (hashVal >> (32 - 14)));
}
if (leftToCover == 1)
hashVal = (name[index] << 0) ^ ((hashVal << 7) | (hashVal >> (32 - 7)));
return hashVal;
}
template<class T>
void
hashLowerBound(T* entry, int& left, int& right, uint32 hashValueOfRequest)
{
int mid;
while (left < right) {
mid = (left + right) / 2;
uint32 hashval = B_BENDIAN_TO_HOST_INT32(entry[mid].hashval);
if (hashval >= hashValueOfRequest) {
right = mid;
continue;
}
if (hashval < hashValueOfRequest)
left = mid + 1;
}
TRACE("left:(%" B_PRId32 "), right:(%" B_PRId32 ")\n", left, right);
}