![]() ![]() ![]() Return BitConverter.ToInt32(BitConverter.GetBytes((number > (32 - positions) Return BitConverter.ToInt32(BitConverter.GetBytes((number > (32 - positions)īitConverter.ToInt32(BitConverter.GetBytes((number > (32 - positions) Uint wrapped = number > (32 - positions) Uint number = BitConverter.ToUInt32(BitConverter.GetBytes(value), 0) ![]() Save the existing bit pattern, but interpret it as an unsigned integer. Since bits are discarded by the left-shift operators in both C# and Visual Basic, this requires creating a left shift-and-wrap method like the following: public int ShiftAndWrap(int value, int positions) Optimally, bits shifted beyond bit 31 should wrap around rather than be discarded. ' The example displays the following output:Ī second alternative solution involves weighting the individual hash codes by left-shifting the hash codes of successive fields by two or more bits. Note, though, that the performance overhead of instantiating a Tuple object may significantly impact the overall performance of an application that stores large numbers of objects in hash tables. The following example shows a possible implementation that uses the Tuple class. One is to return the hash code of a Tuple object that reflects the order of each field. A number of solutions are available so that hash codes in these cases are not identical. The previous example returns the same hash code for (n1, n2) and (n2, n1), and so may generate more collisions than are desirable. Public Overrides Function GetHashCode() As Integer If Not TypeOf obj Is Point Then Return False Public Overrides Function Equals(obj As Object) As Boolean Public Sub New(x As Integer, y As Integer) The example displays the following output: One way to generate a hash code is to combine these fields using an XOR (eXclusive OR) operation, as shown in the following example. ' n = -1973417279, hash code = -1973417279įrequently, a type has multiple data fields that can participate in generating the hash code. ' The example displays output like the following: Let randomN = rnd.Next(Int32.MinValue, Int32.MaxValue) The following example shows such an implementation for a Number structure. One of the simplest ways to compute a hash code for a numeric value that has the same or a smaller range than the Int32 type is to simply return that value. Override this.GetHashCode : unit -> int Public Overridable Function GetHashCode () As Integer Returns Int32Ī hash code for the current object. Virtual int GetHashCode() public virtual int GetHashCode () abstract member GetHashCode : unit -> int ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |