Giter Site home page Giter Site logo

rangecoder-cpp17's Introduction

Range Coder

test

Header-only range coder library.

Usage

#include "rangecoder.h"
#include <queue>
#include <vector>

// Probability model used to encode/decode.
// Note this inherit rangecoder::PModel and implement their virtual method.
// Basic idea is frequency table.
// (Sample implementation is available in test directory)
class PModel : public rangecoder::PModel {
    public:
    auto c_freq(int index) const -> range_t { /* omit */ }
    // Accumrated frequency of index, i.e. sum of frequency of range [min_index, index).
    auto cum_freq(int index) const -> range_t { /* omit */ }
    auto min_index() const -> int { /* omit */ }
    auto max_index() const -> int { /* omit */ }

    // omit
}

int main() {
    // We have data to encode.
    std::vector<int> sequence_of_data = get_data();
    // Some PModel, compatible to data, is required to both encode and decode.
    auto pmodel = PModel();

    // Encode
    auto encoder = rangecoder::RangeEncoder();
    for (auto data: sequence_of_data) {
        encoder.encode(pmodel, data);
    }
    auto bytes = encoder.finish();

    // Convert vector to queue.
    auto bytes_queue = std::queue<rangecoder::byte_t>();
    for (auto byte: bytes) {
        bytes_queue.push(byte);
    }

    // Decode
    auto decoded = std::vector<int>();
    auto decoder = rangecoder::RangeDecoder();
    decoder.start(bytes_queue);
    for (int i = 0; i < sequence_of_data.size(); i++){
        decoded.push_back(decoder.decode(pmodel));
    }

    assert(sequence_of_data == decoded);
}

rangecoder-cpp17's People

Contributors

diegodox avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.