-
Notifications
You must be signed in to change notification settings - Fork 16
first version #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
crofie
wants to merge
2
commits into
2017201979LJN:master
Choose a base branch
from
crofie:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
first version #96
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| _zip: _zip.so main.c | ||
| gcc -o _zip main.c ./_zip.so | ||
| _zip.so: zip.c | ||
| gcc -shared -fPIC -o _zip.so zip.c | ||
| clean: | ||
| rm -f _zip.so |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 35% compression ratio | ||
| wait to upgrade after I get off the train | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #include <stdio.h> | ||
| #include "zip.h" | ||
|
|
||
| int main() | ||
| { | ||
| zip("zip.in", "x.zip"); | ||
| decode("x.zip", "zip.out"); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include "zip.h" | ||
|
|
||
| #define DATA_SIZE 1000000 | ||
| #define MAX_DIS 32768 | ||
| #define SIZE_OF_CHAR 100000 | ||
| #define SIZE_OF_ASC 256 | ||
| #define INF 2e9 | ||
|
|
||
| #define swap(a,b) ((a)^=(b)^=(a)^=(b)) | ||
|
|
||
| struct pair | ||
| { | ||
| int v; | ||
| int id; | ||
| }char_cnt[SIZE_OF_CHAR], huff[SIZE_OF_ASC]; | ||
|
|
||
| struct edge | ||
| { | ||
| int v, next; | ||
| }e[DATA_SIZE]; | ||
|
|
||
| static unsigned char outhuff[DATA_SIZE] = {0}; | ||
| unsigned char zip_mp[SIZE_OF_CHAR] = {0}, inhuff[DATA_SIZE] = {0}; | ||
| int zip_size; | ||
| static int n, head[DATA_SIZE], edge_cnt = 0; | ||
|
|
||
| static void insert(int v1, int v2) | ||
| { | ||
| e[++edge_cnt].v = v2; | ||
| e[edge_cnt].next = head[v1]; | ||
| head[v1] = edge_cnt; | ||
| } | ||
|
|
||
| static void dfs(int x,int fa, int dep) | ||
| { | ||
| int ch = 0; | ||
| for (int i = head[x]; i; i = e[i].next) { | ||
| dfs(e[i].v, x, (dep << 1) + ch); | ||
| ch++; | ||
| } | ||
| if (!head[x]) { | ||
| huff[x].v = dep; | ||
| huff[x].id = x; | ||
| zip_mp[dep] = x; | ||
| } | ||
| } | ||
|
|
||
| static int cmp(const void *v1, const void *v2) | ||
| { | ||
| return ((struct pair *)v1)->v > ((struct pair *)v2)->v; | ||
| } | ||
|
|
||
| void zip(char *filename, char *output) | ||
| { | ||
| FILE *fp = fopen(filename, "r"); | ||
| n = 0; | ||
| while (fread(inhuff + n, 1, 1, fp)) | ||
| n++; | ||
| zip_size = n; | ||
| for (int i = 0; i < SIZE_OF_ASC; i++) { | ||
| char_cnt[i].id = i; | ||
| char_cnt[i].v = 0; | ||
| } | ||
| for (int i = 0; i < n; i++) | ||
| char_cnt[inhuff[i]].v++; | ||
| for (int i = 0; i < SIZE_OF_ASC; i++) | ||
| if (char_cnt[i].v == 0) | ||
| char_cnt[i].v = INF; | ||
| qsort(char_cnt, SIZE_OF_ASC, sizeof(struct pair), cmp); | ||
| int head = 0, asc_cnt = SIZE_OF_ASC; | ||
| int asc_size = SIZE_OF_ASC; | ||
| while (char_cnt[asc_size - 1].v == INF) | ||
| asc_size--; | ||
| while(head < asc_size - 1) { | ||
| insert(asc_cnt, char_cnt[head].id); | ||
| insert(asc_cnt, char_cnt[head + 1].id); | ||
| char_cnt[++head].id = asc_cnt; | ||
| char_cnt[head].v += char_cnt[head - 1].v; | ||
| for (int j = head; j < asc_size - 1; j++) { | ||
| if (char_cnt[j].v > char_cnt[j + 1].v) { | ||
| swap(char_cnt[j].id, char_cnt[j + 1].id); | ||
| swap(char_cnt[j].v, char_cnt[j + 1].v); | ||
| } | ||
| else | ||
| break; | ||
| } | ||
| asc_cnt++; | ||
| } | ||
| int root = char_cnt[asc_size - 1].id; | ||
| dfs(root, 0, 0); | ||
| int out_cnt = 3, out_pos = 0; | ||
| for (int i = 0; i < n; i++) { | ||
| int flag = 0, v = huff[inhuff[i]].v; | ||
| for (int j = 3; j > 0; j--) { | ||
| if (v & (1 << j)) | ||
| flag = 1; | ||
| if (flag) { | ||
| if (v & (1 << j)) | ||
| outhuff[out_pos] += (1 << out_cnt); | ||
| out_cnt--; | ||
| if (out_cnt < 0) { | ||
| out_cnt = 3; | ||
| out_pos++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (out_cnt != 3) | ||
| out_pos++; | ||
| fp = fopen(output, "w"); | ||
| fwrite(outhuff, 1, out_pos, fp); | ||
| } | ||
|
|
||
| static char incode[DATA_SIZE] = {0}; | ||
|
|
||
| void decode(char *input, char *output) | ||
| { | ||
| FILE *fp = fopen(input, "r"); | ||
| FILE *fp2 = fopen(output, "w"); | ||
| int n = 0; | ||
| while (fread(incode + n, 1, 1, fp)) | ||
| n++; | ||
| int encode = 0, pos = -1, _pos = 3; | ||
| for (int i = 0; i < n; i++) { | ||
| if (i % 4 == 0) | ||
| pos++; | ||
| encode = (encode << 1); | ||
| if (incode[pos] & (1 << _pos)) | ||
| encode++; | ||
| if (zip_mp[encode]) { | ||
| fputc(zip_mp[encode], fp2); | ||
| encode = 0; | ||
| } | ||
| _pos--; | ||
| _pos = (_pos + 4) % 4; | ||
| } | ||
| for (int i = 0; i < zip_size; i++) | ||
| fputc(inhuff[i], fp2); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| void zip(char *input_filename, char *output_filename); | ||
|
|
||
| void decode(char *input_filename, char *output_filename); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gu~ gu~ gu~