Commit 11a7b90e authored by Stephanie Gawroriski's avatar Stephanie Gawroriski
Browse files

Add util changes.

parent b39c5fc3
Pipeline #39156 passed with stages
in 1 minute and 30 seconds
......@@ -11,6 +11,7 @@ package cc.squirreljme.runtime.cldc.util;
import java.util.AbstractList;
import java.util.List;
import java.util.RandomAccess;
/**
* Provides a list view of a {@code boolean} array.
......@@ -19,6 +20,7 @@ import java.util.List;
*/
public class BooleanArrayList
extends AbstractList<Boolean>
implements RandomAccess
{
/** The backing array. */
protected final boolean[] array;
......
......@@ -11,6 +11,7 @@ package cc.squirreljme.runtime.cldc.util;
import java.util.AbstractList;
import java.util.List;
import java.util.RandomAccess;
/**
* Provides a list view of a {@code byte} array.
......@@ -19,6 +20,7 @@ import java.util.List;
*/
public class ByteArrayList
extends AbstractList<Byte>
implements RandomAccess
{
/** The backing array. */
protected final byte[] array;
......
......@@ -12,6 +12,7 @@ package cc.squirreljme.runtime.cldc.util;
/**
* Wraps a byte array to provide integer access to it.
*
* @see UnsignedByteIntegerArray
* @since 2019/05/09
*/
public final class ByteIntegerArray
......
// -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
// ---------------------------------------------------------------------------
// Multi-Phasic Applications: SquirrelJME
// Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// Copyright (C) Multi-Phasic Applications <multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------
package cc.squirreljme.runtime.cldc.util;
import java.util.Arrays;
import net.multiphasicapps.collections.IntegerList;
/**
* This contains utilities which operate on character sequences.
*
* @since 2017/11/30
*/
public final class CharSequenceUtils
{
/**
* Not used.
*
* @since 2017/11/30
*/
private CharSequenceUtils()
{
}
/**
* Splits the specified character sequence using the given delimeter and
* returns all of the fields which are contained within. Extra whitespace
* within fields are not trimmed.
*
* @param __delim The delimeter to split fields by.
* @param __s The sequence to split.
* @return An array containing all of the fields.
* @throws NullPointerException On null arguments.
* @since 2017/11/30
*/
public static CharSequence[] fieldSplit(char __delim,
CharSequence __s)
throws NullPointerException
{
if (__s == null)
throw new NullPointerException("NARG");
// Get all indexes of that given character
int[] ind = CharSequenceUtils.multipleIndexOf(__delim, __s);
int delCount = ind.length;
int n = delCount + 1;
CharSequence[] rv = new CharSequence[n];
for (int l = -1, r = 0, i = 0; i < n; i++, l++, r++)
rv[i] = __s.subSequence((l >= 0 ? ind[l] + 1 : 0),
(r < delCount ? ind[r] : __s.length()));
return rv;
}
/**
* Searches the given sequence for the first occurrence of the specified
* character.
*
* @param __c The character to locate.
* @param __s The sequence to look inside.
* @return The index of the first occurrence.
* @throws NullPointerException On null arguments.
* @since 2017/11/30
*/
public static int firstIndex(char __c, CharSequence __s)
throws NullPointerException
{
if (__s == null)
throw new NullPointerException("NARG");
for (int i = 0, n = __s.length(); i < n; i++)
if (__c == __s.charAt(i))
return i;
return -1;
}
/**
* Searches the given sequence for the first occurrence of the specified
* characters.
*
* @param __c The characters to locate.
* @param __s The sequence to look inside.
* @return The index of the first occurrence.
* @throws NullPointerException On null arguments.
* @since 2017/11/30
*/
public static int firstIndex(char[] __c, CharSequence __s)
throws NullPointerException
{
if (__c == null || __s == null)
throw new NullPointerException("NARG");
// For optimization sort the input array to find characters faster
__c = __c.clone();
Arrays.sort(__c);
// Forward to one which assumes sorted input
return CharSequenceUtils.firstIndexSorted(__c, __s);
}
/**
* Searches the given sequence for the first occurrence of the specified
* characters.
*
* @param __c The characters to locate.
* @param __s The sequence to look inside.
* @return The index of the first occurrence.
* @throws NullPointerException On null arguments.
* @since 2017/11/30
*/
public static int firstIndex(String __c, CharSequence __s)
throws NullPointerException
{
if (__c == null || __s == null)
throw new NullPointerException("NARG");
return CharSequenceUtils.firstIndex(__c.toCharArray(), __s);
}
/**
* Searches the given sequence for the first occurrence of the specified
* characters. This assumes that the character set has already been
* sorted.
*
* @param __c The characters to locate, this is required to be sorted.
* @param __s The sequence to look inside.
* @return The index of the first occurrence.
* @throws NullPointerException On null arguments.
* @since 2017/11/30
*/
public static int firstIndexSorted(char[] __c, CharSequence __s)
throws NullPointerException
{
if (__c == null || __s == null)
throw new NullPointerException("NARG");
// Go through ever character
for (int i = 0, n = __s.length(), y = __c.length; i < n; i++)
{
// Use binary search because it is faster than checking each
// and every element
char c = __s.charAt(i);
if (Arrays.binarySearch(__c, c) >= 0)
return i;
}
// Not found
return -1;
}
/**
* Returns an array containing all of the indexes that the specified
* character appears in the given sequence.
*
* @param __c The character to get the indexes for.
* @param __s The sequence to check in.
* @return An array containing the array indexes for the given character,
* if there are none then the array will be empty.
* @throws NullPointerException On null arguments.
* @since 2017/11/26
*/
public static int[] multipleIndexOf(char __c, CharSequence __s)
throws NullPointerException
{
if (__s == null)
throw new NullPointerException("NARG");
IntegerList list = new IntegerList();
// Find every character index
for (int i = 0, n = __s.length(); i < n; i++)
{
char c = __s.charAt(i);
// Add index to list if found
if (c == __c)
list.addInteger(i);
}
// Finish
return list.toIntegerArray();
}
}
......@@ -9,8 +9,10 @@
package cc.squirreljme.runtime.cldc.util;
import cc.squirreljme.runtime.cldc.debug.Debugging;
import java.util.AbstractList;
import java.util.List;
import java.util.RandomAccess;
/**
* Provides a list view of a {@code char} array.
......@@ -19,6 +21,7 @@ import java.util.List;
*/
public class CharacterArrayList
extends AbstractList<Character>
implements RandomAccess
{
/** The backing array. */
protected final char[] array;
......
// -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
// ---------------------------------------------------------------------------
// Multi-Phasic Applications: SquirrelJME
// Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------
package cc.squirreljme.runtime.cldc.util;
import java.util.List;
import java.util.RandomAccess;
/**
* General utilities for collections.
*
* @since 2021/02/25
*/
public final class CollectionUtils
{
/**
* Not used.
*
* @since 2021/02/25
*/
private CollectionUtils()
{
}
/**
* Wraps the given character list as an integer list.
*
* @param __chars The character list to wrap.
* @return The resultant integer list.
* @throws NullPointerException On null arguments.
* @since 2021/02/25
*/
public static List<Integer> asIntegerList(List<Character> __chars)
throws NullPointerException
{
if (__chars == null)
throw new NullPointerException("NARG");
if (__chars instanceof RandomAccess)
return new __CharacterIntegerListRandom__(__chars);
return new __CharacterIntegerListSequential__(__chars);
}
}
......@@ -11,6 +11,7 @@ package cc.squirreljme.runtime.cldc.util;
import java.util.AbstractList;
import java.util.List;
import java.util.RandomAccess;
/**
* Provides a list view of a {@code double} array.
......@@ -19,6 +20,7 @@ import java.util.List;
*/
public class DoubleArrayList
extends AbstractList<Double>
implements RandomAccess
{
/** The backing array. */
protected final double[] array;
......
......@@ -10,6 +10,7 @@
package cc.squirreljme.runtime.cldc.util;
import java.util.AbstractList;
import java.util.RandomAccess;
/**
* Provides a list view of a {@code float} array.
......@@ -18,6 +19,7 @@ import java.util.AbstractList;
*/
public class FloatArrayList
extends AbstractList<Float>
implements RandomAccess
{
/** The backing array. */
protected final float[] array;
......
......@@ -11,6 +11,7 @@ package cc.squirreljme.runtime.cldc.util;
import java.util.AbstractList;
import java.util.List;
import java.util.RandomAccess;
/**
* Provides a list view of a {@code int} array.
......@@ -19,6 +20,7 @@ import java.util.List;
*/
public class IntegerArrayList
extends AbstractList<Integer>
implements RandomAccess
{
/** The backing array. */
protected final int[] array;
......
......@@ -10,6 +10,9 @@
package cc.squirreljme.runtime.cldc.util;
import java.util.Comparator;
import java.util.List;
/**
* This class contains utilities used for {@link IntegerArray}.
*
......@@ -115,6 +118,7 @@ public final class IntegerArrays
* @throws IllegalArgumentException If the from index is greater than to
* index.
* @throws NullPointerException If no array was specified.
* @see ShellSort#sort(List, int, int, Comparator)
* @since 2018/10/28
*/
public static void sort(IntegerArray __a, int __from, int __to)
......@@ -138,26 +142,12 @@ public final class IntegerArrays
// If only two values are being sorted, it is a simple swap check
if (n == 2)
{
int ia = __from,
ib = __from + 1;
// Get both values
int a = __a.get(ia),
b = __a.get(ib);
// If the second is lower than the first, we need to swap
if (b < a)
{
__a.set(ia, b);
__a.set(ib, a);
}
// Nothing else needs to be done
IntegerArrays.__sortTwo(__a, __from);
return;
}
// Work down from the highest gap to the lowest
for (int gap : ShellSort._GAPS)
for (int gap : ShellSort.gaps(n))
{
// Gapped insertion sort
for (int i = gap; i < n; i++)
......@@ -176,5 +166,28 @@ public final class IntegerArrays
}
}
}
/**
* Sorts only two items.
*
* @param __a The array to sort.
* @param __from The source array.
* @since 2021/07/12
*/
private static void __sortTwo(IntegerArray __a, int __from)
{
int ib = __from + 1;
// Get both values
int a = __a.get(__from);
int b = __a.get(ib);
// If the second is lower than the first, we need to swap
if (b < a)
{
__a.set(__from, b);
__a.set(ib, a);
}
}
}
// -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
// ---------------------------------------------------------------------------
// Multi-Phasic Applications: SquirrelJME
// Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// Copyright (C) Multi-Phasic Applications <multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------
package net.multiphasicapps.collections;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Collection;
import java.util.RandomAccess;
/**
* This is a list of integers which is backed by a primitive array rather than
* boxed types.
*
* @since 2017/11/26
*/
public final class IntegerList
extends AbstractList<Integer>
implements RandomAccess
{
/** The array growing size. */
private static final int _GROW_SIZE =
8;
/** The internal integer list. */
private volatile int[] _values;
/** The number of values in the list. */
private volatile int _size;
/**
* Initializes an empty list.
*
* @since 2017/11/26
*/
public IntegerList()
{
}
/**
* Initializes a list using the given collection of integers.
*
* @param __v The collection to source values from.
* @throws NullPointerException On null arguments.
* @since 2017/11/26
*/
public IntegerList(Collection<Integer> __v)
throws NullPointerException
{
if (__v == null)
throw new NullPointerException("NARG");
// Iterate through collections
int n = __v.size(), i = 0;
int[] values = new int[n];
for (Integer v : __v)
values[i++] = v;
// Set
this._values = values;
this._size = n;
}
/**
* Initializes a list using the given integer values from an array.
*
* @param __v The array of integers to use for values.
* @throws NullPointerException On null arguments.
* @since 2017/11/26
*/
public IntegerList(int... __v)
throws NullPointerException
{
if (__v == null)
throw new NullPointerException("NARG");
// Defensive copy
__v = __v.clone();
// Simple set
this._values = __v;
this._size = __v.length;
}
/**
* {@inheritDoc}
* @since 2017/11/26
*/
@Override
public boolean add(Integer __a)
throws NullPointerException
{
if (__a == null)
throw new NullPointerException("NARG");
return this.addInteger(__a);
}
/**
* {@inheritDoc}
* @since 2017/11/26
*/
@Override
public void add(int __i, Integer __v)
throws NullPointerException
{
if (__v == null)
throw new NullPointerException("NARG");
this.addInteger(__i, __v);
}
/**
* Adds the specified integer to the list.
*
* @param __v The value to add.
* @return {@code true} if the list has changed.
* @since 2017/11/26
*/
public boolean addInteger(int __v)
{
this.addInteger(this._size, __v);
return true;
}
/**
* Adds the specified integer to the list at the specified position.
*
* @param __i The index to add the value at.
* @param __v The value to add.
* @throws IndexOutOfBoundsException If the index to add it outside of
* the array bounds.
* @since 2017/11/26
*/
public void addInteger(int __i, int __v)
throws IndexOutOfBoundsException
{
if (__i < 0 || __i > this._size)
throw new IndexOutOfBoundsException("IOOB");
// Existing values
boolean realloced = false;
int[] values = this._values;
int nvalues = (values == null ? 0 : values.length),
size = this._size;
// Need a larger array?