consolidate all repos to one for archive
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include "Buffer.hpp"
|
||||
|
||||
class BitReader
|
||||
{
|
||||
public:
|
||||
int k = 8;
|
||||
Buffer buffer;
|
||||
char x = 0;
|
||||
size_t pos = 0;
|
||||
|
||||
int readInt()
|
||||
{
|
||||
int ret = buffer.buffer[pos];
|
||||
pos += 4;
|
||||
return ret;
|
||||
}
|
||||
|
||||
char readByte()
|
||||
{
|
||||
x = buffer.buffer[pos];
|
||||
pos++;
|
||||
return x;
|
||||
}
|
||||
|
||||
bool readBit()
|
||||
{
|
||||
if (k == 8)
|
||||
{
|
||||
readByte();
|
||||
k = 0;
|
||||
}
|
||||
bool b = (x >> k) & 1;
|
||||
k++;
|
||||
return b;
|
||||
}
|
||||
};
|
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include "Buffer.hpp"
|
||||
|
||||
class BitWriter
|
||||
{
|
||||
public:
|
||||
int k = 0;
|
||||
Buffer buffer;
|
||||
char x = 0;
|
||||
|
||||
void writeByte(uint8_t x)
|
||||
{
|
||||
// f.write((char*)&x, 1);
|
||||
buffer.add_end(&x, sizeof(uint8_t));
|
||||
}
|
||||
|
||||
void writeInt(int x)
|
||||
{
|
||||
buffer.add_end((uint8_t *)&x, sizeof(x));
|
||||
}
|
||||
|
||||
void writeBit(bool b)
|
||||
{
|
||||
if (k == 8)
|
||||
{
|
||||
writeByte(x);
|
||||
k = 0;
|
||||
x = 0;
|
||||
}
|
||||
x ^= (-b ^ x) & (1 << k);
|
||||
k++;
|
||||
}
|
||||
|
||||
void finish()
|
||||
{
|
||||
if (k > 0)
|
||||
writeByte(x);
|
||||
}
|
||||
};
|
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
class Buffer
|
||||
{
|
||||
public:
|
||||
uint8_t *buffer = nullptr;
|
||||
size_t taken = 0;
|
||||
size_t size = 0;
|
||||
std::string file_path;
|
||||
|
||||
Buffer(size_t size);
|
||||
Buffer();
|
||||
~Buffer();
|
||||
bool resize(size_t new_size);
|
||||
int add_end(uint8_t *data, size_t data_size);
|
||||
int add_middle(uint8_t *data, size_t data_size, size_t index);
|
||||
|
||||
// Removes data from buffer without checking if it's in the buffer
|
||||
void remove_fast(uint8_t *data, size_t data_size);
|
||||
|
||||
void remove(size_t index, size_t data_size);
|
||||
|
||||
int find(uint8_t *data, size_t data_size);
|
||||
|
||||
void remove(uint8_t *data, size_t data_size);
|
||||
|
||||
void clear() { taken = 0; }
|
||||
|
||||
bool save_to_file();
|
||||
bool save_to_file(std::string file_path);
|
||||
|
||||
bool load_from_file();
|
||||
bool load_from_file(std::string file_path);
|
||||
};
|
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include "BitWriter.hpp"
|
||||
#include <vector>
|
||||
|
||||
class Compressor
|
||||
{
|
||||
|
||||
public:
|
||||
Compressor() = default;
|
||||
~Compressor() = default;
|
||||
void compress(std::vector<uint8_t> &input);
|
||||
BitWriter bitWriter;
|
||||
|
||||
private:
|
||||
void two_bit_dif(int8_t num);
|
||||
void tree_bit_dif(int8_t num);
|
||||
void four_bit_dif(int8_t num);
|
||||
void five_bit_dif(int8_t num);
|
||||
void encode_diff(int8_t num);
|
||||
void encode_abs(int16_t num);
|
||||
void write_zero();
|
||||
|
||||
int numOfZeros = 0;
|
||||
};
|
@@ -0,0 +1,15 @@
|
||||
#include "BitReader.hpp"
|
||||
#include <vector>
|
||||
|
||||
class Decompressor
|
||||
{
|
||||
public:
|
||||
std::vector<uint8_t> decompress();
|
||||
BitReader bitReader;
|
||||
|
||||
private:
|
||||
int8_t twoZero();
|
||||
int8_t treeZero();
|
||||
int8_t forZero();
|
||||
int8_t fiveZero();
|
||||
};
|
Reference in New Issue
Block a user