vm/src/test/java/org/apidesign/vm4brwsr/Array.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 15 Jan 2013 12:26:19 +0100
changeset 457 b0e82dcf51fb
parent 446 5b3d5ed03014
child 567 62dd2c794431
permissions -rw-r--r--
Initialize multi dimensional arrays with 0 for primitive types
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 /**
    21  *
    22  * @author Jaroslav Tulach <jtulach@netbeans.org>
    23  */
    24 public class Array {
    25     byte[] bytes = { 1 };
    26     short[] shorts = { 2, 3 };
    27     int[] ints = { 4, 5, 6 };
    28     float[] floats = { 7, 8, 9, 10 };
    29     double[][] doubles = { {11}, {12}, {13}, {14}, {15} };
    30     char[] chars = { 'a', 'b' };
    31     
    32     private Array() {
    33     }
    34     
    35     byte bytes() {
    36         return bytes[0];
    37     }
    38     short shorts() {
    39         return shorts[1];
    40     }
    41     
    42     int[] ints() {
    43         return ints;
    44     }
    45     
    46     float floats() {
    47         return floats[3];
    48     }
    49     
    50     double doubles() {
    51         return doubles[4][0];
    52     }
    53     
    54     static double[][] dbls = new double[1][2];
    55     public static double twoDoubles() {
    56         return dbls[0][0] + dbls[0][0];
    57     }
    58 
    59     static int[][] tints = new int[1][2];
    60     public static int twoInts() {
    61         return tints[0][0] + tints[0][0];
    62     }
    63     
    64     private static final Array[] ARR = { new Array(), new Array(), new Array() };
    65     
    66     private static Array[][] arr() {
    67         Array[][] matrix = new Array[3][3];
    68         for (int i = 0; i < ARR.length; i++) {
    69             matrix[i][i] = ARR[i];
    70         }
    71         return matrix;
    72     }
    73     private static <T> T[] filter(T[] in) {
    74         return in;
    75     }
    76     
    77     public static double sum() {
    78         double sum = 0.0;
    79         for (Array[] row : arr()) {
    80             int indx = -1;
    81             for (Array a : row) {
    82                 indx++;
    83                 if (a == null) {
    84                     continue;
    85                 }
    86                 sum += a.bytes();
    87                 sum += a.shorts();
    88                 sum += a.ints()[2];
    89                 sum += a.floats();
    90                 sum += filter(row)[indx].doubles();
    91             }
    92         }
    93         return sum;
    94     }
    95     private static final int[] arr = { 0, 1, 2, 3, 4, 5 };
    96     public static int simple(boolean clone) {
    97         int[] ar;
    98         if (clone) {
    99             ar = arr.clone();
   100         } else {
   101             ar = arr;
   102         }
   103         
   104         int sum = 0;
   105         for (int a : ar) {
   106             sum += a;
   107         }
   108         return sum;
   109     }
   110     
   111     public static int sum(int size) {
   112         int[] arr = new int[size];
   113         return arr[0] + arr[1];
   114     }
   115     
   116     static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) {
   117         while (count-- > 0) {
   118             dst[dstBegin++] = value[srcBegin++];
   119         }
   120     }
   121 
   122     public static char copyArray() {
   123         char[] arr = { '0' };
   124         arraycopy(arr()[0][0].chars, 0, arr, 0, 1);
   125         return arr[0];
   126     }
   127 }