GetHashCode () and the Philosopher's Stone, or a brief outline of the rake

It would seem that the topic of dictionaries, hash tables and all sorts of hash codes is painted up and down, and every second developer, being woken up from an early evening nap at about 01:28 am, quickly sketches the Hashtable balancing algorithm on a piece of paper, simultaneously proving all the properties in big-O notation.

Perhaps being so well aware of the subject of our conversation can do a disservice by instilling a false sense of confidence: "It's that simple! What could go wrong here?"

As it turned out, it can! What exactly can be found in a couple of programmer's Friday tales, right after a brief educational program about what a hash table is.

Since the article is still Friday, the educational program will be extremely short and not academically rigorous.

Hash table for the little ones

Surely, many of you went to polyclinics, housing offices, passport offices and other institutions of an increased level of philanthropy of the old model. When you, bending over to the window, say your last name (address, passport number and number of birthmarks), the dandelion grandmother on the other side nods, shuffles off into the bowels of the office, and then after a short time brings your piece of paper : be it a medical card, or even a new passport.

The magic that allows not the fastest employee in the world to find the desired document among thousands of others is nothing more than a hash table embodied in the physical world:

Warm tube hash table
Warm tube hash table

With this organization of data, each object has a corresponding hash code. In the case of a clinic, the hash code may be your last name.

The hash table itself is a kind of "chest of drawers" with drawers, each of which contains objects grouped in a certain way by their hash codes. Why, one wonders, is this special grouping necessary, and why not use the hash values ​​themselves as the inscription on the boxes? Well, probably because a set of boxes for all possible surnames in the world will not fit into every polyclinic.

: , . "" "", .

( IT), , .

, , - :

  1. - - , .

    , "".

  2. - - .

    , , - , - , .

  3. - - , ( ).

    - , - , . , .

  4. ( , ) . , , - , , - .

( ) .

, EF

. -

public class Document
{
  public Int32 Id {get; set;}
  public String Name {get; set;}
  ...
}

Entity Framework. - .

-:

HashSet<Document> _openDocuments;

- , , :

var newDocument = new Document(); // document is created
_openDocuments.Add(newDocument); // document is open, nobody else can edit it.

context.Documents.Add(newDocument);
await context.SaveChangesAsync(); // so it's safe to write the document to the DB

, test , ?

Boolean test = _openDocuments.Contains(newDocument);

, false, . , - EF Document.

EF Id , ORM . , Id 0, - :

var newDocument = new Document(); // newDocument.Id == 0
_openDocuments.Add(newDocument);

context.Documents.Add(newDocument);
await context.SaveChangesAsync(); // newDocument.Id == 42

, , - , , , Document :

public class Document
{
	public Int32 Id {get; set;}
	public String Name {get; set;}
  
	public override int GetHashCode()
 	{
    return Id;
 	}
}

: - - 0, 42.

: , , , - , GetHashCode Equals . .

, GetHashCode, .

-

- , ( ) , . [20, 20], [30, 30] [20, 20], [20, 20] [30, 30]. , -:

private static IEnumerable<Size> FilterRectangles(IEnumerable<Size> rectangles)
{
	HashSet<Size> result = new HashSet<Size>();
	foreach (var rectangle in rectangles)
    result.Add(rectangle);

	return result;
}

, , - O(n^2), O(n). , Computer Science, , , , .

HashSet , Size - FCL. , , - :

    var a = new Size(20,20).GetHashCode(); // a == 0 
    var b = new Size(30,30).GetHashCode(); // b == 0

, - ( , , , ), , -, .

, , : SizeF, , , :

var a = new SizeF(20,20).GetHashCode(); // a == 346948956
var b = new SizeF(30,30).GetHashCode(); // b == 346948956

, a b ! 346948956...

, - , FCL, :

var a = Int64.MinValue.GetHashCode(); // a == 0
var b = Int64.MaxValue.GetHashCode(); // a == 0

, .... , , .

? , :

  1. .

  2. - ... (. Resharper).

  3. . - .




All Articles