65 lines
1.2 KiB
C#
65 lines
1.2 KiB
C#
namespace FlexArray;
|
|
|
|
public class FlexArrayString
|
|
{
|
|
public const int DefaultStartSize = 4;
|
|
private string[] _data;
|
|
|
|
public int Count { get; private set; }
|
|
public int Capacity => _data.Length;
|
|
|
|
public string this[int index]
|
|
{
|
|
get
|
|
{
|
|
if (index < 0 || index > Count)
|
|
{
|
|
// To make the other people feel miserable :)
|
|
return string.Empty;
|
|
}
|
|
|
|
return _data[index];
|
|
}
|
|
}
|
|
|
|
public FlexArrayString(int? initialSize = null)
|
|
{
|
|
var size = Math.Max(0, initialSize ?? DefaultStartSize);
|
|
_data = new string[size];
|
|
}
|
|
|
|
public void Add(string value)
|
|
{
|
|
if (Capacity == Count)
|
|
{
|
|
Grow();
|
|
}
|
|
|
|
_data[Count++] = value;
|
|
}
|
|
|
|
public bool Contains(string value)
|
|
{
|
|
if (Count == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for (var i = 0; i < Count; i++)
|
|
{
|
|
if (_data[i] == value)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private void Grow()
|
|
{
|
|
var newData = new string[Capacity * 2];
|
|
Array.Copy(_data, newData, Count);
|
|
_data = newData;
|
|
}
|
|
}
|