示例#1
0
void inshuffle(It it, std::size_t n) {
  if (n==0) return;
  for (std::size_t i = 0; 2*i < n; ++i) {
    std::size_t k = 2*i + 1;
    while (2*k <= n) k *= 2;
    typename std::iterator_traits<It>::value_type tmp = it[n+i];
    it[n+i] = it[k-1];
    while (k % 2 == 0) {
      it[k-1] = it[(k/2)-1];
      k /= 2;
    }
    it[k-1] = tmp;
  }
  std::size_t kmin = 1;
  while (2*kmin <= n) kmin *= 2;
  for (std::size_t i = 0; 4*i+1 < n; ++i) {
    std::size_t k = 2*i + 1;
    while (2*k <= n) k *= 2;
    std::size_t k1 = 2*(i+1) + 1;
    while (2*k1 <= n) k1 *= 2;
    if (k > k1) {
      if (k1 < kmin) {
        kmin = k1;
        inshuffle(it+n, i+1);
      }
      else
        inshuffle(it+n+1, i);
    }
  }
  return inshuffle(it+n+(n%2), n/2);
}
示例#2
0
void inshuffle(It it, std::size_t n) {
  while (n > 0) {
    std::size_t kmin = 1;
    while (kmin <= n)
      kmin *= 2;
    {
      std::size_t kk = kmin/2;
      It itn = it + n;
      for (std::size_t i = 0, s = (n+1)/2; i < s; ++i) {
        std::size_t k = (2*i+1)*kk;
        while (k > n) {
          k /= 2;
          kk /= 2;
        }
        // apply the cyclic permutation
        typename std::iterator_traits<It>::value_type tmp = itn[i];
        itn[i] = it[k-1];
        while (k % 2 == 0) {
          it[k-1] = it[(k/2)-1];
          k /= 2;
        }
        it[k-1] = tmp;
      }
    }
    // the optimized computation of k fails for n=2,
    // so skip the 'normalization' loop when possible
    if (n > 3) {
      std::size_t kk = kmin/4;
      for (std::size_t i = 1; 4*i < n+3; ++i) {
        std::size_t k = (2*i+1)*kk;
        if (k > n) {
          kk /= 2;
          if (k < kmin) {
            kmin = k;
            // if kmin is updated, do an in-shuffle
            inshuffle(it+n, i);
          }
          else
            // otherwise do an out-shuffle
            inshuffle(it+n+1, i-1);
        }
      }
    }
    // implement the tail recursion as an iteration
    it += n+(n%2);
    n /= 2;
  }
}
示例#3
0
typename boost::enable_if< is_complex< typename bindings::value_type< VectorW >::type >, void >::type
interlace (VectorW& w) {
  typedef typename bindings::value_type< VectorW >::type value_type;
  typedef typename bindings::remove_imaginary< value_type >::type real_type;
  value_type* pw = bindings::begin_value(w);
  std::ptrdiff_t n = bindings::end_value(w) - pw;
  if (n < 2) return;
  inshuffle(reinterpret_cast<real_type*> (pw)+1, n-1);
}
示例#4
0
int main(){
      int n;
      int i;
      int *A;
      printf("Enter the value of n, (total size of array = 2n): ");
      scanf("%d",&n);
      A = (int *)malloc((2*n+1)*sizeof(int));
      for (i = 0; i <= 2*n; i++){
            A[i] = i;
      }
      
      inshuffle(A,2*n);
      
      printf("\n[");
      for (i = 1; i <= 2*n; i ++){
            printf(" %d",A[i]);
      }
      printf(" ]\n");
      free(A);
      return 1;
}