]> www.ginac.de Git - ginac.git/blob - ginac/coloridx.cpp
GINAC_DECLARE_REGISTERED_CLASS declares duplicate() and compare_same_type(),
[ginac.git] / ginac / coloridx.cpp
1 /** @file coloridx.cpp
2  *
3  *  Implementation of GiNaC's color indices. */
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 #include <stdexcept>
24
25 #include "coloridx.h"
26 #include "archive.h"
27 #include "utils.h"
28 #include "debugmsg.h"
29
30 #ifndef NO_NAMESPACE_GINAC
31 namespace GiNaC {
32 #endif // ndef NO_NAMESPACE_GINAC
33
34 GINAC_IMPLEMENT_REGISTERED_CLASS(coloridx, idx)
35
36 //////////
37 // default constructor, destructor, copy constructor assignment operator and helpers
38 //////////
39
40 // public
41
42 coloridx::coloridx()
43 {
44         debugmsg("coloridx default constructor",LOGLEVEL_CONSTRUCT);
45         // serial is incremented in idx::idx()
46         name="color"+ToString(serial);
47         tinfo_key=TINFO_coloridx;
48 }
49
50 // protected
51
52 void coloridx::copy(const coloridx & other)
53 {
54         inherited::copy(other);
55 }
56
57 void coloridx::destroy(bool call_parent)
58 {
59         if (call_parent) inherited::destroy(call_parent);
60 }
61
62 //////////
63 // other constructors
64 //////////
65
66 // public
67
68 /** Construct symbolic color index, using an automatically generated unique name.
69  *
70  *  @param cov Index is covariant (contravariant otherwise)
71  *  @return newly constructed index */
72 coloridx::coloridx(bool cov) : idx(cov)
73 {
74         debugmsg("coloridx constructor from bool",LOGLEVEL_CONSTRUCT);
75         // serial is incremented in idx::idx(bool)
76         name="color"+ToString(serial);
77         tinfo_key=TINFO_coloridx;
78 }
79
80 /** Construct symbolic color index with specified name.
81  *
82  *  @param n Symbolic index name
83  *  @param cov Index is covariant (contravariant otherwise)
84  *  @return newly constructed index */
85 coloridx::coloridx(const std::string & n, bool cov) : idx(n,cov)
86 {
87         debugmsg("coloridx constructor from string,bool",LOGLEVEL_CONSTRUCT);
88         tinfo_key=TINFO_coloridx;
89 }
90
91 /** Construct symbolic color index with specified name.
92  *
93  *  @param n Symbolic index name
94  *  @param cov Index is covariant (contravariant otherwise)
95  *  @return newly constructed index */
96 coloridx::coloridx(const char * n, bool cov) : idx(n,cov)
97 {
98         debugmsg("coloridx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
99         tinfo_key=TINFO_coloridx;
100 }
101
102 /** Construct numeric color index with specified value.
103  *
104  *  @param v Numeric index value
105  *  @param cov Index is covariant (contravariant otherwise)
106  *  @return newly constructed index */
107 coloridx::coloridx(unsigned v, bool cov) : idx(v,cov)
108 {
109         debugmsg("coloridx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
110         tinfo_key=TINFO_coloridx;
111 }
112
113 //////////
114 // archiving
115 //////////
116
117 /** Construct object from archive_node. */
118 coloridx::coloridx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
119 {
120         debugmsg("coloridx constructor from archive_node", LOGLEVEL_CONSTRUCT);
121 }
122
123 /** Unarchive the object. */
124 ex coloridx::unarchive(const archive_node &n, const lst &sym_lst)
125 {
126         return (new coloridx(n, sym_lst))->setflag(status_flags::dynallocated);
127 }
128
129 /** Archive the object. */
130 void coloridx::archive(archive_node &n) const
131 {
132         inherited::archive(n);
133 }
134
135 //////////
136 // functions overriding virtual functions from bases classes
137 //////////
138
139 // public
140
141 void coloridx::printraw(std::ostream & os) const
142 {
143         debugmsg("coloridx printraw",LOGLEVEL_PRINT);
144
145         os << "coloridx(";
146
147         if (symbolic) {
148                 os << "symbolic,name=" << name;
149         } else {
150                 os << "non symbolic,value=" << value;
151         }
152
153         if (covariant) {
154                 os << ",covariant";
155         } else {
156                 os << ",contravariant";
157         }
158
159         os << ",serial=" << serial;
160         os << ",hash=" << hashvalue << ",flags=" << flags;
161         os << ")";
162 }
163
164 void coloridx::printtree(std::ostream & os, unsigned indent) const
165 {
166         debugmsg("coloridx printtree",LOGLEVEL_PRINT);
167
168         os << std::string(indent,' ') << "coloridx: ";
169
170         if (symbolic) {
171                 os << "symbolic,name=" << name;
172         } else {
173                 os << "non symbolic,value=" << value;
174         }
175
176         if (covariant) {
177                 os << ",covariant";
178         } else {
179                 os << ",contravariant";
180         }
181
182         os << ", serial=" << serial
183            << ", hash=" << hashvalue
184            << " (0x" << std::hex << hashvalue << std::dec << ")"
185            << ", flags=" << flags << std::endl;
186 }
187
188 void coloridx::print(std::ostream & os, unsigned upper_precedence) const
189 {
190         debugmsg("coloridx print",LOGLEVEL_PRINT);
191
192         if (covariant) {
193                 os << "_";
194         } else {
195                 os << "~";
196         }
197         if (symbolic) {
198                 os << name;
199         } else {
200                 os << value;
201         }
202 }
203
204 bool coloridx::info(unsigned inf) const
205 {
206         if (inf==info_flags::coloridx) return true;
207         return idx::info(inf);
208 }
209
210 int coloridx::compare_same_type(const basic & other) const
211 {
212         return inherited::compare_same_type(other);
213 }
214
215 #ifndef NO_NAMESPACE_GINAC
216 } // namespace GiNaC
217 #endif // ndef NO_NAMESPACE_GINAC