Skip to content

Class Permutation

Ori Roth edited this page Apr 2, 2017 · 3 revisions

Synopsis of Enum Permutation

public enum Permutation { 
    /*
     * Utilities (5)
     */
        static int[] random(int n); 
        static int[] increasing(int n); 
        static int[] decreasing(int n); 
        static void swap(int[] a, int i, int j); 
        static long factorial(short n); 
} 

Code

// SSDLPedia
package il.ac.technion.cs.ssdl.utils;

import static il.ac.technion.cs.ssdl.utils.DBC.nonnegative;
import static il.ac.technion.cs.ssdl.utils.DBC.require;
import il.ac.technion.cs.ssdl.stereotypes.Utility;

import java.util.Random;

/**
 * A collection of utility function to generate permutations.
 * 
 * Author: Yossi Gil,
 * See:  19/06/2008
 */
@Utility public enum Permutation {
    ;
    /**
     * n a non-negative integer
     * Return: a random permutation of length n, represented as an array.
     */
    public static int[] random(final int n) {
        nonnegative(n);
        final int[] $ = increasing(n);
        final Random r = new Random(System.nanoTime());
        for (int i = 0; i < n; i++)
            swap($, i, r.nextInt(n));
        return $;
    }
    
    /**
     * n a non-negative integer
     * Return: the increasing permutation of length n, represented as an array.
     */
    public static int[] increasing(final int n) {
        nonnegative(n);
        final int[] $ = new int[n];
        for (int i = 0; i < n; i++)
            $[i] = i;
        return $;
    }
    
    /**
     * n a non-negative integer
     * Return: the decreasing permutation of length n, represented as an array.
     */
    public static int[] decreasing(final int n) {
        nonnegative(n);
        final int[] $ = new int[n];
        for (int i = 0; i < n; i++)
            $[i] = n - 1 - i;
        return $;
    }
    
    /**
     * Swap the contents of two int array cells
     * 
     * a the array with two cells to be swapped
     * i index of this first array cell
     * j index of the second array cell
     */
    public static void swap(final int[] a, final int i, final int j) {
        nonnegative(i);
        nonnegative(j);
        require(i < a.length);
        require(j < a.length);
        if (i == j)
            return;
        final int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    
    /**
     * Compute the factorial of a small integer
     * 
     * n a given integer
     * Return: the factorial of n
     */
    public static long factorial(final short n) {
        if (n <= 1)
            return 1;
        return n * factorial((short) (n - 1));
    }
}

Metrics

Metric Value Acronym Explanation
LOC 86 Lines Of Code Total number of lines in the code
SCC 35 SemiColons Count Total number of semicolon tokens found in the code.
NOT 382 Number Of Tokens Comments, whitespace and text which cannot be made into a token not included.
VCC 1621 Visible Characters Count The total number of non-white (i.e., not space, tab, newline, carriage return, form feed) characters.
CCC 911 Code Characters Count Total number of non-white characters in tokens. White space characters in string and character literals are not counted.
UIC 31 Unique Identifiers Count The number of different identifiers found in the code
WHC 3 Weighted Horizontal Complexity A heuritistic on horizontal complexity

Statistics

Statitic Value
Average token length 2.4
Tokens/line 4.4
Visible characters/line 19
Code characters/line 11
Semicolons/tokens 9%
Comment text percentage 43%

Tokens by kind

Token Kind Occurrences
KEYWORD 67
OPERATOR 26
LITERAL 7
ID 120
PUNCTUATION 162
COMMENT 7
OTHER 180
Clone this wiki locally