3 * Definition of expression pairs (building blocks of expairseq). */
6 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifndef __GINAC_EXPAIR_H__
24 #define __GINAC_EXPAIR_H__
29 #ifndef NO_NAMESPACE_GINAC
31 #endif // ndef NO_NAMESPACE_GINAC
33 /** A pair of expressions.
34 * This similar to, but slightly extended STL's pair<> but we need to account
35 * for methods like .compare() */
42 expair(const expair & other) : rest(other.rest), coeff(other.coeff)
44 GINAC_ASSERT(is_ex_exactly_of_type(coeff,numeric));
47 const expair & operator=(const expair & other)
56 expair(const ex & r, const ex & c) : rest(r), coeff(c)
58 GINAC_ASSERT(is_ex_exactly_of_type(coeff,numeric));
61 bool is_numeric_with_coeff_1(void) const
63 GINAC_ASSERT(is_ex_exactly_of_type(coeff,numeric));
64 return is_ex_exactly_of_type(rest,numeric)
65 && (coeff.is_equal(ex(1)));
68 bool is_equal(const expair & other) const
70 return (rest.is_equal(other.rest) && coeff.is_equal(other.coeff));
73 bool is_less(const expair & other) const
75 return (rest.compare(other.rest)<0)
76 || (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
79 int compare(const expair & other) const
81 int cmpval=rest.compare(other.rest);
82 if (cmpval!=0) return cmpval;
83 cmpval=coeff.compare(other.coeff);
87 bool is_less_old2(const expair & other) const
90 bool this_numeric_with_coeff_1=is_numeric_with_coeff_1();
91 bool other_numeric_with_coeff_1=other.is_numeric_with_coeff_1();
92 if (this_numeric_with_coeff_1) {
93 if (other_numeric_with_coeff_1) {
94 // both have coeff 1: compare rests
95 return rest.compare(other.rest)<0;
97 // only this has coeff 1: >
99 } else if (other_numeric_with_coeff_1) {
100 // only other has coeff 1: <
103 return (rest.compare(other.rest)<0)
104 || (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
106 if (is_ex_exactly_of_type(rest,numeric) &&
107 is_ex_exactly_of_type(other.rest,numeric)) {
108 if (coeff.is_equal(ex(1))) {
109 if ((other.coeff).is_equal(ex(1))) {
110 // both have coeff 1: compare rests
111 return rest.compare(other.rest)<0;
113 // only this has coeff 1: >
115 } else if ((other.coeff).is_equal(ex(1))) {
116 // only other has coeff 1: <
119 // neither has coeff 1: usual compare
121 return (rest.compare(other.rest)<0)
122 || (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
125 int compare_old2(const expair & other) const
127 if (is_ex_exactly_of_type(rest,numeric) &&
128 is_ex_exactly_of_type(other.rest,numeric)) {
129 if ((coeff).is_equal(ex(1))) {
130 if ((other.coeff).is_equal(ex(1))) {
131 // both have coeff 1: compare rests
132 return rest.compare(other.rest);
134 // only this has coeff 1: >
136 } else if ((other.coeff).is_equal(ex(1))) {
137 // only other has coeff 1: <
140 // neither has coeff 1: usual compare
143 bool this_numeric_with_coeff_1=is_numeric_with_coeff_1();
144 bool other_numeric_with_coeff_1=other.is_numeric_with_coeff_1();
145 if (this_numeric_with_coeff_1) {
146 if (other_numeric_with_coeff_1) {
147 // both have coeff 1: compare rests
148 return rest.compare(other.rest);
150 // only this has coeff 1: >
152 } else if (other_numeric_with_coeff_1) {
153 // only other has coeff 1: <
155 // neither has coeff 1: usual compare
158 int cmpval=rest.compare(other.rest);
159 if (cmpval!=0) return cmpval;
160 return coeff.compare(other.coeff);
163 bool is_less_old(const expair & other) const
165 return (rest.compare(other.rest)<0)
166 || (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
168 int compare_old(const expair & other) const
170 int cmpval=rest.compare(other.rest);
171 if (cmpval!=0) return cmpval;
172 cmpval=coeff.compare(other.coeff);
176 void printraw(std::ostream & os) const
192 bool operator()(const expair & lh, const expair & rh) const
194 return lh.is_less(rh);
198 class expair_is_less_old
201 bool operator()(const expair & lh, const expair & rh) const
203 return lh.is_less_old(rh);
207 #ifndef NO_NAMESPACE_GINAC
209 #endif // ndef NO_NAMESPACE_GINAC
211 #endif // ndef __GINAC_EXPAIR_H__