0024645: Pointer to the last is wrong for a tree node
[occt.git] / src / TDataStd / TDataStd_DeltaOnModificationOfByteArray.cxx
CommitLineData
b311480e 1// Created on: 2007-12-05
2// Created by: Sergey ZARITCHNY
973c2be1 3// Copyright (c) 2007-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
16#include <TDataStd_DeltaOnModificationOfByteArray.ixx>
17#include <TDataStd_ByteArray.hxx>
18#include <TDF_DeltaOnModification.hxx>
19#include <TDF_Label.hxx>
20#include <TColStd_HArray1OfInteger.hxx>
21#include <TColStd_HArray1OfByte.hxx>
22#include <TColStd_ListOfInteger.hxx>
23#include <TColStd_ListIteratorOfListOfInteger.hxx>
24#include <TDF_AttributeIterator.hxx>
25
26#ifdef DEB
27#define MAXUP 1000
28#endif
29//=======================================================================
30//function : TDataStd_DeltaOnModificationOfByteArray
31//purpose :
32//=======================================================================
33
34TDataStd_DeltaOnModificationOfByteArray::TDataStd_DeltaOnModificationOfByteArray(const Handle(TDataStd_ByteArray)& OldAtt)
35: TDF_DeltaOnModification(OldAtt)
36{
37 Handle(TDataStd_ByteArray) CurrAtt;
38 if (Label().FindAttribute(OldAtt->ID(),CurrAtt)) {
39 {
40 Handle(TColStd_HArray1OfByte) Arr1, Arr2;
41 Arr1 = OldAtt->InternalArray();
42 Arr2 = CurrAtt->InternalArray();
43#ifdef DEB_DELTA
44 if(Arr1.IsNull())
45 cout <<"DeltaOnModificationOfByteArray:: Old ByteArray is Null" <<endl;
46 if(Arr2.IsNull())
47 cout <<"DeltaOnModificationOfByteArray:: Current ByteArray is Null" <<endl;
48#endif
49
50 if(Arr1.IsNull() || Arr2.IsNull()) return;
51 if(Arr1 != Arr2) {
52 myUp1 = Arr1->Upper();
53 myUp2 = Arr2->Upper();
54 Standard_Integer i, N=0, aCase=0;
55 if(myUp1 == myUp2)
56 {aCase = 1; N = myUp1;}
57 else if(myUp1 < myUp2)
58 {aCase = 2; N = myUp1;}
59 else
60 {aCase = 3; N = myUp2;}//Up1 > Up2
61
62 TColStd_ListOfInteger aList;
63 for(i=Arr1->Lower();i<= N; i++)
64 if(Arr1->Value(i) != Arr2->Value(i))
65 aList.Append(i);
66 if(aCase == 3) {
67 for(i = N+1;i <= myUp1; i++)
68 aList.Append(i);
69 }
70
71 if(aList.Extent()) {
72 myIndxes = new TColStd_HArray1OfInteger(1,aList.Extent());
73 myValues = new TColStd_HArray1OfByte(1,aList.Extent());
74 TColStd_ListIteratorOfListOfInteger anIt(aList);
75 for(i =1;anIt.More();anIt.Next(),i++) {
76 myIndxes->SetValue(i, anIt.Value());
77 myValues->SetValue(i, Arr1->Value(anIt.Value()));
78 }
79 }
80 }
81 }
82 OldAtt->RemoveArray();
83#ifdef DEB
84 if(OldAtt->InternalArray().IsNull())
85 cout << "BackUp Arr is Nullified" << endl;
86#endif
87 }
88}
89
90
91//=======================================================================
92//function : Apply
93//purpose :
94//=======================================================================
95
96void TDataStd_DeltaOnModificationOfByteArray::Apply()
97{
98
99 Handle(TDF_Attribute) TDFAttribute = Attribute();
100 Handle(TDataStd_ByteArray) BackAtt = (*((Handle(TDataStd_ByteArray)*)&TDFAttribute));
101 if(BackAtt.IsNull()) {
102#ifdef DEB
103 cout << "DeltaOnModificationOfByteArray::Apply: OldAtt is Null" <<endl;
104#endif
105 return;
106 }
107
108 Handle(TDataStd_ByteArray) aCurAtt;
109 if (!Label().FindAttribute(BackAtt->ID(),aCurAtt)) {
110
111 Label().AddAttribute(BackAtt);
112 }
113
114 if(aCurAtt.IsNull()) {
115#ifdef DEB
116 cout << "DeltaOnModificationOfByteArray::Apply: CurAtt is Null" <<endl;
117#endif
118 return;
119 }
120 else
121 aCurAtt->Backup();
122
123 Standard_Integer aCase;
124 if(myUp1 == myUp2)
125 aCase = 1;
126 else if(myUp1 < myUp2)
127 aCase = 2;
128 else
129 aCase = 3;//Up1 > Up2
130
131 if (aCase == 1 && (myIndxes.IsNull() || myValues.IsNull()))
132 return;
133
134 Standard_Integer i;
135 Handle(TColStd_HArray1OfByte) BArr = aCurAtt->InternalArray();
136 if(BArr.IsNull()) return;
137 if(aCase == 1)
138 for(i = 1; i <= myIndxes->Upper();i++)
139 BArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));
140 else if(aCase == 2) {
141 Handle(TColStd_HArray1OfByte) byteArr = new TColStd_HArray1OfByte(BArr->Lower(), myUp1);
142 for(i = BArr->Lower(); i <= myUp1 && i <= BArr->Upper(); i++)
143 byteArr->SetValue(i, BArr->Value(i));
144 if(!myIndxes.IsNull() && !myValues.IsNull())
145 for(i = 1; i <= myIndxes->Upper();i++)
146 byteArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));
147 aCurAtt->myValue = byteArr;
148 }
149 else { // aCase == 3
150 Standard_Integer low = BArr->Lower();
151 Handle(TColStd_HArray1OfByte) byteArr = new TColStd_HArray1OfByte(low, myUp1);
152 for(i = BArr->Lower(); i <= myUp2 && i <= BArr->Upper(); i++)
153 byteArr->SetValue(i, BArr->Value(i));
154 if(!myIndxes.IsNull() && !myValues.IsNull())
155 for(i = 1; i <= myIndxes->Upper();i++) {
156#ifdef DEB
157 cout << "i = " << i << " myIndxes->Upper = " << myIndxes->Upper() << endl;
158 cout << "myIndxes->Value(i) = " << myIndxes->Value(i) << endl;
159 cout << "myValues->Value(i) = " << myValues->Value(i) << endl;
160#endif
161 byteArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));
162 }
163 aCurAtt->myValue = byteArr;
164 }
165
166#ifdef DEB
167 cout << " << Array Dump after Delta Apply >>" <<endl;
168 Handle(TColStd_HArray1OfByte) BArr2 = aCurAtt->InternalArray();
169 for(i=BArr2->Lower(); i<=BArr2->Upper() && i<= MAXUP;i++)
170 cout << BArr2->Value(i) << " ";
171 cout <<endl;
172#endif
173}
174
175