/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.apache.groovy.bench

import groovy.transform.CompileStatic

/**
 * Helper class providing array manipulation benchmark implementations.
 */
class Ary {

    /**
     * Performs array operations in dynamic Groovy mode.
     * @param n the size of the arrays
     * @return the sum of first and last elements
     */
    static int ary(int n) {
        int[] x = new int[n]
        int[] y = new int[n]

        for (int i = 0; i < n; i++)
            x[i] = i + 1
        for (int k = 0; k < 1000; k++ )
            for (int j = n-1; j >= 0; j--)
                y[j] += x[j]

        return y[0] + y[n-1]
    }

    /**
     * Performs array operations in {@code @CompileStatic} mode.
     * @param n the size of the arrays
     * @return the sum of first and last elements
     */
    @CompileStatic
    static int aryCS(int n) {
        int[] x = new int[n]
        int[] y = new int[n]

        for (int i = 0; i < n; i++)
            x[i] = i + 1
        for (int k = 0; k < 1000; k++ )
            for (int j = n-1; j >= 0; j--)
                y[j] += x[j]

        return y[0] + y[n-1]
    }

}
