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 /** A pair of expressions.
21 * This similar to, but slightly extended STL's pair<> but we need to account
22 * for methods like .compare() */
28 expair(expair const & other) : rest(other.rest), coeff(other.coeff)
30 ASSERT(is_ex_exactly_of_type(coeff,numeric));
32 expair const & operator=(expair const & other)
40 expair(ex const & r, ex const & c) : rest(r), coeff(c)
42 ASSERT(is_ex_exactly_of_type(coeff,numeric));
45 bool is_numeric_with_coeff_1(void) const
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);
52 bool is_equal(expair const & other) const
54 return (rest.is_equal(other.rest) && coeff.is_equal(other.coeff));
56 bool is_less(expair const & other) const
58 return (rest.compare(other.rest)<0) ||
59 (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
61 int compare(expair const & other) const
63 int cmpval=rest.compare(other.rest);
64 if (cmpval!=0) return cmpval;
65 cmpval=coeff.compare(other.coeff);
69 bool is_less_old2(expair const & other) const
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;
79 // only this has coeff 1: >
81 } else if (other_numeric_with_coeff_1) {
82 // only other has coeff 1: <
85 return (rest.compare(other.rest)<0) ||
86 (!(other.rest.compare(rest)<0) &&
87 (coeff.compare(other.coeff)<0));
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;
96 // only this has coeff 1: >
98 } else if (ex_to_numeric(other.coeff).compare(numONE())==0) {
99 // only other has coeff 1: <
102 // neither has coeff 1: usual compare
104 return (rest.compare(other.rest)<0) ||
105 (!(other.rest.compare(rest)<0) &&
106 (coeff.compare(other.coeff)<0));
108 int compare_old2(expair const & other) const
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);
117 // only this has coeff 1: >
119 } else if (ex_to_numeric(other.coeff).compare(numONE())==0) {
120 // only other has coeff 1: <
123 // neither has coeff 1: usual compare
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);
133 // only this has coeff 1: >
135 } else if (other_numeric_with_coeff_1) {
136 // only other has coeff 1: <
138 // neither has coeff 1: usual compare
141 int cmpval=rest.compare(other.rest);
142 if (cmpval!=0) return cmpval;
143 return coeff.compare(other.coeff);
145 bool is_less_old(expair const & other) const
147 return (rest.compare(other.rest)<0) ||
148 (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
150 int compare_old(expair const & other) const
152 int cmpval=rest.compare(other.rest);
153 if (cmpval!=0) return cmpval;
154 cmpval=coeff.compare(other.coeff);
158 void printraw(ostream & os) const
174 bool operator()(expair const & lh, expair const & rh) const
176 return lh.is_less(rh);
180 class expair_is_less_old
183 bool operator()(expair const & lh, expair const & rh) const
185 return lh.is_less_old(rh);