3 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
20 #ifndef __GINAC_EXPAIR_H__
21 #define __GINAC_EXPAIR_H__
24 #include <ginac/numeric.h>
26 /** A pair of expressions.
27 * This similar to, but slightly extended STL's pair<> but we need to account
28 * for methods like .compare() */
34 expair(expair const & other) : rest(other.rest), coeff(other.coeff)
36 ASSERT(is_ex_exactly_of_type(coeff,numeric));
38 expair const & operator=(expair const & other)
46 expair(ex const & r, ex const & c) : rest(r), coeff(c)
48 ASSERT(is_ex_exactly_of_type(coeff,numeric));
51 bool is_numeric_with_coeff_1(void) const
53 ASSERT(is_ex_exactly_of_type(coeff,numeric));
54 return is_ex_exactly_of_type(rest,numeric) &&
55 (ex_to_numeric(coeff).compare(numONE())==0);
58 bool is_equal(expair const & other) const
60 return (rest.is_equal(other.rest) && coeff.is_equal(other.coeff));
62 bool is_less(expair const & other) const
64 return (rest.compare(other.rest)<0) ||
65 (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
67 int compare(expair const & other) const
69 int cmpval=rest.compare(other.rest);
70 if (cmpval!=0) return cmpval;
71 cmpval=coeff.compare(other.coeff);
75 bool is_less_old2(expair const & other) const
78 bool this_numeric_with_coeff_1=is_numeric_with_coeff_1();
79 bool other_numeric_with_coeff_1=other.is_numeric_with_coeff_1();
80 if (this_numeric_with_coeff_1) {
81 if (other_numeric_with_coeff_1) {
82 // both have coeff 1: compare rests
83 return rest.compare(other.rest)<0;
85 // only this has coeff 1: >
87 } else if (other_numeric_with_coeff_1) {
88 // only other has coeff 1: <
91 return (rest.compare(other.rest)<0) ||
92 (!(other.rest.compare(rest)<0) &&
93 (coeff.compare(other.coeff)<0));
95 if (is_ex_exactly_of_type(rest,numeric) &&
96 is_ex_exactly_of_type(other.rest,numeric)) {
97 if (ex_to_numeric(coeff).compare(numONE())==0) {
98 if (ex_to_numeric(other.coeff).compare(numONE())==0) {
99 // both have coeff 1: compare rests
100 return rest.compare(other.rest)<0;
102 // only this has coeff 1: >
104 } else if (ex_to_numeric(other.coeff).compare(numONE())==0) {
105 // only other has coeff 1: <
108 // neither has coeff 1: usual compare
110 return (rest.compare(other.rest)<0) ||
111 (!(other.rest.compare(rest)<0) &&
112 (coeff.compare(other.coeff)<0));
114 int compare_old2(expair const & other) const
116 if (is_ex_exactly_of_type(rest,numeric) &&
117 is_ex_exactly_of_type(other.rest,numeric)) {
118 if (ex_to_numeric(coeff).compare(numONE())==0) {
119 if (ex_to_numeric(other.coeff).compare(numONE())==0) {
120 // both have coeff 1: compare rests
121 return rest.compare(other.rest);
123 // only this has coeff 1: >
125 } else if (ex_to_numeric(other.coeff).compare(numONE())==0) {
126 // only other has coeff 1: <
129 // neither has coeff 1: usual compare
132 bool this_numeric_with_coeff_1=is_numeric_with_coeff_1();
133 bool other_numeric_with_coeff_1=other.is_numeric_with_coeff_1();
134 if (this_numeric_with_coeff_1) {
135 if (other_numeric_with_coeff_1) {
136 // both have coeff 1: compare rests
137 return rest.compare(other.rest);
139 // only this has coeff 1: >
141 } else if (other_numeric_with_coeff_1) {
142 // only other has coeff 1: <
144 // neither has coeff 1: usual compare
147 int cmpval=rest.compare(other.rest);
148 if (cmpval!=0) return cmpval;
149 return coeff.compare(other.coeff);
151 bool is_less_old(expair const & other) const
153 return (rest.compare(other.rest)<0) ||
154 (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
156 int compare_old(expair const & other) const
158 int cmpval=rest.compare(other.rest);
159 if (cmpval!=0) return cmpval;
160 cmpval=coeff.compare(other.coeff);
164 void printraw(ostream & os) const
180 bool operator()(expair const & lh, expair const & rh) const
182 return lh.is_less(rh);
186 class expair_is_less_old
189 bool operator()(expair const & lh, expair const & rh) const
191 return lh.is_less_old(rh);
195 #endif // ndef __GINAC_EXPAIR_H__