@@ -21,7 +21,7 @@ public abstract class nanoReferenceTableBase<T> : InanoTable
21
21
/// <summary>
22
22
/// Lookup table for finding item ID by item value.
23
23
/// </summary>
24
- private readonly Dictionary < T , ushort > _idsByItemsDictionary ;
24
+ private Dictionary < T , ushort > _idsByItemsDictionary ;
25
25
26
26
/// <summary>
27
27
/// Assembly tables context - contains all tables used for building target assembly.
@@ -31,10 +31,12 @@ public abstract class nanoReferenceTableBase<T> : InanoTable
31
31
/// <summary>
32
32
/// Lookup table for finding item ID by item value.
33
33
/// </summary>
34
- protected readonly IEnumerable < T > _items ;
34
+ protected IEnumerable < T > _items ;
35
35
36
36
public IEnumerable < T > Items => _items ;
37
37
38
+ private readonly IEqualityComparer < T > _comparer ;
39
+
38
40
/// <summary>
39
41
/// Creates new instance of <see cref="nanoReferenceTableBase{T}"/> object.
40
42
/// </summary>
@@ -55,47 +57,26 @@ protected nanoReferenceTableBase(
55
57
56
58
_context = context ;
57
59
60
+ _comparer = comparer ;
61
+
58
62
_items = nanoTableItems ;
59
63
}
60
64
61
65
/// <inheritdoc/>
62
66
public void Write (
63
67
nanoBinaryWriter writer )
64
- {
65
- if ( _context . UsedElements != null )
66
- {
67
- foreach ( var item in _idsByItemsDictionary
68
- . Where ( item => _context . UsedElements . Contains ( ( ( IMetadataTokenProvider ) item . Key ) . MetadataToken ) )
69
- . OrderBy ( item => item . Value )
70
- . Select ( item => item . Key ) )
71
- {
72
- WriteSingleItem ( writer , item ) ;
73
- }
74
- }
75
- else
76
- {
77
- foreach ( var item in _idsByItemsDictionary
78
- . OrderBy ( item => item . Value )
79
- . Select ( item => item . Key ) )
80
- {
81
- WriteSingleItem ( writer , item ) ;
82
- }
83
- }
84
- }
85
-
86
- public void ForEachItems ( Action < uint , T > action )
87
68
{
88
69
foreach ( var item in _idsByItemsDictionary
89
- . OrderBy ( item => item . Value ) )
70
+ . OrderBy ( item => item . Value )
71
+ . Select ( item => item . Key ) )
90
72
{
91
- action ( item . Value , item . Key ) ;
73
+ WriteSingleItem ( writer , item ) ;
92
74
}
93
75
}
94
76
95
- public void ForEachItemInUse ( Action < uint , T > action )
77
+ public void ForEachItems ( Action < uint , T > action )
96
78
{
97
79
foreach ( var item in _idsByItemsDictionary
98
- . Where ( item => _context . UsedElements . Contains ( ( ( IMetadataTokenProvider ) item . Key ) . MetadataToken ) )
99
80
. OrderBy ( item => item . Value ) )
100
81
{
101
82
action ( item . Value , item . Key ) ;
@@ -114,16 +95,6 @@ public void AllocateStrings()
114
95
AllocateSingleItemStrings ( item ) ;
115
96
}
116
97
}
117
- public void AllocateStringsInUse ( )
118
- {
119
- foreach ( var item in _idsByItemsDictionary
120
- . Where ( item => _context . UsedElements . Contains ( ( ( IMetadataTokenProvider ) item . Key ) . MetadataToken ) )
121
- . OrderBy ( item => item . Value )
122
- . Select ( item => item . Key ) )
123
- {
124
- AllocateSingleItemStrings ( item ) ;
125
- }
126
- }
127
98
128
99
/// <summary>
129
100
/// Writes string reference ID related to passed string value into output stream.
@@ -179,28 +150,25 @@ protected abstract void WriteSingleItem(
179
150
nanoBinaryWriter writer ,
180
151
T item ) ;
181
152
182
- public IEnumerable < T > GetUsedItems ( )
153
+ /// <summary>
154
+ /// Remove unused items from table.
155
+ /// </summary>
156
+ public void RemoveUnusedItems ( HashSet < MetadataToken > set )
183
157
{
158
+ // build a collection of the current items that are present in the used items set
184
159
List < T > usedItems = new List < T > ( ) ;
185
160
186
- if ( _context . UsedElements != null )
187
- {
188
-
189
- foreach ( var item in _idsByItemsDictionary
190
- . Where ( item => _context . UsedElements . Contains ( ( ( IMetadataTokenProvider ) item . Key ) . MetadataToken ) ) )
191
- {
192
- usedItems . Add ( item . Key ) ;
193
- }
194
- }
195
- else
161
+ foreach ( var item in _idsByItemsDictionary
162
+ . Where ( item => set . Contains ( ( ( IMetadataTokenProvider ) item . Key ) . MetadataToken ) ) )
196
163
{
197
- foreach ( var item in _idsByItemsDictionary )
198
- {
199
- usedItems . Add ( item . Key ) ;
200
- }
164
+ usedItems . Add ( item . Key ) ;
201
165
}
202
166
203
- return usedItems ;
167
+ // re-create the items dictionary with the used items only
168
+ _idsByItemsDictionary = usedItems
169
+ . Select ( ( reference , index ) => new { reference , index } )
170
+ . ToDictionary ( item => item . reference , item => ( ushort ) item . index ,
171
+ _comparer ) ;
204
172
}
205
173
}
206
174
}
0 commit comments