SHARE
TWEET

Untitled

a guest May 21st, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define BUF_SIZE 4 * 1024 * 1024
  2. #define SEQ_SIZE 1000000
  3.  
  4. int add(int* key, int* val, int size, int x) {
  5.     if (size == 0) {
  6.         key[0] = x;
  7.         val[0] = 1;
  8.         return 1;
  9.     }
  10.     for (int i = 0; i < size; i++) {
  11.         if (key[i] == x) {
  12.             val[i]++;
  13.             return size;
  14.         } else if (seq[i] > x) {
  15.             for (int j = size; j >= i; j--) {
  16.                 key[j + 1] = key[j];
  17.                 val[j + 1] = val[j];
  18.             }
  19.             key[i] = x;
  20.             val[i] = 1;
  21.             return size + 1;
  22.         }
  23.     }
  24. }
  25.  
  26. if (file) {
  27.     unsigned char* buf = malloc(sizeof(unsigned char) * BUF_SIZE);
  28.     int* zero_k = malloc(sizeof(int) * SEQ_SIZE);
  29.     int* one_k = malloc(sizeof(int) * SEQ_SIZE);
  30.     int* zero_v = malloc(sizeof(int) * SEQ_SIZE);
  31.     int* one_v = malloc(sizeof(int) * SEQ_SIZE);
  32.     int size0 = 0;
  33.     int size1 = 1;
  34.     int len0 = 0;
  35.     int len1 = 0;
  36.     while (!feof(file)) {
  37.         size_t len = fread(buf, sizeof(unsigned char), BUF_SIZE, file);
  38.         for (int i = 0; i < len; i++) {
  39.             for (int j = 7; j >= 0; j--) {
  40.                 int x = !!(buf[i] & (1 << j));
  41.                 if (x) {
  42.                     if (len0 > 0)
  43.                         size0 = add(zero_k, zero_v, size0, len0);
  44.                     len0 = 0;
  45.                     len1++;
  46.                 } else {
  47.                     if (len1 > 0)
  48.                         size1 = add(one_k, one_v, size1, len1);
  49.                     len1 = 0;
  50.                     len0++;
  51.                 }
  52.             }
  53.         }
  54.     }
  55.     if (len0 > 0)
  56.         size0 = add(zero_k, zero_v, size0, len0);
  57.     if (len1 > 0)
  58.         size1 = add(one_k, one_v, size1, len1);
  59.     for (int i = 0; i < size0; i++) {
  60.         printf("%d %d ", zero_k[i], zero_v[i]);
  61.     }
  62.     for (int i = 0; i < size1; i++) {
  63.         printf("%d %d ", one_k[i], one_v[i]);
  64.     }
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top