mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-06 16:57:04 +08:00
Make it easier to swap around some hashing algorithms
This commit is contained in:
parent
dce9e17c34
commit
4275ec6878
@ -11,7 +11,7 @@
|
||||
#ifndef CHAISCRIPT_ALGEBRAIC_HPP_
|
||||
#define CHAISCRIPT_ALGEBRAIC_HPP_
|
||||
|
||||
#include "../utility/fnv1a.hpp"
|
||||
#include "../utility/hash.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
@ -57,37 +57,37 @@ namespace chaiscript
|
||||
#pragma warning(disable : 4307)
|
||||
#endif
|
||||
|
||||
const auto op_hash = utility::fnv1a_32(t_str);
|
||||
const auto op_hash = utility::hash(t_str);
|
||||
switch (op_hash) {
|
||||
case utility::fnv1a_32("=="): { return Opers::equals; }
|
||||
case utility::fnv1a_32("<"): { return Opers::less_than; }
|
||||
case utility::fnv1a_32(">"): { return Opers::greater_than; }
|
||||
case utility::fnv1a_32("<="): { return Opers::less_than_equal; }
|
||||
case utility::fnv1a_32(">="): { return Opers::greater_than_equal; }
|
||||
case utility::fnv1a_32("!="): { return Opers::not_equal; }
|
||||
case utility::fnv1a_32("="): { return Opers::assign; }
|
||||
case utility::fnv1a_32("++"): { return Opers::pre_increment; }
|
||||
case utility::fnv1a_32("--"): { return Opers::pre_decrement; }
|
||||
case utility::fnv1a_32("*="): { return Opers::assign_product; }
|
||||
case utility::fnv1a_32("+="): { return Opers::assign_sum; }
|
||||
case utility::fnv1a_32("-="): { return Opers::assign_difference; }
|
||||
case utility::fnv1a_32("&="): { return Opers::assign_bitwise_and; }
|
||||
case utility::fnv1a_32("|="): { return Opers::assign_bitwise_or; }
|
||||
case utility::fnv1a_32("<<="): { return Opers::assign_shift_left; }
|
||||
case utility::fnv1a_32(">>="): { return Opers::assign_shift_right; }
|
||||
case utility::fnv1a_32("%="): { return Opers::assign_remainder; }
|
||||
case utility::fnv1a_32("^="): { return Opers::assign_bitwise_xor; }
|
||||
case utility::fnv1a_32("<<"): { return Opers::shift_left; }
|
||||
case utility::fnv1a_32(">>"): { return Opers::shift_right; }
|
||||
case utility::fnv1a_32("%"): { return Opers::remainder; }
|
||||
case utility::fnv1a_32("&"): { return Opers::bitwise_and; }
|
||||
case utility::fnv1a_32("|"): { return Opers::bitwise_or; }
|
||||
case utility::fnv1a_32("^"): { return Opers::bitwise_xor; }
|
||||
case utility::fnv1a_32("~"): { return Opers::bitwise_complement; }
|
||||
case utility::fnv1a_32("+"): { return t_is_unary ? Opers::unary_plus : Opers::sum; }
|
||||
case utility::fnv1a_32("-"): { return t_is_unary ? Opers::unary_minus : Opers::difference; }
|
||||
case utility::fnv1a_32("/"): { return Opers::quotient; }
|
||||
case utility::fnv1a_32("*"): { return Opers::product; }
|
||||
case utility::hash("=="): { return Opers::equals; }
|
||||
case utility::hash("<"): { return Opers::less_than; }
|
||||
case utility::hash(">"): { return Opers::greater_than; }
|
||||
case utility::hash("<="): { return Opers::less_than_equal; }
|
||||
case utility::hash(">="): { return Opers::greater_than_equal; }
|
||||
case utility::hash("!="): { return Opers::not_equal; }
|
||||
case utility::hash("="): { return Opers::assign; }
|
||||
case utility::hash("++"): { return Opers::pre_increment; }
|
||||
case utility::hash("--"): { return Opers::pre_decrement; }
|
||||
case utility::hash("*="): { return Opers::assign_product; }
|
||||
case utility::hash("+="): { return Opers::assign_sum; }
|
||||
case utility::hash("-="): { return Opers::assign_difference; }
|
||||
case utility::hash("&="): { return Opers::assign_bitwise_and; }
|
||||
case utility::hash("|="): { return Opers::assign_bitwise_or; }
|
||||
case utility::hash("<<="): { return Opers::assign_shift_left; }
|
||||
case utility::hash(">>="): { return Opers::assign_shift_right; }
|
||||
case utility::hash("%="): { return Opers::assign_remainder; }
|
||||
case utility::hash("^="): { return Opers::assign_bitwise_xor; }
|
||||
case utility::hash("<<"): { return Opers::shift_left; }
|
||||
case utility::hash(">>"): { return Opers::shift_right; }
|
||||
case utility::hash("%"): { return Opers::remainder; }
|
||||
case utility::hash("&"): { return Opers::bitwise_and; }
|
||||
case utility::hash("|"): { return Opers::bitwise_or; }
|
||||
case utility::hash("^"): { return Opers::bitwise_xor; }
|
||||
case utility::hash("~"): { return Opers::bitwise_complement; }
|
||||
case utility::hash("+"): { return t_is_unary ? Opers::unary_plus : Opers::sum; }
|
||||
case utility::hash("-"): { return t_is_unary ? Opers::unary_minus : Opers::difference; }
|
||||
case utility::hash("/"): { return Opers::quotient; }
|
||||
case utility::hash("*"): { return Opers::product; }
|
||||
default: { return Opers::invalid; }
|
||||
}
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
|
||||
@ -36,32 +36,32 @@ namespace chaiscript
|
||||
static bool is_reserved_word(const T &s) noexcept
|
||||
{
|
||||
const static std::unordered_set<std::uint32_t> words{
|
||||
utility::fnv1a_32("def"),
|
||||
utility::fnv1a_32("fun"),
|
||||
utility::fnv1a_32("while"),
|
||||
utility::fnv1a_32("for"),
|
||||
utility::fnv1a_32("if"),
|
||||
utility::fnv1a_32("else"),
|
||||
utility::fnv1a_32("&&"),
|
||||
utility::fnv1a_32("||"),
|
||||
utility::fnv1a_32(","),
|
||||
utility::fnv1a_32("auto"),
|
||||
utility::fnv1a_32("return"),
|
||||
utility::fnv1a_32("break"),
|
||||
utility::fnv1a_32("true"),
|
||||
utility::fnv1a_32("false"),
|
||||
utility::fnv1a_32("class"),
|
||||
utility::fnv1a_32("attr"),
|
||||
utility::fnv1a_32("var"),
|
||||
utility::fnv1a_32("global"),
|
||||
utility::fnv1a_32("GLOBAL"),
|
||||
utility::fnv1a_32("_"),
|
||||
utility::fnv1a_32("__LINE__"),
|
||||
utility::fnv1a_32("__FILE__"),
|
||||
utility::fnv1a_32("__FUNC__"),
|
||||
utility::fnv1a_32("__CLASS__")};
|
||||
utility::hash("def"),
|
||||
utility::hash("fun"),
|
||||
utility::hash("while"),
|
||||
utility::hash("for"),
|
||||
utility::hash("if"),
|
||||
utility::hash("else"),
|
||||
utility::hash("&&"),
|
||||
utility::hash("||"),
|
||||
utility::hash(","),
|
||||
utility::hash("auto"),
|
||||
utility::hash("return"),
|
||||
utility::hash("break"),
|
||||
utility::hash("true"),
|
||||
utility::hash("false"),
|
||||
utility::hash("class"),
|
||||
utility::hash("attr"),
|
||||
utility::hash("var"),
|
||||
utility::hash("global"),
|
||||
utility::hash("GLOBAL"),
|
||||
utility::hash("_"),
|
||||
utility::hash("__LINE__"),
|
||||
utility::hash("__FILE__"),
|
||||
utility::hash("__FUNC__"),
|
||||
utility::hash("__CLASS__")};
|
||||
|
||||
return words.count(utility::fnv1a_32(s)) == 1;
|
||||
return words.count(utility::hash(s)) == 1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
||||
@ -27,7 +27,7 @@
|
||||
#include "chaiscript_common.hpp"
|
||||
#include "chaiscript_optimizer.hpp"
|
||||
#include "chaiscript_tracer.hpp"
|
||||
#include "../utility/fnv1a.hpp"
|
||||
#include "../utility/hash.hpp"
|
||||
#include "../utility/static_string.hpp"
|
||||
|
||||
#if defined(CHAISCRIPT_UTF16_UTF32)
|
||||
@ -934,7 +934,7 @@ namespace chaiscript
|
||||
if (Id_()) {
|
||||
|
||||
auto text = Position::str(start, m_position);
|
||||
const auto text_hash = utility::fnv1a_32(text);
|
||||
const auto text_hash = utility::hash(text);
|
||||
|
||||
if (validate) {
|
||||
validate_object_name(text);
|
||||
@ -946,29 +946,29 @@ namespace chaiscript
|
||||
#endif
|
||||
|
||||
switch (text_hash) {
|
||||
case utility::fnv1a_32("true"): {
|
||||
case utility::hash("true"): {
|
||||
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col, const_var(true)));
|
||||
} break;
|
||||
case utility::fnv1a_32("false"): {
|
||||
case utility::hash("false"): {
|
||||
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col, const_var(false)));
|
||||
} break;
|
||||
case utility::fnv1a_32("Infinity"): {
|
||||
case utility::hash("Infinity"): {
|
||||
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
|
||||
const_var(std::numeric_limits<double>::infinity())));
|
||||
} break;
|
||||
case utility::fnv1a_32("NaN"): {
|
||||
case utility::hash("NaN"): {
|
||||
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
|
||||
const_var(std::numeric_limits<double>::quiet_NaN())));
|
||||
} break;
|
||||
case utility::fnv1a_32("__LINE__"): {
|
||||
case utility::hash("__LINE__"): {
|
||||
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
|
||||
const_var(start.line)));
|
||||
} break;
|
||||
case utility::fnv1a_32("__FILE__"): {
|
||||
case utility::hash("__FILE__"): {
|
||||
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
|
||||
const_var(m_filename)));
|
||||
} break;
|
||||
case utility::fnv1a_32("__FUNC__"): {
|
||||
case utility::hash("__FUNC__"): {
|
||||
std::string fun_name = "NOT_IN_FUNCTION";
|
||||
for (size_t idx = m_match_stack.size() - 1; idx > 0; --idx)
|
||||
{
|
||||
@ -981,7 +981,7 @@ namespace chaiscript
|
||||
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
|
||||
const_var(fun_name)));
|
||||
} break;
|
||||
case utility::fnv1a_32("__CLASS__"): {
|
||||
case utility::hash("__CLASS__"): {
|
||||
std::string fun_name = "NOT_IN_CLASS";
|
||||
for (size_t idx = m_match_stack.size() - 1; idx > 1; --idx)
|
||||
{
|
||||
@ -995,7 +995,7 @@ namespace chaiscript
|
||||
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
|
||||
const_var(fun_name)));
|
||||
} break;
|
||||
case utility::fnv1a_32("_"): {
|
||||
case utility::hash("_"): {
|
||||
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
|
||||
Boxed_Value(std::make_shared<dispatch::Placeholder_Object>())));
|
||||
} break;
|
||||
|
||||
@ -1,68 +0,0 @@
|
||||
// This file is distributed under the BSD License.
|
||||
// See "license.txt" for details.
|
||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_UTILITY_FNV1A_HPP_
|
||||
#define CHAISCRIPT_UTILITY_FNV1A_HPP_
|
||||
|
||||
|
||||
#include <cstdint>
|
||||
#include "../chaiscript_defines.hpp"
|
||||
|
||||
|
||||
namespace chaiscript
|
||||
{
|
||||
|
||||
|
||||
namespace utility
|
||||
{
|
||||
template<typename Itr>
|
||||
static constexpr std::uint32_t fnv1a_32(Itr begin, Itr end) noexcept {
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
||||
#endif
|
||||
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4307)
|
||||
#endif
|
||||
std::uint32_t h = 0x811c9dc5;
|
||||
|
||||
while (begin != end) {
|
||||
h = (h ^ (*begin)) * 0x01000193;
|
||||
++begin;
|
||||
}
|
||||
return h;
|
||||
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
template<size_t N>
|
||||
static constexpr std::uint32_t fnv1a_32(const char (&str)[N]) noexcept {
|
||||
return fnv1a_32(std::begin(str), std::end(str)-1);
|
||||
}
|
||||
|
||||
static constexpr std::uint32_t fnv1a_32(const std::string_view &sv) noexcept {
|
||||
return fnv1a_32(sv.begin(), sv.end());
|
||||
}
|
||||
|
||||
static std::uint32_t fnv1a_32(const std::string &s) noexcept {
|
||||
return fnv1a_32(s.begin(), s.end());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
101
include/chaiscript/utility/hash.hpp
Normal file
101
include/chaiscript/utility/hash.hpp
Normal file
@ -0,0 +1,101 @@
|
||||
// This file is distributed under the BSD License.
|
||||
// See "license.txt" for details.
|
||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_UTILITY_FNV1A_HPP_
|
||||
#define CHAISCRIPT_UTILITY_FNV1A_HPP_
|
||||
|
||||
|
||||
#include <cstdint>
|
||||
#include "../chaiscript_defines.hpp"
|
||||
|
||||
|
||||
namespace chaiscript
|
||||
{
|
||||
namespace utility
|
||||
{
|
||||
namespace fnv1a {
|
||||
template<typename Itr>
|
||||
static constexpr std::uint32_t hash(Itr begin, Itr end) noexcept {
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
||||
#endif
|
||||
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4307)
|
||||
#endif
|
||||
std::uint32_t h = 0x811c9dc5;
|
||||
|
||||
while (begin != end) {
|
||||
h = (h ^ (*begin)) * 0x01000193;
|
||||
++begin;
|
||||
}
|
||||
return h;
|
||||
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
template<size_t N>
|
||||
static constexpr std::uint32_t hash(const char (&str)[N]) noexcept {
|
||||
return hash(std::begin(str), std::end(str)-1);
|
||||
}
|
||||
|
||||
static constexpr std::uint32_t hash(const std::string_view &sv) noexcept {
|
||||
return hash(sv.begin(), sv.end());
|
||||
}
|
||||
|
||||
static inline std::uint32_t hash(const std::string &s) noexcept {
|
||||
return hash(s.begin(), s.end());
|
||||
}
|
||||
}
|
||||
|
||||
namespace jenkins_one_at_a_time {
|
||||
template<typename Itr>
|
||||
static constexpr std::uint32_t hash(Itr begin, Itr end) noexcept {
|
||||
std::uint32_t hash = 0;
|
||||
|
||||
while (begin != end) {
|
||||
hash += *begin;
|
||||
hash += hash << 10;
|
||||
hash ^= hash >> 6;
|
||||
++begin;
|
||||
}
|
||||
|
||||
hash += hash << 3;
|
||||
hash ^= hash >> 11;
|
||||
hash += hash << 15;
|
||||
return hash;
|
||||
}
|
||||
|
||||
template<size_t N>
|
||||
static constexpr std::uint32_t hash(const char (&str)[N]) noexcept {
|
||||
return hash(std::begin(str), std::end(str)-1);
|
||||
}
|
||||
|
||||
static constexpr std::uint32_t hash(const std::string_view &sv) noexcept {
|
||||
return hash(sv.begin(), sv.end());
|
||||
}
|
||||
|
||||
static inline std::uint32_t hash(const std::string &s) noexcept {
|
||||
return hash(s.begin(), s.end());
|
||||
}
|
||||
}
|
||||
|
||||
using fnv1a::hash;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
x
Reference in New Issue
Block a user