using System.Collections; using System.Collections.Generic; using System.Text; using System; namespace Rokojori { public class Lists { public static List CombineAll( params List[] lists ) { var list = new List(); foreach ( var l in lists ) { list.AddRange( l ); } return list; } public static List ToList( T[] array ) { var list = new List(); list.AddRange( array ); return list; } public static bool Has( List list, T item ) { return list.IndexOf( item ) != - 1; } public static T Pop( List 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( List list ) { return list.Count == 0 ? default(T) : list[ list.Count - 1 ]; } public static void RemoveIncreasingIndices( List list, List removals ) { for ( var i = removals.Count - 1; i >= 0; i-- ) { list.RemoveAt( i ); } } public static int CountItems( List list, Predicate test ) { var result = 0; for ( int i = 0; i < list.Count; i++ ) { if ( test( list[ i ] ) ) { result++; } } return result; } public static int CountItems( T[] list, Predicate test ) { var result = 0; for ( int i = 0; i < list.Length; i++ ) { if ( test( list[ i ] ) ) { result++; } } return result; } public static bool AreEntriesEqual( List a, List b ) { if ( a.Count != b.Count ) { return false; } for ( int i = 0; i < a.Count; i++ ) { var isEqual = EqualityComparer.Default.Equals( a[ i ], b[ i ]); if ( ! isEqual ) { return false; } } return true; } public static string Join( List 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(); } public static List From( T[] array ) { var copy = new List(); copy.AddRange( array ); return copy; } public static List From( List list ) { var copy = new List(); copy.AddRange( list ); return copy; } public static List From<[Godot.MustBeVariant] T>( Godot.Collections.Array list ) { var copy = new List(); copy.AddRange( list ); return copy; } public static void Filter( List inputList, List list, Func filter ) { for ( int i = 0; i < inputList.Count; i++ ) { if ( filter( inputList[ i ], i ) ) { list.Add( inputList[ i ] ); } } } public static void Filter( List inputList, List list, Func filter ) { for ( int i = 0; i < inputList.Count; i++ ) { if ( filter( inputList[ i ] ) ) { list.Add( inputList[ i ] ); } } } public static List Filter( List inputList, Func filter ) { var list = new List(); for ( int i = 0; i < inputList.Count; i++ ) { if ( filter( inputList[ i ] ) ) { list.Add( inputList[ i ] ); } } return list; } public static List FilterType( List inputList ) where R:T { var list = new List(); inputList.ForEach ( e => { if ( e == null || ! typeof( R ).IsAssignableFrom( e.GetType() ) ) { return; } list.Add( (R) e ); } ); return list; } public static T Find( List list, Func callback ) { for ( int i = 0; i < list.Count; i++ ) { if ( callback( list[ i ] ) ) { return list[ i ]; } } return default( T ); } public static List Map( List inputList, Func mapper, List list = null ) { if ( list == null ) { list = new List(); } for ( int i = 0; i < inputList.Count; i++ ) { list.Add( mapper( inputList[ i ], i ) ); } return list; } public static List Map( T[] inputList, Func mapper, List list = null ) { if ( list == null ) { list = new List(); } for ( int i = 0; i < inputList.Length; i++ ) { list.Add( mapper( inputList[ i ], i ) ); } return list; } public static List Map( List inputList, Func mapper, List list = null ) { if ( list == null ) { list = new List(); } for ( int i = 0; i < inputList.Count; i++ ) { list.Add( mapper( inputList[ i ] ) ); } return list; } public static List Map<[Godot.MustBeVariant]T,U>( Godot.Collections.Array inputList, Func mapper, List list = null ) { if ( list == null ) { list = new List(); } for ( int i = 0; i < inputList.Count; i++ ) { list.Add( mapper( inputList[ i ] ) ); } return list; } public static List Map( T[] inputList, Func mapper, List list = null ) { if ( list == null ) { list = new List(); } for ( int i = 0; i < inputList.Length; i++ ) { list.Add( mapper( inputList[ i ] ) ); } return list; } } }