如何根据项目在树中的位置为树状数据结构编写哈希码?
每个项目看起来像这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该信息不是节点的一部分,而是树的一部分。所以这将是一个非常糟糕的主意(根据外部因素定义某些东西的哈希码)。
幸运的是,正如 @spintheblack 指出的那样,绝对没有理由在这里重写 GethashCode() 。
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.
从我的评论中复制每个评论:)。为什么需要哈希值?简单地使用默认的对象哈希函数有什么问题?如果这是一个完整的二叉树,您可以将节点映射到整数值,但是对于任意的 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.
实现接口 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.
这应该可以解决问题。警告:请勿使用此方法重写 GetHashCode。 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.
这是我完成我认为你真正需要做的事情的方法。这将遍历整个树处理效果。除非您需要从树的中间开始,否则不需要链接到父级。当然,这只是我对你可能想做的事情的疯狂解释。
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.