1
0
Fork 0
arangodb/3rdParty/iresearch/core/utils/string.cpp

99 lines
2.8 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2016 by EMC Corporation, All Rights Reserved
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is EMC Corporation
///
/// @author Andrey Abramov
/// @author Vasiliy Nabatchikov
////////////////////////////////////////////////////////////////////////////////
#include <chrono>
#include "MurmurHash/MurmurHash3.h"
#include "integer.hpp"
#include "string.hpp"
// -----------------------------------------------------------------------------
// --SECTION-- hash function
// -----------------------------------------------------------------------------
NS_LOCAL
inline uint32_t get_seed() {
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch()
);
return static_cast<uint32_t>(ms.count());
}
template<typename T>
inline size_t get_hash(const T* value, size_t size) {
static const auto seed = get_seed();
size_t length = std::min(size, size_t(irs::integer_traits<int>::const_max));
uint32_t code;
// hash as much as possible if length greater than std::numeric_limits<int>::max
MurmurHash3_x86_32(value, int(length), seed, &code);
return code;
}
NS_END
NS_ROOT
/* -------------------------------------------------------------------
* basic_string_ref
* ------------------------------------------------------------------*/
#if defined(_MSC_VER) && defined(IRESEARCH_DLL)
template class IRESEARCH_API basic_string_ref<char>;
template class IRESEARCH_API basic_string_ref<byte_type>;
#endif
NS_BEGIN(hash_utils)
size_t hash(const std::string& value) {
return get_hash(value.c_str(), value.size());
}
size_t hash(const bstring& value) {
return get_hash(value.c_str(), value.size());
}
size_t hash(const char* value) {
return get_hash(value, std::char_traits<char>::length(value) * sizeof(char));
}
size_t hash(const wchar_t* value) {
return get_hash(value, std::char_traits<wchar_t>::length(value) * sizeof(wchar_t));
}
size_t hash(const bytes_ref& value) {
return get_hash(value.c_str(), value.size());
}
size_t hash(const string_ref& value) {
return get_hash(value.c_str(), value.size());
}
NS_END // detail
NS_END