]> www.ginac.de Git - ginac.git/blob - ginac/coloridx.cpp
221e86b6aeaf261e995d7fb823b25131a8bf81cf
[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 basic * coloridx::duplicate() const
142 {
143         debugmsg("coloridx duplicate",LOGLEVEL_DUPLICATE);
144         return new coloridx(*this);
145 }
146
147 void coloridx::printraw(std::ostream & os) const
148 {
149         debugmsg("coloridx printraw",LOGLEVEL_PRINT);
150
151         os << "coloridx(";
152
153         if (symbolic) {
154                 os << "symbolic,name=" << name;
155         } else {
156                 os << "non symbolic,value=" << value;
157         }
158
159         if (covariant) {
160                 os << ",covariant";
161         } else {
162                 os << ",contravariant";
163         }
164
165         os << ",serial=" << serial;
166         os << ",hash=" << hashvalue << ",flags=" << flags;
167         os << ")";
168 }
169
170 void coloridx::printtree(std::ostream & os, unsigned indent) const
171 {
172         debugmsg("coloridx printtree",LOGLEVEL_PRINT);
173
174         os << std::string(indent,' ') << "coloridx: ";
175
176         if (symbolic) {
177                 os << "symbolic,name=" << name;
178         } else {
179                 os << "non symbolic,value=" << value;
180         }
181
182         if (covariant) {
183                 os << ",covariant";
184         } else {
185                 os << ",contravariant";
186         }
187
188         os << ", serial=" << serial
189            << ", hash=" << hashvalue
190            << " (0x" << std::hex << hashvalue << std::dec << ")"
191            << ", flags=" << flags << std::endl;
192 }
193
194 void coloridx::print(std::ostream & os, unsigned upper_precedence) const
195 {
196         debugmsg("coloridx print",LOGLEVEL_PRINT);
197
198         if (covariant) {
199                 os << "_";
200         } else {
201                 os << "~";
202         }
203         if (symbolic) {
204                 os << name;
205         } else {
206                 os << value;
207         }
208 }
209
210 bool coloridx::info(unsigned inf) const
211 {
212         if (inf==info_flags::coloridx) return true;
213         return idx::info(inf);
214 }
215
216 #ifndef NO_NAMESPACE_GINAC
217 } // namespace GiNaC
218 #endif // ndef NO_NAMESPACE_GINAC