2012-05-31 04:49:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-08-07 01:04:37 +00:00
|
|
|
#include <cassert>
|
2020-11-21 10:26:38 +00:00
|
|
|
#include <stdexcept> // for std::logic_error
|
2012-05-31 04:49:55 +00:00
|
|
|
#include <string>
|
2013-06-30 16:56:00 +00:00
|
|
|
#include <vector>
|
2014-01-08 16:33:28 +00:00
|
|
|
#include <functional>
|
2020-09-14 13:33:36 +00:00
|
|
|
#include <iosfwd>
|
2013-10-11 11:43:50 +00:00
|
|
|
|
2020-03-19 10:38:34 +00:00
|
|
|
#include <common/types.h>
|
2017-06-23 20:22:35 +00:00
|
|
|
#include <common/unaligned.h>
|
|
|
|
|
|
|
|
#include <city.h>
|
|
|
|
|
2019-01-31 15:38:21 +00:00
|
|
|
#if defined(__SSE2__)
|
2017-04-01 07:20:54 +00:00
|
|
|
#include <emmintrin.h>
|
2016-01-13 20:21:56 +00:00
|
|
|
#endif
|
2014-11-09 12:43:09 +00:00
|
|
|
|
2019-01-31 15:38:21 +00:00
|
|
|
#if defined(__SSE4_2__)
|
2017-12-27 19:26:18 +00:00
|
|
|
#include <smmintrin.h>
|
|
|
|
#include <nmmintrin.h>
|
|
|
|
#endif
|
2016-07-31 05:56:36 +00:00
|
|
|
|
2014-04-28 01:48:24 +00:00
|
|
|
|
2020-07-22 13:18:21 +00:00
|
|
|
/**
|
|
|
|
* The std::string_view-like container to avoid creating strings to find substrings in the hash table.
|
|
|
|
*/
|
2014-04-28 01:48:24 +00:00
|
|
|
struct StringRef
|
2012-05-31 04:49:55 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * data = nullptr;
|
|
|
|
size_t size = 0;
|
2012-05-31 04:49:55 +00:00
|
|
|
|
2020-07-22 13:18:21 +00:00
|
|
|
/// Non-constexpr due to reinterpret_cast.
|
2020-01-03 14:29:31 +00:00
|
|
|
template <typename CharT, typename = std::enable_if_t<sizeof(CharT) == 1>>
|
2020-08-12 14:07:56 +00:00
|
|
|
StringRef(const CharT * data_, size_t size_) : data(reinterpret_cast<const char *>(data_)), size(size_)
|
|
|
|
{
|
|
|
|
/// Sanity check for overflowed values.
|
|
|
|
assert(size < 0x8000000000000000ULL);
|
|
|
|
}
|
2020-07-22 13:18:21 +00:00
|
|
|
|
|
|
|
constexpr StringRef(const char * data_, size_t size_) : data(data_), size(size_) {}
|
2020-01-03 14:29:31 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
StringRef(const std::string & s) : data(s.data()), size(s.size()) {}
|
2020-07-22 20:10:12 +00:00
|
|
|
constexpr explicit StringRef(std::string_view s) : data(s.data()), size(s.size()) {}
|
2020-07-22 20:13:57 +00:00
|
|
|
constexpr StringRef(const char * data_) : StringRef(std::string_view{data_}) {}
|
2020-03-15 13:52:46 +00:00
|
|
|
constexpr StringRef() = default;
|
2013-04-09 19:04:25 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string toString() const { return std::string(data, size); }
|
2015-02-26 14:52:03 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
explicit operator std::string() const { return toString(); }
|
2020-03-15 13:52:46 +00:00
|
|
|
constexpr explicit operator std::string_view() const { return {data, size}; }
|
2014-04-28 01:48:24 +00:00
|
|
|
};
|
2012-05-31 04:49:55 +00:00
|
|
|
|
2020-07-22 19:44:11 +00:00
|
|
|
/// Here constexpr doesn't implicate inline, see https://www.viva64.com/en/w/v1043/
|
2020-10-27 11:04:03 +00:00
|
|
|
/// nullptr can't be used because the StringRef values are used in SipHash's pointer arithmetic
|
2020-07-28 22:08:09 +00:00
|
|
|
/// and the UBSan thinks that something like nullptr + 8 is UB.
|
|
|
|
constexpr const inline char empty_string_ref_addr{};
|
|
|
|
constexpr const inline StringRef EMPTY_STRING_REF{&empty_string_ref_addr, 0};
|
2020-07-22 13:18:21 +00:00
|
|
|
|
2016-05-28 10:35:44 +00:00
|
|
|
using StringRefs = std::vector<StringRef>;
|
2013-06-30 16:56:00 +00:00
|
|
|
|
2014-04-28 01:48:24 +00:00
|
|
|
|
2019-01-31 15:38:21 +00:00
|
|
|
#if defined(__SSE2__)
|
2016-01-13 20:21:56 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/** Compare strings for equality.
|
|
|
|
* The approach is controversial and does not win in all cases.
|
|
|
|
* For more information, see hash_map_string_2.cpp
|
2014-11-09 12:43:09 +00:00
|
|
|
*/
|
2014-11-03 21:08:56 +00:00
|
|
|
|
2014-11-09 12:43:09 +00:00
|
|
|
inline bool compareSSE2(const char * p1, const char * p2)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return 0xFFFF == _mm_movemask_epi8(_mm_cmpeq_epi8(
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p1)),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p2))));
|
2014-11-09 12:43:09 +00:00
|
|
|
}
|
2014-11-03 21:08:56 +00:00
|
|
|
|
2015-07-13 21:28:19 +00:00
|
|
|
inline bool compareSSE2x4(const char * p1, const char * p2)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return 0xFFFF == _mm_movemask_epi8(
|
|
|
|
_mm_and_si128(
|
|
|
|
_mm_and_si128(
|
|
|
|
_mm_cmpeq_epi8(
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p1)),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p2))),
|
|
|
|
_mm_cmpeq_epi8(
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p1) + 1),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p2) + 1))),
|
|
|
|
_mm_and_si128(
|
|
|
|
_mm_cmpeq_epi8(
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p1) + 2),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p2) + 2)),
|
|
|
|
_mm_cmpeq_epi8(
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p1) + 3),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p2) + 3)))));
|
2015-07-13 21:28:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-09 12:43:09 +00:00
|
|
|
inline bool memequalSSE2Wide(const char * p1, const char * p2, size_t size)
|
|
|
|
{
|
2021-09-19 19:26:16 +00:00
|
|
|
/** The order of branches and the trick with overlapping comparisons
|
|
|
|
* are the same as in memcpy implementation.
|
|
|
|
* See the comments in base/glibc-compatibility/memcpy/memcpy.h
|
|
|
|
*/
|
|
|
|
|
2021-09-09 02:38:48 +00:00
|
|
|
if (size <= 16)
|
|
|
|
{
|
|
|
|
if (size >= 8)
|
|
|
|
{
|
|
|
|
/// Chunks of 8..16 bytes.
|
|
|
|
return unalignedLoad<uint64_t>(p1) == unalignedLoad<uint64_t>(p2)
|
|
|
|
&& unalignedLoad<uint64_t>(p1 + size - 8) == unalignedLoad<uint64_t>(p2 + size - 8);
|
|
|
|
}
|
|
|
|
else if (size >= 4)
|
|
|
|
{
|
|
|
|
/// Chunks of 4..7 bytes.
|
|
|
|
return unalignedLoad<uint32_t>(p1) == unalignedLoad<uint32_t>(p2)
|
|
|
|
&& unalignedLoad<uint32_t>(p1 + size - 4) == unalignedLoad<uint32_t>(p2 + size - 4);
|
|
|
|
}
|
|
|
|
else if (size >= 2)
|
|
|
|
{
|
|
|
|
/// Chunks of 2..3 bytes.
|
|
|
|
return unalignedLoad<uint16_t>(p1) == unalignedLoad<uint16_t>(p2)
|
|
|
|
&& unalignedLoad<uint16_t>(p1 + size - 2) == unalignedLoad<uint16_t>(p2 + size - 2);
|
|
|
|
}
|
|
|
|
else if (size >= 1)
|
|
|
|
{
|
|
|
|
/// A single byte.
|
|
|
|
return *p1 == *p2;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (size >= 64)
|
|
|
|
{
|
|
|
|
if (compareSSE2x4(p1, p2))
|
|
|
|
{
|
|
|
|
p1 += 64;
|
|
|
|
p2 += 64;
|
|
|
|
size -= 64;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-09 02:38:48 +00:00
|
|
|
switch (size / 16)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-02 03:01:41 +00:00
|
|
|
case 3: if (!compareSSE2(p1 + 32, p2 + 32)) return false; [[fallthrough]];
|
|
|
|
case 2: if (!compareSSE2(p1 + 16, p2 + 16)) return false; [[fallthrough]];
|
2021-09-09 02:42:13 +00:00
|
|
|
case 1: if (!compareSSE2(p1, p2)) return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 02:42:13 +00:00
|
|
|
return compareSSE2(p1 + size - 16, p2 + size - 16);
|
2014-04-28 01:48:24 +00:00
|
|
|
}
|
2012-06-01 10:45:29 +00:00
|
|
|
|
2016-01-13 20:21:56 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-09 12:43:09 +00:00
|
|
|
|
|
|
|
inline bool operator== (StringRef lhs, StringRef rhs)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size != rhs.size)
|
|
|
|
return false;
|
2014-11-09 12:43:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size == 0)
|
|
|
|
return true;
|
2014-11-09 12:43:09 +00:00
|
|
|
|
2019-01-31 15:38:21 +00:00
|
|
|
#if defined(__SSE2__)
|
2017-04-01 07:20:54 +00:00
|
|
|
return memequalSSE2Wide(lhs.data, rhs.data, lhs.size);
|
2016-01-13 20:21:56 +00:00
|
|
|
#else
|
2017-04-01 07:20:54 +00:00
|
|
|
return 0 == memcmp(lhs.data, rhs.data, lhs.size);
|
2016-01-13 20:21:56 +00:00
|
|
|
#endif
|
2014-11-09 12:43:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 05:45:41 +00:00
|
|
|
inline bool operator!= (StringRef lhs, StringRef rhs)
|
2014-04-28 01:48:24 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return !(lhs == rhs);
|
2014-04-28 01:48:24 +00:00
|
|
|
}
|
2012-05-31 04:49:55 +00:00
|
|
|
|
2014-08-18 05:45:41 +00:00
|
|
|
inline bool operator< (StringRef lhs, StringRef rhs)
|
2014-04-28 01:48:24 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
int cmp = memcmp(lhs.data, rhs.data, std::min(lhs.size, rhs.size));
|
|
|
|
return cmp < 0 || (cmp == 0 && lhs.size < rhs.size);
|
2014-08-18 05:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator> (StringRef lhs, StringRef rhs)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
int cmp = memcmp(lhs.data, rhs.data, std::min(lhs.size, rhs.size));
|
|
|
|
return cmp > 0 || (cmp == 0 && lhs.size > rhs.size);
|
2014-04-28 01:48:24 +00:00
|
|
|
}
|
2012-05-31 04:49:55 +00:00
|
|
|
|
2014-04-29 00:28:18 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/** Hash functions.
|
|
|
|
* You can use either CityHash64,
|
|
|
|
* or a function based on the crc32 statement,
|
|
|
|
* which is obviously less qualitative, but on real data sets,
|
|
|
|
* when used in a hash table, works much faster.
|
|
|
|
* For more information, see hash_map_string_3.cpp
|
2014-11-09 12:43:09 +00:00
|
|
|
*/
|
|
|
|
|
2016-09-23 05:49:55 +00:00
|
|
|
struct StringRefHash64
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t operator() (StringRef x) const
|
|
|
|
{
|
2017-06-21 08:35:38 +00:00
|
|
|
return CityHash_v1_0_2::CityHash64(x.data, x.size);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-09-23 05:49:55 +00:00
|
|
|
};
|
|
|
|
|
2019-01-31 15:38:21 +00:00
|
|
|
#if defined(__SSE4_2__)
|
2016-01-13 20:21:56 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// Parts are taken from CityHash.
|
2014-11-09 12:43:09 +00:00
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
inline UInt64 hashLen16(UInt64 u, UInt64 v)
|
2014-11-09 12:43:09 +00:00
|
|
|
{
|
2017-06-21 08:35:38 +00:00
|
|
|
return CityHash_v1_0_2::Hash128to64(CityHash_v1_0_2::uint128(u, v));
|
2014-11-09 12:43:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
inline UInt64 shiftMix(UInt64 val)
|
2014-11-09 12:43:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return val ^ (val >> 47);
|
2014-11-09 12:43:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
inline UInt64 rotateByAtLeast1(UInt64 val, int shift)
|
2014-11-09 12:43:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return (val >> shift) | (val << (64 - shift));
|
2014-11-09 12:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t hashLessThan8(const char * data, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr UInt64 k2 = 0x9ae16a3b2f90404fULL;
|
|
|
|
static constexpr UInt64 k3 = 0xc949d7c7509e6557ULL;
|
|
|
|
|
|
|
|
if (size >= 4)
|
|
|
|
{
|
|
|
|
UInt64 a = unalignedLoad<uint32_t>(data);
|
|
|
|
return hashLen16(size + (a << 3), unalignedLoad<uint32_t>(data + size - 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
{
|
|
|
|
uint8_t a = data[0];
|
|
|
|
uint8_t b = data[size >> 1];
|
|
|
|
uint8_t c = data[size - 1];
|
|
|
|
uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
|
|
|
|
uint32_t z = size + (static_cast<uint32_t>(c) << 2);
|
|
|
|
return shiftMix(y * k2 ^ z * k3) * k2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return k2;
|
2014-11-09 12:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t hashLessThan16(const char * data, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (size > 8)
|
|
|
|
{
|
|
|
|
UInt64 a = unalignedLoad<UInt64>(data);
|
|
|
|
UInt64 b = unalignedLoad<UInt64>(data + size - 8);
|
|
|
|
return hashLen16(a, rotateByAtLeast1(b + size, size)) ^ b;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashLessThan8(data, size);
|
2014-11-09 12:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CRC32Hash
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t operator() (StringRef x) const
|
|
|
|
{
|
|
|
|
const char * pos = x.data;
|
|
|
|
size_t size = x.size;
|
2014-11-09 12:43:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
2014-11-09 12:43:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (size < 8)
|
|
|
|
{
|
|
|
|
return hashLessThan8(x.data, x.size);
|
|
|
|
}
|
2014-11-09 12:43:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * end = pos + size;
|
|
|
|
size_t res = -1ULL;
|
2014-11-09 12:43:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
UInt64 word = unalignedLoad<UInt64>(pos);
|
|
|
|
res = _mm_crc32_u64(res, word);
|
2014-11-09 12:43:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
pos += 8;
|
|
|
|
} while (pos + 8 < end);
|
2014-11-09 12:43:09 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
UInt64 word = unalignedLoad<UInt64>(end - 8); /// I'm not sure if this is normal.
|
2017-04-01 07:20:54 +00:00
|
|
|
res = _mm_crc32_u64(res, word);
|
2014-11-09 12:43:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
|
|
|
}
|
2014-11-09 12:43:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct StringRefHash : CRC32Hash {};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-06-20 11:32:45 +00:00
|
|
|
struct CRC32Hash
|
|
|
|
{
|
2017-12-28 12:58:39 +00:00
|
|
|
size_t operator() (StringRef /* x */) const
|
2017-06-20 11:32:45 +00:00
|
|
|
{
|
|
|
|
throw std::logic_error{"Not implemented CRC32Hash without SSE"};
|
2018-11-10 20:09:07 +00:00
|
|
|
}
|
2017-06-20 11:32:45 +00:00
|
|
|
};
|
|
|
|
|
2016-09-23 05:49:55 +00:00
|
|
|
struct StringRefHash : StringRefHash64 {};
|
2014-04-08 12:54:32 +00:00
|
|
|
|
2014-11-09 12:43:09 +00:00
|
|
|
#endif
|
|
|
|
|
2014-04-29 00:28:18 +00:00
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
template <>
|
|
|
|
struct hash<StringRef> : public StringRefHash {};
|
2014-04-29 00:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZeroTraits
|
2014-04-28 01:48:24 +00:00
|
|
|
{
|
2019-06-28 18:09:22 +00:00
|
|
|
inline bool check(const StringRef & x) { return 0 == x.size; }
|
2017-04-01 07:20:54 +00:00
|
|
|
inline void set(StringRef & x) { x.size = 0; }
|
2018-08-10 04:02:56 +00:00
|
|
|
}
|
2012-09-17 07:54:49 +00:00
|
|
|
|
2014-04-29 00:28:18 +00:00
|
|
|
|
2020-09-14 13:33:36 +00:00
|
|
|
std::ostream & operator<<(std::ostream & os, const StringRef & str);
|