-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add elastic size to get bytes of member (#51)
- Loading branch information
Showing
8 changed files
with
154 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#pragma once | ||
#include "zig_zag.hpp" | ||
|
||
namespace elastic | ||
{ | ||
template <integer_t _Ty> | ||
std::size_t element_size(const _Ty& t) | ||
{ | ||
auto temp = zigzag_encode(t); | ||
std::size_t size = 1; | ||
|
||
while ((temp >>= zig_zag_bit) != 0) | ||
size++; | ||
|
||
return size; | ||
} | ||
|
||
template <float_point_t _Ty> | ||
std::size_t element_size(const _Ty&) | ||
{ | ||
return sizeof(_Ty); | ||
} | ||
|
||
template<sequence_t _Ty> | ||
std::size_t element_size(const _Ty& t); | ||
|
||
template<aggregate_class_t _Ty> | ||
std::size_t element_size(const _Ty& t) | ||
{ | ||
constexpr auto N = reflect::tuple_size_v<std::remove_cvref_t<_Ty>>; | ||
|
||
auto func = [&]<std::size_t... I>(std::index_sequence<I...>) | ||
{ | ||
return (element_size(reflect::get<I>(t)) + ...); | ||
}; | ||
|
||
return func(std::make_index_sequence<N>{}); | ||
} | ||
|
||
template <sequence_t _Ty> | ||
std::size_t element_size(const _Ty& t) | ||
{ | ||
using type = std::remove_cvref_t<_Ty>; | ||
|
||
std::size_t size = 0; | ||
|
||
using value_type = typename type::value_type; | ||
|
||
if constexpr (sizeof(value_type) == 1) | ||
{ | ||
size = t.size(); | ||
} | ||
else | ||
{ | ||
for (const auto& value : t) | ||
{ | ||
size += element_size(value); | ||
} | ||
} | ||
|
||
return size; | ||
} | ||
} // namespace elastic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
#include "type_traits.hpp" | ||
|
||
namespace elastic | ||
{ | ||
inline static constexpr int32_t zig_zag_bit = 7; | ||
|
||
template <elastic::integer_t _Ty> | ||
elastic::zig_zag_t<_Ty> zigzag_encode(_Ty value) | ||
{ | ||
using value_type = elastic::zig_zag_t<_Ty>; | ||
|
||
using remove_unsigned_type = elastic::remove_unsigned_t<_Ty>; | ||
|
||
constexpr auto size = sizeof(_Ty) * 8 - 1; | ||
|
||
return static_cast<remove_unsigned_type>(value) << 1 ^ static_cast<remove_unsigned_type>(value) >> size; | ||
} | ||
|
||
template <elastic::integer_t _Ty> | ||
_Ty zigzag_decode(elastic::zig_zag_t<_Ty> value) | ||
{ | ||
return static_cast<_Ty>((value >> 1) ^ (~(value & 1) + 1)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.