1 // SALOME SALOMEDS : data structure of SALOME and sources of Salome data server
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : SALOMEDS_TableOfRealAttribute.cxx
8 // Author : Michael Ponikarov
13 #include <SALOMEDS_TableOfRealAttribute.ixx>
14 #include <Standard_Failure.hxx>
15 #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 SetRowTitle(anIndex,aTable->GetRowTitle(anIndex));
238 for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++)
239 SetColumnTitle(anIndex, 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->SetRowTitle(anIndex,GetRowTitle(anIndex));
265 for(anIndex = 1; anIndex <= GetNbColumns(); anIndex++)
266 aTable->SetColumnTitle(anIndex, GetColumnTitle(anIndex));
270 Handle_TColStd_HSequenceOfInteger SALOMEDS_TableOfRealAttribute::GetSetRowIndices(const Standard_Integer theRow)
272 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
274 Standard_Integer i, aShift = myNbColumns*(theRow-1);
275 for(i = 1; i <= myNbColumns; i++) {
276 if(myTable.IsBound(aShift + i)) aSeq->Append(i);
282 Handle_TColStd_HSequenceOfInteger SALOMEDS_TableOfRealAttribute::GetSetColumnIndices(const Standard_Integer theColumn)
284 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
286 Standard_Integer i, anIndex;
287 for(i = 1; i <= myNbRows; i++) {
288 anIndex = myNbColumns*(i-1)+theColumn;
289 if(myTable.IsBound(anIndex)) aSeq->Append(i);
297 void SALOMEDS_TableOfRealAttribute::ConvertToString(ostrstream& theStream)
303 l = myTitle.Length();
304 theStream << l << "\n";
306 theStream << myTitle.Value(i) << "\n";
309 theStream << myNbRows << "\n";
312 for(i=1; i<=myNbRows; i++) {
313 l = myRows->Value(i).Length();
314 theStream << l << "\n";
316 theStream << myRows->Value(i).Value(j) << "\n";
320 theStream << myNbColumns << "\n";
323 for(i=1; i<=myNbColumns; i++) {
324 l = myCols->Value(i).Length();
325 theStream << l << "\n";
327 theStream << myCols->Value(i).Value(j) << "\n";
330 //Store the table values
331 l = myTable.Extent();
332 theStream << l << "\n";
333 char *aBuffer = new char[128];
334 TColStd_DataMapIteratorOfDataMapOfIntegerReal anIterator(myTable);
335 for(; anIterator.More(); anIterator.Next()) {
336 theStream << anIterator.Key() << "\n";
337 sprintf(aBuffer, "%.64e", anIterator.Value());
338 theStream << aBuffer << "\n";
346 bool SALOMEDS_TableOfRealAttribute::RestoreFromString(istrstream& theStream)
352 Standard_ExtCharacter anExtChar;
353 TCollection_ExtendedString aStr;
358 myTitle = TCollection_ExtendedString(l, 0);
359 for(i=1; i<=l; i++) {
360 theStream >> anExtChar;
361 myTitle.SetValue(i, anExtChar);
365 theStream >> myNbRows;
369 for(i=1; i<=myNbRows; i++) {
371 aStr = TCollection_ExtendedString(l,0);
372 for(j=1; j<=l; j++) {
373 theStream >> anExtChar;
374 aStr.SetValue(j, anExtChar);
376 myRows->Append(aStr);
380 theStream >> myNbColumns;
384 for(i=1; i<=myNbColumns; i++) {
386 aStr = TCollection_ExtendedString(l,0);
387 for(j=1; j<=l; j++) {
388 theStream >> anExtChar;
389 aStr.SetValue(j, anExtChar);
391 myCols->Append(aStr);
394 //Restore the table values
397 for(i=1; i<=l; i++) {
398 Standard_Integer aKey;
399 Standard_Real aValue;
402 myTable.Bind(aKey, aValue);