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

350 lines
7.0 KiB
C#
Raw Normal View History

2024-05-12 17:03:20 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;
namespace Rokojori
{
public class Lists
{
2024-07-26 09:26:24 +00:00
public static List<T> CombineAll<T>( params List<T>[] lists )
{
var list = new List<T>();
foreach ( var l in lists )
{
list.AddRange( l );
}
return list;
}
2024-08-04 16:57:47 +00:00
public static List<T> From<T>( params T[] elements )
{
return ToList( elements );
}
2024-05-12 17:03:20 +00:00
public static List<T> ToList<T>( T[] array )
{
var list = new List<T>();
list.AddRange( array );
return list;
}
public static bool Has<T>( List<T> list, T item )
{
return list.IndexOf( item ) != - 1;
}
2024-08-11 17:38:06 +00:00
public static T RemoveAt<T>( List<T> list, int index )
{
if ( list.Count == 0 || index < 0 || ( index > ( list.Count - 1 ) ) )
{
return default(T);
}
var first = list[ index ];
list.RemoveAt( index );
return first;
}
public static T RemoveFirst<T>( List<T> list )
{
return RemoveAt( list, 0 );
}
public static T RemoveLast<T>( List<T> list )
{
return RemoveAt( list, list.Count - 1 );
}
2024-05-12 17:03:20 +00:00
public static T Pop<T>( List<T> list )
{
if ( list.Count == 0 ){ return default(T); }
var element = list[ list.Count - 1 ];
list.RemoveAt( list.Count - 1 );
return element;
}
public static T Last<T>( List<T> list )
{
return list.Count == 0 ? default(T) : list[ list.Count - 1 ];
}
2024-05-19 15:59:41 +00:00
public static void RemoveIncreasingIndices<T>( List<T> list, List<int> removals )
{
for ( var i = removals.Count - 1; i >= 0; i-- )
{
list.RemoveAt( i );
}
}
2024-05-12 17:03:20 +00:00
public static int CountItems<T>( List<T> list, Predicate<T> test )
{
var result = 0;
for ( int i = 0; i < list.Count; i++ )
{
if ( test( list[ i ] ) )
{
result++;
}
}
return result;
}
public static int CountItems<T>( T[] list, Predicate<T> test )
{
var result = 0;
for ( int i = 0; i < list.Length; i++ )
{
if ( test( list[ i ] ) )
{
result++;
}
}
return result;
}
public static bool AreEntriesEqual<T>( List<T> a, List<T> b )
{
if ( a.Count != b.Count )
{
return false;
}
for ( int i = 0; i < a.Count; i++ )
{
var isEqual = EqualityComparer<T>.Default.Equals( a[ i ], b[ i ]);
if ( ! isEqual )
{
return false;
}
}
return true;
}
public static string Join<T>( List<T> array, string seperator = ", " )
{
var sb = new StringBuilder();
for ( var i = 0; i < array.Count; i++ )
{
if ( i != 0 )
{
sb.Append( seperator );
}
sb.Append( array[ i ] );
}
return sb.ToString();
}
2024-08-04 16:57:47 +00:00
2024-05-12 17:03:20 +00:00
public static List<T> From<T>( List<T> list )
{
var copy = new List<T>();
copy.AddRange( list );
return copy;
}
2024-05-19 15:59:41 +00:00
public static List<T> From<[Godot.MustBeVariant] T>( Godot.Collections.Array<T> list )
{
var copy = new List<T>();
copy.AddRange( list );
return copy;
}
2024-08-11 17:38:06 +00:00
public static List<U> FilterAndMap<T,U>( List<T> list, Func<T,int,bool> filter, Func<T,U> map)
{
var mapped = new List<U>();
for ( int i = 0; i < list.Count; i++ )
{
if ( ! filter( list[ i ], i ) )
{
continue;
}
mapped.Add( map( list[ i ] ) );
}
return mapped;
}
2024-05-12 17:03:20 +00:00
public static void Filter<T>( List<T> inputList, List<T> list, Func<T,int,bool> filter )
{
for ( int i = 0; i < inputList.Count; i++ )
{
if ( filter( inputList[ i ], i ) )
{
list.Add( inputList[ i ] );
}
}
}
public static void Filter<T>( List<T> inputList, List<T> list, Func<T,bool> filter )
{
for ( int i = 0; i < inputList.Count; i++ )
{
if ( filter( inputList[ i ] ) )
{
list.Add( inputList[ i ] );
}
}
}
2024-08-11 17:38:06 +00:00
2024-05-12 17:03:20 +00:00
public static List<T> Filter<T>( List<T> inputList, Func<T,bool> filter )
{
var list = new List<T>();
for ( int i = 0; i < inputList.Count; i++ )
{
if ( filter( inputList[ i ] ) )
{
list.Add( inputList[ i ] );
}
}
return list;
}
public static List<R> FilterType<T,R>( List<T> inputList ) where R:T
{
var list = new List<R>();
inputList.ForEach
(
e =>
{
if ( e == null || ! typeof( R ).IsAssignableFrom( e.GetType() ) )
{
return;
}
list.Add( (R) e );
}
);
return list;
2024-08-11 17:38:06 +00:00
}
public static void Add<T>( List<T> list, params T[] entries )
{
list.AddRange( entries );
}
public static void Insert<T>( List<T> list, T item, int index )
{
if ( index >= list.Count )
{
list.Add( item );
return;
}
index = index < 0 ? 0 : index;
list.Insert( index, item );
2024-05-12 17:03:20 +00:00
}
public static T Find<T>( List<T> list, Func<T,bool> callback )
{
for ( int i = 0; i < list.Count; i++ )
{
if ( callback( list[ i ] ) )
{
return list[ i ];
}
}
return default( T );
}
public static List<U> Map<T,U>( List<T> inputList, Func<T,int,U> mapper, List<U> list = null )
{
if ( list == null )
{
list = new List<U>();
}
for ( int i = 0; i < inputList.Count; i++ )
{
list.Add( mapper( inputList[ i ], i ) );
}
return list;
}
public static List<U> Map<T,U>( T[] inputList, Func<T,int,U> mapper, List<U> list = null )
{
if ( list == null )
{
list = new List<U>();
}
for ( int i = 0; i < inputList.Length; i++ )
{
list.Add( mapper( inputList[ i ], i ) );
}
return list;
}
public static List<U> Map<T,U>( List<T> inputList, Func<T,U> mapper, List<U> list = null )
{
if ( list == null )
{
list = new List<U>();
}
for ( int i = 0; i < inputList.Count; i++ )
{
list.Add( mapper( inputList[ i ] ) );
}
return list;
}
2024-05-19 15:59:41 +00:00
public static List<U> Map<[Godot.MustBeVariant]T,U>( Godot.Collections.Array<T> inputList, Func<T,U> mapper, List<U> list = null )
{
if ( list == null )
{
list = new List<U>();
}
for ( int i = 0; i < inputList.Count; i++ )
{
list.Add( mapper( inputList[ i ] ) );
}
return list;
}
2024-05-12 17:03:20 +00:00
public static List<U> Map<T,U>( T[] inputList, Func<T,U> mapper, List<U> list = null )
{
if ( list == null )
{
list = new List<U>();
}
for ( int i = 0; i < inputList.Length; i++ )
{
list.Add( mapper( inputList[ i ] ) );
}
return list;
}
2024-05-19 15:59:41 +00:00
2024-05-12 17:03:20 +00:00
}
}