-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
141 lines (115 loc) · 3.41 KB
/
utils.cpp
File metadata and controls
141 lines (115 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* @file utils.cpp
* @author Marián Tarageľ (xtarag01)
* @brief Helpful utilities for work with bits and vectors
* @date 8.5.2024
*/
#include <cstdio>
#include <cmath>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <numeric>
#include "utils.hpp"
int current_bit = 0;
unsigned long int buffer_index = 0;
std::vector<bool> int2ba(unsigned long int num, unsigned long int length)
{
std::vector<bool> result;
// covert numver to binary
while (num > 0) {
if (num & 1) {
result.push_back(1);
} else {
result.push_back(0);
}
num >>= 1;
}
// add leading zeros
while (result.size() < length)
result.push_back(0);
reverse(result.begin(), result.end());
return result;
}
unsigned long int ba2int(std::vector<bool> bits)
{
unsigned long int result = 0;
// convert binary number to decimal
unsigned long int power = 0;
for (auto i = bits.rbegin(); i != bits.rend(); i++) {
if (*i)
result += pow(2, power);
power++;
}
return result;
}
void write_bits(std::vector<bool> bits, FILE *fp)
{
static uint8_t bit_buffer = 0;
// write every bit to a bit buffer
for (bool bit : bits) {
bit_buffer <<= 1;
if (bit)
bit_buffer |= 1;
current_bit++;
if (current_bit == 8) { // if the buffer is full write it to the file
fwrite(&bit_buffer, 1, 1, fp);
current_bit = 0;
bit_buffer = 0;
}
}
}
void flush_buffer(FILE *fp, std::vector<bool> end_symbol)
{
int i = 0;
while (current_bit) {
write_bits({end_symbol[i]}, fp);
i++;
}
}
bool read_next_bit(std::vector<u_int8_t> buffer)
{
std::vector<bool> binary = int2ba(buffer[buffer_index], 8);
bool bit = binary[current_bit];
current_bit++;
if (current_bit == 8) { // if the current bit index is outside of a byte
current_bit = 0;
buffer_index++;
}
return bit;
}
unsigned long int read_bits(std::vector<uint8_t> buffer, unsigned long int num)
{
std::vector<bool> bits;
for (unsigned long int i = 0; i < num; i++) {
bool bit = read_next_bit(buffer);
bits.push_back(bit);
}
unsigned long int result = ba2int(bits); // convert bits to decimal
return result;
}
std::vector<size_t> sort_permutation(const std::vector<uint8_t> &vec)
{
std::vector<size_t> p(vec.size());
iota(p.begin(), p.end(), 0); // create a vector with increasing values (0, 1, 2, 3, ...)
stable_sort(p.begin(), p.end(), [&](size_t i, size_t j){ return vec[i] < vec[j]; }); // sort this vector based on input vector
return p;
}
std::vector<uint8_t> apply_permutation(const std::vector<uint8_t> &vec, const std::vector<size_t> &p)
{
std::vector<uint8_t> sorted_vec(vec.size());
// transform original vector based on the indexes from sort permutation
transform(p.begin(), p.end(), sorted_vec.begin(), [&](size_t i){ return vec[i]; });
return sorted_vec;
}
std::vector<uint8_t> transpose(std::vector<uint8_t> buffer, unsigned long int width)
{
unsigned long int height = buffer.size() / width;
std::vector<uint8_t> transposed;
for (unsigned long int y = 0; y < height; y++) {
for (unsigned long int x = 0; x < buffer.size(); x+=height) {
transposed.push_back(buffer[x + y]);
}
}
return transposed;
}