-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompression.cpp
More file actions
108 lines (100 loc) · 2.51 KB
/
Compression.cpp
File metadata and controls
108 lines (100 loc) · 2.51 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
#include "Compression.h"
/**
* Constructor
*/
Compression::Compression(std::string path) : dangling(0), list_of_words(0) {
this->readData(path);
}
/**
* Read data from input file to memory
*/
void Compression::readData(std::string path) {
std::ifstream file;
file.open(path, file.in);
char line[256];
if (file.is_open()) {
while (!file.eof()) {
file.getline(line, 256);
list_of_words.emplace_back(extractValue(line));
}
} else {
std::cout << "Bad path." << std::endl;
}
file.close();
}
/**
* Manage the UD check algorithm
*/
bool Compression::UD_check() {
int *steps = new int;
for (size_t i = 0; i < sizeof(list_of_words) / sizeof(int); i++) {
for (size_t j = 0; j < sizeof(list_of_words) / sizeof(int); j++) {
if (i != j) {
if (startsWith(list_of_words[i], list_of_words[j], steps)) {
int temp = list_of_words[j] << *(steps);
dangling.push_back((list_of_words[i] ^ temp));
}
}
}
}
return isDanglingInCode();
}
/**
* Looks for all danglings in the list of words, in order to confirm UD
*/
bool Compression::isDanglingInCode() {
if (!dangling.size()) {
return true;
}
for (size_t i = 0; i < sizeof(dangling) / sizeof(int); i++) {
if (contains(dangling[i])) {
return false;
}
}
return true;
}
/**
* Looks for one dangling in the list of words
*/
bool Compression::contains(int word) {
int size = sizeof(list_of_words) / sizeof(int);
for (int i = 0; i < size; i++) {
if (word == i) {
return true;
}
}
return false;
}
/**
* Checks if word2 is prefix of word1, if yes assign the number of bit shifting to steps variable
*/
bool Compression::startsWith(int word1, int word2, int *steps) {
int temp = word1;
int counter = 0;
while (temp > 0) {
if ((temp ^ word2) == 0) {
*steps = counter;
return true;
}
temp = (temp >> 1);
counter++;
}
return false;
}
/**
* Extract the value of the word from the string
*/
int Compression::extractValue(char *line) {
int index = 4, ind = 0, check = line[index++], counter = 0, ar[50];
while (check != 41) {
counter++;
ar[ind++] = check - 48;
check = line[index++];
}
check = 0;
index = 0;
while (counter-- > 0) {
check += ar[--ind] * std::pow(10, index++);
}
return check;
}