Skip to content

Instantly share code, notes, and snippets.

@JuanDiegoMontoya
Created October 11, 2020 10:26
Show Gist options
  • Save JuanDiegoMontoya/5b95b2bac71708ad87964036b53a8759 to your computer and use it in GitHub Desktop.
Save JuanDiegoMontoya/5b95b2bac71708ad87964036b53a8759 to your computer and use it in GitHub Desktop.
An implementation of delta encoding for signed integral types in C++20.
#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