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_TableOfIntegerAttribute.cxx
8 // Author : Michael Ponikarov
13 #include <SALOMEDS_TableOfIntegerAttribute.ixx>
14 #include <Standard_Failure.hxx>
15 #include <TColStd_DataMapIteratorOfDataMapOfIntegerInteger.hxx>
19 const Standard_GUID& SALOMEDS_TableOfIntegerAttribute::GetID()
21 static Standard_GUID SALOMEDS_TableOfIntegerAttributeID ("128371A0-8F52-11d6-A8A3-0001021E8C7F");
22 return SALOMEDS_TableOfIntegerAttributeID;
25 Handle(SALOMEDS_TableOfIntegerAttribute) SALOMEDS_TableOfIntegerAttribute::Set(const TDF_Label& label)
27 Handle(SALOMEDS_TableOfIntegerAttribute) anAttr;
28 if (!label.FindAttribute(SALOMEDS_TableOfIntegerAttribute::GetID(),anAttr)) {
29 anAttr = new SALOMEDS_TableOfIntegerAttribute();
30 label.AddAttribute(anAttr);
35 SALOMEDS_TableOfIntegerAttribute::SALOMEDS_TableOfIntegerAttribute() {
36 myRows = new TColStd_HSequenceOfExtendedString();
37 myCols = new TColStd_HSequenceOfExtendedString();
42 void SALOMEDS_TableOfIntegerAttribute::SetNbColumns(const Standard_Integer theNbColumns)
46 TColStd_DataMapOfIntegerInteger aMap;
50 TColStd_DataMapIteratorOfDataMapOfIntegerInteger anIterator(aMap);
51 for(; anIterator.More(); anIterator.Next()) {
52 int aRow = (int)(anIterator.Key()/myNbColumns) + 1;
53 int aCol = (int)(anIterator.Key() - myNbColumns*(aRow-1));
54 if(aCol == 0) { aCol = myNbColumns; aRow--; }
55 if(aCol > theNbColumns) continue;
56 int aKey = (aRow-1)*theNbColumns+aCol;
57 myTable.Bind(aKey, anIterator.Value());
60 myNbColumns = theNbColumns;
62 while (myCols->Length() < myNbColumns) { // append empty columns titles
63 myCols->Append(TCollection_ExtendedString(""));
67 void SALOMEDS_TableOfIntegerAttribute::SetTitle(const TCollection_ExtendedString& theTitle) {
72 TCollection_ExtendedString SALOMEDS_TableOfIntegerAttribute::GetTitle() const {
76 void SALOMEDS_TableOfIntegerAttribute::SetRowData(const Standard_Integer theRow,
77 const Handle(TColStd_HSequenceOfInteger)& theData)
79 if(theData->Length() > myNbColumns) SetNbColumns(theData->Length());
83 while (myRows->Length() < theRow) { // append new row titles
84 myRows->Append(TCollection_ExtendedString(""));
87 Standard_Integer i, aShift = (theRow-1)*myNbColumns, aLength = theData->Length();
88 for(i = 1; i <= aLength; i++) {
89 myTable.Bind(aShift + i, theData->Value(i));
92 if(theRow > myNbRows) myNbRows = theRow;
95 Handle(TColStd_HSequenceOfInteger) SALOMEDS_TableOfIntegerAttribute::GetRowData(const Standard_Integer theRow)
97 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger();
98 Standard_Integer i, aShift = (theRow-1)*myNbColumns;
99 for(i = 1; i <= myNbColumns; i++) {
100 if(myTable.IsBound(aShift+i))
101 aSeq->Append(myTable.Find(aShift+i));
109 void SALOMEDS_TableOfIntegerAttribute::SetRowTitle(const Standard_Integer theRow,
110 const TCollection_ExtendedString& theTitle) {
112 myRows->SetValue(theRow,theTitle);
115 TCollection_ExtendedString SALOMEDS_TableOfIntegerAttribute::GetRowTitle(const Standard_Integer theRow) const {
116 return myRows->Value(theRow);
120 void SALOMEDS_TableOfIntegerAttribute::SetColumnData(const Standard_Integer theColumn,
121 const Handle(TColStd_HSequenceOfInteger)& theData)
123 if(theColumn > myNbColumns) SetNbColumns(theColumn);
127 Standard_Integer i, aLength = theData->Length();
128 for(i = 1; i <= aLength; i++) {
129 myTable.Bind(myNbColumns*(i-1)+theColumn, theData->Value(i));
132 if(aLength > myNbRows) {
134 while (myRows->Length() < myNbRows) { // append empty row titles
135 myRows->Append(TCollection_ExtendedString(""));
141 Handle(TColStd_HSequenceOfInteger) SALOMEDS_TableOfIntegerAttribute::GetColumnData(const Standard_Integer theColumn)
143 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
145 Standard_Integer i, anIndex;
146 for(i = 1; i <= myNbRows; i++) {
147 anIndex = myNbColumns*(i-1) + theColumn;
148 if(myTable.IsBound(anIndex))
149 aSeq->Append(myTable.Find(anIndex));
157 void SALOMEDS_TableOfIntegerAttribute::SetColumnTitle(const Standard_Integer theColumn,
158 const TCollection_ExtendedString& theTitle) {
160 while(myCols->Length() < theColumn) myCols->Append(TCollection_ExtendedString(""));
161 myCols->SetValue(theColumn,theTitle);
164 TCollection_ExtendedString SALOMEDS_TableOfIntegerAttribute::GetColumnTitle(const Standard_Integer theColumn) const {
165 if(myCols.IsNull()) return "";
166 if(myCols->Length() < theColumn) return "";
167 return myCols->Value(theColumn);
171 Standard_Integer SALOMEDS_TableOfIntegerAttribute::GetNbRows() const
176 Standard_Integer SALOMEDS_TableOfIntegerAttribute::GetNbColumns() const
181 void SALOMEDS_TableOfIntegerAttribute::PutValue(const Standard_Integer theValue,
182 const Standard_Integer theRow,
183 const Standard_Integer theColumn)
185 if(theColumn > myNbColumns) SetNbColumns(theColumn);
187 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
188 myTable.Bind(anIndex, theValue);
190 if(theRow > myNbRows) {
191 while (myRows->Length() < theRow) { // append empty row titles
192 myRows->Append(TCollection_ExtendedString(""));
198 Standard_Boolean SALOMEDS_TableOfIntegerAttribute::HasValue(const Standard_Integer theRow,
199 const Standard_Integer theColumn)
201 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
202 return myTable.IsBound(anIndex);
205 Standard_Integer SALOMEDS_TableOfIntegerAttribute::GetValue(const Standard_Integer theRow,
206 const Standard_Integer theColumn)
208 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
209 if(myTable.IsBound(anIndex)) return myTable.Find(anIndex);
211 Standard_Failure::Raise("Invalid cell index");
215 const Standard_GUID& SALOMEDS_TableOfIntegerAttribute::ID() const
220 void SALOMEDS_TableOfIntegerAttribute::Restore(const Handle(TDF_Attribute)& with)
222 Standard_Integer anIndex;
223 Handle(SALOMEDS_TableOfIntegerAttribute) aTable = Handle(SALOMEDS_TableOfIntegerAttribute)::DownCast(with);
229 myTable = aTable->myTable;
230 myNbRows = aTable->myNbRows;
231 myNbColumns = aTable->myNbColumns;
232 myTitle = aTable->myTitle;
234 for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
235 myRows->Append(aTable->GetRowTitle(anIndex));
237 for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++)
238 myCols->Append(aTable->GetColumnTitle(anIndex));
241 Handle(TDF_Attribute) SALOMEDS_TableOfIntegerAttribute::NewEmpty() const
243 return new SALOMEDS_TableOfIntegerAttribute();
246 void SALOMEDS_TableOfIntegerAttribute::Paste(const Handle(TDF_Attribute)& into,
247 const Handle(TDF_RelocationTable)&) const
249 Standard_Integer anIndex;
250 Handle(SALOMEDS_TableOfIntegerAttribute) aTable = Handle(SALOMEDS_TableOfIntegerAttribute)::DownCast(into);
252 aTable->myTable.Clear();
253 aTable->myCols->Clear();
254 aTable->myRows->Clear();
256 aTable->myTable = myTable;
257 aTable->myTitle = myTitle;
258 aTable->myNbRows = myNbRows;
259 aTable->myNbColumns = myNbColumns;
261 for(anIndex = 1; anIndex <= GetNbRows();anIndex++)
262 aTable->myRows->Append(GetRowTitle(anIndex));
263 for(anIndex = 1; anIndex <= GetNbColumns(); anIndex++)
264 aTable->myCols->Append(GetColumnTitle(anIndex));
268 Handle_TColStd_HSequenceOfInteger SALOMEDS_TableOfIntegerAttribute::GetSetRowIndices(const Standard_Integer theRow)
270 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
272 Standard_Integer i, aShift = myNbColumns*(theRow-1);
273 for(i = 1; i <= myNbColumns; i++) {
274 if(myTable.IsBound(aShift + i)) aSeq->Append(i);
280 Handle_TColStd_HSequenceOfInteger SALOMEDS_TableOfIntegerAttribute::GetSetColumnIndices(const Standard_Integer theColumn)
282 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
284 Standard_Integer i, anIndex;
285 for(i = 1; i <= myNbRows; i++) {
286 anIndex = myNbColumns*(i-1)+theColumn;
287 if(myTable.IsBound(anIndex)) aSeq->Append(i);
294 void SALOMEDS_TableOfIntegerAttribute::ConvertToString(ostrstream& theStream)
298 theStream.precision(64);
301 l = myTitle.Length();
302 theStream << l << "\n";
304 theStream << myTitle.Value(i) << "\n";
307 theStream << myNbRows << "\n";
310 for(i=1; i<=myNbRows; i++) {
311 l = myRows->Value(i).Length();
312 theStream << l << "\n";
314 theStream << myRows->Value(i).Value(j) << "\n";
318 theStream << myNbColumns << "\n";
321 for(i=1; i<=myNbColumns; i++) {
322 l = myCols->Value(i).Length();
323 theStream << l << "\n";
325 theStream << myCols->Value(i).Value(j) << "\n";
328 //Store the table values
329 l = myTable.Extent();
330 theStream << l << "\n";
331 TColStd_DataMapIteratorOfDataMapOfIntegerInteger anIterator(myTable);
332 for(; anIterator.More(); anIterator.Next()) {
333 theStream << anIterator.Key() << "\n";
334 theStream << anIterator.Value() << "\n";
340 bool SALOMEDS_TableOfIntegerAttribute::RestoreFromString(istrstream& theStream)
346 Standard_ExtCharacter anExtChar;
347 TCollection_ExtendedString aStr;
352 myTitle = TCollection_ExtendedString(l, 0);
353 for(i=1; i<=l; i++) {
354 theStream >> anExtChar;
355 myTitle.SetValue(i, anExtChar);
359 theStream >> myNbRows;
363 for(i=1; i<=myNbRows; i++) {
365 aStr = TCollection_ExtendedString(l,0);
366 for(j=1; j<=l; j++) {
367 theStream >> anExtChar;
368 aStr.SetValue(j, anExtChar);
370 myRows->Append(aStr);
374 theStream >> myNbColumns;
378 for(i=1; i<=myNbColumns; i++) {
380 aStr = TCollection_ExtendedString(l,0);
381 for(j=1; j<=l; j++) {
382 theStream >> anExtChar;
383 aStr.SetValue(j, anExtChar);
385 myCols->Append(aStr);
388 //Restore the table values
391 for(i=1; i<=l; i++) {
395 myTable.Bind(aKey, aValue);