Salome HOME
PR: manual correction while merge PAL_OCC to HEAD
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_TableOfRealAttribute.cxx
1 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SALOMEDS_TableOfRealAttribute.cxx
8 //  Author : Michael Ponikarov
9 //  Module : SALOME
10 //  $Header$
11
12 using namespace std;
13 #include <SALOMEDS_TableOfRealAttribute.ixx>
14 #include <Standard_Failure.hxx>
15 #include <TColStd_DataMapIteratorOfDataMapOfIntegerReal.hxx>
16
17 #include <string>
18 #include <stdio.h>
19
20 const Standard_GUID& SALOMEDS_TableOfRealAttribute::GetID() 
21 {
22   static Standard_GUID SALOMEDS_TableOfRealAttributeID ("128371A1-8F52-11d6-A8A3-0001021E8C7F");
23   return SALOMEDS_TableOfRealAttributeID;
24 }
25
26 Handle(SALOMEDS_TableOfRealAttribute) SALOMEDS_TableOfRealAttribute::Set(const TDF_Label& label) 
27 {
28   Handle(SALOMEDS_TableOfRealAttribute) anAttr;
29   if (!label.FindAttribute(SALOMEDS_TableOfRealAttribute::GetID(),anAttr)) {
30     anAttr = new SALOMEDS_TableOfRealAttribute();
31     label.AddAttribute(anAttr);
32   }
33   return anAttr;
34 }
35
36 SALOMEDS_TableOfRealAttribute::SALOMEDS_TableOfRealAttribute() {
37   myRows = new TColStd_HSequenceOfExtendedString();
38   myCols = new TColStd_HSequenceOfExtendedString();
39   myNbRows = 0;
40   myNbColumns = 0;
41 }
42
43 void SALOMEDS_TableOfRealAttribute::SetNbColumns(const Standard_Integer theNbColumns)
44 {
45   Backup();
46   
47   TColStd_DataMapOfIntegerReal aMap;
48   aMap = myTable;
49   myTable.Clear();
50
51   TColStd_DataMapIteratorOfDataMapOfIntegerReal anIterator(aMap);
52   for(; anIterator.More(); anIterator.Next()) {
53     int aRow = (int)(anIterator.Key()/myNbColumns) + 1;
54     int aCol = (int)(anIterator.Key() - myNbColumns*(aRow-1));
55     if(aCol == 0) { aCol = myNbColumns; aRow--; }
56     if(aCol > theNbColumns) continue;
57     int aKey = (aRow-1)*theNbColumns+aCol;
58     myTable.Bind(aKey, anIterator.Value());
59   }
60
61   myNbColumns = theNbColumns;
62
63   while (myCols->Length() < myNbColumns) { // append empty columns titles
64     myCols->Append(TCollection_ExtendedString(""));
65   }
66 }
67
68 void SALOMEDS_TableOfRealAttribute::SetTitle(const TCollection_ExtendedString& theTitle) {
69   Backup();
70   myTitle = theTitle;
71 }
72
73 TCollection_ExtendedString SALOMEDS_TableOfRealAttribute::GetTitle() const {
74   return myTitle;
75 }
76
77 void SALOMEDS_TableOfRealAttribute::SetRowData(const Standard_Integer theRow,
78                                                   const Handle(TColStd_HSequenceOfReal)& theData) 
79 {
80   if(theData->Length() > myNbColumns) SetNbColumns(theData->Length());
81
82   Backup();
83
84   while (myRows->Length() < theRow) { // append new row titles
85     myRows->Append(TCollection_ExtendedString(""));
86   }
87
88   Standard_Integer i, aShift = (theRow-1)*myNbColumns, aLength = theData->Length();
89   for(i = 1; i <= aLength; i++) {
90     myTable.Bind(aShift + i, theData->Value(i));
91   }
92
93   if(theRow > myNbRows) myNbRows = theRow;
94 }
95
96 Handle(TColStd_HSequenceOfReal) SALOMEDS_TableOfRealAttribute::GetRowData(const Standard_Integer theRow)
97 {
98   Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal();
99   Standard_Integer i, aShift = (theRow-1)*myNbColumns;
100   for(i = 1; i <= myNbColumns; i++) {
101      if(myTable.IsBound(aShift+i)) 
102        aSeq->Append(myTable.Find(aShift+i));
103      else
104        aSeq->Append(0.);
105   }
106   
107   return aSeq;
108 }
109
110 void SALOMEDS_TableOfRealAttribute::SetRowTitle(const Standard_Integer theRow,
111                                                    const TCollection_ExtendedString& theTitle) {
112   Backup();
113   myRows->SetValue(theRow,theTitle);
114 }
115
116 TCollection_ExtendedString SALOMEDS_TableOfRealAttribute::GetRowTitle(const Standard_Integer theRow) const {
117   return myRows->Value(theRow);
118 }
119
120
121 void SALOMEDS_TableOfRealAttribute::SetColumnData(const Standard_Integer theColumn,
122                                                      const Handle(TColStd_HSequenceOfReal)& theData) 
123 {
124   if(theColumn > myNbColumns) SetNbColumns(theColumn);
125
126   Backup();
127
128   Standard_Integer i, aLength = theData->Length();
129   for(i = 1; i <= aLength; i++) {
130     myTable.Bind(myNbColumns*(i-1)+theColumn, theData->Value(i));
131   }
132
133   if(aLength > myNbRows) {
134     myNbRows = aLength;
135     while (myRows->Length() < myNbRows) { // append empty row titles
136       myRows->Append(TCollection_ExtendedString(""));
137     }
138   }
139 }
140
141
142 Handle(TColStd_HSequenceOfReal) SALOMEDS_TableOfRealAttribute::GetColumnData(const Standard_Integer theColumn)
143 {
144   Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal;
145   
146   Standard_Integer i, anIndex;
147   for(i = 1; i <= myNbRows; i++) {
148     anIndex = myNbColumns*(i-1) + theColumn;
149     if(myTable.IsBound(anIndex)) 
150       aSeq->Append(myTable.Find(anIndex));
151     else
152       aSeq->Append(0.);
153   }
154   
155   return aSeq;
156 }
157
158 void SALOMEDS_TableOfRealAttribute::SetColumnTitle(const Standard_Integer theColumn,
159                                                       const TCollection_ExtendedString& theTitle) {
160   Backup();
161   while(myCols->Length() < theColumn) myCols->Append(TCollection_ExtendedString(""));
162   myCols->SetValue(theColumn,theTitle);
163 }
164
165 TCollection_ExtendedString SALOMEDS_TableOfRealAttribute::GetColumnTitle(const Standard_Integer theColumn) const {
166   if(myCols.IsNull()) return "";
167   if(myCols->Length() < theColumn) return "";
168   return myCols->Value(theColumn);
169 }
170
171
172 Standard_Integer SALOMEDS_TableOfRealAttribute::GetNbRows() const
173 {
174   return myNbRows;
175 }
176
177 Standard_Integer SALOMEDS_TableOfRealAttribute::GetNbColumns() const
178 {
179   return myNbColumns;
180 }
181
182 void SALOMEDS_TableOfRealAttribute::PutValue(const Standard_Real theValue,
183                                              const Standard_Integer theRow,
184                                              const Standard_Integer theColumn) 
185 {
186   if(theColumn > myNbColumns) SetNbColumns(theColumn);
187
188   Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
189   myTable.Bind(anIndex, theValue);
190
191   if(theRow > myNbRows) {
192     while (myRows->Length() < theRow) { // append empty row titles
193       myRows->Append(TCollection_ExtendedString(""));
194     }
195     myNbRows = theRow;
196   }
197 }
198
199 Standard_Boolean SALOMEDS_TableOfRealAttribute::HasValue(const Standard_Integer theRow,
200                                                          const Standard_Integer theColumn) 
201 {
202   Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
203   return myTable.IsBound(anIndex); 
204 }
205
206 Standard_Real SALOMEDS_TableOfRealAttribute::GetValue(const Standard_Integer theRow,
207                                                       const Standard_Integer theColumn) 
208 {
209   Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
210   if(myTable.IsBound(anIndex)) return myTable.Find(anIndex);
211   
212   Standard_Failure::Raise("Invalid cell index");
213   return 0.;
214 }
215
216 const Standard_GUID& SALOMEDS_TableOfRealAttribute::ID() const
217 {
218   return GetID();
219 }
220
221 void SALOMEDS_TableOfRealAttribute::Restore(const Handle(TDF_Attribute)& with) 
222 {
223   Standard_Integer anIndex;
224   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(with);
225
226   myTable.Clear();
227   myCols->Clear();
228   myRows->Clear();
229
230   myTable = aTable->myTable;
231   myNbRows = aTable->myNbRows;
232   myNbColumns = aTable->myNbColumns;
233   myTitle = aTable->myTitle;
234   
235   for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
236     myRows->Append(aTable->GetRowTitle(anIndex));
237
238   for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++) 
239     myCols->Append(aTable->GetColumnTitle(anIndex));
240 }
241
242 Handle(TDF_Attribute) SALOMEDS_TableOfRealAttribute::NewEmpty() const
243 {
244   return new SALOMEDS_TableOfRealAttribute();
245 }
246
247 void SALOMEDS_TableOfRealAttribute::Paste(const Handle(TDF_Attribute)& into,
248                                              const Handle(TDF_RelocationTable)&) const
249 {
250   Standard_Integer anIndex;
251   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(into);
252
253   aTable->myTable.Clear();
254   aTable->myCols->Clear();
255   aTable->myRows->Clear();
256
257   aTable->myTable = myTable;
258   aTable->myTitle = myTitle;
259   aTable->myNbRows = myNbRows;
260   aTable->myNbColumns = myNbColumns;
261
262   for(anIndex = 1; anIndex <= GetNbRows();anIndex++)
263     aTable->myRows->Append(GetRowTitle(anIndex));
264   for(anIndex = 1; anIndex <= GetNbColumns(); anIndex++) 
265     aTable->myCols->Append(GetColumnTitle(anIndex));
266 }
267
268
269 Handle_TColStd_HSequenceOfInteger SALOMEDS_TableOfRealAttribute::GetSetRowIndices(const Standard_Integer theRow)
270 {
271   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
272
273   Standard_Integer i, aShift = myNbColumns*(theRow-1);
274   for(i = 1; i <= myNbColumns; i++) {
275     if(myTable.IsBound(aShift + i)) aSeq->Append(i);
276   }
277   
278   return aSeq;
279 }
280
281 Handle_TColStd_HSequenceOfInteger SALOMEDS_TableOfRealAttribute::GetSetColumnIndices(const Standard_Integer theColumn)
282 {
283   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
284
285   Standard_Integer i, anIndex;
286   for(i = 1; i <= myNbRows; i++) {
287     anIndex = myNbColumns*(i-1)+theColumn;
288     if(myTable.IsBound(anIndex)) aSeq->Append(i);
289   }
290   
291   return aSeq;
292 }
293
294
295
296 void SALOMEDS_TableOfRealAttribute::ConvertToString(ostrstream& theStream)
297 {
298   int i, j, l;
299
300   
301   //Title
302   l = myTitle.Length();
303   theStream << l << "\n";
304   for(i=1; i<=l; i++)
305     theStream << myTitle.Value(i) << "\n";
306
307   //Nb rows
308   theStream << myNbRows << "\n";
309
310   //Rows titles
311   for(i=1; i<=myNbRows; i++) {
312     l = myRows->Value(i).Length();
313     theStream << l << "\n";
314     for(j=1; j<=l; j++)
315       theStream << myRows->Value(i).Value(j) << "\n";
316   }
317
318   //Nb columns
319   theStream << myNbColumns << "\n";
320
321   //Columns titles
322   for(i=1; i<=myNbColumns; i++) {
323     l = myCols->Value(i).Length();
324     theStream << l << "\n";
325     for(j=1; j<=l; j++)
326       theStream << myCols->Value(i).Value(j) << "\n";
327   }
328
329   //Store the table values
330   l = myTable.Extent();
331   theStream << l << "\n";
332   char *aBuffer = new char[128];
333   TColStd_DataMapIteratorOfDataMapOfIntegerReal anIterator(myTable);
334   for(; anIterator.More(); anIterator.Next()) {
335     theStream << anIterator.Key() << "\n";
336     sprintf(aBuffer, "%.64e", anIterator.Value());
337     theStream << aBuffer << "\n";
338   }
339   
340   delete []aBuffer;
341
342   return;
343 }
344
345 bool SALOMEDS_TableOfRealAttribute::RestoreFromString(istrstream& theStream)
346 {
347   Backup();
348
349   int i, j, l;
350
351   Standard_ExtCharacter anExtChar;
352   TCollection_ExtendedString aStr;
353
354   //Title
355   theStream >> l;
356
357   myTitle = TCollection_ExtendedString(l, 0);
358   for(i=1; i<=l; i++) {
359     theStream >> anExtChar;
360     myTitle.SetValue(i, anExtChar);
361   }
362
363   //Nb rows
364   theStream >> myNbRows;
365
366   //Rows titles
367   myRows->Clear();  
368   for(i=1; i<=myNbRows; i++) { 
369     theStream >> l;
370     aStr = TCollection_ExtendedString(l,0);
371     for(j=1; j<=l; j++) {
372       theStream >> anExtChar;
373       aStr.SetValue(j, anExtChar);
374     }
375     myRows->Append(aStr);
376   }
377
378   //Nb columns
379   theStream >> myNbColumns;
380
381   //Columns titles
382   myCols->Clear();
383   for(i=1; i<=myNbColumns; i++) {
384     theStream >> l;
385     aStr = TCollection_ExtendedString(l,0);
386     for(j=1; j<=l; j++) {
387       theStream >> anExtChar;
388       aStr.SetValue(j, anExtChar);
389     }
390     myCols->Append(aStr);
391   }
392
393   //Restore the table values
394   theStream >> l;
395   myTable.Clear();
396   for(i=1; i<=l; i++) {
397     Standard_Integer aKey;
398     Standard_Real aValue;
399     theStream >> aKey;
400     theStream >> aValue;
401     myTable.Bind(aKey, aValue);
402   }
403
404   return true;
405 }