]> www.ginac.de Git - ginac.git/blob - ginac/indexed.cpp
- implemented global class registry (for class basic and derived classes)
[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(indexed const & other)
55 {
56     debugmsg("indexed copy constructor",LOGLEVEL_CONSTRUCT);
57     copy (other);
58 }
59
60 indexed const & indexed::operator=(indexed const & 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(indexed const & 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(ex const & 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(ex const & i1, ex const & 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(ex const & i1, ex const & i2, ex const & 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(ex const & i1, ex const & i2, ex const & i3, ex const & 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(exvector const & 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 }
143
144 /** Unarchive the object. */
145 ex indexed::unarchive(const archive_node &n, const lst &sym_lst)
146 {
147     return (new indexed(n, sym_lst))->setflag(status_flags::dynallocated);
148 }
149
150 /** Archive the object. */
151 void indexed::archive(archive_node &n) const
152 {
153     inherited::archive(n);
154 }
155
156 //////////
157 // functions overriding virtual functions from bases classes
158 //////////
159
160 // public
161
162 basic * indexed::duplicate() const
163 {
164     debugmsg("indexed duplicate",LOGLEVEL_DUPLICATE);
165     return new indexed(*this);
166 }
167
168 void indexed::printraw(ostream & os) const
169 {
170     debugmsg("indexed printraw",LOGLEVEL_PRINT);
171     os << "indexed(indices=";
172     printrawindices(os);
173     os << ",hash=" << hashvalue << ",flags=" << flags << ")";
174 }
175
176 void indexed::printtree(ostream & os, unsigned indent) const
177 {
178     debugmsg("indexed printtree",LOGLEVEL_PRINT);
179     os << string(indent,' ') << "indexed: " << seq.size() << " indices";
180     os << ",hash=" << hashvalue << ",flags=" << flags << endl;
181     printtreeindices(os,indent);
182 }
183
184 void indexed::print(ostream & os, unsigned upper_precedence) const
185 {
186     debugmsg("indexed print",LOGLEVEL_PRINT);
187     os << "UNNAMEDINDEX";
188     printindices(os);
189 }
190
191 void indexed::printcsrc(ostream & os, unsigned type,
192                         unsigned upper_precedence) const
193 {
194     debugmsg("indexed print csrc",LOGLEVEL_PRINT);
195     print(os,upper_precedence);
196 }
197
198 bool indexed::info(unsigned inf) const
199 {
200     if (inf==info_flags::indexed) return true;
201     if (inf==info_flags::has_indices) return seq.size()!=0;
202     return inherited::info(inf);
203 }
204
205 exvector indexed::get_indices(void) const
206 {
207     return seq;
208
209     /*
210     idxvector filtered_indices;
211     filtered_indices.reserve(indices.size());
212     for (idxvector::const_iterator cit=indices.begin(); cit!=indices.end(); ++cit) {
213         if ((*cit).get_type()==t) {
214             filtered_indices.push_back(*cit);
215         }
216     }
217     return filtered_indices;
218     */
219 }
220
221 // protected
222
223 int indexed::compare_same_type(basic const & other) const
224 {
225     GINAC_ASSERT(is_of_type(other,indexed));
226     return inherited::compare_same_type(other);
227 }
228
229 bool indexed::is_equal_same_type(basic const & other) const
230 {
231     GINAC_ASSERT(is_of_type(other,indexed));
232     return inherited::is_equal_same_type(other);
233 }
234
235 unsigned indexed::return_type(void) const
236 {
237     return return_types::noncommutative;
238 }
239    
240 unsigned indexed::return_type_tinfo(void) const
241 {
242     return tinfo_key;
243 }
244
245 ex indexed::thisexprseq(exvector const & v) const
246 {
247     return indexed(v);
248 }
249
250 ex indexed::thisexprseq(exvector * vp) const
251 {
252     return indexed(vp);
253 }
254
255 //////////
256 // virtual functions which can be overridden by derived classes
257 //////////
258
259 // none
260
261 //////////
262 // non-virtual functions in this class
263 //////////
264
265 // protected
266
267 void indexed::printrawindices(ostream & os) const
268 {
269     if (seq.size()!=0) {
270         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
271             (*cit).printraw(os);
272             os << ",";
273         }
274     }
275 }
276
277 void indexed::printtreeindices(ostream & os, unsigned indent) const
278 {
279     if (seq.size()!=0) {
280         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
281             os << string(indent+delta_indent,' ');
282             (*cit).printraw(os);
283             os << endl;
284         }
285     }
286 }
287
288 void indexed::printindices(ostream & os) const
289 {
290     if (seq.size()!=0) {
291         if (seq.size()>1) {
292             os << "{";
293         }
294         exvector::const_iterator last=seq.end()-1;
295         exvector::const_iterator cit=seq.begin();
296         for (; cit!=last; ++cit) {
297             (*cit).print(os);
298             os << ",";
299         }
300         (*cit).print(os);
301         if (seq.size()>1) {
302             os << "}";
303         }
304     }
305 }
306
307 bool indexed::all_of_type_idx(void) const
308 {
309     // used only inside of ASSERTs
310     for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
311         if (!is_ex_of_type(*cit,idx)) return false;
312     }
313     return true;
314 }
315
316 //////////
317 // static member variables
318 //////////
319
320 // none
321
322 //////////
323 // global constants
324 //////////
325
326 const indexed some_indexed;
327 type_info const & typeid_indexed=typeid(some_indexed);
328
329 #ifndef NO_GINAC_NAMESPACE
330 } // namespace GiNaC
331 #endif // ndef NO_GINAC_NAMESPACE