0024428: Implementation of LGPL license
[occt.git] / src / TDataStd / TDataStd_DeltaOnModificationOfIntPackedMap.cxx
1 // Created on: 2008-01-23
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2008-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and / or modify it
8 // under the terms of the GNU Lesser General Public version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <TDataStd_DeltaOnModificationOfIntPackedMap.ixx>
17 #include <TDF_DeltaOnModification.hxx>
18 #include <TDF_Label.hxx>
19 #include <TColStd_PackedMapOfInteger.hxx>
20 #include <TColStd_HPackedMapOfInteger.hxx>
21 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
22
23 #ifdef DEB
24 #define MAXUP 1000
25 #endif
26
27 //=======================================================================
28 //function : TDataStd_DeltaOnModificationOfIntPackedMap
29 //purpose  : 
30 //=======================================================================
31
32 TDataStd_DeltaOnModificationOfIntPackedMap::TDataStd_DeltaOnModificationOfIntPackedMap(
33                              const Handle(TDataStd_IntPackedMap)& OldAtt)
34 : TDF_DeltaOnModification(OldAtt)
35 {
36   Handle(TDataStd_IntPackedMap) CurrAtt;
37   if (Label().FindAttribute(OldAtt->ID(),CurrAtt)) {
38     {
39       Handle(TColStd_HPackedMapOfInteger) aMap1, aMap2;
40       aMap1 = OldAtt->GetHMap();
41       aMap2 = CurrAtt->GetHMap();
42 #ifdef DEB
43       if(aMap1.IsNull())
44         cout <<"DeltaOnModificationOfIntPackedMap:: Old Map is Null" <<endl;
45       if(aMap2.IsNull())
46         cout <<"DeltaOnModificationOfIntPackedMap:: Current Map is Null" <<endl;
47 #endif
48       
49       if(aMap1.IsNull() || aMap2.IsNull()) return;
50       if(aMap1 != aMap2) {
51         if(!aMap1->Map().HasIntersection(aMap2->Map()))
52           return; // no intersection: use full-scale backup
53
54         if(aMap1->Map().IsSubset(aMap2->Map())) { 
55           myDeletion = new TColStd_HPackedMapOfInteger();
56           myDeletion->ChangeMap().Subtraction(aMap2->Map(), aMap1->Map());
57         } else if(aMap2->Map().IsSubset(aMap1->Map())) { 
58           myAddition = new TColStd_HPackedMapOfInteger();
59           myAddition->ChangeMap().Subtraction(aMap1->Map(), aMap2->Map());
60         } else {
61           myAddition = new TColStd_HPackedMapOfInteger();
62           myAddition->ChangeMap().Subtraction(aMap1->Map(), aMap2->Map());
63           myDeletion = new TColStd_HPackedMapOfInteger();
64           myDeletion->ChangeMap().Subtraction(aMap2->Map(), aMap1->Map());
65         }
66       }
67     }
68     OldAtt->RemoveMap();
69 #ifdef DEB
70     if(OldAtt->GetHMap().IsNull())
71       cout << "BackUp Arr is Nullified" << endl;
72 #endif
73   }
74 }
75
76
77 //=======================================================================
78 //function : Apply
79 //purpose  : 
80 //=======================================================================
81
82 void TDataStd_DeltaOnModificationOfIntPackedMap::Apply()
83 {
84
85   Handle(TDF_Attribute) aTDFAttribute = Attribute();
86   Handle(TDataStd_IntPackedMap) aBackAtt = (*((Handle(TDataStd_IntPackedMap)*)&aTDFAttribute));
87   if(aBackAtt.IsNull()) {
88 #ifdef DEB
89     cout << "DeltaOnModificationOfIntPAckedMap::Apply: OldAtt is Null" <<endl;
90 #endif
91     return;
92   }
93   
94   Handle(TDataStd_IntPackedMap) aCurAtt;
95   if (!Label().FindAttribute(aBackAtt->ID(),aCurAtt)) {
96
97     Label().AddAttribute(aBackAtt);
98   }
99
100   if(aCurAtt.IsNull()) {
101 #ifdef DEB
102     cout << "DeltaOnModificationOfIntAPckedMAp::Apply: CurAtt is Null" <<endl;
103 #endif
104     return;
105   }
106   else 
107     aCurAtt->Backup();
108
109   
110   
111   Handle(TColStd_HPackedMapOfInteger) IntMap = aCurAtt->GetHMap();
112   if(IntMap.IsNull()) return;
113
114   if(myDeletion.IsNull() && myAddition.IsNull())
115     return;
116   else {
117     if(!myDeletion.IsNull()) {
118   
119       if(myDeletion->Map().Extent())
120         IntMap->ChangeMap().Subtract(myDeletion->Map());
121     } 
122     if(!myAddition.IsNull()) {
123       if(myAddition->Map().Extent())
124         IntMap->ChangeMap().Unite(myAddition->Map());
125     }
126   }
127   
128 #ifdef DEB    
129   cout << " << Map Dump after Delta Apply >>" <<endl;
130   Handle(TColStd_HPackedMapOfInteger) aIntMap = aCurAtt->GetHMap();
131   TColStd_MapIteratorOfPackedMapOfInteger it(aIntMap->Map());
132   for (Standard_Integer i=1;it.More() && i <= MAXUP; it.Next(), i++) 
133     cout << it.Key() << "  ";
134   cout <<endl;
135 #endif
136 }
137
138