@@ -8,8 +8,12 @@ namespace Microsoft.VisualStudioTools.Project
88{
99 internal sealed class HierarchyIdMap
1010 {
11- private readonly ConcurrentDictionary < int , WeakReference < HierarchyNode > > nodes = new ConcurrentDictionary < int , WeakReference < HierarchyNode > > ( ) ;
12- private readonly ConcurrentStack < int > freedIds = new ConcurrentStack < int > ( ) ;
11+ private readonly ConcurrentDictionary < uint , WeakReference < HierarchyNode > > nodes = new ConcurrentDictionary < uint , WeakReference < HierarchyNode > > ( ) ;
12+ private readonly ConcurrentStack < uint > freedIds = new ConcurrentStack < uint > ( ) ;
13+
14+ public readonly static HierarchyIdMap Instance = new HierarchyIdMap ( ) ;
15+
16+ private HierarchyIdMap ( ) { }
1317
1418 /// <summary>
1519 /// Must be called from the UI thread
@@ -33,15 +37,21 @@ public uint Add(HierarchyNode node)
3337#endif
3438 if ( ! this . freedIds . TryPop ( out var idx ) )
3539 {
36- idx = this . nodes . Count ;
40+ idx = this . NextIndex ( ) ;
3741 }
3842
3943 var addSuccess = this . nodes . TryAdd ( idx , new WeakReference < HierarchyNode > ( node ) ) ;
4044 Debug . Assert ( addSuccess , "Failed to add a new item" ) ;
4145
42- return ( uint ) idx + 1 ;
46+ return idx ;
47+ }
48+
49+ private uint NextIndex ( )
50+ {
51+ return ( uint ) this . nodes . Count + 1 ;
4352 }
4453
54+
4555 /// <summary>
4656 /// Must be called from the UI thread
4757 /// </summary>
@@ -51,7 +61,7 @@ public void Remove(HierarchyNode node)
5161
5262 Debug . Assert ( node != null , "Called with null node" ) ;
5363
54- var idx = ( int ) node . ID - 1 ;
64+ var idx = node . ID ;
5565
5666 var removeCheck = this . nodes . TryRemove ( idx , out var weakRef ) ;
5767
@@ -71,14 +81,11 @@ public HierarchyNode this[uint itemId]
7181 {
7282 Debug . Assert ( VisualStudio . Shell . ThreadHelper . CheckAccess ( ) ) ;
7383
74- var idx = ( int ) itemId - 1 ;
75- if ( 0 <= idx && idx < this . nodes . Count )
84+ var idx = itemId ;
85+ if ( this . nodes . TryGetValue ( idx , out var reference ) && reference != null && reference . TryGetTarget ( out var node ) )
7686 {
77- if ( this . nodes . TryGetValue ( idx , out var reference ) && reference != null && reference . TryGetTarget ( out var node ) )
78- {
79- Debug . Assert ( node != null ) ;
80- return node ;
81- }
87+ Debug . Assert ( node != null ) ;
88+ return node ;
8289 }
8390
8491 // This is a valid return value, this gets called by VS after we deleted the item
0 commit comments