-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathelastic.h
214 lines (195 loc) · 5.96 KB
/
elastic.h
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include"constant.h"
using namespace std;
typedef unsigned char byte;
byte bBuffer[4 * MAXLOG];
int int_to_zigzag(int n){
return (n << 1) ^ (n >> 31);
}
int zigzag_to_int(int n){
return (((unsigned int)n) >> 1) ^ -(n & 1);
}
int write_to_buffer(int zz, byte* buf, int size){
int ret = 0;
for(int i = 0 ; i < size; i++){
if ((zz & (~0x7f)) == 0){
buf[i] = (byte)zz;
ret = i + 1;
break;
}else{
buf[i] = (byte)((zz & 0x7f) | 0x80);
zz = ((unsigned int )zz) >> 7;
}
}
return ret;
}
unsigned int read_from_buffer(byte* buf, long& start, int max_size){
unsigned int ret = 0;
int offset = 0;
int i = start;
for(; i - start < max_size; i++, offset += 7){
byte n = buf[i];
if ((n & 0x80) != 0x80){
ret |= (n << offset);
i++;
break;
}else{
ret |= ((n & 0x7f) << offset);
}
}
start = i;
return ret;
}
int write_to_file(int num, FILE* file){
byte tBuffer[5];
memset(tBuffer, 0, sizeof(byte)*5);
int ret = write_to_buffer(num, tBuffer, 5);
fwrite(tBuffer, sizeof(byte), ret, file);
return ret;
}
void PositiveEncoder(FILE* out, int * array, int size, bool enable=0){
int minus = 0;
unsigned int* minus_value = (unsigned int *) malloc(size * sizeof(unsigned int));
int* minus_loc = (int *)malloc(size * sizeof(int));
memset(minus_value, 0, sizeof(unsigned int)*size);
memset(minus_loc, 0, sizeof(int) * size);
for (int i = 0; i < size; i++){
if (array[i] < 0){
minus_value[minus] = -array[i];
minus_loc[minus] = i;
minus++;
}
}
write_to_file(size, out);
write_to_file(minus, out);
for (int i = 0; i < minus; i++){
write_to_file(minus_loc[i], out);
write_to_file(minus_value[i], out);
//printf("Write Minus Loc: %d, Minux Value: %d\n", minus_loc[i], minus_value[i]);
}
free(minus_value);
free(minus_loc);
for (int i = 0; i < size;i++){
if (array[i] < 0) continue;
write_to_file(array[i], out);
}
}
void PositiveDecoder(const char * file_name, int* buffer){
FILE* in;
if ((in = fopen(file_name, "rb")) == NULL){
printf("Fail to open input file");
exit(0);
}
fseek(in, 0, SEEK_END);
long lSize = ftell(in);
rewind(in);
long result = fread(bBuffer, 1, lSize, in);
if (result != lSize){
printf("Reading entair file fail");
exit(0);
}
fclose(in);
long start = 0;
int tot = read_from_buffer(bBuffer, start, 5);
int minus = read_from_buffer(bBuffer, start, 5);
cout << minus << endl;
//printf("Read minus: %d", minus);
unsigned int * minus_value = (unsigned int*)malloc(minus * sizeof(unsigned int));
int * minus_loc = (int*)malloc(minus * sizeof(int));
memset(minus_value, 0, sizeof(unsigned int)*minus);
memset(minus_loc, 0, sizeof(int) * minus);
for (int i = 0; i < minus; i++){
minus_loc[i] = read_from_buffer(bBuffer, start, 5);
minus_value[i] = read_from_buffer(bBuffer, start, 5);
//printf("Minus Loc: %d, Minux Value: %d\n", minus_loc[i], minus_value[i]);
}
int count = 0;
int now_minus = 0;
printf("start: %ld, result: %ld", start, result);
while(count < tot){
//printf("start: %d, result: %d\n", start, (int)result);
if (now_minus < minus && count == minus_loc[now_minus]){
buffer[count] = -minus_value[now_minus];
now_minus++;
count++;
continue;
}
int ret = read_from_buffer(bBuffer, start, 5);
buffer[count] = ret;
count++;
}
free(minus_value);
free(minus_loc);
}
void ZigzagEncoder(FILE* out, int * array, int size){
write_to_file(size, out);
for (int i = 0; i < size;i++){
int zigzag = int_to_zigzag(array[i]);
write_to_file(zigzag, out);
}
}
void IntEncoder(const char * file_name, int * array, int size, int type, bool diff=false){
if (diff){
int last = array[0];
for (int i = 1; i < size; i++){
int diff_res = array[i] - last;
last = array[i];
array[i] = diff_res;
}
}
FILE* out;
if ((out = fopen(file_name, "w")) == NULL){
printf("Fail to open output file");
exit(0);
}
if (type == 0){
for (int i = 0; i < size;i++){
fprintf(out, "%d\n", array[i]);
}
}
if (type == 1){
ZigzagEncoder(out, array, size);
}
fclose(out);
return;
}
void ZigzagDecoder(const char * file_name, int* buffer){
long start = 0;
FILE* in;
if ((in = fopen(file_name, "rb")) == NULL){
printf("Fail to open output file\n");
exit(0);
}
fseek(in, 0, SEEK_END);
long lSize = ftell(in);
rewind(in);
long result = fread(bBuffer, 1, lSize, in);
if (result != lSize){
printf("Reading entair file fail");
exit(0);
}
fclose(in);
int count = 0;
int tot = read_from_buffer(bBuffer, start, 5);
while(count < tot){
int zigzag = read_from_buffer(bBuffer, start, 5);
int ret = zigzag_to_int(zigzag);
buffer[count++] = ret;
}
}
void StringEncoder(const char* file_name, string * array, int size){
FILE* out;
if ((out = fopen(file_name, "w")) == NULL){
printf("%s", file_name);
printf("Fail to open output file\n");
exit(0);
}
for(int i = 0; i < size;i++){
fprintf(out, "%s\n", array[i].c_str());
}
fclose(out);
}