22using System . Diagnostics . CodeAnalysis ;
33using System . Globalization ;
44using System . Linq ;
5+ using System . Runtime . InteropServices ;
56
67namespace ValveKeyValue
78{
@@ -55,7 +56,7 @@ public partial class KVObject : IReadOnlyDictionary<string, KVObject>
5556 public int Count => ValueType switch
5657 {
5758 KVValueType . Collection => GetCollectionCount ( ) ,
58- KVValueType . Array => GetArrayList ( ) . Count ,
59+ KVValueType . Array => ( ( List < KVObject > ) _ref ! ) . Count ,
5960 _ => 0 ,
6061 } ;
6162
@@ -223,10 +224,11 @@ public KVObject this[int index]
223224 {
224225 if ( ValueType == KVValueType . Array )
225226 {
226- return GetArrayList ( ) [ index ] ;
227+ var listArray = ( List < KVObject > ) _ref ! ;
228+ return listArray [ index ] ;
227229 }
228230
229- if ( _ref is List < KeyValuePair < string , KVObject > > list && ValueType == KVValueType . Collection )
231+ if ( ValueType == KVValueType . Collection && _ref is List < KeyValuePair < string , KVObject > > list )
230232 {
231233 return list [ index ] . Value ;
232234 }
@@ -280,7 +282,7 @@ public bool ContainsKey(string name)
280282 /// </summary>
281283 public IEnumerable < KeyValuePair < string , KVObject > > Children => ValueType switch
282284 {
283- KVValueType . Collection => GetCollectionChildren ( ) ,
285+ KVValueType . Collection => EnumerateCollection ( ) ,
284286 KVValueType . Array => EnumerateArray ( ) ,
285287 _ => [ ] ,
286288 } ;
@@ -345,7 +347,8 @@ public void Add(KVObject value)
345347 throw new InvalidOperationException ( $ "Cannot add an array element to a { ValueType } value.") ;
346348 }
347349
348- GetArrayList ( ) . Add ( value ?? Null ( ) ) ;
350+ var list = ( List < KVObject > ) _ref ! ;
351+ list . Add ( value ?? Null ( ) ) ;
349352 }
350353
351354 /// <summary>
@@ -374,7 +377,8 @@ public void RemoveAt(int index)
374377 throw new InvalidOperationException ( $ "Cannot remove by index from a { ValueType } value.") ;
375378 }
376379
377- GetArrayList ( ) . RemoveAt ( index ) ;
380+ var list = ( List < KVObject > ) _ref ! ;
381+ list . RemoveAt ( index ) ;
378382 }
379383
380384 /// <summary>
@@ -492,7 +496,7 @@ public static KVObject Blob(byte[] data)
492496
493497 #endregion
494498
495- #region Blob access
499+ #region Blob and list access
496500
497501 /// <summary>Gets the binary blob data as a byte array.</summary>
498502 public byte [ ] AsBlob ( )
@@ -505,26 +509,19 @@ public byte[] AsBlob()
505509 return ( byte [ ] ) _ref ! ;
506510 }
507511
508- /// <summary>Gets the binary blob data as a span.</summary>
509- public ReadOnlySpan < byte > AsSpan ( )
512+ /// <summary>Gets the array elements as a span.</summary>
513+ public Span < KVObject > AsArraySpan ( )
510514 {
511- if ( ValueType != KVValueType . BinaryBlob )
515+ if ( ValueType != KVValueType . Array )
512516 {
513- throw new InvalidOperationException ( $ "Cannot get blob span from a { ValueType } value.") ;
517+ throw new InvalidOperationException ( $ "Cannot get list from a { ValueType } value.") ;
514518 }
515519
516- return ( ( byte [ ] ) _ref ! ) . AsSpan ( ) ;
520+ return CollectionsMarshal . AsSpan ( ( List < KVObject > ) _ref ! ) ;
517521 }
518522
519523 #endregion
520524
521- #region Internal accessors
522-
523- internal List < KVObject > GetArrayList ( )
524- => ( List < KVObject > ) _ref ! ;
525-
526- #endregion
527-
528525 #region Private helpers
529526
530527 private enum InsertionBehavior
@@ -593,7 +590,7 @@ private bool TryInsert(string key, KVObject value, InsertionBehavior behavior)
593590 _ => 0 ,
594591 } ;
595592
596- private IEnumerable < KeyValuePair < string , KVObject > > GetCollectionChildren ( ) => _ref switch
593+ private IEnumerable < KeyValuePair < string , KVObject > > EnumerateCollection ( ) => _ref switch
597594 {
598595 Dictionary < string , KVObject > dict => dict ,
599596 List < KeyValuePair < string , KVObject > > list => list ,
@@ -602,10 +599,10 @@ private bool TryInsert(string key, KVObject value, InsertionBehavior behavior)
602599
603600 private IEnumerable < KeyValuePair < string , KVObject > > EnumerateArray ( )
604601 {
605- var list = GetArrayList ( ) ;
606- for ( var i = 0 ; i < list . Count ; i ++ )
602+ var list = ( List < KVObject > ) _ref ! ;
603+ foreach ( var item in list )
607604 {
608- yield return new KeyValuePair < string , KVObject > ( null ! , list [ i ] ) ;
605+ yield return new KeyValuePair < string , KVObject > ( null ! , item ) ;
609606 }
610607 }
611608
@@ -628,8 +625,8 @@ private static bool TryFindInList(List<KeyValuePair<string, KVObject>> list, str
628625 {
629626 KVValueType . String => $ "\" { _ref } \" ",
630627 KVValueType . Null => "null" ,
631- KVValueType . Collection => $ "Collection ({ GetCollectionCount ( ) } items)",
632- KVValueType . Array => $ "Array ({ GetArrayList ( ) . Count } items)",
628+ KVValueType . Collection => $ "Collection ({ Count } items)",
629+ KVValueType . Array => $ "Array ({ Count } items)",
633630 _ => $ "{ ToString ( CultureInfo . InvariantCulture ) } ({ ValueType } )",
634631 } ;
635632
0 commit comments