public TumblingMaxAggregate(IComparerExpression <T> comparer) { Contract.Requires(comparer != null); var stateExpression = Expression.Parameter(typeof(MinMaxState <T>), "state"); var timestampExpression = Expression.Parameter(typeof(long), "timestamp"); var inputExpression = Expression.Parameter(typeof(T), "input"); Expression <Func <MinMaxState <T> > > constructor = () => new MinMaxState <T>(); Expression <Func <MinMaxState <T>, long> > currentTimestamp = (state) => state.currentTimestamp; Expression <Func <MinMaxState <T>, T> > currentValue = (state) => state.currentValue; var currentTimestampExpression = currentTimestamp.ReplaceParametersInBody(stateExpression); var comparerExpression = comparer.GetCompareExpr().ReplaceParametersInBody( inputExpression, currentValue.ReplaceParametersInBody(stateExpression)); var typeInfo = typeof(MinMaxState <T>).GetTypeInfo(); this.accumulate = Expression.Lambda <Func <MinMaxState <T>, long, T, MinMaxState <T> > >( Expression.Condition( Expression.OrElse( Expression.Equal(currentTimestampExpression, Expression.Constant(InvalidSyncTime)), Expression.GreaterThan(comparerExpression, Expression.Constant(0))), Expression.MemberInit( (NewExpression)constructor.Body, Expression.Bind(typeInfo.GetField("currentTimestamp"), timestampExpression), Expression.Bind(typeInfo.GetField("currentValue"), inputExpression)), stateExpression), stateExpression, timestampExpression, inputExpression); }
private static IComparerExpression <T> Reverse(IComparerExpression <T> comparer) { Contract.Requires(comparer != null); Expression <Comparison <T> > expression = comparer.GetCompareExpr(); Expression <Comparison <T> > template = (left, right) => CallInliner.Call(expression, right, left); Expression <Comparison <T> > reversedExpression = template.InlineCalls(); return(new ComparerExpression <T>(reversedExpression)); }
public SlidingMinAggregate(IComparerExpression <T> comparer, QueryContainer container) { Contract.Requires(comparer != null); this.comparer = comparer.GetCompareExpr().Compile(); var generator = comparer.CreateSortedDictionaryGenerator <T, long>(container); Expression <Func <Func <SortedDictionary <T, long> >, MinMaxState <T> > > template = (g) => new MinMaxState <T> { savedValues = new SortedMultiSet <T>(g), currentValue = default, currentTimestamp = InvalidSyncTime
public static IComparerExpression <TNewInput> TransformInput <TOldInput, TNewInput>(this IComparerExpression <TOldInput> comparer, Expression <Func <TNewInput, TOldInput> > transform) { Contract.Requires(comparer != null); Contract.Requires(transform != null); var expression = comparer.GetCompareExpr(); Expression <Comparison <TNewInput> > template = (left, right) => CallInliner.Call(expression, CallInliner.Call(transform, left), CallInliner.Call(transform, right)); var transformedExpression = template.InlineCalls(); return(new ComparerExpression <TNewInput>(transformedExpression)); }
public static Expression <Func <SortedDictionary <TKey, TValue> > > CreateSortedDictionaryGenerator <TKey, TValue>(this IComparerExpression <TKey> comparerExp, QueryContainer container) { if (ComparerExpression <TKey> .IsSimpleDefault(comparerExp)) { return(() => new SortedDictionary <TKey, TValue>()); } var expr = comparerExp.GetCompareExpr(); if (container == null) { Expression <Func <Comparison <TKey>, SortedDictionary <TKey, TValue> > > template = (c) => new SortedDictionary <TKey, TValue>(Comparer <TKey> .Create(c)); var replaced = template.ReplaceParametersInBody(expr); return(Expression.Lambda <Func <SortedDictionary <TKey, TValue> > >(replaced)); } var expression = expr.ToString(); var vars = VariableFinder.Find(expr).Select(o => o.GetHashCode()); var captures = vars.Aggregate(expression.GetHashCode(), (a, i) => a ^ i); var key = Tuple.Create(expression + string.Concat(vars.Select(o => o.ToString(CultureInfo.InvariantCulture))), typeof(TKey), typeof(TValue)); Type temp; lock (sentinel) { if (!DictionaryTypes.TryGetValue(key, out temp)) { string typeName = Prefix + captures.ToString(CultureInfo.InvariantCulture) + typeof(TKey).ToString().GetHashCode().ToString(CultureInfo.InvariantCulture) + typeof(TValue).ToString().GetHashCode().ToString(CultureInfo.InvariantCulture); typeName = typeName.Replace("-", "_"); var builderCode = new GeneratedSortedDictionary(typeName).TransformText(); var assemblyReferences = Transformer.AssemblyReferencesNeededFor(typeof(SortedDictionary <,>)); var a = Transformer.CompileSourceCode(builderCode, assemblyReferences, out string errorMessages); temp = a.GetType(typeName + "`2"); temp = temp.MakeGenericType(typeof(TKey), typeof(TValue)); var init = temp.GetTypeInfo().GetMethod("Initialize", BindingFlags.Static | BindingFlags.Public); init.Invoke(null, new object[] { Comparer <TKey> .Create(expr.Compile()) }); DictionaryTypes.Add(key, temp); } if (!container.TryGetSortedDictionaryType(key, out Type other)) { container.RegisterSortedDictionaryType(key, temp); } } return(Expression.Lambda <Func <SortedDictionary <TKey, TValue> > >(Expression.New(temp))); }
private static IComparerExpression <T> ThenOrderBy(IComparerExpression <T> comparer1, IComparerExpression <T> comparer2) { Contract.Requires(comparer1 != null); Contract.Requires(comparer2 != null); Expression <Comparison <T> > primary = comparer1.GetCompareExpr(); Expression <Comparison <T> > secondary = comparer2.GetCompareExpr(); Expression <Comparison <T> > template = (left, right) => CallInliner.Call(primary, left, right) == 0 ? CallInliner.Call(secondary, left, right) : CallInliner.Call(primary, left, right); Expression <Comparison <T> > newExpression = template.InlineCalls(); return(new ComparerExpression <T>(newExpression)); }
public SynchronousGAPipe( SprayGroupImportStreamable <TKey, TSpray> stream, IStreamObserver <TKey, TSpray> observer, IComparerExpression <TSpray> spraySortOrderComparer, int totalBranches, bool multicast) : base(stream, observer) { this.spraySortOrderComparer = spraySortOrderComparer?.GetCompareExpr(); this.spraySortOrderComparerFunc = this.spraySortOrderComparer?.Compile(); this.totalBranches = totalBranches; this.multicast = multicast; this.l1_spray = 0; this.Observers = new List <IStreamObserver <TKey, TSpray> >(); this.pool = MemoryManager.GetMemoryPool <TKey, TSpray>(stream.Properties.IsColumnar); }
public FixedIntervalMinAggregate(IComparerExpression <T> comparer, QueryContainer container) { Contract.Requires(comparer != null); this.comparer = comparer.GetCompareExpr().Compile(); this.generator = comparer.CreateSortedDictionaryGenerator <T, long>(container); }
public static bool ExpressionEquals <T>(this IComparerExpression <T> source, IComparerExpression <T> other) => EqualityComparer.IsEqual(source.GetCompareExpr(), other.GetCompareExpr());