1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2013 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _Standard_Integer_HeaderFile
16 #define _Standard_Integer_HeaderFile
18 #include <Standard_Std.hxx>
19 #include <Standard_TypeDef.hxx>
20 #include <Standard_values.h>
26 // ------------------------------------------------------------------
27 // Abs : Returns the absolute value of an Integer
28 // ------------------------------------------------------------------
29 inline Standard_Integer Abs (const Standard_Integer Value)
31 return Value >= 0 ? Value : -Value;
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; }
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; }
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)
53 return Val1 >= Val2 ? Val1 : Val2;
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)
62 return Val1 <= Val2 ? Val1 : Val2;
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; }
72 // ------------------------------------------------------------------
73 // Square : Returns the square of an integer
74 // ------------------------------------------------------------------
75 inline Standard_Integer Square(const Standard_Integer Value)
76 { return Value * Value; }
78 // ------------------------------------------------------------------
79 // IntegerFirst : Returns the minimum value of an integer
80 // ------------------------------------------------------------------
81 inline Standard_Integer IntegerFirst()
84 // ------------------------------------------------------------------
85 // IntegerLast : Returns the maximum value of an integer
86 // ------------------------------------------------------------------
87 inline Standard_Integer IntegerLast()
90 // ------------------------------------------------------------------
91 // IntegerSize : Returns the size in digits of an integer
92 // ------------------------------------------------------------------
93 inline Standard_Integer IntegerSize()
94 { return BITS(Standard_Integer); }
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)
109 return static_cast<Standard_Integer> ((theValue & theMask) % theUpperBound + 1);
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)
119 // return (Abs (theMe) % theUpper) + 1;
120 return IntegerHashCode(theValue, IntegerLast(), theUpperBound);
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)
129 return theOne == theTwo;
132 //! Computes a hash value for the given unsigned integer, in range [1, theUpperBound]
133 //! @tparam TheUnsignedInteger the type of the given value (it is "unsigned int",
134 //! and must not be the same as Standard_Size, because the overload of the HashCode function
135 //! for Standard_Size type is already presented in Standard_Size.hxx)
136 //! @param theValue the unsigned integer which hash code is to be computed
137 //! @return a hash value computed for the given unsigned integer, in range [1, theUpperBound]
138 template <typename TheUnsignedInteger>
139 typename opencascade::std::enable_if<!opencascade::std::is_same<Standard_Size, unsigned int>::value
140 && opencascade::std::is_same<TheUnsignedInteger, unsigned int>::value,
141 Standard_Integer>::type
142 HashCode (const TheUnsignedInteger theValue, const Standard_Integer theUpperBound)
144 return HashCode (static_cast<Standard_Integer> (theValue), theUpperBound);
147 //! Computes a hash code for the given value of the "long long int" type, in range [1, theUpperBound]
148 //! @param theValue the value of the "long long int" type which hash code is to be computed
149 //! @param theUpperBound the upper bound of the range a computing hash code must be within
150 //! @return a computed hash code, in range [1, theUpperBound]
151 inline Standard_Integer HashCode (const long long int theValue, const Standard_Integer theUpperBound)
153 return IntegerHashCode(theValue, 0x7fffffffffffffff, theUpperBound);
156 #if (defined(_LP64) || defined(__LP64__) || defined(_WIN64)) || defined(__APPLE__)
158 //! Computes a hash code for the given value of the Standard_Utf32Char type, in range [1, theUpperBound]
159 //! @param theValue the value of the Standard_Utf32Char type which hash code is to be computed
160 //! @param theUpperBound the upper bound of the range a computing hash code must be within
161 //! @return a computed hash code, in range [1, theUpperBound]
162 inline Standard_Integer HashCode (const Standard_Utf32Char theValue,
163 const Standard_Integer theUpperBound)
165 return IntegerHashCode(theValue, IntegerLast(), theUpperBound);
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)
174 return theOne == theTwo;