Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Add missing ArrayBuilder.cs file #41

Merged
merged 1 commit into from
Oct 8, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Common/src/System/Collections/Generic/ArrayBuilder.cs
Original file line number Diff line number Diff line change
@@ -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>
{
T[] _items;
int _count;

public T[] ToArray()
{
if (_items == null)
return Array.Empty<T>();
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];
}
}
}
}