Commit aebe21aa authored by Philip Trettner's avatar Philip Trettner

added hash and byte array view

parent 935e9509
......@@ -81,6 +81,14 @@ struct array_view
constexpr bool empty() const { return _size == 0; }
array_view<std::byte const> as_bytes() const
{
#ifndef GLOW_HAS_GLM
static_assert(std::is_trivially_copyable_v<T>, "cannot make byte view for non-trivial type");
#endif
return array_view<std::byte const>{reinterpret_cast<std::byte const*>(_data), _size * sizeof(T)};
}
private:
T* _data = nullptr;
size_t _size = 0;
......@@ -96,6 +104,20 @@ auto make_array_view(Range&& r)
namespace detail
{
template <class Range>
constexpr bool can_make_array_view = convertible_to_array_view<Range, void>;
constexpr bool can_make_array_view = convertible_to_array_view<Range, void const>;
}
template <class T>
array_view<std::byte const> as_byte_view(T const& v)
{
if constexpr (detail::can_make_array_view<T const&>)
return make_array_view(v).as_bytes();
else
{
#ifndef GLOW_HAS_GLM
static_assert(std::is_trivially_copyable_v<T>, "type must be trivial");
#endif
return array_view<T const>(&v, 1).as_bytes();
}
}
}
#include "hash.hh"
#include <glow/detail/xxHash/xxh3.hh>
size_t glow::hash_xxh3(array_view<const std::byte> data, size_t seed)
{
//
return XXH3_64bits_withSeed(data.data(), data.size(), seed);
}
#pragma once
#include <cstddef>
#include <glow/common/array_view.hh>
namespace glow
{
// returns a hash of the data by executing https://github.com/Cyan4973/xxHash
[[nodiscard]] size_t hash_xxh3(array_view<std::byte const> data, size_t seed);
}
This diff is collapsed.
/*
* xxHash - Extremely Fast Hash algorithm
* Copyright (C) 2012-2020 Yann Collet
*
* BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You can contact the author at:
* - xxHash homepage: https://www.xxhash.com
* - xxHash source repository: https://github.com/Cyan4973/xxHash
*/
/*
* xxhash.c instantiates functions defined in xxhash.h
*/
#define XXH_STATIC_LINKING_ONLY /* access advanced declarations */
#define XXH_IMPLEMENTATION /* access definitions */
#include "xxhash.hh"
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment