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