- modified GiNaC headers to Alexander's liking
[ginac.git] / ginac / indexed.cpp
1 /** @file indexed.cpp
2  *
3  *  Implementation of GiNaC's index carrying objects.
4  *
5  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <string>
23
24 #include "indexed.h"
25 #include "ex.h"
26 #include "idx.h"
27
28 //////////
29 // default constructor, destructor, copy constructor assignment operator and helpers
30 //////////
31
32 // public
33
34 indexed::indexed()
35 {
36     debugmsg("indexed default constructor",LOGLEVEL_CONSTRUCT);
37     tinfo_key=TINFO_indexed;
38 }
39
40 indexed::~indexed()
41 {
42     debugmsg("indexed destructor",LOGLEVEL_DESTRUCT);
43     destroy(0);
44 }
45
46 indexed::indexed(indexed const & other)
47 {
48     debugmsg("indexed copy constructor",LOGLEVEL_CONSTRUCT);
49     copy (other);
50 }
51
52 indexed const & indexed::operator=(indexed const & other)
53 {
54     debugmsg("indexed operator=",LOGLEVEL_ASSIGNMENT);
55     if (this != &other) {
56         destroy(1);
57         copy(other);
58     }
59     return *this;
60 }
61
62 // protected
63
64 void indexed::copy(indexed const & other)
65 {
66     exprseq::copy(other);
67 }
68
69 void indexed::destroy(bool call_parent)
70 {
71     if (call_parent) {
72         exprseq::destroy(call_parent);
73     }
74 }
75
76 //////////
77 // other constructors
78 //////////
79
80 // public
81
82 indexed::indexed(ex const & i1) : exprseq(i1)
83 {
84     debugmsg("indexed constructor from ex",LOGLEVEL_CONSTRUCT);
85     tinfo_key=TINFO_indexed;
86     ASSERT(all_of_type_idx());
87 }
88
89 indexed::indexed(ex const & i1, ex const & i2) : exprseq(i1,i2)
90 {
91     debugmsg("indexed constructor from ex,ex",LOGLEVEL_CONSTRUCT);
92     tinfo_key=TINFO_indexed;
93     ASSERT(all_of_type_idx());
94 }
95
96 indexed::indexed(ex const & i1, ex const & i2, ex const & i3)
97     : exprseq(i1,i2,i3)
98 {
99     debugmsg("indexed constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
100     tinfo_key=TINFO_indexed;
101     ASSERT(all_of_type_idx());
102 }
103
104 indexed::indexed(exvector const & iv) : exprseq(iv)
105 {
106     debugmsg("indexed constructor from exvector",LOGLEVEL_CONSTRUCT);
107     tinfo_key=TINFO_indexed;
108     ASSERT(all_of_type_idx());
109 }
110
111 indexed::indexed(exvector * ivp) : exprseq(ivp)
112 {
113     debugmsg("indexed constructor from exvector *",LOGLEVEL_CONSTRUCT);
114     tinfo_key=TINFO_indexed;
115     ASSERT(all_of_type_idx());
116 }
117
118 //////////
119 // functions overriding virtual functions from bases classes
120 //////////
121
122 // public
123
124 basic * indexed::duplicate() const
125 {
126     debugmsg("indexed duplicate",LOGLEVEL_DUPLICATE);
127     return new indexed(*this);
128 }
129
130 void indexed::printraw(ostream & os) const
131 {
132     debugmsg("indexed printraw",LOGLEVEL_PRINT);
133     os << "indexed(indices=";
134     printrawindices(os);
135     os << ",hash=" << hashvalue << ",flags=" << flags << ")";
136 }
137
138 void indexed::printtree(ostream & os, unsigned indent) const
139 {
140     debugmsg("indexed printtree",LOGLEVEL_PRINT);
141     os << string(indent,' ') << "indexed: " << seq.size() << " indices";
142     os << ",hash=" << hashvalue << ",flags=" << flags << endl;
143     printtreeindices(os,indent);
144 }
145
146 void indexed::print(ostream & os, unsigned upper_precedence) const
147 {
148     debugmsg("indexed print",LOGLEVEL_PRINT);
149     os << "UNNAMEDINDEX";
150     printindices(os);
151 }
152
153 void indexed::printcsrc(ostream & os, unsigned type,
154                         unsigned upper_precedence) const
155 {
156     debugmsg("indexed print csrc",LOGLEVEL_PRINT);
157     print(os,upper_precedence);
158 }
159
160 bool indexed::info(unsigned inf) const
161 {
162     if (inf==info_flags::indexed) return true;
163     if (inf==info_flags::has_indices) return seq.size()!=0;
164     return exprseq::info(inf);
165 }
166
167 exvector indexed::get_indices(void) const
168 {
169     return seq;
170
171     /*
172     idxvector filtered_indices;
173     filtered_indices.reserve(indices.size());
174     for (idxvector::const_iterator cit=indices.begin(); cit!=indices.end(); ++cit) {
175         if ((*cit).get_type()==t) {
176             filtered_indices.push_back(*cit);
177         }
178     }
179     return filtered_indices;
180     */
181 }
182
183 // protected
184
185 int indexed::compare_same_type(basic const & other) const
186 {
187     ASSERT(is_of_type(other,indexed));
188     return exprseq::compare_same_type(other);
189 }
190
191 bool indexed::is_equal_same_type(basic const & other) const
192 {
193     ASSERT(is_of_type(other,indexed));
194     return exprseq::is_equal_same_type(other);
195 }
196
197 unsigned indexed::return_type(void) const
198 {
199     return return_types::noncommutative;
200 }
201    
202 unsigned indexed::return_type_tinfo(void) const
203 {
204     return tinfo_key;
205 }
206
207 ex indexed::thisexprseq(exvector const & v) const
208 {
209     return indexed(v);
210 }
211
212 ex indexed::thisexprseq(exvector * vp) const
213 {
214     return indexed(vp);
215 }
216
217 //////////
218 // virtual functions which can be overridden by derived classes
219 //////////
220
221 // none
222
223 //////////
224 // non-virtual functions in this class
225 //////////
226
227 // protected
228
229 void indexed::printrawindices(ostream & os) const
230 {
231     if (seq.size()!=0) {
232         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
233             (*cit).printraw(os);
234             os << ",";
235         }
236     }
237 }
238
239 void indexed::printtreeindices(ostream & os, unsigned indent) const
240 {
241     if (seq.size()!=0) {
242         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
243             os << string(indent+delta_indent,' ');
244             (*cit).printraw(os);
245             os << endl;
246         }
247     }
248 }
249
250 void indexed::printindices(ostream & os) const
251 {
252     if (seq.size()!=0) {
253         if (seq.size()>1) {
254             os << "{";
255         }
256         exvector::const_iterator last=seq.end()-1;
257         exvector::const_iterator cit=seq.begin();
258         for (; cit!=last; ++cit) {
259             (*cit).print(os);
260             os << ",";
261         }
262         (*cit).print(os);
263         if (seq.size()>1) {
264             os << "}";
265         }
266     }
267 }
268
269 bool indexed::all_of_type_idx(void) const
270 {
271     // used only inside of ASSERTs
272     for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
273         if (!is_ex_of_type(*cit,idx)) return false;
274     }
275     return true;
276 }
277
278 //////////
279 // static member variables
280 //////////
281
282 // none
283
284 //////////
285 // global constants
286 //////////
287
288 const indexed some_indexed;
289 type_info const & typeid_indexed=typeid(some_indexed);
290