Enum Permutation

From Ssdlpedia

(Redirected from Class Permutation)
Jump to: navigation, search

Contents

[edit] 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); 
} 



[edit] 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));
    }
}

[edit] Metrics

MetricValueAcronymExplanation
LOC86Lines Of CodeTotal number of lines in the code
SCC35SemiColons CountTotal number of semicolon tokens found in the code.
NOT382Number Of TokensComments, whitespace and text which cannot be made into a token not included.
VCC1621Visible Characters CountThe total number of non-white (i.e., not space, tab, newline, carriage return, form feed) characters.
CCC911Code Characters CountTotal number of non-white characters in tokens. White space characters in string and character literals are not counted.
UIC31Unique Identifiers CountThe number of different identifiers found in the code
WHC3Weighted Horizontal ComplexityA heuritistic on horizontal complexity

[edit] Statistics

StatiticValue
Average token length 2.4
Tokens/line 4.4
Visible characters/line 19
Code characters/line 11
Semicolons/tokens9%
Comment text percentage43%

[edit] Tokens by Kind

Token KindOccurrences
KEYWORD67
OPERATOR26
LITERAL7
ID120
PUNCTUATION162
COMMENT7
OTHER180
Personal tools