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