This repository was archived by the owner on Jan 23, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +196
-129
lines changed
System/Collections/Generic Expand file tree Collapse file tree 4 files changed +196
-129
lines changed Original file line number Diff line number Diff line change 670670 </ItemGroup >
671671
672672 <Import Project =" GenerateCompilerResponseFile.targets" />
673- </Project >
673+ </Project >
Original file line number Diff line number Diff line change 7373 <Compile Include =" $(MSBuildThisFileDirectory)System\Collections\Generic\KeyNotFoundException.cs" />
7474 <Compile Include =" $(MSBuildThisFileDirectory)System\Collections\Generic\KeyValuePair.cs" />
7575 <Compile Include =" $(MSBuildThisFileDirectory)System\Collections\Generic\NonRandomizedStringEqualityComparer.cs" />
76+ <Compile Include =" $(MSBuildThisFileDirectory)System\Collections\Generic\ValueListBuilder.cs" />
7677 <Compile Include =" $(MSBuildThisFileDirectory)System\Collections\Generic\List.cs" />
7778 <Compile Include =" $(MSBuildThisFileDirectory)System\Collections\HashHelpers.cs" />
7879 <Compile Include =" $(MSBuildThisFileDirectory)System\Collections\ICollection.cs" />
Original file line number Diff line number Diff line change 1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+ // See the LICENSE file in the project root for more information.
4+
5+ using System . Buffers ;
6+ using System . Diagnostics ;
7+ using System . Runtime . CompilerServices ;
8+
9+ namespace System . Collections . Generic
10+ {
11+ internal ref struct ValueListBuilder < T >
12+ {
13+ private Span < T > _span ;
14+ private T [ ] _arrayFromPool ;
15+ private int _pos ;
16+
17+ public ValueListBuilder ( Span < T > initialSpan )
18+ {
19+ _span = initialSpan ;
20+ _arrayFromPool = null ;
21+ _pos = 0 ;
22+ }
23+
24+ public int Length => _pos ;
25+
26+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
27+ public void Append ( T item )
28+ {
29+ int pos = _pos ;
30+ if ( pos >= _span . Length )
31+ Grow ( ) ;
32+
33+ _span [ pos ] = item ;
34+ _pos = pos + 1 ;
35+ }
36+
37+ public ReadOnlySpan < T > AsReadOnlySpan ( )
38+ {
39+ return _span . Slice ( 0 , _pos ) ;
40+ }
41+
42+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
43+ public void Dispose ( )
44+ {
45+ if ( _arrayFromPool != null )
46+ {
47+ ArrayPool < T > . Shared . Return ( _arrayFromPool ) ;
48+ _arrayFromPool = null ;
49+ }
50+ }
51+
52+ private void Grow ( )
53+ {
54+ T [ ] array = ArrayPool < T > . Shared . Rent ( _span . Length * 2 ) ;
55+
56+ bool success = _span . TryCopyTo ( array ) ;
57+ Debug . Assert ( success ) ;
58+
59+ T [ ] toReturn = _arrayFromPool ;
60+ _span = _arrayFromPool = array ;
61+ if ( toReturn != null )
62+ {
63+ ArrayPool < T > . Shared . Return ( toReturn ) ;
64+ }
65+ }
66+ }
67+ }
You can’t perform that action at this time.
0 commit comments