From df659e32141c24a05bf80ffafaecb5dcdb4e11b3 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Thu, 8 Oct 2015 12:36:10 -0700 Subject: [PATCH] Add missing ArrayBuilder.cs file --- .../Collections/Generic/ArrayBuilder.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/Common/src/System/Collections/Generic/ArrayBuilder.cs diff --git a/src/Common/src/System/Collections/Generic/ArrayBuilder.cs b/src/Common/src/System/Collections/Generic/ArrayBuilder.cs new file mode 100644 index 00000000000..9a969515fb3 --- /dev/null +++ b/src/Common/src/System/Collections/Generic/ArrayBuilder.cs @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; + +namespace System.Collections.Generic +{ + // + // Helper class for building lists that avoids unnecessary allocation + // + internal struct ArrayBuilder + { + T[] _items; + int _count; + + public T[] ToArray() + { + if (_items == null) + return Array.Empty(); + if (_count != _items.Length) + Array.Resize(ref _items, _count); + return _items; + } + + public void Add(T item) + { + if (_items == null || _count == _items.Length) + Array.Resize(ref _items, 2 * _count + 1); + _items[_count++] = item; + } + + public int Count + { + get + { + return _count; + } + } + + public T this[int index] + { + get + { + return _items[index]; + } + } + } +}