Android Open Source - gameengine Long Array






From Project

Back to project page gameengine.

License

The source code is released under:

Apache License

If you think the Android project gameengine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright 2011 See AUTHORS file.//from  w w  w  .j  ava 2 s  .c o m
 * 
 * Licensed 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 com.badlogic.gdx.utils;

import java.util.Arrays;

import com.badlogic.gdx.math.MathUtils;

/**
 * A resizable, ordered or unordered long array. Avoids the boxing that occurs
 * with ArrayList<Long>. If unordered, this class avoids a memory copy when
 * removing elements (the last element is moved to the removed element's
 * position).
 * 
 * @author Nathan Sweet
 */
public class LongArray {
    public long[] items;
    public int size;
    public boolean ordered;

    /** Creates an ordered array with a capacity of 16. */
    public LongArray() {
        this(true, 16);
    }

    /** Creates an ordered array with the specified capacity. */
    public LongArray(int capacity) {
        this(true, capacity);
    }

    /**
     * @param ordered
     *            If false, methods that remove elements may change the order of
     *            other elements in the array, which avoids a memory copy.
     * @param capacity
     *            Any elements added beyond this will cause the backing array to
     *            be grown.
     */
    public LongArray(boolean ordered, int capacity) {
        this.ordered = ordered;
        items = new long[capacity];
    }

    /**
     * Creates a new array containing the elements in the specific array. The
     * new array will be ordered if the specific array is ordered. The capacity
     * is set to the number of elements, so any subsequent elements added will
     * cause the backing array to be grown.
     */
    public LongArray(LongArray array) {
        this.ordered = array.ordered;
        size = array.size;
        items = new long[size];
        System.arraycopy(array.items, 0, items, 0, size);
    }

    /**
     * Creates a new ordered array containing the elements in the specified
     * array. The capacity is set to the number of elements, so any subsequent
     * elements added will cause the backing array to be grown.
     */
    public LongArray(long[] array) {
        this(true, array);
    }

    /**
     * Creates a new array containing the elements in the specified array. The
     * capacity is set to the number of elements, so any subsequent elements
     * added will cause the backing array to be grown.
     * 
     * @param ordered
     *            If false, methods that remove elements may change the order of
     *            other elements in the array, which avoids a memory copy.
     */
    public LongArray(boolean ordered, long[] array) {
        this(ordered, array.length);
        size = array.length;
        System.arraycopy(array, 0, items, 0, size);
    }

    public void add(long value) {
        long[] items = this.items;
        if (size == items.length)
            items = resize(Math.max(8, (int) (size * 1.75f)));
        items[size++] = value;
    }

    public void addAll(LongArray array) {
        addAll(array, 0, array.size);
    }

    public void addAll(LongArray array, int offset, int length) {
        if (offset + length > array.size)
            throw new IllegalArgumentException("offset + length must be <= size: " + offset + " + " + length + " <= " + array.size);
        addAll(array.items, offset, length);
    }

    public void addAll(long[] array) {
        addAll(array, 0, array.length);
    }

    public void addAll(long[] array, int offset, int length) {
        long[] items = this.items;
        int sizeNeeded = size + length - offset;
        if (sizeNeeded >= items.length)
            items = resize(Math.max(8, (int) (sizeNeeded * 1.75f)));
        System.arraycopy(array, offset, items, size, length);
        size += length;
    }

    public long get(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(String.valueOf(index));
        return items[index];
    }

    public void set(int index, long value) {
        if (index >= size)
            throw new IndexOutOfBoundsException(String.valueOf(index));
        items[index] = value;
    }

    public void insert(int index, long value) {
        long[] items = this.items;
        if (size == items.length)
            items = resize(Math.max(8, (int) (size * 1.75f)));
        if (ordered)
            System.arraycopy(items, index, items, index + 1, size - index);
        else
            items[size] = items[index];
        size++;
        items[index] = value;
    }

    public void swap(int first, int second) {
        if (first >= size)
            throw new IndexOutOfBoundsException(String.valueOf(first));
        if (second >= size)
            throw new IndexOutOfBoundsException(String.valueOf(second));
        long[] items = this.items;
        long firstValue = items[first];
        items[first] = items[second];
        items[second] = firstValue;
    }

    public boolean contains(long value) {
        int i = size - 1;
        long[] items = this.items;
        while (i >= 0)
            if (items[i--] == value)
                return true;
        return false;
    }

    public int indexOf(long value) {
        long[] items = this.items;
        for (int i = 0, n = size; i < n; i++)
            if (items[i] == value)
                return i;
        return -1;
    }

    public int lastIndexOf(char value) {
        long[] items = this.items;
        for (int i = size - 1; i >= 0; i--)
            if (items[i] == value)
                return i;
        return -1;
    }

    public boolean removeValue(long value) {
        long[] items = this.items;
        for (int i = 0, n = size; i < n; i++) {
            if (items[i] == value) {
                removeIndex(i);
                return true;
            }
        }
        return false;
    }

    /** Removes and returns the item at the specified index. */
    public long removeIndex(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(String.valueOf(index));
        long[] items = this.items;
        long value = items[index];
        size--;
        if (ordered)
            System.arraycopy(items, index + 1, items, index, size - index);
        else
            items[index] = items[size];
        return value;
    }

    /** Removes and returns the last item. */
    public long pop() {
        return items[--size];
    }

    /** Returns the last item. */
    public long peek() {
        return items[size - 1];
    }

    /** Returns the first item. */
    public long first() {
        return items[0];
    }

    public void clear() {
        size = 0;
    }

    /**
     * Reduces the size of the backing array to the size of the actual items.
     * This is useful to release memory when many items have been removed, or if
     * it is known that more items will not be added.
     */
    public void shrink() {
        resize(size);
    }

    /**
     * Increases the size of the backing array to acommodate the specified
     * number of additional items. Useful before adding many items to avoid
     * multiple backing array resizes.
     * 
     * @return {@link #items}
     */
    public long[] ensureCapacity(int additionalCapacity) {
        int sizeNeeded = size + additionalCapacity;
        if (sizeNeeded >= items.length)
            resize(Math.max(8, sizeNeeded));
        return items;
    }

    protected long[] resize(int newSize) {
        long[] newItems = new long[newSize];
        long[] items = this.items;
        System.arraycopy(items, 0, newItems, 0, Math.min(size, newItems.length));
        this.items = newItems;
        return newItems;
    }

    public void sort() {
        Arrays.sort(items, 0, size);
    }

    public void reverse() {
        for (int i = 0, lastIndex = size - 1, n = size / 2; i < n; i++) {
            int ii = lastIndex - i;
            long temp = items[i];
            items[i] = items[ii];
            items[ii] = temp;
        }
    }

    public void shuffle() {
        for (int i = size - 1; i >= 0; i--) {
            int ii = MathUtils.random(i);
            long temp = items[i];
            items[i] = items[ii];
            items[ii] = temp;
        }
    }

    /**
     * Reduces the size of the array to the specified size. If the array is
     * already smaller than the specified size, no action is taken.
     */
    public void truncate(int newSize) {
        if (size > newSize)
            size = newSize;
    }

    /** Returns a random item from the array, or zero if the array is empty. */
    public long random() {
        if (size == 0)
            return 0;
        return items[MathUtils.random(0, size - 1)];
    }

    public long[] toArray() {
        long[] array = new long[size];
        System.arraycopy(items, 0, array, 0, size);
        return array;
    }

    public String toString() {
        if (size == 0)
            return "[]";
        long[] items = this.items;
        StringBuilder buffer = new StringBuilder(32);
        buffer.append('[');
        buffer.append(items[0]);
        for (int i = 1; i < size; i++) {
            buffer.append(", ");
            buffer.append(items[i]);
        }
        buffer.append(']');
        return buffer.toString();
    }

    public String toString(String separator) {
        if (size == 0)
            return "";
        long[] items = this.items;
        StringBuilder buffer = new StringBuilder(32);
        buffer.append(items[0]);
        for (int i = 1; i < size; i++) {
            buffer.append(separator);
            buffer.append(items[i]);
        }
        return buffer.toString();
    }
}




Java Source Code List

com.badlogic.gdx.math.MathUtils.java
com.badlogic.gdx.math.Matrix3.java
com.badlogic.gdx.math.Matrix4.java
com.badlogic.gdx.math.Quaternion.java
com.badlogic.gdx.math.Vector2.java
com.badlogic.gdx.math.Vector3.java
com.badlogic.gdx.physics.box2d.BodyDef.java
com.badlogic.gdx.physics.box2d.Body.java
com.badlogic.gdx.physics.box2d.ChainShape.java
com.badlogic.gdx.physics.box2d.CircleShape.java
com.badlogic.gdx.physics.box2d.ContactFilter.java
com.badlogic.gdx.physics.box2d.ContactImpulse.java
com.badlogic.gdx.physics.box2d.ContactListener.java
com.badlogic.gdx.physics.box2d.Contact.java
com.badlogic.gdx.physics.box2d.DestructionListener.java
com.badlogic.gdx.physics.box2d.EdgeShape.java
com.badlogic.gdx.physics.box2d.Filter.java
com.badlogic.gdx.physics.box2d.FixtureDef.java
com.badlogic.gdx.physics.box2d.Fixture.java
com.badlogic.gdx.physics.box2d.JointDef.java
com.badlogic.gdx.physics.box2d.JointEdge.java
com.badlogic.gdx.physics.box2d.Joint.java
com.badlogic.gdx.physics.box2d.Manifold.java
com.badlogic.gdx.physics.box2d.MassData.java
com.badlogic.gdx.physics.box2d.PolygonShape.java
com.badlogic.gdx.physics.box2d.QueryCallback.java
com.badlogic.gdx.physics.box2d.RayCastCallback.java
com.badlogic.gdx.physics.box2d.Shape.java
com.badlogic.gdx.physics.box2d.Transform.java
com.badlogic.gdx.physics.box2d.WorldManifold.java
com.badlogic.gdx.physics.box2d.World.java
com.badlogic.gdx.physics.box2d.joints.DistanceJointDef.java
com.badlogic.gdx.physics.box2d.joints.DistanceJoint.java
com.badlogic.gdx.physics.box2d.joints.FrictionJointDef.java
com.badlogic.gdx.physics.box2d.joints.FrictionJoint.java
com.badlogic.gdx.physics.box2d.joints.GearJointDef.java
com.badlogic.gdx.physics.box2d.joints.GearJoint.java
com.badlogic.gdx.physics.box2d.joints.MouseJointDef.java
com.badlogic.gdx.physics.box2d.joints.MouseJoint.java
com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef.java
com.badlogic.gdx.physics.box2d.joints.PrismaticJoint.java
com.badlogic.gdx.physics.box2d.joints.PulleyJointDef.java
com.badlogic.gdx.physics.box2d.joints.PulleyJoint.java
com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef.java
com.badlogic.gdx.physics.box2d.joints.RevoluteJoint.java
com.badlogic.gdx.physics.box2d.joints.RopeJointDef.java
com.badlogic.gdx.physics.box2d.joints.RopeJoint.java
com.badlogic.gdx.physics.box2d.joints.WeldJointDef.java
com.badlogic.gdx.physics.box2d.joints.WeldJoint.java
com.badlogic.gdx.physics.box2d.joints.WheelJointDef.java
com.badlogic.gdx.physics.box2d.joints.WheelJoint.java
com.badlogic.gdx.utils.Array.java
com.badlogic.gdx.utils.ComparableTimSort.java
com.badlogic.gdx.utils.Disposable.java
com.badlogic.gdx.utils.GdxRuntimeException.java
com.badlogic.gdx.utils.LongArray.java
com.badlogic.gdx.utils.LongMap.java
com.badlogic.gdx.utils.NumberUtils.java
com.badlogic.gdx.utils.Pool.java
com.badlogic.gdx.utils.Sort.java
com.badlogic.gdx.utils.StringBuilder.java
com.badlogic.gdx.utils.TimSort.java
com.garrapeta.MathUtils.java
com.garrapeta.gameengine.Actor.java
com.garrapeta.gameengine.AsyncGameMessage.java
com.garrapeta.gameengine.BitmapManager.java
com.garrapeta.gameengine.Box2DActor.java
com.garrapeta.gameengine.Box2DWorld.java
com.garrapeta.gameengine.GameMessage.java
com.garrapeta.gameengine.GameView.java
com.garrapeta.gameengine.GameWorld.java
com.garrapeta.gameengine.ShapeDrawer.java
com.garrapeta.gameengine.SyncGameMessage.java
com.garrapeta.gameengine.Viewport.java
com.garrapeta.gameengine.actor.Box2DAtomicActor.java
com.garrapeta.gameengine.actor.Box2DCircleActor.java
com.garrapeta.gameengine.actor.Box2DEdgeActor.java
com.garrapeta.gameengine.actor.Box2DLoopActor.java
com.garrapeta.gameengine.actor.Box2DOpenChainActor.java
com.garrapeta.gameengine.actor.Box2DPolygonActor.java
com.garrapeta.gameengine.actor.IAtomicActor.java
com.garrapeta.gameengine.actor.SimpleActor.java
com.garrapeta.gameengine.module.LevelActionsModule.java
com.garrapeta.gameengine.module.LoadedLevelActionsModule.java
com.garrapeta.gameengine.module.SoundModule.java
com.garrapeta.gameengine.module.VibrationModule.java
com.garrapeta.gameengine.utils.IOUtils.java
com.garrapeta.gameengine.utils.LogX.java
com.garrapeta.gameengine.utils.PhysicsUtils.java
com.garrapeta.gameengine.utils.Pool.java