0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / Standard / Standard_Integer.hxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2013 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 #ifndef _Standard_Integer_HeaderFile
16 #define _Standard_Integer_HeaderFile
17
18 #include <Standard_Std.hxx>
19 #include <Standard_TypeDef.hxx>
20 #include <Standard_values.h>
21
22 // ===============
23 // Inline methods
24 // ===============
25
26 // ------------------------------------------------------------------
27 // Abs : Returns the absolute value of an Integer
28 // ------------------------------------------------------------------
29 inline  Standard_Integer Abs (const Standard_Integer Value)
30 {
31   return Value >= 0 ? Value : -Value;
32 }
33
34 // ------------------------------------------------------------------
35 // IsEven : Returns Standard_True if an integer is even
36 // ------------------------------------------------------------------
37 inline Standard_Boolean IsEven (const Standard_Integer Value)
38 { return Value % 2 == 0; }
39
40
41 // ------------------------------------------------------------------
42 // IsOdd : Returns Standard_True if an integer is odd
43 // ------------------------------------------------------------------
44 inline Standard_Boolean IsOdd (const Standard_Integer Value)
45 { return Value % 2 == 1; }
46
47 // ------------------------------------------------------------------
48 // Max : Returns the maximum integer between two integers
49 // ------------------------------------------------------------------
50 inline Standard_Integer  Max (const Standard_Integer Val1,
51                               const Standard_Integer Val2)
52 {
53   return Val1 >= Val2 ? Val1 : Val2;
54 }
55
56 // ------------------------------------------------------------------
57 // Min : Returns the minimum integer between two integers
58 // ------------------------------------------------------------------
59 inline Standard_Integer  Min (const Standard_Integer Val1,
60                               const Standard_Integer Val2)
61 {
62   return Val1 <= Val2 ? Val1 : Val2;
63 }
64
65 // ------------------------------------------------------------------
66 // Modulus : Returns the remainder of division between two integers
67 // ------------------------------------------------------------------
68 inline Standard_Integer  Modulus (const Standard_Integer Value,
69                                   const Standard_Integer Divisor)
70 { return Value % Divisor; }
71
72 // ------------------------------------------------------------------
73 // Square : Returns the square of an integer
74 // ------------------------------------------------------------------
75 inline Standard_Integer Square(const Standard_Integer Value)
76 { return Value * Value; }
77
78 // ------------------------------------------------------------------
79 // IntegerFirst : Returns the minimum value of an integer
80 // ------------------------------------------------------------------
81 inline Standard_Integer  IntegerFirst()
82 { return INT_MIN; }
83
84 // ------------------------------------------------------------------
85 // IntegerLast : Returns the maximum value of an integer
86 // ------------------------------------------------------------------
87 inline Standard_Integer  IntegerLast()
88 { return INT_MAX; }
89
90 // ------------------------------------------------------------------
91 // IntegerSize : Returns the size in digits of an integer
92 // ------------------------------------------------------------------
93 inline Standard_Integer  IntegerSize()
94 { return BITS(Standard_Integer); }
95
96
97 //! Computes a hash code for the given value of some integer type, in range [1, theUpperBound]
98 //! @tparam TheInteger the type of the integer which hash code is to be computed
99 //! @param theValue the value of the TheInteger type which hash code is to be computed
100 //! @param theMask the mask for the last bits of the value that are used in the computation of a hash code
101 //! @param theUpperBound the upper bound of the range a computing hash code must be within
102 //! @return a computed hash code, in range [1, theUpperBound]
103 template <typename TheInteger>
104 typename opencascade::std::enable_if<opencascade::is_integer<TheInteger>::value, Standard_Integer>::type
105 IntegerHashCode (const TheInteger                                                theValue,
106                  const typename opencascade::disable_deduction<TheInteger>::type theMask,
107                  const Standard_Integer                                          theUpperBound)
108 {
109   return static_cast<Standard_Integer> ((theValue & theMask) % theUpperBound + 1);
110 }
111
112 //! Computes a hash code for the given value of the Standard_Integer type, in range [1, theUpperBound]
113 //! @param theValue the value of the Standard_Integer type which hash code is to be computed
114 //! @param theUpperBound the upper bound of the range a computing hash code must be within
115 //! @return a computed hash code, in range [1, theUpperBound]
116 inline Standard_Integer HashCode (const Standard_Integer theValue,
117                                   const Standard_Integer theUpperBound)
118 {
119   // return (Abs (theMe) % theUpper) + 1;
120   return IntegerHashCode(theValue, IntegerLast(), theUpperBound);
121 }
122
123 // ------------------------------------------------------------------
124 // IsEqual : Returns Standard_True if two integers are equal
125 // ------------------------------------------------------------------
126 inline Standard_Boolean IsEqual (const Standard_Integer theOne,
127                                  const Standard_Integer theTwo)
128 {
129   return theOne == theTwo;
130 }
131
132 //! Computes a hash value for the given unsigned integer, in range [1, theUpperBound]
133 //! @param theValue the unsigned integer which hash code is to be computed
134 //! @param theUpperBound the upper bound of the range a computing hash code must be within
135 //! @return a hash value computed for the given unsigned integer, in range [1, theUpperBound]
136 inline Standard_Integer HashCode (const unsigned int theValue, const Standard_Integer theUpperBound)
137 {
138   return ::HashCode (static_cast<Standard_Integer> (theValue), theUpperBound);
139 }
140
141 //! Computes a hash code for the given value of the "long long int" type, in range [1, theUpperBound]
142 //! @param theValue the value of the "long long int" type which hash code is to be computed
143 //! @param theUpperBound the upper bound of the range a computing hash code must be within
144 //! @return a computed hash code, in range [1, theUpperBound]
145 inline Standard_Integer HashCode (const long long int theValue, const Standard_Integer theUpperBound)
146 {
147   return IntegerHashCode(theValue, 0x7fffffffffffffff, theUpperBound);
148 }
149
150 #if (defined(_LP64) || defined(__LP64__) || defined(_WIN64)) || defined(__APPLE__)
151
152 //! Computes a hash code for the given value of the Standard_Utf32Char type, in the range [1, theUpperBound]
153 //! @tparam TheUtf32Char the type of the given value (it is Standard_Utf32Char,
154 //! and must not be the same as "unsigned int", because the overload of the HashCode function
155 //! for "unsigned int" type is already presented in Standard_Integer.hxx)
156 //! @param theValue the value of the Standard_Utf32Char type which hash code is to be computed
157 //! @param theUpperBound the upper bound of the range a computing hash code must be within
158 //! @return a computed hash code, in the range [1, theUpperBound]
159 template <typename TheUtf32Char>
160 typename opencascade::std::enable_if<!opencascade::std::is_same<Standard_Utf32Char, unsigned int>::value
161                                        && opencascade::std::is_same<TheUtf32Char, Standard_Utf32Char>::value,
162                                      Standard_Integer>::type
163 HashCode (const TheUtf32Char theValue, const Standard_Integer theUpperBound)
164 {
165   return IntegerHashCode (theValue, IntegerLast(), theUpperBound);
166 }
167
168 // ------------------------------------------------------------------
169 // IsEqual : Returns Standard_True if two integers are equal
170 // ------------------------------------------------------------------
171 inline Standard_Boolean IsEqual (const Standard_Utf32Char theOne,
172                                  const Standard_Utf32Char theTwo)
173 {
174   return theOne == theTwo;
175 }
176
177 #endif
178
179 #endif