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