If the x length of the vector is L, the first N loops must be shifted to the left.
If the initial position to be moved is I, the acrobatics method is x [I] <-> x [I + N] <-> x [I + 2N]... <-> X [I + kN], when (I + kN) % L = I, it is not shifted, and shift x [I + kN] = x [I] at this time. If the maximum number of M-grams between L and N is d, the first N-bits can be shifted to the left after the d-shift is completed.
To prove the feasibility of this method, that is, to prove whether this method has completed the loop of all BITs to shift N bits left.
1. If the current BIT is set to I, the (I + N) % L shifts the loop left to the I bit. Here, the vector can be understood as a ring connected to the beginning and end, see Figure 1)
2. How can I prove that a total of 1 BITs have been moved?
Proof: after d-shift, the first N-bit cycle can be shifted to the left, considering how many bits are moved each time. If L = α d and N = β d are set, I + kN = mL + I is set because each shift ends at (I + kN) % L = I, then k β d = m α d. Due to α and beta, k = α is the minimum value that makes the equation true, this proves that each shift moves the Alpha bit and ends. Only after the d-shift can all the L = α d-bits be moved to the left.
3. How can we prove that each shift will not affect the previously moved bits?
Proof: There are two proof cases
(1) In a single shift, there is no case that a single position is operated twice, that is, it proves that (I + kN) % L = I can occur twice in a shift. In 2, it has been proved that to make the equation true, at least α needs to be displaced, which proves that in a shift, there is no possibility of a second operation being operated.
(2) During multiple shifts, no operated bits are operated again. The proof is as follows:
Start from 0 and perform the first shift, that is, when I = 0, then x [0] Then x [0 + N], x [0 + N] Then x [(0 + 2N) % L]
...... X [(0 + (α-1) N) % L] ß x [0] In the first shift, it will not affect 1, 2, 3, 4 ...... D-1 and its multiples are proved as follows: Set kN % L = x... R, kN = xL + r, that is, k β d-x α d = r => (k β-x α) d = r
This proves that r can only be a multiple of d, not 1, 2, 3, 4 ...... D-1 and its multiples.
When I = I + 1 = 1 at the end of the first operation, the next cycle will be carried out. Likewise, it can be proved that the shift operation will not affect 0, 2, 3,
4 ...... D-1 and its multiples. When I = D-1 is executed, the shift ends, and the α d bit is removed.
End of proof till now
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201308/084807306.png "width =" 250 "height =" 225 "title =" QQ20130819171040.png "/>
Figure 1
Implementation Code 1:
int* vector_loop1(int* array,int len,int loop_num) { int i=0,j=0,temp; int d = gcd(len,loop_num); while(d) { temp = array[i]; while((i+loop_num)%len != j) { array[i] = array[(i+loop_num)%len]; i = (i+loop_num)%len; } array[i] = temp; j++; i=j; d--; } return array; }
Implementation Code 2
int* vector_loop2(int* array,int len,int loop_num) { int i=0,j=0,temp; temp = array[i]; for(int k=0;k<len;k++) { if((i+loop_num)%len == j) { array[i] = temp; j++; i=j; } else { array[i] = array[(i+loop_num)%len]; i = (i+loop_num)%len; } } return array; }
This article from the "Fly Forward" blog, please be sure to keep this source http://speedonward.blog.51cto.com/4500531/1277391