I am currently using Dictionary<int,node> to store about 10,000 nodes. The key is used as an identification number for subsequent searches, and "node" is a class containing some data. Other classes in the program use an identification number as a pointer to a node. (This may seem ineffective. However, explaining my argument for using a dictionary for this is beyond the scope of my question.)
However, 20% of the nodes are duplicated. What I want to do is when I add a node check to make sure that this is all set. if so, use this identification number. If not create a new one.
This is my current solution to the problem:
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( ); }
The problem is that when you try to add a new node to a list of 100,000 nodes, it takes 78 milliseconds to add a node. This is unacceptable because I could add an extra 1000 nodes at any given time.
So, is there a better way to do this? I'm not looking for someone to write code for me, I'm just looking for guidance.
source share