rj-action-library/Runtime/Tools/Arrays.cs

161 lines
3.2 KiB
C#
Raw Normal View History

2024-08-04 09:08:12 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;
namespace Rokojori
{
public class Arrays
{
2025-01-03 12:09:23 +00:00
public static T[] Concat<T>( T[] a, T[] b )
{
var o = new T[ a.Length + b.Length ];
a.CopyTo( o, 0 );
b.CopyTo( o, a.Length );
return o;
}
2024-09-14 06:41:52 +00:00
public static int IndexOf<T>( T[] values, T other )
{
return Array.IndexOf( values, other );
}
2024-12-01 17:07:41 +00:00
public static U[] Map<T,U>( T[] values, Func<T,U> mapper )
{
var u = new U[ values.Length ];
for ( int i = 0; i < values.Length; i++ )
{
u[ i ] = mapper( values[ i ] );
}
return u;
}
2025-01-08 18:46:17 +00:00
2024-09-14 06:41:52 +00:00
public static int FindIndex<T>( T[] values, Func<T,bool> predicate )
{
if ( values == null )
{
return -1;
}
2024-09-14 06:41:52 +00:00
for ( int i = 0; i < values.Length; i++ )
{
if ( predicate( values[ i ] ) )
{
return i;
}
}
return -1;
}
2025-01-26 09:15:28 +00:00
public static T Find<T>( T[] values, Func<T,bool> predicate )
{
var entryIndex = FindIndex( values, predicate );
return entryIndex == -1 ? default(T) : values[ entryIndex ];
}
2024-08-05 07:14:00 +00:00
public static bool Contains <T>( T[] values, T other )
{
return Array.IndexOf( values, other ) != -1;
}
public static bool ContainsEqual<T>( T[] values, T other )
{
for ( int i = 0; i < values.Length; i++ )
{
if ( other.Equals( values[ i ] ) )
{
return true;
}
}
return false;
}
2024-08-05 07:14:00 +00:00
public static T[] Add<T>( T[] values, T other )
{
var newValues = new T[ values.Length + 1 ];
Array.Copy( values, newValues, values.Length );
newValues[ values.Length ] = other;
return newValues;
}
public static T[] RemoveIndex<T>( T[] values, int index )
{
if ( index <= -1 || index >= values.Length )
{
return values;
}
if ( values.Length == 1 && index == 0 )
{
return new T[ 0 ];
}
var newValues = new T[ values.Length - 1 ];
if ( index != 0 )
{
Array.Copy( values, newValues, index );
}
if ( index != ( values.Length - 1 ) )
{
Array.Copy( values, index + 1, newValues, index, ( values.Length - 1 ) - index );
}
return newValues;
}
2024-08-04 09:08:12 +00:00
public static void ForEach<T>( T[] values, Action<T> callback )
{
foreach ( var it in values )
{
callback( it );
}
}
2024-11-13 19:33:08 +00:00
public static bool AreArraysAndEntriesEqual( object objA, object objB )
{
if ( ! ( objA.GetType().IsArray && objB.GetType().IsArray ) )
{
return false;
}
return Lists.AreEntriesEqual(
Lists.FromEnumerable<object>( objA as IEnumerable ),
Lists.FromEnumerable<object>( objA as IEnumerable )
);
}
public static bool AreEntriesEqual<T>( T[] a, T[] b )
{
if ( a.Length != b.Length )
{
return false;
}
for ( int i = 0; i < a.Length; i++ )
{
var isEqual = EqualityComparer<T>.Default.Equals( a[ i ], b[ i ]);
if ( ! isEqual )
{
return false;
}
}
return true;
}
2024-08-04 09:08:12 +00:00
}
}