File: ComparisonExpression.h

package info (click to toggle)
primrose 6%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,300 kB
  • ctags: 3,385
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 95; perl: 67
file content (237 lines) | stat: -rw-r--r-- 5,428 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Modification History 
 *
 * 2003-May-2   Jason Rohrer
 * Created.
 */


 
#ifndef COMPARISON_EXPRESSION_INCLUDED
#define COMPARISON_EXPRESSION_INCLUDED 
 
#include "Expression.h" 
#include "BinaryOperationExpression.h" 



/**
 * Expression implementation of a binary comparison operation.
 *
 * Evaluating this expression returns 1 if the comparisson is true
 * and 0 if it is false.
 *
 * @author Jason Rohrer 
 */
class ComparisonExpression : public BinaryOperationExpression { 
    
    public:

        static const int GREATER_THAN = 0;
        static const int LESS_THAN = 1;
        static const int GREATER_THAN_OR_EQUAL_TO = 2;
        static const int LESS_THAN_OR_EQUAL_TO = 3;
        static const int EQUAL_TO = 4;
        static const int NOT_EQUAL_TO = 5;
        
        /**
         * Constructs a binary comparison operation expression.
         *
         * Both arguments are destroyed when the class is destroyed.
         *
         * @param inComparison the comparison to make, one of
         *   GREATER_THAN, LESS_THAN, GREATER_THAN_OR_EQUAL_TO,
         *   LESS_THAN_OR_EQUAL_TO, EQUAL_TO, NOT_EQUAL_TO.
         * @param inArgumentA the first argument.
         * @param inArgumentB the second argument.
         */
        ComparisonExpression( int inComparison,
                              Expression *inArgumentA, 
                              Expression *inArgumentB );

        

        /**
         * Gets the comparison being made.
         *
         * @return one of GREATER_THAN, LESS_THAN, GREATER_THAN_OR_EQUAL_TO,
         *   LESS_THAN_OR_EQUAL_TO, EQUAL_TO, NOT_EQUAL_TO.
         */
        int getComparison();



        /**
         * Sets the comparison to make.
         *
         * @param inComparison the comparison to make, one of
         *   GREATER_THAN, LESS_THAN, GREATER_THAN_OR_EQUAL_TO,
         *   LESS_THAN_OR_EQUAL_TO, EQUAL_TO, NOT_EQUAL_TO.
         */
        void setComparison( int inComparison );


        
        /**
         * A static version of getID().
         */
        static long staticGetID();
        
        
        // implements the Expression interface
        virtual double evaluate();
        virtual long getID();
        virtual void print();
        virtual Expression *copy();
        
    protected:
        static long mID;

        int mComparison;
        
    };



// static init
long ComparisonExpression::mID = 15;



inline ComparisonExpression::ComparisonExpression(
    int inComparison,
    Expression *inArgumentA, 
    Expression *inArgumentB ) 
    : BinaryOperationExpression( inArgumentA, inArgumentB ),
      mComparison( inComparison ) {
    
    }



inline int ComparisonExpression::getComparison() {
    return mComparison;
    }



inline void ComparisonExpression::setComparison( int inComparison ) {
    mComparison = inComparison;
    }



inline double ComparisonExpression::evaluate() {
    if( mComparison == GREATER_THAN ) {
        if( mArgumentA->evaluate() > mArgumentB->evaluate() ) {
            return 1;
            }
        else {
            return 0;
            }
        }
    else if( mComparison == LESS_THAN ) {
        if( mArgumentA->evaluate() < mArgumentB->evaluate() ) {
            return 1;
            }
        else {
            return 0;
            }
        }
    else if( mComparison == GREATER_THAN_OR_EQUAL_TO ) {
        if( mArgumentA->evaluate() >= mArgumentB->evaluate() ) {
            return 1;
            }
        else {
            return 0;
            }
        }
    else if( mComparison == LESS_THAN_OR_EQUAL_TO ) {
        if( mArgumentA->evaluate() <= mArgumentB->evaluate() ) {
            return 1;
            }
        else {
            return 0;
            }
        }
    else if( mComparison == EQUAL_TO ) {
        if( mArgumentA->evaluate() == mArgumentB->evaluate() ) {
            return 1;
            }
        else {
            return 0;
            }
        }
    else if( mComparison == NOT_EQUAL_TO ) {
        if( mArgumentA->evaluate() != mArgumentB->evaluate() ) {
            return 1;
            }
        else {
            return 0;
            }
        }
    else {
        // unknown comparison type
        // default to false
        return 0;
        }
    }

    

inline long ComparisonExpression::getID() {    
    return mID;
    }



inline long ComparisonExpression::staticGetID() {
    return mID;
    }



inline void ComparisonExpression::print() {
    printf( "( " );
    
    mArgumentA->print();

    if( mComparison == GREATER_THAN ) {
        printf( " > " );
        }
    else if( mComparison == LESS_THAN ) {
        printf( " < " );
        }
    else if( mComparison == GREATER_THAN_OR_EQUAL_TO ) {
        printf( " >= " );
        }
    else if( mComparison == LESS_THAN_OR_EQUAL_TO ) {
        printf( " <= " );
        }
    else if( mComparison == EQUAL_TO ) {
        printf( " == " );
        }
    else if( mComparison == NOT_EQUAL_TO ) {
        printf( " != " );
        }
    else {
        printf( " UNKNOWN_COMPARISON " );
        }

    mArgumentB->print();
    printf( " )" );
    }
            


inline Expression *ComparisonExpression::copy() {
    ComparisonExpression *copy =
        new ComparisonExpression( mComparison,
                                  mArgumentA->copy(),
                                  mArgumentB->copy() );
    return copy;
    }



#endif