6d42b773909e5017ae94bd372fdab739cf5d4be4
[occt.git] / src / TDataStd / TDataStd_IntPackedMap.cxx
1 // File:        TDataStd_IntPackedMap.cxx
2 // Created:     Tue Jul 31 17:29:51 2007
3 // Author:      Sergey ZARITCHNY
4 //              <sergey.zaritchny@opencascade.com>
5 //Copyright:    Open CasCade SA 2007
6
7
8 #include <TDataStd_IntPackedMap.ixx>
9 #include <TColStd_PackedMapOfInteger.hxx>
10 #include <TColStd_HPackedMapOfInteger.hxx>
11 #include <TDF_DefaultDeltaOnModification.hxx>
12 #include <TDataStd_DeltaOnModificationOfIntPackedMap.hxx>
13 #include <Standard_GUID.hxx>
14 #include <TDF_Label.hxx>
15 #include <TDF_Attribute.hxx>
16 //=======================================================================
17 //function : GetID
18 //purpose  : 
19 //=======================================================================
20
21 const Standard_GUID& TDataStd_IntPackedMap::GetID()
22 {
23   static Standard_GUID theGUID ("7031faff-161e-44df-8239-7c264a81f5a1");
24   return theGUID;
25 }
26
27 //=======================================================================
28 //function : Set
29 //purpose  : 
30 //=======================================================================
31
32 Handle(TDataStd_IntPackedMap) TDataStd_IntPackedMap::Set (const TDF_Label& theLabel, 
33                                                           const Standard_Boolean isDelta)
34 {
35   Handle(TDataStd_IntPackedMap) anAtt;
36   if (!theLabel.FindAttribute(TDataStd_IntPackedMap::GetID(), anAtt))
37   {
38     anAtt = new TDataStd_IntPackedMap;
39     anAtt->Clear();
40     anAtt->SetDelta(isDelta);
41     theLabel.AddAttribute(anAtt);
42   }
43   return anAtt;
44 }
45 //=======================================================================
46 //function : Constructor
47 //purpose  : 
48 //=======================================================================
49 TDataStd_IntPackedMap::TDataStd_IntPackedMap ()
50      :myIsDelta(Standard_False)
51 {
52   myMap = new TColStd_HPackedMapOfInteger ();
53 }
54 //=======================================================================
55 //function : ChangeMap
56 //purpose  : 
57 //=======================================================================
58
59 Standard_Boolean TDataStd_IntPackedMap::ChangeMap (const Handle(TColStd_HPackedMapOfInteger)& theMap)
60 {
61   if(theMap.IsNull()) return Standard_False;  
62   if (myMap != theMap)
63   {
64     if (!myMap->Map().IsEqual(theMap->Map()))
65     {
66       Backup();      
67       myMap->ChangeMap().Assign(theMap->Map());
68       return Standard_True;
69     }
70   }
71   return Standard_False;
72 }
73 //=======================================================================
74 //function : Clear
75 //purpose  : 
76 //=======================================================================
77
78 Standard_Boolean TDataStd_IntPackedMap::Clear ()
79 {
80   if (!myMap->Map().IsEmpty())
81   {
82     Backup();
83     myMap = new TColStd_HPackedMapOfInteger;
84     return Standard_True;
85   }
86   return Standard_False;
87 }
88
89 //=======================================================================
90 //function : Contains
91 //purpose  : 
92 //=======================================================================
93 Standard_Boolean TDataStd_IntPackedMap::Contains(const Standard_Integer theKey) const
94 {
95   return myMap->Map().Contains(theKey);
96 }
97
98 //=======================================================================
99 //function : Add
100 //purpose  : 
101 //=======================================================================
102
103 Standard_Boolean TDataStd_IntPackedMap::Add(const Standard_Integer theKey)
104 {
105   Standard_Boolean aResult = !myMap->Map().Contains(theKey);
106   if (aResult)
107   {
108     Backup();
109     aResult = myMap->ChangeMap().Add(theKey);
110   }
111   return aResult;
112 }
113 //=======================================================================
114 //function : Remove
115 //purpose  : 
116 //=======================================================================
117
118 Standard_Boolean TDataStd_IntPackedMap::Remove(const Standard_Integer theKey)
119 {
120   Standard_Boolean aResult = myMap->Map().Contains(theKey);
121   if (aResult)
122   {
123     Backup();
124     aResult = myMap->ChangeMap().Remove(theKey);
125   }
126   return aResult;
127 }
128
129 //=======================================================================
130 //function : NewEmpty
131 //purpose  : 
132 //=======================================================================
133
134 Handle(TDF_Attribute) TDataStd_IntPackedMap::NewEmpty () const
135 {
136   return new TDataStd_IntPackedMap;
137 }
138
139 //=======================================================================
140 //function : Restore
141 //purpose  : 
142 //=======================================================================
143
144 void TDataStd_IntPackedMap::Restore (const Handle(TDF_Attribute)& theWith)
145 {
146   Handle(TDataStd_IntPackedMap) aWith = 
147     Handle(TDataStd_IntPackedMap)::DownCast(theWith);
148   if (aWith->myMap.IsNull())
149     myMap.Nullify();
150   else
151   {
152     myMap = new TColStd_HPackedMapOfInteger;
153     myMap->ChangeMap().Assign(aWith->myMap->Map());
154     myIsDelta = aWith->myIsDelta;
155   }
156 }
157
158 //=======================================================================
159 //function : Paste
160 //purpose  : 
161 //=======================================================================
162
163 void TDataStd_IntPackedMap::Paste (const Handle(TDF_Attribute)& theInto,
164                               const Handle(TDF_RelocationTable)&) const
165
166   Handle(TDataStd_IntPackedMap) anInto = 
167     Handle(TDataStd_IntPackedMap)::DownCast(theInto);
168   if(!anInto.IsNull()) {
169     anInto->ChangeMap(myMap);
170     anInto->SetDelta(myIsDelta);
171   }
172 }
173
174 //=======================================================================
175 //function : ID
176 //purpose  : 
177 //=======================================================================
178 const Standard_GUID& TDataStd_IntPackedMap::ID() const 
179 { return GetID(); }
180
181 //=======================================================================
182 //function : Dump
183 //purpose  : 
184 //=======================================================================
185
186 Standard_OStream& TDataStd_IntPackedMap::Dump(Standard_OStream& theOS) const
187 {
188   Standard_OStream& anOS = TDF_Attribute::Dump( theOS );
189   anOS << "IntPackedMap size = " << Extent();
190   anOS << " Delta is " << myIsDelta ? "ON":"OFF";
191   anOS << endl;
192   return anOS;
193 }
194
195 //=======================================================================
196 //function : DeltaOnModification
197 //purpose  : 
198 //=======================================================================
199
200 Handle(TDF_DeltaOnModification) TDataStd_IntPackedMap::DeltaOnModification
201 (const Handle(TDF_Attribute)& OldAttribute) const
202 {
203   if(myIsDelta)
204     return new TDataStd_DeltaOnModificationOfIntPackedMap(*((Handle(TDataStd_IntPackedMap)*)&OldAttribute));
205   else return new TDF_DefaultDeltaOnModification(OldAttribute);
206 }
207
208