Salome HOME
a21c67a3f9f70433ce024230677d74eadc24e6f3
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_PassKey.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        GEOMAlgo_PassKey.cxx
24 // Created:
25 // Author:      Peter KURNEV
26 //              <peter@PREFEX>
27 //
28 #include <GEOMAlgo_PassKey.hxx>
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <TColStd_ListIteratorOfListOfInteger.hxx>
33 #include <TColStd_ListOfInteger.hxx>
34
35 #ifdef WIN32
36 #pragma warning( disable : 4101)
37 #endif
38
39 static
40   Standard_Integer NormalizedId(const Standard_Integer aId,
41                                 const Standard_Integer aDiv);
42
43 //=======================================================================
44 //function :
45 //purpose  :
46 //=======================================================================
47   GEOMAlgo_PassKey::GEOMAlgo_PassKey()
48 {
49  Clear();
50 }
51 //=======================================================================
52 //function :
53 //purpose  :
54 //=======================================================================
55   GEOMAlgo_PassKey::GEOMAlgo_PassKey(const GEOMAlgo_PassKey& aOther)
56 {
57   myNbIds=aOther.myNbIds;
58   mySum=aOther.mySum;
59   myMap=aOther.myMap;
60 }
61 //=======================================================================
62 //function :Assign
63 //purpose  :
64 //=======================================================================
65   GEOMAlgo_PassKey& GEOMAlgo_PassKey::Assign(const GEOMAlgo_PassKey& aOther)
66 {
67   myNbIds=aOther.myNbIds;
68   mySum=aOther.mySum;
69   myMap=aOther.myMap;
70   return *this;
71 }
72 //=======================================================================
73 //function :~
74 //purpose  :
75 //=======================================================================
76   GEOMAlgo_PassKey::~GEOMAlgo_PassKey()
77 {
78 }
79 //=======================================================================
80 //function :Clear
81 //purpose  :
82 //=======================================================================
83   void GEOMAlgo_PassKey::Clear()
84 {
85   myNbIds=0;
86   mySum=0;
87   myMap.Clear();
88 }
89 //=======================================================================
90 //function :SetIds
91 //purpose  :
92 //=======================================================================
93   void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1)
94
95 {
96   Clear();
97   myNbIds=1;
98   myMap.Add(aId1);
99   mySum=NormalizedId(aId1, myNbIds);
100 }
101 //=======================================================================
102 //function :SetIds
103 //purpose  :
104 //=======================================================================
105   void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1,
106                                 const Standard_Integer aId2)
107 {
108   TColStd_ListOfInteger aLI;
109   //
110   aLI.Append(aId1);
111   aLI.Append(aId2);
112   SetIds(aLI);
113 }
114 //=======================================================================
115 //function :SetIds
116 //purpose  :
117 //=======================================================================
118   void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1,
119                                 const Standard_Integer aId2,
120                                 const Standard_Integer aId3)
121 {
122   TColStd_ListOfInteger aLI;
123   //
124   aLI.Append(aId1);
125   aLI.Append(aId2);
126   aLI.Append(aId3);
127   SetIds(aLI);
128 }
129 //=======================================================================
130 //function :SetIds
131 //purpose  :
132 //=======================================================================
133   void GEOMAlgo_PassKey::SetIds(const Standard_Integer aId1,
134                                 const Standard_Integer aId2,
135                                 const Standard_Integer aId3,
136                                 const Standard_Integer aId4)
137 {
138   TColStd_ListOfInteger aLI;
139   //
140   aLI.Append(aId1);
141   aLI.Append(aId2);
142   aLI.Append(aId3);
143   aLI.Append(aId4);
144   SetIds(aLI);
145 }
146 //=======================================================================
147 //function :SetIds
148 //purpose  :
149 //=======================================================================
150   void GEOMAlgo_PassKey::SetIds(const TColStd_ListOfInteger& aLI)
151 {
152   Standard_Integer i, aId, aIdN;
153   TColStd_ListIteratorOfListOfInteger aIt;
154   //
155   Clear();
156   aIt.Initialize(aLI);
157   for (; aIt.More(); aIt.Next()) {
158     aId=aIt.Value();
159     myMap.Add(aId);
160   }
161   myNbIds=myMap.Extent();
162   for(i=1; i<=myNbIds; ++i) {
163     aId=myMap(i);
164     aIdN=NormalizedId(aId, myNbIds);
165     mySum+=aIdN;
166   }
167 }
168 //=======================================================================
169 //function :NbIds
170 //purpose  :
171 //=======================================================================
172   Standard_Integer GEOMAlgo_PassKey::NbIds()const
173 {
174   return myNbIds;
175 }
176 //=======================================================================
177 //function :Id
178 //purpose  :
179 //=======================================================================
180   Standard_Integer GEOMAlgo_PassKey::Id(const Standard_Integer aIndex) const
181 {
182   if (aIndex<1 || aIndex>myNbIds) {
183     return -1;
184   }
185   return myMap(aIndex);
186 }
187 //=======================================================================
188 //function :IsEqual
189 //purpose  :
190 //=======================================================================
191   Standard_Boolean GEOMAlgo_PassKey::IsEqual(const GEOMAlgo_PassKey& aOther) const
192 {
193   Standard_Boolean bRet;
194   Standard_Integer i, aId;
195   //
196   bRet=Standard_False;
197   //
198   if (myNbIds!=aOther.myNbIds) {
199     return bRet;
200   }
201   for (i=1; i<=myNbIds; ++i) {
202     aId=myMap(i);
203     if (!aOther.myMap.Contains(aId)) {
204       return bRet;
205     }
206   }
207   return !bRet;
208 }
209 //=======================================================================
210 //function : HashCode
211 //purpose  :
212 //=======================================================================
213   Standard_Integer GEOMAlgo_PassKey::HashCode(const Standard_Integer aUpper) const
214 {
215   return ::HashCode(mySum, aUpper);
216 }
217 //=======================================================================
218 //function : Dump
219 //purpose  :
220 //=======================================================================
221   void GEOMAlgo_PassKey::Dump(const Standard_Integer )const
222 {
223 }
224 //=======================================================================
225 // function: NormalizedId
226 // purpose :
227 //=======================================================================
228 Standard_Integer NormalizedId(const Standard_Integer aId,
229                               const Standard_Integer aDiv)
230 {
231   Standard_Integer aMax, aTresh, aIdRet;
232   //
233   aIdRet=aId;
234   aMax=::IntegerLast();
235   aTresh=aMax/aDiv;
236   if (aId>aTresh) {
237     aIdRet=aId%aTresh;
238   }
239   return aIdRet;
240 }