aboutsummaryrefslogtreecommitdiffstats
path: root/src/algorithms/arrays/Rotate.java
blob: 22ba0331de305fa40282197ace6188d3c7c52bee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
 * Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package algorithms.arrays;

import java.util.*;


/**
 * Class for rotating arrays
 */
public class Rotate
{
    /*
     *  There are many thing you can change in this class to adapt it, for example,
     *  if you want in-place rotation, which probabily is a bit slower (even though
     *  it is less naïve), you can reverse the array, split it at the index 'steps',
     *  and reverse those partitions and the concatinate back again, of cause, you
     *  would only reverse parts of the array, not split and concatinate it.
     *  You could also reverse the array while rotating it, either reverse the
     *  input or the output.
     */
    
    
£<for T in boolean char byte short int long float double T; do
  P= O="$T" cast=
£>[ $T = T ] && P="<T> " O=Object cast="(T[])"
    /**
     * Rotates an array
     *
     * @param   items  The array to rotate
     * @param   steps  The number of steps to higher index to rotate elements
     * @return         The array rotated
     */
£>[ $T = T ] &&
    @SuppressWarnings("unchecked")
    public static £{P}£{T}[] rotateClone(£{T}[] items, int steps)
    {
	£{O}[] rc = new £{O}[items.length];
	
	int n = items.length;
	if ((n & -n) == n) /* power of 2 */
	    for (int i = 0, m = n - 1; i < n; i++)
		rc[(steps + i) & m] = items[i];
	else
	    for (int i = 0; i < n; i++)
		rc[(steps + i) % n] = items[i];
	
	return £{cast}rc;
    }
£>done
}