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