Salome HOME
61b300f9c4625bc9e5ee7d668ba7cd4096e72232
[modules/gui.git] / src / SVTK / SVTK_Hash.h
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 //  SALOME SALOMEGUI : 
24 //  File   : SVTK_Hash.h
25 //  Author : Roman NIKOLAEV
26
27 #ifndef SVTK_HASH_H
28 #define SVTK_HASH_H
29
30 #include <vector>
31 #include <NCollection_IndexedMap.hxx>
32 #include <NCollection_Map.hxx>
33 #include <Standard_Integer.hxx>
34 #include <vtkType.h>
35 #include <limits>
36
37 typedef std::vector<Standard_Integer> SVTK_ListOfInteger;
38 typedef std::vector<vtkIdType> SVTK_ListOfVtk;
39
40 class SVTK_Hasher {
41
42 public:
43   static Standard_Integer HashCode(const std::vector<Standard_Integer> ids,
44                                    const Standard_Integer              upper)
45   {
46     Standard_Integer seed = (Standard_Integer)ids.size();
47     for ( Standard_Integer v : ids )
48       seed ^= v + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
49
50     return ::HashCode(seed,upper);
51   }
52
53   static Standard_Boolean IsEqual(const SVTK_ListOfInteger& theKey1,
54                                   const SVTK_ListOfInteger& theKey2)
55   {
56     return theKey1 == theKey2;
57   }
58 };
59
60 class SVTK_vtkHasher {
61
62 public:
63   static vtkIdType HashCode(const std::vector<vtkIdType> ids,
64                             const Standard_Integer       upper)
65   {
66     vtkIdType seed = (vtkIdType)ids.size();
67     for ( vtkIdType v : ids )
68       seed ^= v + 0x9e3779b97f4a7c15 + ( seed << 6 ) + ( seed >> 2 );
69
70     return ::HashCode((Standard_Integer) seed, upper);
71   }
72
73   static vtkIdType IsEqual(const SVTK_ListOfVtk& theKey1,
74                            const SVTK_ListOfVtk& theKey2)
75   {
76     return theKey1 == theKey2;
77   }
78 };
79
80 struct svtkIdHasher
81 {
82   static int HashCode(const vtkIdType theValue,  const int theUpperBound)
83   {
84     return static_cast<int> ((theValue & (std::numeric_limits<vtkIdType>::max)()) % theUpperBound + 1);
85   }
86
87   static bool IsEqual( const vtkIdType& id1, const vtkIdType& id2 )
88   {
89     return id1 == id2;
90   }
91 };
92
93 typedef NCollection_IndexedMap<SVTK_ListOfInteger,SVTK_Hasher> SVTK_IndexedMapOfIds;
94 typedef NCollection_IndexedMap<SVTK_ListOfVtk, SVTK_vtkHasher> SVTK_IndexedMapOfVtkIds;
95 typedef NCollection_Map< vtkIdType, svtkIdHasher > SVTK_TVtkIDsMap;
96 typedef NCollection_Map< vtkIdType, svtkIdHasher >::Iterator SVTK_TVtkIDsMapIterator;
97 typedef NCollection_IndexedMap<vtkIdType,svtkIdHasher> SVTK_TIndexedMapOfVtkId;
98
99 #endif // SVTK_HASH_H