Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. typedef unsigned char byte;
  8.  
  9. byte *rotlexcmp_buf = NULL;
  10. int rottexcmp_bufsize = 0;
  11.  
  12. int rotlexcmp(const void *l, const void *r)
  13. {
  14.     int li = *(const int*)l, ri = *(const int*)r, ac=rottexcmp_bufsize;
  15.     while (rotlexcmp_buf[li] == rotlexcmp_buf[ri])
  16.     {
  17.         if (++li == rottexcmp_bufsize)
  18.             li = 0;
  19.         if (++ri == rottexcmp_bufsize)
  20.             ri = 0;
  21.         if (!--ac)
  22.             return 0;
  23.     }
  24.     if (rotlexcmp_buf[li] > rotlexcmp_buf[ri])
  25.         return 1;
  26.     else
  27.         return -1;
  28. }
  29.  
  30. void bwt_encode(byte *buf_in, byte *buf_out, int size, int *primary_index)
  31. {
  32.     int indices[size];
  33.     int i;
  34.  
  35.     for(i=0; i<size; i++)
  36.         indices[i] = i;
  37.     rotlexcmp_buf = buf_in;
  38.     rottexcmp_bufsize = size;
  39.     qsort (indices, size, sizeof(int), rotlexcmp);
  40.  
  41.     for (i=0; i<size; i++)
  42.         buf_out[i] = buf_in[(indices[i]+size-1)%size];
  43.     for (i=0; i<size; i++)
  44.     {
  45.         if (indices[i] == 1) {
  46.             *primary_index = i;
  47.             return;
  48.         }
  49.     }
  50.     assert (0);
  51. }
  52.  
  53. void bwt_decode(byte *buf_in, byte *buf_out, int size, int primary_index)
  54. {
  55.     byte F[size];
  56.     int buckets[256];
  57.     int i,j,k;
  58.     int indices[size];
  59.  
  60.     for (i=0; i<256; i++)
  61.         buckets[i] = 0;
  62.     for (i=0; i<size; i++)
  63.         buckets[buf_in[i]] ++;
  64.     for (i=0,k=0; i<256; i++)
  65.         for (j=0; j<buckets[i]; j++)
  66.             F[k++] = i;
  67.     assert (k==size);
  68.     for (i=0,j=0; i<256; i++)
  69.     {
  70.         while (i>F[j] && j<size)
  71.             j++;
  72.         buckets[i] = j; // it will get fake values if there is no i in F, but
  73.                         // that won't bring us any problems
  74.     }
  75.     for(i=0; i<size; i++)
  76.         indices[buckets[buf_in[i]]++] = i;
  77.     for(i=0,j=primary_index; i<size; i++)
  78.     {
  79.         buf_out[i] = buf_in[j];
  80.         j=indices[j];
  81.     }
  82. }
  83.  
  84. int main()
  85. //using namespace std;
  86. {
  87.     byte buf1[] = "Wikipedia";
  88.     int size = strlen(buf1);
  89.     byte buf2[size];
  90.     byte buf3[size];
  91.     int primary_index;
  92.  
  93.     bwt_encode (buf1, buf2, size, &primary_index);
  94.     bwt_decode (buf2, buf3, size, primary_index);
  95.  
  96.     assert (!memcmp (buf1, buf3, size));
  97.     printf ("Result is the same as input, that is: <%.*s>\n", size, buf3);
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement