-
Notifications
You must be signed in to change notification settings - Fork 16
Miloris's Homework10 #99
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
Miloris
wants to merge
3
commits into
2017201979LJN:master
Choose a base branch
from
Miloris: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
Changes from all commits
Commits
Show all changes
3 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,125 @@ | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #include "zlib.h" | ||
| #include "splay.h" | ||
| #include "compression.h" | ||
|
|
||
| // Read data to build original tree | ||
| void read_data(FILE *fp){ | ||
| int n = 0; | ||
| int i = 0; | ||
| double d = 0.0; | ||
| char c; | ||
| fscanf(fp, "%d", &n); | ||
| for(i = 1; i <= n; i++){ | ||
| fscanf(fp, "%lf %c", &d, &c); | ||
| insert_node(d, c); | ||
| } | ||
| return ; | ||
| } | ||
|
|
||
|
|
||
| void build_tree_from_raw_file(){ | ||
| FILE* fin = fopen("data.in", "r"); | ||
| read_data(fin); | ||
| fclose(fin); | ||
| return ; | ||
| } | ||
|
|
||
| void dfs_tree_save(char *buffer, struct tree_node *p){ | ||
| if (p == NULL) { | ||
| sprintf(buffer + strlen(buffer), "0;"); | ||
| return ; | ||
| } | ||
| sprintf(buffer + strlen(buffer), "1;%.3lf;%c;", p->val, p->c); | ||
| dfs_tree_save(buffer, p->ch[0]); | ||
| dfs_tree_save(buffer, p->ch[1]); | ||
| return ; | ||
| } | ||
|
|
||
| int save_tree_to_bin_file() | ||
| { | ||
| // Create buffers | ||
| char *raw_buf = malloc(sizeof(char) * FILE_BUFFER), // Temporary write | ||
| *cmp_buf = malloc(sizeof(char) * FILE_BUFFER); // Compress | ||
| strcpy(raw_buf, ""); | ||
| strcpy(cmp_buf, ""); | ||
| // Dump tree to source buffer | ||
| dfs_tree_save(raw_buf, root); | ||
| // Compress buffer | ||
| unsigned long int dest_len = FILE_BUFFER, | ||
| src_len = strlen(raw_buf); | ||
| int ret_res = compress2(cmp_buf, &dest_len, (const unsigned char*)raw_buf, | ||
| src_len, Z_BEST_COMPRESSION); | ||
| if (ret_res != Z_OK) { | ||
| printf("Error while compressing data.\n"); | ||
| free(raw_buf); | ||
| free(cmp_buf); | ||
| return 1; | ||
| } | ||
| // Write buffer to file | ||
| FILE* fout = fopen("compressed.sv", "wb"); | ||
| printf("Finish: %d, %d\nCompression Ratio: %.2lf%%\n", | ||
| (int)src_len, (int)dest_len, | ||
| 100.0 * (double)dest_len / (double)src_len); | ||
| fwrite(cmp_buf, sizeof(char), dest_len, fout); | ||
| fclose(fout); | ||
| // Finalize | ||
| free(raw_buf); | ||
| free(cmp_buf); | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| struct tree_node* restore_node(char* buffer, int *pointer, struct tree_node *parent){ | ||
| int is_valid_node = 0; | ||
| int scan_len = 0; | ||
| sscanf(buffer + *pointer, "%d;%n", &is_valid_node, &scan_len); | ||
| *pointer += scan_len; | ||
| if (!is_valid_node) | ||
| return NULL; | ||
| // Is not null node, read node data | ||
| struct tree_node *p = malloc(sizeof(struct tree_node)); | ||
| sscanf(buffer + *pointer, "%lf;%c;%n", &p->val, &p->c, &scan_len); | ||
| *pointer += scan_len; | ||
| p->parent = parent; | ||
| p->ch[0] = restore_node(buffer, pointer, p); | ||
| p->ch[1] = restore_node(buffer, pointer, p); | ||
| p->size = 1 + (p->ch[0] ? p->ch[0]->size : 0) + | ||
| (p->ch[1] ? p->ch[1]->size : 0); | ||
| return p; | ||
| } | ||
|
|
||
| int load_tree_from_bin_file(){ | ||
| // Create buffers | ||
| char *cmp_buf = malloc(sizeof(char) * FILE_BUFFER), // Compressed data | ||
| *raw_buf = malloc(sizeof(char) * FILE_BUFFER); // Actual data read | ||
| strcpy(cmp_buf, ""); | ||
| strcpy(raw_buf, ""); | ||
| // Read file into buffer | ||
| unsigned long int src_len = FILE_BUFFER, | ||
| dest_len = FILE_BUFFER; | ||
| FILE* fin = fopen("compressed.sv", "rb"); | ||
| fread(cmp_buf, sizeof(char), FILE_BUFFER, fin); | ||
| fclose(fin); | ||
| // Decompressing file | ||
| int ret_res = uncompress(raw_buf, &dest_len, (const unsigned char*)cmp_buf,src_len); | ||
| if (ret_res != Z_OK) { | ||
| printf("Error while compressing data.\n"); | ||
| free(raw_buf); | ||
| free(cmp_buf); | ||
| return 1; | ||
| } | ||
| // Construct tree | ||
| int pointer = 0; | ||
| root = restore_node(raw_buf, &pointer, NULL); | ||
| // Finalize | ||
| free(raw_buf); | ||
| free(cmp_buf); | ||
| 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,22 @@ | ||
| #ifndef _COMPRESSION_H | ||
| #define _COMPRESSION_H | ||
|
|
||
| #define FILE_BUFFER 2097152 | ||
| /* 1;-10000.000;c; 15 chars for present node | ||
| * 0; 2 chars for empty node | ||
| * 15*100000 maximum, buffer open to 2097152 | ||
| */ | ||
|
|
||
| void read_data(FILE *fp); | ||
|
|
||
| void build_tree_from_raw_file(); | ||
|
|
||
| void dfs_tree_save(char *buffer, struct tree_node *p); | ||
|
|
||
| int save_tree_to_bin_file(); | ||
|
|
||
| struct tree_node* restore_node(char *buffer, int *pointer, struct tree_node *parent); | ||
|
|
||
| int load_tree_from_bin_file(); | ||
|
|
||
| #endif |
Oops, something went wrong.
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.
qaq