Exemple #1
0
// see http://www.geeksforgeeks.org/array-rotation/
void db_nodes_left_shift(dBNode *nlist, size_t n, size_t shift)
{
  if(n == 0 || shift == 0) return;

  // Method A) Using three reverse operations
  // db_nodes_reverse(nlist, shift);
  // db_nodes_reverse(nlist+shift, n-shift);
  // db_nodes_reverse(nlist, n);

  // Method B) Using GCD
  size_t i, j, k, gcd = calc_GCD(n, shift);
  dBNode tmp;

  // Copy from k -> j, stop if k == i, since nlist[i] already overwritten
  for(i = 0; i < gcd; i++) {
    tmp = nlist[i];
    for(j = i; 1; j = k) {
      k = j+shift;
      if(k >= n) k -= n;
      if(k == i) break;
      nlist[j] = nlist[k];
    }
    nlist[j] = tmp;
  }
}
Exemple #2
0
static void test_util_calc_GCD()
{
  test_status("Testing get_GCD()");
  TASSERT(calc_GCD(0,0) == 0);
  TASSERT(calc_GCD(10,0) == 10);
  TASSERT(calc_GCD(0,10) == 10);
  TASSERT(calc_GCD(2,2) == 2);
  TASSERT(calc_GCD(1,1) == 1);
  TASSERT(calc_GCD(1,2) == 1);
  TASSERT(calc_GCD(1,100) == 1);
  TASSERT(calc_GCD(2,4) == 2);
  TASSERT(calc_GCD(7,5) == 1);
  TASSERT(calc_GCD(18,6) == 6);
  TASSERT(calc_GCD(3,6) == 3);
  TASSERT(calc_GCD(100,120) == 20);
  TASSERT(calc_GCD(100,125) == 25);
}