/*
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection.arrays;

import lombok.experimental.UtilityClass;
import net.ranides.assira.collection.iterators.IntListIterator;
import net.ranides.assira.collection.iterators.RandomAccessIntIterator;
import net.ranides.assira.collection.lists.IntList;
import net.ranides.assira.collection.lists.NativeArrayList;

/**
 * Trivial methods for conversion int[] arrays into type-specific IntList, IntIterator etc
 *
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 */
@UtilityClass
public class IntArrays {

    /**
     * Returns IntList view which reflects provided values.
     * It is vararg method, but it can takes int[] too. Changes inside array will be visible in IntList.
     *
     * @param array array
     * @return view
     */
    public static IntList asList(int... array) {
		return NativeArrayList.wrap(array);
	}

    /**
     * Returns IntListIterator traversing provided array
     *
     * @param array array
     * @return iterator
     */
    public static IntListIterator iterator(int[] array) {
        return iterator(array, 0, array.length);
    }

    /**
     * Returns IntListIterator traversing provided array fragment
     *
     * @param array array
     * @param begin begin
     * @param end end
     * @return iterator
     */
    public static IntListIterator iterator(int[] array, int begin, int end) {
        return new RandomAccessIntIterator() {
            @Override
            protected int size() {
                return end-begin;
            }

            @Override
            protected int get(int index) {
                return array[begin+index];
            }

            @Override
            protected void set(int index, int value) {
                array[begin+index] = value;
            }
        };
    }
}
