2012-05-31 04:49:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <city.h>
|
|
|
|
|
|
|
|
#include <string>
|
2013-06-30 16:56:00 +00:00
|
|
|
#include <vector>
|
2012-05-31 04:49:55 +00:00
|
|
|
|
2014-01-08 16:33:28 +00:00
|
|
|
#include <functional>
|
2013-10-11 11:43:50 +00:00
|
|
|
|
2012-05-31 04:49:55 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
/// Штука, чтобы не создавать строки для поиска подстроки в хэш таблице.
|
|
|
|
struct StringRef
|
|
|
|
{
|
|
|
|
const char * data;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
StringRef(const char * data_, size_t size_) : data(data_), size(size_) {}
|
2012-05-31 05:41:56 +00:00
|
|
|
StringRef(const unsigned char * data_, size_t size_) : data(reinterpret_cast<const char *>(data_)), size(size_) {}
|
2012-05-31 04:49:55 +00:00
|
|
|
StringRef(const std::string & s) : data(s.data()), size(s.size()) {}
|
|
|
|
StringRef() : data(NULL), size(0) {}
|
2013-04-09 19:04:25 +00:00
|
|
|
|
|
|
|
std::string toString() const { return std::string(data, size); }
|
2012-05-31 04:49:55 +00:00
|
|
|
};
|
|
|
|
|
2013-06-30 16:56:00 +00:00
|
|
|
typedef std::vector<StringRef> StringRefs;
|
|
|
|
|
2012-05-31 04:49:55 +00:00
|
|
|
inline bool operator==(StringRef lhs, StringRef rhs)
|
|
|
|
{
|
2012-06-01 10:45:29 +00:00
|
|
|
/// Так почему-то быстрее, чем return lhs.size == rhs.size && 0 == memcmp(lhs.data, rhs.data, lhs.size);
|
|
|
|
|
|
|
|
if (lhs.size != rhs.size)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t pos = 0; pos < lhs.size; ++pos)
|
|
|
|
if (lhs.data[pos] != rhs.data[pos])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2012-05-31 04:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(StringRef lhs, StringRef rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StringRefHash
|
|
|
|
{
|
|
|
|
inline size_t operator() (StringRef x) const
|
|
|
|
{
|
|
|
|
return CityHash64(x.data, x.size);
|
|
|
|
}
|
|
|
|
};
|
2012-09-17 07:54:49 +00:00
|
|
|
|
|
|
|
struct StringRefZeroTraits
|
|
|
|
{
|
|
|
|
static inline bool check(DB::StringRef x) { return NULL == x.data; }
|
|
|
|
static inline void set(DB::StringRef & x) { x.data = NULL; }
|
|
|
|
};
|
2013-03-28 10:48:26 +00:00
|
|
|
|
|
|
|
inline bool operator==(StringRef lhs, const char * rhs)
|
|
|
|
{
|
|
|
|
for (size_t pos = 0; pos < lhs.size; ++pos)
|
|
|
|
{
|
|
|
|
if (!rhs[pos] || lhs.data[pos] != rhs[pos])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-05-31 04:49:55 +00:00
|
|
|
}
|
2013-10-11 11:43:50 +00:00
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
2014-01-08 16:33:28 +00:00
|
|
|
template <>
|
|
|
|
struct hash<DB::StringRef>
|
2013-10-11 11:43:50 +00:00
|
|
|
{
|
2014-01-08 16:33:28 +00:00
|
|
|
size_t operator()(const DB::StringRef & x) const
|
2013-10-11 11:43:50 +00:00
|
|
|
{
|
2014-01-08 16:33:28 +00:00
|
|
|
return CityHash64(x.data, x.size);
|
|
|
|
}
|
|
|
|
};
|
2013-10-11 11:43:50 +00:00
|
|
|
}
|