forked from matildeopbravo/shafa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.c
178 lines (157 loc) · 3.51 KB
/
data.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* @file data.c
* @author Alexandre Flores, Guilherme Fernandes, Maria Rita, Mariana Rodrigues,
Matilde Bravo e Miguel Gomes.
* @date 03 Janeiro 2021
* @brief Estruturas de dados auxiliares.
*/
#include "data.h"
#include "dynamic_arrays.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *replace_str(char *str, char *orig, char *rep) {
static char buffer[4096];
char *p;
if (!(p = strstr(str, orig)))
return str;
strncpy(buffer, str, p - str);
buffer[p - str] = '\0';
sprintf(buffer + (p - str), "%s%s", rep, p + strlen(orig));
return buffer;
}
ByteVec *loadArray(FILE *file, size_t block_size) {
size_t i = 0;
ByteVec *self = byte_vec_new();
uint8_t x = 0;
while (i < block_size && (fread(&x, sizeof(x), 1, file) == 1)) {
byte_vec_push(self, x);
i++;
}
return self;
}
FreqBlock *initializeFreq(size_t array[uint_range]) {
FreqBlock *e = (FreqBlock *)calloc(1, sizeof(FreqBlock));
e->prox = NULL;
size_t i;
i = 0;
for (; i < uint_range; i = i + 1)
e->freq[i] = array[i];
return e;
}
void free_Freq(FreqBlock *e) {
FreqBlock *aux;
while (e) {
aux = e;
e = e->prox;
aux->prox = NULL;
/* free(aux->freq); */
free(aux);
}
}
Blocks *initializeBlocks() {
Blocks *e = (Blocks *)calloc(1, sizeof(Blocks));
return e;
}
void free_Blocks(Blocks *e) {
Blocks *aux;
while (e) {
aux = e;
byte_vec_del(e->blocklist);
e = e->prox;
aux->prox = NULL;
free(aux);
}
}
Blocks_C *initializeBlocks_C() {
Blocks_C *e = (Blocks_C *)calloc(1, sizeof(Blocks_C));
return e;
}
void free_Blocks_C(Blocks_C *e) {
Blocks_C *aux;
while (e) {
aux = e;
tupple_vec_del(e->tBList);
e = e->prox;
free(aux);
}
}
BlockFiles *initializeBlockFiles() {
BlockFiles *e = (BlockFiles *)calloc(1, sizeof(BlockFiles));
e->compression_type = NOT_COMPRESSED;
return e;
}
void free_Blocks_file(BlockFiles *e) {
if (e->blocks)
free_Blocks(e->blocks);
if (e->blocks_c)
free_Blocks_C(e->blocks_c);
free(e);
}
void addedBlockTOBloc_file(BlockFiles *e, Blocks *block) {
Blocks *aux = e->blocks;
if (aux) {
while (aux->prox)
aux = aux->prox;
aux->prox = block;
} else {
e->blocks = block;
}
e->num_blocks = e->num_blocks + 1;
}
void addedBlock_CTOBloc_file(BlockFiles *e, Blocks_C *self) {
Blocks_C *aux = e->blocks_c;
if (aux) {
while (aux->prox)
aux = aux->prox;
aux->prox = self;
} else {
e->blocks_c = self;
}
}
void arrayToFreqBlock(size_t array[uint_range], FreqBlock *e) {
FreqBlock *aux, *new;
aux = e;
new = initializeFreq(array);
new->prox = NULL;
if (aux) {
while (aux->prox) {
aux = (aux->prox);
}
aux->prox = new;
} else
e = new;
}
void print_freq(FreqBlock *freq) {
FreqBlock *aux = freq;
size_t i;
while (aux) {
for (i = 0; i < uint_range; i++)
printf(" %d,", aux->freq[i]);
printf("\n");
aux = aux->prox;
}
}
void printByteVec(ByteVec const *self) {
size_t i = 0;
size_t used = byte_vec_used(self);
for (; i < used; i++)
printf("%d ", byte_vec_index(self, i));
printf("\n");
}
void printEqual(TuppleVec const *vec) {
size_t counter = 0;
size_t i = 0;
for (; i < tupple_vec_used(vec); i++) {
ByteTupple self = tupple_vec_index(vec, i);
printf("%d,%d ", self.byte, self.count);
counter = self.count + counter;
}
printf("\n %ld \n", counter);
}
size_t size_last_block_C_rle(Blocks_C *self) {
Blocks_C *aux = self;
while (aux->prox)
aux = aux->prox;
return aux->block_size;
}