Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <limits.h>
- #include "my.qsort.h"
- #define TYPE char
- #define MEM_AVAILIBLE 4096
- #define LARGE (MEM_AVAILIBLE / sizeof(TYPE))
- #define FILES 8
- typedef unsigned int uint;
- int main(int argc, char** argv){
- FILE* input = fopen("Weetabix.mp4", "rb");
- FILE* files[FILES * 2];
- uint how_many;
- char buf[LARGE];
- char filename[] = "0.bin";
- char from = 'a';
- char to = 'a' + FILES;
- char* buffers[FILES];
- char* brick;
- uint files_active = FILES;
- for(char c = from; c < to; ++c){
- filename[0] = c;
- files[c - 'a'] = fopen(filename, "wb");
- }
- char cur = 0;
- while(!feof(input)){
- /* first time. read from source file */
- how_many = fread(&buf, sizeof(TYPE), LARGE, input);
- my::qsort(buf, how_many);
- fwrite(&buf, sizeof(TYPE), how_many, files[cur]);
- if(++cur == FILES) cur = 0;
- }
- fcloseall();
- while(true){
- uint block_size = LARGE / (files_active + 1);
- uint brick_size = LARGE - block_size * files_active;
- brick = (char*) calloc(brick_size, sizeof(char));
- for(char c = from; c < from + files_active; ++c)
- filename[0] = c, files[c - 'a'] = fopen(filename, "rb");
- for(char c = to; c < to + files_active; ++c)
- filename[0] = c, files[c - 'a'] = fopen(filename, "wb");
- bool flag = true;
- for(uint i = from - 'a'; i < from + files_active - 'a'; ++i){
- buffers[i] = buf + i * files_active;
- fread(buffers[i], sizeof(TYPE), block_size, files[i]);
- my::qsort(buffers[i], block_size);
- }
- uint where_i_am[FILES] = {0};
- uint to_cur = to;
- while(true){
- for(uint i = 0; i < brick_size; ++i){
- for(uint j = from + 1; j < from + files_active; ++j){
- if(where_i_am[j] < from + files_active &&
- buffers[where_i_am[j]] < buffers[to_cur]){
- to_cur = j;
- }
- }
- ++where_i_am[to_cur];
- }
- }
- while(false){
- }
- char tmp = from;
- from = to;
- to = tmp;
- fcloseall();
- free(brick);
- break;
- }
- }
Add Comment
Please, Sign In to add comment