0030550: Coding - Integer overflow in Standard_CString HashCodes
[occt.git] / src / BOPTools / BOPTools_Set.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15
16 #include <BOPTools_Set.hxx>
17 #include <BRep_Tool.hxx>
18 #include <TopExp_Explorer.hxx>
19 #include <TopoDS_Edge.hxx>
20 #include <TopoDS_Shape.hxx>
21 #include <TopTools_MapOfShape.hxx>
22
23 static 
24   Standard_Integer NormalizedIds(const Standard_Integer aId,
25                                  const Standard_Integer aDiv);
26
27 //=======================================================================
28 //function : 
29 //purpose  : 
30 //=======================================================================
31 BOPTools_Set::BOPTools_Set() 
32 :
33   myAllocator(NCollection_BaseAllocator::CommonBaseAllocator()),
34   myShapes(myAllocator)
35 {
36   myNbShapes=0;
37   mySum=0;
38   myUpper=432123;
39 }
40 //=======================================================================
41 //function : 
42 //purpose  : 
43 //=======================================================================
44 BOPTools_Set::BOPTools_Set
45   (const Handle(NCollection_BaseAllocator)& theAllocator) 
46 :
47   myAllocator(theAllocator),
48   myShapes(myAllocator)
49
50   myNbShapes=0;
51   mySum=0;  
52   myUpper=432123;
53 }
54 //=======================================================================
55 //function :~ 
56 //purpose  : 
57 //=======================================================================
58 BOPTools_Set::~BOPTools_Set()
59 {
60   Clear();
61 }
62 //=======================================================================
63 //function : Clear
64 //purpose  : 
65 //=======================================================================
66 void BOPTools_Set::Clear()
67
68   myNbShapes=0;
69   mySum=0;
70   myShapes.Clear();
71 }
72 //=======================================================================
73 //function : NbShapes
74 //purpose  : 
75 //=======================================================================
76 Standard_Integer BOPTools_Set::NbShapes()const
77 {
78   return myNbShapes;
79 }
80 //=======================================================================
81 //function :Assign
82 //purpose  : 
83 //=======================================================================
84 BOPTools_Set& BOPTools_Set::Assign(const BOPTools_Set& theOther)
85
86   TopTools_ListIteratorOfListOfShape aIt;
87   //
88   myShape=theOther.myShape;
89   myNbShapes=theOther.myNbShapes;
90   mySum=theOther.mySum;
91   myUpper=theOther.myUpper;
92   myAllocator=theOther.myAllocator;
93   //
94   myShapes.Clear();
95   aIt.Initialize(theOther.myShapes);
96   for (; aIt.More(); aIt.Next()) {
97     const TopoDS_Shape& aSx=aIt.Value();
98     myShapes.Append(aSx);
99   }
100   return *this;
101 }
102 //=======================================================================
103 //function : Shape
104 //purpose  : 
105 //=======================================================================
106 const TopoDS_Shape& BOPTools_Set::Shape()const
107 {
108   return myShape;
109 }
110
111 //=======================================================================
112 // function : HashCode
113 // purpose  :
114 //=======================================================================
115 Standard_Integer BOPTools_Set::HashCode (const Standard_Integer theUpperBound) const
116 {
117   return ::HashCode (mySum, theUpperBound);
118 }
119
120 //=======================================================================
121 //function : IsEqual
122 //purpose  : 
123 //=======================================================================
124 Standard_Boolean BOPTools_Set::IsEqual
125   (const BOPTools_Set& theOther)const
126 {
127   Standard_Boolean bRet;
128   //
129   bRet=Standard_False;
130   //
131   if (theOther.myNbShapes!=myNbShapes) {
132     return bRet;
133   }
134   //
135   TopTools_MapOfShape aM1;
136   TopTools_ListIteratorOfListOfShape aIt;
137   //
138   aIt.Initialize(myShapes);
139   for (; aIt.More(); aIt.Next()) {
140     const TopoDS_Shape& aSx1=aIt.Value();
141     aM1.Add(aSx1);
142   }
143   //
144   aIt.Initialize(theOther.myShapes);
145   for (; aIt.More(); aIt.Next()) {
146     const TopoDS_Shape& aSx2=aIt.Value();
147     if (!aM1.Contains(aSx2)) {
148       return bRet;
149     }
150   }
151   //
152   return !bRet;
153 }
154 //=======================================================================
155 //function : Add
156 //purpose  : 
157 //=======================================================================
158 void BOPTools_Set::Add(const TopoDS_Shape& theS,
159                        const TopAbs_ShapeEnum theType)
160 {
161   Standard_Integer aId, aIdN;
162   TopAbs_Orientation aOr;
163   TopExp_Explorer aExp;
164   //
165   myShape=theS;
166   myShapes.Clear();
167   myNbShapes=0;
168   mySum=0;
169   //
170   aExp.Init(theS, theType);
171   for (; aExp.More(); aExp.Next()) {
172     const TopoDS_Shape& aSx=aExp.Current();
173     if (theType==TopAbs_EDGE) {
174       const TopoDS_Edge& aEx=*((TopoDS_Edge*)&aSx);
175       if (BRep_Tool::Degenerated(aEx)) {
176         continue;
177       }
178     }
179     //
180     aOr=aSx.Orientation();
181     if (aOr==TopAbs_INTERNAL) {
182       TopoDS_Shape aSy;
183       //
184       aSy=aSx;
185       //
186       aSy.Orientation(TopAbs_FORWARD);
187       myShapes.Append(aSy);
188       //
189       aSy.Orientation(TopAbs_REVERSED);
190       myShapes.Append(aSy);
191     }
192     else {
193       myShapes.Append(aSx);
194     }
195   }
196   //
197   myNbShapes=myShapes.Extent();
198   if (!myNbShapes) {
199     return;
200   }
201   // 
202   TopTools_ListIteratorOfListOfShape aIt;
203   //
204   aIt.Initialize(myShapes);
205   for (; aIt.More(); aIt.Next()) {
206     const TopoDS_Shape& aSx=aIt.Value();
207     aId=aSx.HashCode(myUpper);
208     aIdN=NormalizedIds(aId, myNbShapes);
209     mySum+=aIdN;
210   }
211 }
212 //=======================================================================
213 // function: NormalizedIds
214 // purpose : 
215 //=======================================================================
216 Standard_Integer NormalizedIds(const Standard_Integer aId,
217                                const Standard_Integer aDiv)
218 {
219   Standard_Integer aMax, aTresh, aIdRet;
220   //
221   aIdRet=aId;
222   aMax=::IntegerLast();
223   aTresh=aMax/aDiv;
224   if (aId>aTresh) {
225     aIdRet=aId%aTresh;
226   }
227   return aIdRet;
228 }