1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Linq . Expressions ;
5
+
6
+ namespace Streamx . Linq . ExTree {
7
+ class ExpressionStack : List < Expression > {
8
+ private bool _reduced ;
9
+ private readonly List < Expression > _ordered ;
10
+ private BranchExpression _parent ;
11
+ public MethodVisitor . LocalVariable [ ] LocalVariables ;
12
+
13
+ public BranchExpression Parent {
14
+ get => _parent ;
15
+ private set {
16
+ _parent = value ;
17
+ LocalVariables = ( MethodVisitor . LocalVariable [ ] ) value ? . Parent ? . LocalVariables ? . Clone ( ) ;
18
+ }
19
+ }
20
+
21
+ public int Depth => Parent ? . Depth ?? 0 ;
22
+
23
+ public bool IsReduced => _reduced ;
24
+
25
+ public void Reduce ( ) => _reduced = true ;
26
+
27
+ public ExpressionStack ( ) : this ( null ) { }
28
+
29
+ ExpressionStack ( BranchExpression parent ) {
30
+ Parent = parent ;
31
+ _ordered = parent ? . Parent . _ordered ?? new List < Expression > ( 64 ) ;
32
+ }
33
+
34
+ public void TrackOrder ( Expression e ) => _ordered . Add ( e ) ;
35
+ public void TrackOrder ( Expression e , Expression after ) => _ordered . Insert ( _ordered . LastIndexOf ( after ) + 1 , e ) ;
36
+
37
+ public void Push ( Expression item ) {
38
+ Add ( item ) ;
39
+ _ordered . Add ( item ) ;
40
+ }
41
+
42
+ public Expression Pop ( ) {
43
+ if ( Count == 0 )
44
+ return null ;
45
+
46
+ Expression obj = Peek ( ) ;
47
+ RemoveAt ( Count - 1 ) ;
48
+
49
+ return obj ;
50
+ }
51
+
52
+ public Expression Peek ( ) {
53
+ Expression obj = this [ Count - 1 ] ;
54
+
55
+ return obj ;
56
+ }
57
+
58
+ public override string ToString ( ) {
59
+ return $ "Count = { Count } ";
60
+ }
61
+
62
+ public void Sort ( IList < Expression > expressions ) {
63
+ Expression [ ] copy = expressions . ToArray ( ) ;
64
+ int [ ] indices = new int [ copy . Length ] ;
65
+ int [ ] orders = new int [ copy . Length ] ;
66
+
67
+ for ( int i = 0 ; i < copy . Length ; i ++ ) {
68
+ orders [ i ] = i ;
69
+ indices [ i ] = _ordered . LastIndexOf ( copy [ i ] ) ;
70
+ }
71
+
72
+ Array . Sort ( orders , ( i1 ,
73
+ i2 ) => indices [ i1 ] - indices [ i2 ] ) ;
74
+
75
+ for ( int i = 0 ; i < copy . Length ; i ++ )
76
+ expressions [ i ] = copy [ orders [ i ] ] ;
77
+ }
78
+
79
+ public class BranchExpression : Expression {
80
+ private readonly ExpressionStack _true ;
81
+ private readonly ExpressionStack _false ;
82
+
83
+ public Expression Test { get ; }
84
+
85
+ public ExpressionStack True => _true ;
86
+
87
+ public ExpressionStack False => _false ;
88
+
89
+ public ExpressionStack Parent { get ; }
90
+
91
+ public int Depth => Parent . Depth + 1 ;
92
+
93
+
94
+ public BranchExpression ( ExpressionStack parent , Expression test ,
95
+ ExpressionStack trueE = null , ExpressionStack falseE = null ) {
96
+ Parent = parent ;
97
+ Test = test ;
98
+
99
+ if ( trueE != null ) {
100
+ _true = trueE ;
101
+ _true . Parent = ( this ) ;
102
+ }
103
+ else
104
+ _true = new ExpressionStack ( this ) ;
105
+
106
+ if ( falseE != null ) {
107
+ _false = falseE ;
108
+ _false . Parent = ( this ) ;
109
+ }
110
+ else
111
+ _false = new ExpressionStack ( this ) ;
112
+ }
113
+
114
+ public ExpressionStack Get ( bool side ) =>
115
+ side ? True : False ;
116
+
117
+ public override ExpressionType NodeType => ExpressionType . Conditional ;
118
+ public override Type Type => typeof ( void ) ;
119
+
120
+ public override string ToString ( ) {
121
+ return $ "({ Test } ? { True } : { False } )";
122
+ }
123
+ }
124
+ }
125
+ }
0 commit comments