【发布时间】:2011-12-19 17:11:22
【问题描述】:
目前我使用Dictionary<int,node> 来存储大约 10,000 个节点。密钥用作稍后查找的 ID 号,“节点”是包含一些数据的类。程序中的其他类使用 ID 号作为指向节点的指针。 (这听起来可能效率低下。但是,解释我为此使用字典的原因超出了我的问题范围。)
但是,20% 的节点是重复的。 我想要做的是当我添加一个节点时检查它是否已经准备就绪。如果确实如此,则使用该 ID 号。如果不创建一个新的。
这是我目前对该问题的解决方案:
public class nodeDictionary
{
Dictionary<int, node> dict = new Dictionary<int, node>( );
public int addNewNode( latLng ll )
{
node n = new node( ll );
if ( dict.ContainsValue( n ) )
{
foreach ( KeyValuePair<int, node> kv in dict )
{
if ( kv.Value == n )
{
return kv.Key;
}
}
}
else
{
if ( dict.Count != 0 )
{
dict.Add( dict.Last( ).Key + 1, n );
return dict.Last( ).Key + 1;
}
else
{
dict.Add( 0, n );
return 0;
}
}
throw new Exception( );
}//end add new node
}
问题在于,当尝试将新节点添加到 100,000 个节点的列表中时,添加节点需要 78 毫秒。这是不可接受的,因为我可以在任何给定时间添加额外的 1,000 个节点。
那么,有没有更好的方法来做到这一点?我不是在找人为我编写代码,我只是在寻找指导。
【问题讨论】:
标签: c# .net data-structures collections