Skip to content

Commit f506279

Browse files
committed
Add overloads that accept capacity
1 parent 5ebeb88 commit f506279

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

ValveKeyValue/ValveKeyValue.Test/KVObjectApiTestCase.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,46 @@ public void EmptyArrayFactory()
496496
Assert.That((string)arr[0], Is.EqualTo("element"));
497497
}
498498

499+
[Test]
500+
public void ArrayFactoryWithCapacity()
501+
{
502+
var arr = KVObject.Array(4);
503+
504+
Assert.That(arr.IsArray, Is.True);
505+
Assert.That(arr.ValueType, Is.EqualTo(KVValueType.Array));
506+
Assert.That(arr.Count, Is.EqualTo(0));
507+
508+
arr.Add("a");
509+
arr.Add("b");
510+
Assert.That(arr.Count, Is.EqualTo(2));
511+
}
512+
513+
[Test]
514+
public void CollectionFactoryWithCapacity()
515+
{
516+
var obj = KVObject.Collection(4);
517+
518+
Assert.That(obj.ValueType, Is.EqualTo(KVValueType.Collection));
519+
Assert.That(obj.Count, Is.EqualTo(0));
520+
521+
obj.Add("key", "value");
522+
Assert.That(obj.Count, Is.EqualTo(1));
523+
Assert.That((string)obj["key"], Is.EqualTo("value"));
524+
}
525+
526+
[Test]
527+
public void ListCollectionFactoryWithCapacity()
528+
{
529+
var obj = KVObject.ListCollection(4);
530+
531+
Assert.That(obj.ValueType, Is.EqualTo(KVValueType.Collection));
532+
Assert.That(obj.Count, Is.EqualTo(0));
533+
534+
obj.Add("key", "value");
535+
Assert.That(obj.Count, Is.EqualTo(1));
536+
Assert.That((string)obj["key"], Is.EqualTo("value"));
537+
}
538+
499539
[Test]
500540
public void DefaultConstructorCreatesDictBacked()
501541
{

ValveKeyValue/ValveKeyValue.Test/Test Data/apisurface.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,14 @@ public class ValveKeyValue.KVObject
156156
public void Add(string key, ValveKeyValue.KVObject value);
157157
public void Add(ValveKeyValue.KVObject value);
158158
public static ValveKeyValue.KVObject Array();
159+
public static ValveKeyValue.KVObject Array(int capacity);
159160
public static ValveKeyValue.KVObject Array(System.Collections.Generic.IEnumerable`1[[ValveKeyValue.KVObject]] elements);
160161
public byte[] AsBlob();
161162
public ReadOnlySpan`1[[byte]] AsSpan();
162163
public static ValveKeyValue.KVObject Blob(byte[] data);
163164
public void Clear();
164165
public static ValveKeyValue.KVObject Collection();
166+
public static ValveKeyValue.KVObject Collection(int capacity);
165167
public static ValveKeyValue.KVObject Collection(System.Collections.Generic.IEnumerable`1[[System.Collections.Generic.KeyValuePair`2[[string, ValveKeyValue.KVObject]]]] children);
166168
public bool ContainsKey(string name);
167169
public bool Equals(object obj);
@@ -181,6 +183,7 @@ public class ValveKeyValue.KVObject
181183
public int GetHashCode();
182184
public Type GetType();
183185
public static ValveKeyValue.KVObject ListCollection();
186+
public static ValveKeyValue.KVObject ListCollection(int capacity);
184187
public static ValveKeyValue.KVObject ListCollection(System.Collections.Generic.IEnumerable`1[[System.Collections.Generic.KeyValuePair`2[[string, ValveKeyValue.KVObject]]]] children);
185188
protected object MemberwiseClone();
186189
public static ValveKeyValue.KVObject Null();

ValveKeyValue/ValveKeyValue/KVObject.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,12 @@ public void Clear()
368368
public static KVObject Collection()
369369
=> new();
370370

371+
/// <summary>
372+
/// Creates an empty dictionary-backed collection with the specified capacity.
373+
/// </summary>
374+
public static KVObject Collection(int capacity)
375+
=> new(KVValueType.Collection, new Dictionary<string, KVObject>(capacity));
376+
371377
/// <summary>
372378
/// Creates a dictionary-backed collection from the given children.
373379
/// </summary>
@@ -392,6 +398,13 @@ public static KVObject Collection(IEnumerable<KeyValuePair<string, KVObject>> ch
392398
public static KVObject ListCollection()
393399
=> new(KVValueType.Collection, new List<KeyValuePair<string, KVObject>>());
394400

401+
/// <summary>
402+
/// Creates an empty list-backed collection with the specified capacity.
403+
/// Preserves insertion order and allows duplicate keys (used for KV1 format).
404+
/// </summary>
405+
public static KVObject ListCollection(int capacity)
406+
=> new(KVValueType.Collection, new List<KeyValuePair<string, KVObject>>(capacity));
407+
395408
/// <summary>
396409
/// Creates a list-backed collection from the given children.
397410
/// Preserves insertion order and allows duplicate keys (used for KV1 format).
@@ -408,6 +421,12 @@ public static KVObject ListCollection(IEnumerable<KeyValuePair<string, KVObject>
408421
public static KVObject Array()
409422
=> new(KVValueType.Array, new List<KVObject>());
410423

424+
/// <summary>
425+
/// Creates an empty array-valued <see cref="KVObject"/> with the specified capacity.
426+
/// </summary>
427+
public static KVObject Array(int capacity)
428+
=> new(KVValueType.Array, new List<KVObject>(capacity));
429+
411430
/// <summary>
412431
/// Creates an array-valued <see cref="KVObject"/> from the given elements.
413432
/// </summary>

0 commit comments

Comments
 (0)