#ifndef around namespace GiNaC { }
[ginac.git] / ginac / indexed.cpp
1 /** @file indexed.cpp
2  *
3  *  Implementation of GiNaC's index carrying objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999 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 <string>
24
25 #include "indexed.h"
26 #include "ex.h"
27 #include "idx.h"
28 #include "debugmsg.h"
29
30 #ifndef NO_GINAC_NAMESPACE
31 namespace GiNaC {
32 #endif // ndef NO_GINAC_NAMESPACE
33
34 //////////
35 // default constructor, destructor, copy constructor assignment operator and helpers
36 //////////
37
38 // public
39
40 indexed::indexed()
41 {
42     debugmsg("indexed default constructor",LOGLEVEL_CONSTRUCT);
43     tinfo_key=TINFO_indexed;
44 }
45
46 indexed::~indexed()
47 {
48     debugmsg("indexed destructor",LOGLEVEL_DESTRUCT);
49     destroy(0);
50 }
51
52 indexed::indexed(indexed const & other)
53 {
54     debugmsg("indexed copy constructor",LOGLEVEL_CONSTRUCT);
55     copy (other);
56 }
57
58 indexed const & indexed::operator=(indexed const & other)
59 {
60     debugmsg("indexed operator=",LOGLEVEL_ASSIGNMENT);
61     if (this != &other) {
62         destroy(1);
63         copy(other);
64     }
65     return *this;
66 }
67
68 // protected
69
70 void indexed::copy(indexed const & other)
71 {
72     exprseq::copy(other);
73 }
74
75 void indexed::destroy(bool call_parent)
76 {
77     if (call_parent) {
78         exprseq::destroy(call_parent);
79     }
80 }
81
82 //////////
83 // other constructors
84 //////////
85
86 // public
87
88 indexed::indexed(ex const & i1) : exprseq(i1)
89 {
90     debugmsg("indexed constructor from ex",LOGLEVEL_CONSTRUCT);
91     tinfo_key=TINFO_indexed;
92     GINAC_ASSERT(all_of_type_idx());
93 }
94
95 indexed::indexed(ex const & i1, ex const & i2) : exprseq(i1,i2)
96 {
97     debugmsg("indexed constructor from ex,ex",LOGLEVEL_CONSTRUCT);
98     tinfo_key=TINFO_indexed;
99     GINAC_ASSERT(all_of_type_idx());
100 }
101
102 indexed::indexed(ex const & i1, ex const & i2, ex const & i3)
103     : exprseq(i1,i2,i3)
104 {
105     debugmsg("indexed constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
106     tinfo_key=TINFO_indexed;
107     GINAC_ASSERT(all_of_type_idx());
108 }
109
110 indexed::indexed(exvector const & iv) : exprseq(iv)
111 {
112     debugmsg("indexed constructor from exvector",LOGLEVEL_CONSTRUCT);
113     tinfo_key=TINFO_indexed;
114     GINAC_ASSERT(all_of_type_idx());
115 }
116
117 indexed::indexed(exvector * ivp) : exprseq(ivp)
118 {
119     debugmsg("indexed constructor from exvector *",LOGLEVEL_CONSTRUCT);
120     tinfo_key=TINFO_indexed;
121     GINAC_ASSERT(all_of_type_idx());
122 }
123
124 //////////
125 // functions overriding virtual functions from bases classes
126 //////////
127
128 // public
129
130 basic * indexed::duplicate() const
131 {
132     debugmsg("indexed duplicate",LOGLEVEL_DUPLICATE);
133     return new indexed(*this);
134 }
135
136 void indexed::printraw(ostream & os) const
137 {
138     debugmsg("indexed printraw",LOGLEVEL_PRINT);
139     os << "indexed(indices=";
140     printrawindices(os);
141     os << ",hash=" << hashvalue << ",flags=" << flags << ")";
142 }
143
144 void indexed::printtree(ostream & os, unsigned indent) const
145 {
146     debugmsg("indexed printtree",LOGLEVEL_PRINT);
147     os << string(indent,' ') << "indexed: " << seq.size() << " indices";
148     os << ",hash=" << hashvalue << ",flags=" << flags << endl;
149     printtreeindices(os,indent);
150 }
151
152 void indexed::print(ostream & os, unsigned upper_precedence) const
153 {
154     debugmsg("indexed print",LOGLEVEL_PRINT);
155     os << "UNNAMEDINDEX";
156     printindices(os);
157 }
158
159 void indexed::printcsrc(ostream & os, unsigned type,
160                         unsigned upper_precedence) const
161 {
162     debugmsg("indexed print csrc",LOGLEVEL_PRINT);
163     print(os,upper_precedence);
164 }
165
166 bool indexed::info(unsigned inf) const
167 {
168     if (inf==info_flags::indexed) return true;
169     if (inf==info_flags::has_indices) return seq.size()!=0;
170     return exprseq::info(inf);
171 }
172
173 exvector indexed::get_indices(void) const
174 {
175     return seq;
176
177     /*
178     idxvector filtered_indices;
179     filtered_indices.reserve(indices.size());
180     for (idxvector::const_iterator cit=indices.begin(); cit!=indices.end(); ++cit) {
181         if ((*cit).get_type()==t) {
182             filtered_indices.push_back(*cit);
183         }
184     }
185     return filtered_indices;
186     */
187 }
188
189 // protected
190
191 int indexed::compare_same_type(basic const & other) const
192 {
193     GINAC_ASSERT(is_of_type(other,indexed));
194     return exprseq::compare_same_type(other);
195 }
196
197 bool indexed::is_equal_same_type(basic const & other) const
198 {
199     GINAC_ASSERT(is_of_type(other,indexed));
200     return exprseq::is_equal_same_type(other);
201 }
202
203 unsigned indexed::return_type(void) const
204 {
205     return return_types::noncommutative;
206 }
207    
208 unsigned indexed::return_type_tinfo(void) const
209 {
210     return tinfo_key;
211 }
212
213 ex indexed::thisexprseq(exvector const & v) const
214 {
215     return indexed(v);
216 }
217
218 ex indexed::thisexprseq(exvector * vp) const
219 {
220     return indexed(vp);
221 }
222
223 //////////
224 // virtual functions which can be overridden by derived classes
225 //////////
226
227 // none
228
229 //////////
230 // non-virtual functions in this class
231 //////////
232
233 // protected
234
235 void indexed::printrawindices(ostream & os) const
236 {
237     if (seq.size()!=0) {
238         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
239             (*cit).printraw(os);
240             os << ",";
241         }
242     }
243 }
244
245 void indexed::printtreeindices(ostream & os, unsigned indent) const
246 {
247     if (seq.size()!=0) {
248         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
249             os << string(indent+delta_indent,' ');
250             (*cit).printraw(os);
251             os << endl;
252         }
253     }
254 }
255
256 void indexed::printindices(ostream & os) const
257 {
258     if (seq.size()!=0) {
259         if (seq.size()>1) {
260             os << "{";
261         }
262         exvector::const_iterator last=seq.end()-1;
263         exvector::const_iterator cit=seq.begin();
264         for (; cit!=last; ++cit) {
265             (*cit).print(os);
266             os << ",";
267         }
268         (*cit).print(os);
269         if (seq.size()>1) {
270             os << "}";
271         }
272     }
273 }
274
275 bool indexed::all_of_type_idx(void) const
276 {
277     // used only inside of ASSERTs
278     for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
279         if (!is_ex_of_type(*cit,idx)) return false;
280     }
281     return true;
282 }
283
284 //////////
285 // static member variables
286 //////////
287
288 // none
289
290 //////////
291 // global constants
292 //////////
293
294 const indexed some_indexed;
295 type_info const & typeid_indexed=typeid(some_indexed);
296
297 #ifndef NO_GINAC_NAMESPACE
298 } // namespace GiNaC
299 #endif // ndef NO_GINAC_NAMESPACE