1 // SALOME SALOMEDS : data structure of SALOME and sources of Salome data server
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
5 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
7 // File : SALOMEDS_TableOfRealAttribute.cxx
8 // Author : Michael Ponikarov
12 #include <SALOMEDS_TableOfRealAttribute.ixx>
13 #include <Standard_Failure.hxx>
14 #include <TColStd_DataMapIteratorOfDataMapOfIntegerReal.hxx>
20 const Standard_GUID& SALOMEDS_TableOfRealAttribute::GetID()
22 static Standard_GUID SALOMEDS_TableOfRealAttributeID ("128371A1-8F52-11d6-A8A3-0001021E8C7F");
23 return SALOMEDS_TableOfRealAttributeID;
26 Handle(SALOMEDS_TableOfRealAttribute) SALOMEDS_TableOfRealAttribute::Set(const TDF_Label& label)
28 Handle(SALOMEDS_TableOfRealAttribute) anAttr;
29 if (!label.FindAttribute(SALOMEDS_TableOfRealAttribute::GetID(),anAttr)) {
30 anAttr = new SALOMEDS_TableOfRealAttribute();
31 label.AddAttribute(anAttr);
36 SALOMEDS_TableOfRealAttribute::SALOMEDS_TableOfRealAttribute() {
37 myRows = new TColStd_HSequenceOfExtendedString();
38 myCols = new TColStd_HSequenceOfExtendedString();
43 void SALOMEDS_TableOfRealAttribute::SetNbColumns(const Standard_Integer theNbColumns)
47 TColStd_DataMapOfIntegerReal aMap;
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());
61 myNbColumns = theNbColumns;
63 while (myCols->Length() < myNbColumns) { // append empty columns titles
64 myCols->Append(TCollection_ExtendedString(""));
68 void SALOMEDS_TableOfRealAttribute::SetTitle(const TCollection_ExtendedString& theTitle) {
73 TCollection_ExtendedString SALOMEDS_TableOfRealAttribute::GetTitle() const {
77 void SALOMEDS_TableOfRealAttribute::SetRowData(const Standard_Integer theRow,
78 const Handle(TColStd_HSequenceOfReal)& theData)
80 if(theData->Length() > myNbColumns) SetNbColumns(theData->Length());
84 while (myRows->Length() < theRow) { // append new row titles
85 myRows->Append(TCollection_ExtendedString(""));
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));
93 if(theRow > myNbRows) myNbRows = theRow;
96 Handle(TColStd_HSequenceOfReal) SALOMEDS_TableOfRealAttribute::GetRowData(const Standard_Integer theRow)
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));
110 void SALOMEDS_TableOfRealAttribute::SetRowTitle(const Standard_Integer theRow,
111 const TCollection_ExtendedString& theTitle) {
113 myRows->SetValue(theRow,theTitle);
116 TCollection_ExtendedString SALOMEDS_TableOfRealAttribute::GetRowTitle(const Standard_Integer theRow) const {
117 return myRows->Value(theRow);
121 void SALOMEDS_TableOfRealAttribute::SetColumnData(const Standard_Integer theColumn,
122 const Handle(TColStd_HSequenceOfReal)& theData)
124 if(theColumn > myNbColumns) SetNbColumns(theColumn);
128 Standard_Integer i, aLength = theData->Length();
129 for(i = 1; i <= aLength; i++) {
130 myTable.Bind(myNbColumns*(i-1)+theColumn, theData->Value(i));
133 if(aLength > myNbRows) {
135 while (myRows->Length() < myNbRows) { // append empty row titles
136 myRows->Append(TCollection_ExtendedString(""));
142 Handle(TColStd_HSequenceOfReal) SALOMEDS_TableOfRealAttribute::GetColumnData(const Standard_Integer theColumn)
144 Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal;
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));
158 void SALOMEDS_TableOfRealAttribute::SetColumnTitle(const Standard_Integer theColumn,
159 const TCollection_ExtendedString& theTitle) {
161 while(myCols->Length() < theColumn) myCols->Append(TCollection_ExtendedString(""));
162 myCols->SetValue(theColumn,theTitle);
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);
172 Standard_Integer SALOMEDS_TableOfRealAttribute::GetNbRows() const
177 Standard_Integer SALOMEDS_TableOfRealAttribute::GetNbColumns() const
182 void SALOMEDS_TableOfRealAttribute::PutValue(const Standard_Real theValue,
183 const Standard_Integer theRow,
184 const Standard_Integer theColumn)
186 if(theColumn > myNbColumns) SetNbColumns(theColumn);
188 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
189 myTable.Bind(anIndex, theValue);
191 if(theRow > myNbRows) {
192 while (myRows->Length() < theRow) { // append empty row titles
193 myRows->Append(TCollection_ExtendedString(""));
199 Standard_Boolean SALOMEDS_TableOfRealAttribute::HasValue(const Standard_Integer theRow,
200 const Standard_Integer theColumn)
202 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
203 return myTable.IsBound(anIndex);
206 Standard_Real SALOMEDS_TableOfRealAttribute::GetValue(const Standard_Integer theRow,
207 const Standard_Integer theColumn)
209 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
210 if(myTable.IsBound(anIndex)) return myTable.Find(anIndex);
212 Standard_Failure::Raise("Invalid cell index");
216 const Standard_GUID& SALOMEDS_TableOfRealAttribute::ID() const
221 void SALOMEDS_TableOfRealAttribute::Restore(const Handle(TDF_Attribute)& with)
223 Standard_Integer anIndex;
224 Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(with);
230 myTable = aTable->myTable;
231 myNbRows = aTable->myNbRows;
232 myNbColumns = aTable->myNbColumns;
233 myTitle = aTable->myTitle;
235 for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
236 myRows->Append(aTable->GetRowTitle(anIndex));
238 for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++)
239 myCols->Append(aTable->GetColumnTitle(anIndex));
242 Handle(TDF_Attribute) SALOMEDS_TableOfRealAttribute::NewEmpty() const
244 return new SALOMEDS_TableOfRealAttribute();
247 void SALOMEDS_TableOfRealAttribute::Paste(const Handle(TDF_Attribute)& into,
248 const Handle(TDF_RelocationTable)&) const
250 Standard_Integer anIndex;
251 Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(into);
253 aTable->myTable.Clear();
254 aTable->myCols->Clear();
255 aTable->myRows->Clear();
257 aTable->myTable = myTable;
258 aTable->myTitle = myTitle;
259 aTable->myNbRows = myNbRows;
260 aTable->myNbColumns = myNbColumns;
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));
269 Handle_TColStd_HSequenceOfInteger SALOMEDS_TableOfRealAttribute::GetSetRowIndices(const Standard_Integer theRow)
271 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
273 Standard_Integer i, aShift = myNbColumns*(theRow-1);
274 for(i = 1; i <= myNbColumns; i++) {
275 if(myTable.IsBound(aShift + i)) aSeq->Append(i);
281 Handle_TColStd_HSequenceOfInteger SALOMEDS_TableOfRealAttribute::GetSetColumnIndices(const Standard_Integer theColumn)
283 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
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);
296 void SALOMEDS_TableOfRealAttribute::ConvertToString(ostrstream& theStream)
302 l = myTitle.Length();
303 theStream << l << "\n";
305 theStream << myTitle.Value(i) << "\n";
308 theStream << myNbRows << "\n";
311 for(i=1; i<=myNbRows; i++) {
312 l = myRows->Value(i).Length();
313 theStream << l << "\n";
315 theStream << myRows->Value(i).Value(j) << "\n";
319 theStream << myNbColumns << "\n";
322 for(i=1; i<=myNbColumns; i++) {
323 l = myCols->Value(i).Length();
324 theStream << l << "\n";
326 theStream << myCols->Value(i).Value(j) << "\n";
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";
345 bool SALOMEDS_TableOfRealAttribute::RestoreFromString(istrstream& theStream)
351 Standard_ExtCharacter anExtChar;
352 TCollection_ExtendedString aStr;
357 myTitle = TCollection_ExtendedString(l, 0);
358 for(i=1; i<=l; i++) {
359 theStream >> anExtChar;
360 myTitle.SetValue(i, anExtChar);
364 theStream >> myNbRows;
368 for(i=1; i<=myNbRows; i++) {
370 aStr = TCollection_ExtendedString(l,0);
371 for(j=1; j<=l; j++) {
372 theStream >> anExtChar;
373 aStr.SetValue(j, anExtChar);
375 myRows->Append(aStr);
379 theStream >> myNbColumns;
383 for(i=1; i<=myNbColumns; i++) {
385 aStr = TCollection_ExtendedString(l,0);
386 for(j=1; j<=l; j++) {
387 theStream >> anExtChar;
388 aStr.SetValue(j, anExtChar);
390 myCols->Append(aStr);
393 //Restore the table values
396 for(i=1; i<=l; i++) {
397 Standard_Integer aKey;
398 Standard_Real aValue;
401 myTable.Bind(aKey, aValue);