Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Elias omega coding
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Encoding === <syntaxhighlight lang="cpp" line="1">// elias_omega_encode is written as a template function taking in an integer // type parameter, because Integer can be a big integer class, and Elias omega // encoding will still encode it extremely efficiently (10^10000 only uses 23 // bits more than the binary representation of 10^10000) #include <vector> template <class Integer> constexpr std::vector<bool> little_endian_binary(Integer num){ std::vector<bool> bits{}; while (num != 0){ bits.push_back(num & 0b1u); num >>= 1; } return bits; } constexpr std::vector<bool> concat(std::vector<bool> l, std::vector<bool> r){ for (bool const i : r){ l.push_back(i); } return l; } //////////////////////////////////////////////////////////////////////////////// // TEMPLATE PARAMETERS // // Integer: Type of num // //////////////////////////////////////////////////////////////////////////////// // PARAMETERS // // num: Integer storing a positive integer to convert to Elias omega encoding // //////////////////////////////////////////////////////////////////////////////// // RETURNS // // std::vector<bool> storing Elias omega encoding of num // //////////////////////////////////////////////////////////////////////////////// // EXCEPTIONS // // std::range_error if num is <= 0, as num must be >= 1 // // std::bad_alloc if memory runs out // // Any exception thrown by Integer bitwise or conversion operators // //////////////////////////////////////////////////////////////////////////////// template <class Integer> constexpr std::vector<bool> elias_omega_encode(Integer num){ if (num <= 0){ throw std::range_error("elias_omega_encode expects a value >= 1"); } std::vector<bool> bits = {false}; while (num != 1){ std::vector<bool> le_binary = little_endian_binary(std::move(num)); auto const sizem1 = le_binary.size() - 1; bits = concat(std::move(le_binary), std::move(bits)); num = static_cast<Integer>(sizem1); } return bits; }</syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)