aboutsummaryrefslogtreecommitdiffstats
path: root/consteval_huffman.hpp
diff options
context:
space:
mode:
authorclyne <clyne@bitgloo.com>2020-12-29 10:59:09 -0500
committerGitHub <noreply@github.com>2020-12-29 10:59:09 -0500
commit0a217fa4864741f19e516d0e99a05068695d0ed4 (patch)
tree8193b515150d1e16149fe064a95e11e28ac456a7 /consteval_huffman.hpp
parentd405af197f1df84389afdcd1a340839dabe4b642 (diff)
Add size()
Diffstat (limited to 'consteval_huffman.hpp')
-rw-r--r--consteval_huffman.hpp34
1 files changed, 24 insertions, 10 deletions
diff --git a/consteval_huffman.hpp b/consteval_huffman.hpp
index 594313e..e86a295 100644
--- a/consteval_huffman.hpp
+++ b/consteval_huffman.hpp
@@ -12,6 +12,9 @@
namespace detail
{
+ // Provides a string container for the huffman compressor.
+ // Using this allows for automatic string data length measurement, as
+ // well as implementation of the _huffman suffix.
template<unsigned long int N>
struct huffman_string_container {
char data[N];
@@ -32,14 +35,18 @@ namespace detail
* minimal run-time interface for decompressing the data.
* @tparam data The string of data to be compressed.
*/
-template<detail::huffman_string_container data>
- requires(data.size() > 0)
+template<auto data>
+ requires(
+ std::same_as<decltype(data),
+ const detail::huffman_string_container<data.size()>> &&
+ data.size() > 0)
class huffman_compressor
{
using size_t = long int;
using usize_t = unsigned long int;
// Note: class internals need to be defined before the public interface.
+ // See the bottom of the class definition for usage.
private:
// Node structure used to build a tree for calculating Huffman codes.
struct node {
@@ -316,6 +323,16 @@ public:
friend class huffman_compressor;
};
+ // Stick the forward_iterator check here just so it's run
+ consteval huffman_compressor() noexcept
+ requires (std::forward_iterator<decode_info>)
+ {
+ if constexpr (bytes_saved() > 0) {
+ build_decode_tree();
+ compress();
+ }
+ }
+
auto begin() const noexcept {
return decode_info(this);
}
@@ -325,14 +342,11 @@ public:
auto cbegin() const noexcept { begin(); }
auto cend() const noexcept { end(); }
- // Stick the requires clause here just so it's run
- consteval huffman_compressor() noexcept
- requires (std::forward_iterator<decode_info>)
- {
- if constexpr (bytes_saved() > 0) {
- build_decode_tree();
- compress();
- }
+ auto size() const noexcept {
+ if constexpr (bytes_saved() > 0)
+ return compressed_size();
+ else
+ return uncompressed_size();
}
private: