using System.Collections; using System.Collections.Generic; using System.Text; using System; using Godot; namespace Rokojori { public class Lists { public static void Sort( List data, Func getValue ) { ValueSorter.SortList( data, getValue ); } public class IndexLerpResult { public int closestIndex = -1; public int secondIndex = -1; public float lerpAmount = 0.5f; } public static List Clone( List data ) { if ( data == null ) { return null; } var cloned = new List( data.Count ); cloned.AddRange( data ); return cloned; } public static IndexLerpResult LerpIndex( List data, float value, Func getValue, bool sort = false ) { if ( sort ) { Sort( data, getValue ); } var result = new IndexLerpResult(); result.closestIndex = ClosestIndex( data, value, getValue ); result.secondIndex = SecondClosestIndex( data, result.closestIndex, value, getValue ); if ( result.closestIndex == result.secondIndex ) { return result; } var closestValue = getValue( data[ result.closestIndex ] ); var secondValue = getValue( data[ result.secondIndex ] ); var min = closestValue; var max = secondValue; var flip = false; if ( closestValue > secondValue ) { flip = true; min = secondValue; max = closestValue; } result.lerpAmount = MathX.Normalize( value, min, max ); if ( flip ) { result.lerpAmount = 1f - result.lerpAmount; } return result; } public static int SecondClosestIndex( List data, int closest, float compareValue, Func getValue, bool sort = false ) { if ( sort ) { Sort( data, getValue ); } if ( data.Count == 1 ) { return 0; } if ( closest == 0 ) { return closest + 1; } if ( closest == data.Count - 1 ) { return data.Count - 2; } var before = data[ closest - 1 ]; var after = data[ closest + 1 ]; var bD = Mathf.Abs( getValue( before ) - compareValue ); var aD = Mathf.Abs( getValue( after ) - compareValue ); if ( bD < aD ) { return closest - 1 ; } return closest + 1; } public static int ClosestIndex( List data, float compareValue, Func getValue ) { var index = -1; var distance = float.MaxValue; for ( int i = 0 ; i < data.Count; i++ ) { var d = Mathf.Abs( getValue( data[ i ] ) - compareValue ); if ( d < distance ) { index = i; distance = d; } } return index; } public static List CollectIndices( List list, Func evaluator ) { var output = new List(); for ( int i = 0; i < list.Count; i++ ) { if ( evaluator( list[ i ] ) ) { output.Add( i ); } } return output; } public static List CombineAll( params List[] lists ) { var list = new List(); foreach ( var l in lists ) { list.AddRange( l ); } return list; } public static List From( params T[] elements ) { return ToList( elements ); } 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 RemoveAt( List 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( List list ) { return RemoveAt( list, 0 ); } public static T RemoveLast( List list ) { return RemoveAt( list, list.Count - 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 List FromEnumerable( IEnumerable enumerable ) { var listA = new List(); foreach ( var it in enumerable ) { listA.Add( (T) it ); } return listA; } public static bool AreListsAndEntriesEqual( object objA, object objB ) { if ( ! ( ReflectionHelper.IsList( objA ) && ReflectionHelper.IsList( objB ) ) ) { return false; } return AreEntriesEqual( FromEnumerable( objA as IEnumerable ), FromEnumerable( objB as IEnumerable ) ); } 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 ); } var obj = (object) array[ i ]; RJLog.Stringify( array[ i ], sb ); } return sb.ToString(); } 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( RegexUtility.NumberToString( array[ i ] ) ); } return sb.ToString(); } 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 List FilterAndMap( List list, Func filter, Func map) { var mapped = new List(); for ( int i = 0; i < list.Count; i++ ) { if ( ! filter( list[ i ], i ) ) { continue; } mapped.Add( map( list[ i ] ) ); } return mapped; } 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 void Add( List list, params T[] entries ) { list.AddRange( entries ); } public static void Insert( List list, T item, int index ) { if ( index >= list.Count ) { list.Add( item ); return; } index = index < 0 ? 0 : index; list.Insert( index, item ); } 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; } } }