]> www.ginac.de Git - ginac.git/blob - ginac/expair.h
skeleton implementation of new color class
[ginac.git] / ginac / expair.h
1 /** @file expair.h
2  *
3  *  Definition of expression pairs (building blocks of expairseq). */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #ifndef __GINAC_EXPAIR_H__
24 #define __GINAC_EXPAIR_H__
25
26 #include "ex.h"
27 #include "numeric.h"
28
29 namespace GiNaC {
30
31 /** A pair of expressions.
32  *  This is similar to STL's pair<>.  It is slightly extended since we need to
33  *  account for methods like .compare().  Also, since this is meant for use by
34  *  class expairseq it must satisfy the invariance that the member coeff must
35  *  be of type numeric. */
36 class expair
37 {
38 public:
39         expair() : rest(0), coeff(1) { }
40         ~expair() { }
41         expair(const expair & other) : rest(other.rest), coeff(other.coeff)
42         {
43                 GINAC_ASSERT(is_ex_exactly_of_type(coeff,numeric));
44         }
45         const expair & operator=(const expair & other)
46         {
47                 if (this != &other) {
48                         rest = other.rest;
49                         coeff = other.coeff;
50                 }
51                 return *this;
52         }
53         
54         /** Construct an expair from two ex. */
55         expair(const ex & r, const ex & c) : rest(r), coeff(c)
56         {
57                 GINAC_ASSERT(is_ex_exactly_of_type(coeff,numeric));
58         }
59         
60         /** Member-wise check for canonical ordering equality. */
61         bool is_equal(const expair & other) const
62         {
63                 return (rest.is_equal(other.rest) && coeff.is_equal(other.coeff));
64         }
65         
66         /** Member-wise check for canonical ordering lessness. */
67         bool is_less(const expair & other) const 
68         {
69                 int restcmp = rest.compare(other.rest);
70                 return ((restcmp<0) ||
71                         (!(restcmp>0) && (coeff.compare(other.coeff)<0)));
72         }
73         
74         /** Member-wise check for canonical ordering. */
75         int compare(const expair & other) const
76         {
77                 int restcmp = rest.compare(other.rest);
78                 if (restcmp!=0)
79                         return restcmp;
80                 else
81                         return coeff.compare(other.coeff);
82         }
83         
84         /** Output to ostream in ugly raw format. */
85         void printraw(std::ostream & os) const
86         {
87                 os << "expair(";
88                 rest.printraw(os);
89                 os << ",";
90                 coeff.printraw(os);
91                 os << ")";
92         }
93         
94         /** True if this is of the form (numeric,ex(1)). */
95         bool is_canonical_numeric(void) const
96         {
97                 GINAC_ASSERT(is_ex_exactly_of_type(coeff,numeric));
98                 return (is_ex_exactly_of_type(rest,numeric) &&
99                         (coeff.is_equal(1)));
100         }
101         
102         ex rest;    ///< first member of pair, an arbitrary expression
103         ex coeff;   ///< second member of pair, must be numeric
104 };
105
106 /** Function object for insertion into third argument of STL's sort() etc. */
107 class expair_is_less
108 {
109 public:
110         bool operator()(const expair &lh, const expair &rh) const
111         {
112                 return lh.is_less(rh);
113         }
114 };
115
116 } // namespace GiNaC
117
118 #endif // ndef __GINAC_EXPAIR_H__