Created
October 11, 2020 10:26
-
-
Save JuanDiegoMontoya/5b95b2bac71708ad87964036b53a8759 to your computer and use it in GitHub Desktop.
An implementation of delta encoding for signed integral types in C++20.
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
#pragma once | |
#include <span> | |
#include <vector> | |
#include <concepts> | |
namespace Compression | |
{ | |
template<std::signed_integral T> | |
std::vector<T> EncodeDelta(std::span<T> array) | |
{ | |
std::vector<T> encoded; | |
T prev{ 0 }; | |
for (T current : array) | |
{ | |
T delta = current - prev; | |
prev = current; | |
encoded.push_back(delta); | |
} | |
return encoded; | |
} | |
template<std::signed_integral T> | |
std::vector<T> DecodeDelta(std::span<T> encoded) | |
{ | |
std::vector<T> decoded; | |
T prev{ 0 }; | |
for (T delta : encoded) | |
{ | |
T current = delta + prev; | |
prev = current; | |
decoded.push_back(current); | |
} | |
return decoded; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment