SHARE
TWEET

in-place interleave

a guest Feb 8th, 2011 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void deinterleave_split(int *a, int len)
  2. {
  3.     if (2 == len)
  4.     {
  5.         // not needed
  6.     }
  7.     else if (4 == len)
  8.     {
  9.         int t = a[1];
  10.         a[1] = a[2];
  11.         a[2] = t;
  12.     }
  13.     else if (0 == (len % 4))
  14.     {
  15.         deinterleave_split(&a[0], len/2);
  16.         deinterleave_split(&a[len/2], len/2);
  17.         for (int i = len/4; i < len/2; i++)
  18.         {
  19.             int t = a[i];
  20.             a[i] = a[i+len/4];
  21.             a[i+len/4] = t;
  22.         }
  23.     }
  24.     else
  25.     {
  26.         int t;
  27.         deinterleave_split(&a[0], len-2);
  28.         t = a[len-2];
  29.         memmove(&a[len/2],&a[len/2-1],sizeof(int)*(len/2-1));
  30.         a[len/2-1] = t;
  31.     }
  32. }
  33.  
  34. void interleave_split(int *a, int len)
  35. {
  36.     if (2 == len)
  37.     {
  38.         // no need
  39.     }
  40.     else if (4 == len)
  41.     {
  42.         int t = a[1];
  43.         a[1] = a[2];
  44.         a[2] = t;
  45.     }
  46.     else if (0 == (len % 4))
  47.     {
  48.         for (int i = len/4; i < len/2; i++)
  49.         {
  50.             int t = a[i];
  51.             a[i] = a[i+len/4];
  52.             a[i+len/4] = t;
  53.         }
  54.         interleave_split(&a[0], len/2);
  55.         interleave_split(&a[len/2], len/2);
  56.     }
  57.     else
  58.     {
  59.         int t;
  60.         t = a[len/2-1];
  61.         memmove(&a[len/2-1],&a[len/2],sizeof(int)*(len/2-1));
  62.         a[len-2] = t;
  63.         interleave_split(&a[0], len-2);
  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