How to write a hash code for a tree-like data structure based on the location of an item in a tree?

Each item is as follows:

public interface IEffect
{
    string Name { get; }
    bool Compute ( );

    List<IEffect> SubEffects { get; set; }
    IEffect ElseIfEffect { get; set; }
}

I want to create a tree structure using many instances of these elements connected to each other, forming a tree structure. But later I want to hash each element into a dictionary, so I thought if I could create a hash value based on where they are in the tree, then I could get unique hash values.

Any ideas on how to do this?

+3
source share
5 answers

depending on where they are on the tree

node, . ( somethings HashCode ).

, @spintheblack, GethashCode() .

+4

:). -? , - ? , , n- , .

+2

. IEffect ToString GetHashCode

ToString shold IEffect, GetHashCode ToString(). GetHashCode()

, .

+2

. . GetHashCode . GetHashCode , . , -. , , . , , , , .

class MyEffect : IEffect
{
    IEffect _owner;
    public MyEffect(IEffect owner) 
    {
        _owner = owner;
    }

    public int GetFunkyHash()
    {
        int hash = this.GetHashCode();
        int index = _owner.IndexOf(item);
        return hash | index.GetHashCode();
    }
}
+2

, , , . . , . , , .

public void HandleEffects(IEffect effect)
{
    if(effect.Compute())
        foreach(IEffect child in effect.SubEffects)
            HandleEffects(child);

    else
        HandleEffect(effect.ElseEffect);
}
+1

Source: https://habr.com/ru/post/1791322/


All Articles