]> www.ginac.de Git - ginac.git/blob - ginac/expair.h
- enforced GiNaC coding standards :-)
[ginac.git] / ginac / expair.h
1 /** @file expair.h
2  *
3  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 /** A pair of expressions.
21  *  This similar to, but slightly extended STL's pair<> but we need to account
22  *  for methods like .compare() */
23 class expair
24 {
25 public:
26     expair() {}
27     ~expair() {}
28     expair(expair const & other) : rest(other.rest), coeff(other.coeff)
29     {
30         ASSERT(is_ex_exactly_of_type(coeff,numeric));
31     }
32     expair const & operator=(expair const & other)
33     {
34         if (this != &other) {
35             rest=other.rest;
36             coeff=other.coeff;
37         }
38         return *this;
39     }
40     expair(ex const & r, ex const & c) : rest(r), coeff(c)
41     {
42         ASSERT(is_ex_exactly_of_type(coeff,numeric));
43     }
44     
45     bool is_numeric_with_coeff_1(void) const
46     {
47         ASSERT(is_ex_exactly_of_type(coeff,numeric));
48         return is_ex_exactly_of_type(rest,numeric) &&
49                (ex_to_numeric(coeff).compare(numONE())==0);
50     }
51
52     bool is_equal(expair const & other) const
53     {
54         return (rest.is_equal(other.rest) && coeff.is_equal(other.coeff));
55     }
56     bool is_less(expair const & other) const 
57     {
58         return (rest.compare(other.rest)<0) ||
59                (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
60     }
61     int compare(expair const & other) const
62     {
63         int cmpval=rest.compare(other.rest);
64         if (cmpval!=0) return cmpval;
65         cmpval=coeff.compare(other.coeff);
66         return cmpval;
67     }
68
69     bool is_less_old2(expair const & other) const 
70     {
71         /*
72         bool this_numeric_with_coeff_1=is_numeric_with_coeff_1();
73         bool other_numeric_with_coeff_1=other.is_numeric_with_coeff_1();
74         if (this_numeric_with_coeff_1) {
75             if (other_numeric_with_coeff_1) {
76                 // both have coeff 1: compare rests
77                 return rest.compare(other.rest)<0;
78             }
79             // only this has coeff 1: >
80             return false;
81         } else if (other_numeric_with_coeff_1) {
82             // only other has coeff 1: <
83             return true;
84         }
85         return (rest.compare(other.rest)<0) ||
86                (!(other.rest.compare(rest)<0) &&
87                  (coeff.compare(other.coeff)<0));
88         */
89         if (is_ex_exactly_of_type(rest,numeric) &&
90             is_ex_exactly_of_type(other.rest,numeric)) {
91             if (ex_to_numeric(coeff).compare(numONE())==0) {
92                 if (ex_to_numeric(other.coeff).compare(numONE())==0) {
93                     // both have coeff 1: compare rests
94                     return rest.compare(other.rest)<0;
95                 }
96                 // only this has coeff 1: >
97                 return false;
98             } else if (ex_to_numeric(other.coeff).compare(numONE())==0) {
99                 // only other has coeff 1: <
100                 return true;
101             }
102             // neither has coeff 1: usual compare        
103         }
104         return (rest.compare(other.rest)<0) ||
105                (!(other.rest.compare(rest)<0) &&
106                  (coeff.compare(other.coeff)<0));
107     }
108     int compare_old2(expair const & other) const
109     {
110         if (is_ex_exactly_of_type(rest,numeric) &&
111             is_ex_exactly_of_type(other.rest,numeric)) {
112             if (ex_to_numeric(coeff).compare(numONE())==0) {
113                 if (ex_to_numeric(other.coeff).compare(numONE())==0) {
114                     // both have coeff 1: compare rests
115                     return rest.compare(other.rest);
116                 }
117                 // only this has coeff 1: >
118                 return 1;
119             } else if (ex_to_numeric(other.coeff).compare(numONE())==0) {
120                 // only other has coeff 1: <
121                 return -1;
122             }
123             // neither has coeff 1: usual compare        
124         }
125         /*
126         bool this_numeric_with_coeff_1=is_numeric_with_coeff_1();
127         bool other_numeric_with_coeff_1=other.is_numeric_with_coeff_1();
128         if (this_numeric_with_coeff_1) {
129             if (other_numeric_with_coeff_1) {
130                 // both have coeff 1: compare rests
131                 return rest.compare(other.rest);
132             }
133             // only this has coeff 1: >
134             return 1;
135         } else if (other_numeric_with_coeff_1) {
136             // only other has coeff 1: <
137             return -1;
138             // neither has coeff 1: usual compare        
139         }
140         */
141         int cmpval=rest.compare(other.rest);
142         if (cmpval!=0) return cmpval;
143         return coeff.compare(other.coeff);
144     }
145     bool is_less_old(expair const & other) const 
146     {
147         return (rest.compare(other.rest)<0) ||
148                (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
149     }
150     int compare_old(expair const & other) const
151     {
152         int cmpval=rest.compare(other.rest);
153         if (cmpval!=0) return cmpval;
154         cmpval=coeff.compare(other.coeff);
155         return cmpval;
156     }
157
158     void printraw(ostream & os) const
159     {
160         os << "expair(";
161         rest.printraw(os);
162         os << ",";
163         coeff.printraw(os);
164         os << ")";
165     }
166
167     ex rest;
168     ex coeff;
169 };
170
171 class expair_is_less
172 {
173 public:
174     bool operator()(expair const & lh, expair const & rh) const
175     {
176         return lh.is_less(rh);
177     }
178 };
179
180 class expair_is_less_old
181 {
182 public:
183     bool operator()(expair const & lh, expair const & rh) const
184     {
185         return lh.is_less_old(rh);
186     }
187 };
188
189
190