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