如何根据项目在树中的位置为树状数据结构编写哈希码?

发布于 2024-10-16 14:53:08 字数 342 浏览 2 评论 0原文

每个项目看起来像这样:

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

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

我想使用这些项目的许多实例创建一个树状结构,这些实例相互连接形成一个树状结构。但后来我想将每个项目散列到字典中,所以我想如果我可以根据它们在树上的位置创建一个散列值,那么我可以获得足够唯一的散列值。

关于如何做到这一点有什么想法吗?

Each item looks like this:

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

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

I want to create a tree-like structure using many instances of these items connected to each other forming a tree-like structure. But later I want to hash each item to a Dictionary, so I thought if I could create a hash value based on where they are on the tree, then I could get unique-enough hash values.

Any ideas on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笑看君怀她人 2024-10-23 14:53:08

基于它们在树上的位置

该信息不是节点的一部分,而是树的一部分。所以这将是一个非常糟糕的主意(根据外部因素定义某些东西的哈希码)。

幸运的是,正如 @spintheblack 指出的那样,绝对没有理由在这里重写 GethashCode() 。

based on where they are on the tree

That information is not part of the node but of the tree. So it would be a very bad idea (defining somethings HashCode on external factors).

Luckily, as @spintheblack points out, there is absolutely no reason to override GethashCode() here.

残花月 2024-10-23 14:53:08

从我的评论中复制每个评论:)。为什么需要哈希值?简单地使用默认的对象哈希函数有什么问题?如果这是一个完整的二叉树,您可以将节点映射到整数值,但是对于任意的 n 叉树,我没有看到将位置编码为值的简单方法。

Copied from my comment per comments :). Why do you need a hash value? What's wrong with simply using the default object hash function? If this were a complete binary tree, you could map nodes to integer values but with an arbitrary n-ary tree, I don't see a simple way to encode the position to a value.

且行且努力 2024-10-23 14:53:08

实现接口 IEffect 的每个项目都应该覆盖 ToString & GetHashCode

ToString 应包含 IEffect 属性的唯一状态(包括所选值)
GetHashCode 应该是 ToString().GetHashCode(),

并且根据对象内部数据,每个对象都有一个唯一的哈希值。

every item the implements the interface IEffect should override ToString & GetHashCode

ToString shold include unique state of the IEffect properties including the selected value
GetHashCode should be ToString().GetHashCode()

and there u have a unique hash for each object based on your object inner data.

夜吻♂芭芘 2024-10-23 14:53:08

这应该可以解决问题。警告:请勿使用此方法重写 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();
    }
}

This should do the trick. Warning: Don't override GetHashCode with this method. GetHashCode should not mutate because an objects position in a parent entity has changed. Only use this trick if you have other plans for the hash code. Here is a rough sample of something that should do what you ask. This only shows how you would find the position of the current parent, but you could extend it to walk the tree until it has no parent.

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();
    }
}
情话难免假 2024-10-23 14:53:08

这是我完成我认为你真正需要做的事情的方法。这将遍历整个树处理效果。除非您需要从树的中间开始,否则不需要链接到父级。当然,这只是我对你可能想做的事情的疯狂解释。

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

    else
        HandleEffect(effect.ElseEffect);
}

Here is how I've done what I think you really need to do. This will walk the entire tree processing effets. No need to link to a parent unless you need to start in the middle of a tree. Of course, this is just my wild interpretation of what you might be trying to do.

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

    else
        HandleEffect(effect.ElseEffect);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文