Remove useless variables (warnings)
[scilab.git] / scilab / modules / ast / includes / run_MatrixExp.hxx
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2008-2008 - DIGITEO - Antoine ELIAS
4  *
5  *  This file must be used under the terms of the CeCILL.
6  *  This source file is licensed as described in the file COPYING, which
7  *  you should have received as part of this distribution.  The terms
8  *  are also available at
9  *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 // This code is separated in run_MatrixExp.hxx
14 // but will be inlined in runvisitor.hxx
15 // using #include with RunVisitorT class declaration.
16 //
17 // If you need additionnal headers, please add it in runvisitor.hxx
18
19 /*
20   [1,2;3,4] with/without special character $ and :
21 */
22 void visitprivate(const MatrixExp &e)
23 {
24     try
25     {
26         std::list<MatrixLineExp *>::const_iterator row;
27         std::list<Exp *>::const_iterator col;
28         InternalType *poResult = NULL;
29         list<InternalType*> rowList;
30
31         if (e.lines_get().size() == 0)
32         {
33             result_set(Double::Empty());
34             return;
35         }
36
37         //do all [x,x]
38         for (row = e.lines_get().begin() ; row != e.lines_get().end() ; row++)
39         {
40             InternalType* poRow = NULL;
41             for (col = (*row)->columns_get().begin() ; col != (*row)->columns_get().end() ; col++)
42             {
43                 (*col)->accept(*this);
44
45                 InternalType *pIT = result_get();
46                 if (pIT == NULL)
47                 {
48                     continue;
49                 }
50
51                 //reset result but whitout delete the value
52                 result_set(NULL);
53
54                 if (pIT->isGenericType() == false)
55                 {
56                     std::wostringstream os;
57                     os << _W("unable to concatenate\n");
58                     throw ScilabError(os.str(), 999, (*col)->location_get());
59                 }
60
61                 GenericType* pGT = pIT->getAs<GenericType>();
62
63                 if (pGT->isImplicitList() && pGT->getAs<ImplicitList>()->isComputable())
64                 {
65                     ImplicitList *pIL = pGT->getAs<ImplicitList>();
66                     InternalType* pIT2 = pIL->extractFullMatrix();
67                     if (pGT->isDeletable())
68                     {
69                         delete pGT;
70                     }
71                     pGT = pIT2->getAs<GenericType>();
72                 }
73
74                 if (pGT->isDouble() && pGT->getAs<Double>()->isEmpty())
75                 {
76                     continue;
77                 }
78
79                 if (poRow == NULL)
80                 {
81                     //first loop
82                     poRow = pGT;
83                     continue;
84                 }
85
86                 GenericType* pGTResult = poRow->getAs<GenericType>();
87                 //check dimension
88                 if (pGT->getDims() != 2 || pGT->getRows() != pGTResult->getRows())
89                 {
90                     std::wostringstream os;
91                     os << _W("inconsistent row/column dimensions\n");
92                     throw ScilabError(os.str(), 999, (*row)->location_get());
93                 }
94
95                 // if we concatenate [Double Sparse], transform the Double to Sparse and perform [Sparse Sparse]
96                 // this avoids to allocate a Double result of size of Double+Sparse and initialize all elements.
97                 if (pGT->isSparse() && pGTResult->isDouble())
98                 {
99                     poRow = new types::Sparse(*pGTResult->getAs<types::Double>());
100                 }
101                 else if (pGT->isSparseBool() && pGTResult->isBool()) // [Bool SparseBool] => [SparseBool SparseBool]
102                 {
103                     poRow = new types::SparseBool(*pGTResult->getAs<types::Bool>());
104                 }
105
106                 InternalType *p = AddElementToVariable(NULL, poRow, pGTResult->getRows(), pGTResult->getCols() + pGT->getCols());
107                 p = AddElementToVariable(p, pGT, 0, pGTResult->getCols());
108                 if (poRow->isDeletable())
109                 {
110                     delete poRow;
111                 }
112
113                 if (pGT->isDeletable())
114                 {
115                     delete pGT;
116                 }
117                 poRow = p;
118             }
119
120             if (poRow == NULL)
121             {
122                 continue;
123             }
124
125             GenericType* pGT = poRow->getAs<GenericType>();
126             if (poResult == NULL)
127             {
128                 poResult = pGT;
129                 continue;
130             }
131
132             //check dimension
133             GenericType* pGTResult = poResult->getAs<GenericType>();
134             //check dimension
135             if (pGT->getCols() != pGTResult->getCols())
136             {
137                 std::wostringstream os;
138                 os << _W("inconsistent row/column dimensions\n");
139                 throw ScilabError(os.str(), 999, (*e.lines_get().begin())->location_get());
140             }
141
142             // if we concatenate [Double Sparse], transform the Double to Sparse and perform [Sparse Sparse]
143             // this avoids to allocate a Double result of size of Double+Sparse and initialize all elements.
144             if (pGT->isSparse() && pGTResult->isDouble())
145             {
146                 poResult = new types::Sparse(*pGTResult->getAs<types::Double>());
147             }
148             else if (pGT->isSparseBool() && pGTResult->isBool()) // [Bool SparseBool] => [SparseBool SparseBool]
149             {
150                 poResult = new types::SparseBool(*pGTResult->getAs<types::Bool>());
151             }
152
153             InternalType *p = AddElementToVariable(NULL, poResult, pGTResult->getRows() + pGT->getRows(), pGT->getCols());
154             p = AddElementToVariable(p, pGT, pGTResult->getRows(), 0);
155             if (poResult->isDeletable())
156             {
157                 delete poResult;
158             }
159
160             if (pGT->isDeletable())
161             {
162                 delete pGT;
163             }
164
165             poResult = p;
166         }
167
168         if (poResult)
169         {
170             result_set(poResult);
171         }
172         else
173         {
174             result_set(Double::Empty());
175         }
176     }
177     catch (ScilabError error)
178     {
179         throw error;
180     }
181 }