|
| 1 | +using System.Linq.Expressions; |
| 2 | +using static Hyperbee.Expressions.ExpressionExtensions; |
| 3 | + |
| 4 | +namespace Hyperbee.Expressions.Lab; |
| 5 | + |
| 6 | +public delegate Expression MapBody( ParameterExpression item ); |
| 7 | +public delegate Expression MapBodyIndex( ParameterExpression item, ParameterExpression index ); |
| 8 | +public delegate Expression MapBodyIndexSource( ParameterExpression item, ParameterExpression index, Expression source ); |
| 9 | + |
| 10 | +public class MapExpression : Expression |
| 11 | +{ |
| 12 | + public Expression Collection { get; } |
| 13 | + public Expression Body { get; } |
| 14 | + |
| 15 | + public ParameterExpression Item { get; } |
| 16 | + public ParameterExpression Index { get; } |
| 17 | + public ParameterExpression Source { get; } |
| 18 | + public ParameterExpression ResultList { get; } |
| 19 | + |
| 20 | + public Type ResultType { get; } |
| 21 | + |
| 22 | + private MapExpression( Expression collection, Type resultType ) |
| 23 | + { |
| 24 | + ArgumentNullException.ThrowIfNull( collection ); |
| 25 | + ArgumentNullException.ThrowIfNull( resultType ); |
| 26 | + |
| 27 | + if ( !typeof( System.Collections.IEnumerable ).IsAssignableFrom( collection.Type ) ) |
| 28 | + throw new ArgumentException( "Collection must be IEnumerable", nameof( collection ) ); |
| 29 | + |
| 30 | + Collection = collection; |
| 31 | + ResultType = resultType; |
| 32 | + |
| 33 | + var enumerableType = collection.Type.GetInterfaces() |
| 34 | + .Concat( [collection.Type] ) |
| 35 | + .FirstOrDefault( t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof( IEnumerable<> ) ); |
| 36 | + |
| 37 | + var itemType = enumerableType?.GetGenericArguments()[0] ?? typeof( object ); |
| 38 | + |
| 39 | + Item = Variable( itemType, "item" ); |
| 40 | + ResultList = Variable( typeof( List<> ).MakeGenericType( resultType ), "result" ); |
| 41 | + } |
| 42 | + |
| 43 | + public MapExpression( Expression collection, Type resultType, MapBody body ) : this( collection, resultType ) |
| 44 | + { |
| 45 | + Body = body( Item ); |
| 46 | + } |
| 47 | + |
| 48 | + public MapExpression( Expression collection, Type resultType, MapBodyIndex body ) : this( collection, resultType ) |
| 49 | + { |
| 50 | + Index = Variable( typeof( int ), "index" ); |
| 51 | + Body = body( Item, Index ); |
| 52 | + } |
| 53 | + |
| 54 | + public MapExpression( Expression collection, Type resultType, MapBodyIndexSource body ) : this( collection, resultType ) |
| 55 | + { |
| 56 | + Index = Variable( typeof( int ), "index" ); |
| 57 | + Source = Variable( collection.Type, "source" ); |
| 58 | + Body = body( Item, Index, Collection ); |
| 59 | + } |
| 60 | + |
| 61 | + public MapExpression( Expression collection, Type resultType, Expression body ) : this( collection, resultType ) |
| 62 | + { |
| 63 | + ArgumentNullException.ThrowIfNull( body ); |
| 64 | + Body = body; |
| 65 | + } |
| 66 | + |
| 67 | + public override ExpressionType NodeType => ExpressionType.Extension; |
| 68 | + public override Type Type => typeof( List<> ).MakeGenericType( ResultType ); |
| 69 | + public override bool CanReduce => true; |
| 70 | + |
| 71 | + public override Expression Reduce() |
| 72 | + { |
| 73 | + var addMethod = typeof( List<> ) |
| 74 | + .MakeGenericType( ResultType ) |
| 75 | + .GetMethod( "Add" )!; |
| 76 | + |
| 77 | + if ( Source != null && Index != null ) |
| 78 | + { |
| 79 | + return Block( |
| 80 | + [Item, ResultList, Index, Source], |
| 81 | + Assign( ResultList, New( ResultList.Type ) ), |
| 82 | + Assign( Source, Collection ), |
| 83 | + Assign( Index, Constant( 0 ) ), |
| 84 | + ForEach( Source, Item, |
| 85 | + Block( |
| 86 | + Call( ResultList, addMethod, Body ), |
| 87 | + PostIncrementAssign( Index ) |
| 88 | + ) |
| 89 | + ), |
| 90 | + ResultList |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + if ( Index != null ) |
| 95 | + { |
| 96 | + return Block( |
| 97 | + [Item, ResultList, Index], |
| 98 | + Assign( ResultList, New( ResultList.Type ) ), |
| 99 | + Assign( Index, Constant( 0 ) ), |
| 100 | + ForEach( Collection, Item, |
| 101 | + Block( |
| 102 | + Call( ResultList, addMethod, Body ), |
| 103 | + PostIncrementAssign( Index ) |
| 104 | + ) |
| 105 | + ), |
| 106 | + ResultList |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + return Block( |
| 111 | + [Item, ResultList], |
| 112 | + Assign( ResultList, New( ResultList.Type ) ), |
| 113 | + ForEach( Collection, Item, |
| 114 | + Call( ResultList, addMethod, Body ) |
| 115 | + ), |
| 116 | + ResultList |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + protected override Expression VisitChildren( ExpressionVisitor visitor ) |
| 121 | + { |
| 122 | + var newCollection = visitor.Visit( Collection ); |
| 123 | + var newBody = visitor.Visit( Body ); |
| 124 | + |
| 125 | + if ( newCollection == Collection && newBody == Body ) |
| 126 | + return this; |
| 127 | + |
| 128 | + return new MapExpression( newCollection, ResultType, newBody ); |
| 129 | + } |
| 130 | +} |
| 131 | +public static partial class ExpressionExtensions |
| 132 | +{ |
| 133 | + public static MapExpression Map( Expression collection, Type resultType, MapBody body ) |
| 134 | + => new( collection, resultType, body ); |
| 135 | + |
| 136 | + public static MapExpression Map( Expression collection, MapBody body ) |
| 137 | + => new( collection, GetGenericType( collection.Type ), body ); |
| 138 | + |
| 139 | + public static MapExpression Map( Expression collection, Type resultType, MapBodyIndex body ) |
| 140 | + => new( collection, resultType, body ); |
| 141 | + |
| 142 | + public static MapExpression Map( Expression collection, MapBodyIndex body ) |
| 143 | + => new( collection, GetGenericType( collection.Type ), body ); |
| 144 | + |
| 145 | + public static MapExpression Map( Expression collection, Type resultType, MapBodyIndexSource body ) |
| 146 | + => new( collection, resultType, body ); |
| 147 | + |
| 148 | + public static MapExpression Map( Expression collection, MapBodyIndexSource body ) |
| 149 | + => new( collection, GetGenericType( collection.Type ), body ); |
| 150 | + |
| 151 | + private static Type GetGenericType( Type type ) |
| 152 | + { |
| 153 | + var enumerableType = type == typeof(IEnumerable<>) |
| 154 | + ? type |
| 155 | + : type.GetInterfaces() |
| 156 | + .FirstOrDefault( t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>) ); |
| 157 | + |
| 158 | + var enumerableGenericType = enumerableType?.GetGenericArguments()[0] ?? typeof( object ); |
| 159 | + |
| 160 | + if( enumerableGenericType == null ) |
| 161 | + throw new ArgumentException( "Collection must be IEnumerable", nameof( type ) ); |
| 162 | + |
| 163 | + return enumerableGenericType; |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments