You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.4 KiB
93 lines
2.4 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Container
|
|
{
|
|
public class FixedRow<T> : IEnumerable<T>
|
|
{
|
|
int fixSize;
|
|
|
|
T[] data;
|
|
|
|
public int Count => data.Length;
|
|
public T this[int index] => data[index];
|
|
|
|
public FixedRow(int fixSize)
|
|
{
|
|
data = new T[0];
|
|
this.fixSize = fixSize;
|
|
}
|
|
|
|
public void PushBack(T v)
|
|
{
|
|
if (data.Length >= fixSize) return;
|
|
|
|
T[] newData = new T[data.Length + 1];
|
|
Array.Copy(data, newData, data.Length);
|
|
newData[data.Length] = v;
|
|
|
|
data = newData;
|
|
}
|
|
|
|
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>)data).GetEnumerator();
|
|
IEnumerator IEnumerable.GetEnumerator() => data.GetEnumerator();
|
|
}
|
|
|
|
public class HorizontalDataMatrix<T> : IEnumerable<FixedRow<T>>
|
|
{
|
|
int columnCount;
|
|
int count;
|
|
|
|
List<FixedRow<T>> data;
|
|
|
|
public int ColumnCount => columnCount;
|
|
public int RowCount => data.Count;
|
|
public int Count => count;
|
|
|
|
public FixedRow<T> this[int index] => data[index];
|
|
public T this[int row, int column] => data[row][column];
|
|
|
|
public HorizontalDataMatrix(int columnCount)
|
|
{
|
|
this.columnCount = columnCount;
|
|
data = new List<FixedRow<T>>();
|
|
count = 0;
|
|
}
|
|
|
|
public HorizontalDataMatrix(IEnumerable<T> source, int columnCount)
|
|
{
|
|
this.columnCount = columnCount;
|
|
data = new List<FixedRow<T>>();
|
|
count = 0;
|
|
|
|
foreach (var v in source)
|
|
PushBack(v);
|
|
}
|
|
|
|
public void PushBack(T v)
|
|
{
|
|
int row = count / columnCount;
|
|
|
|
if (row >= data.Count)
|
|
data.Add(new FixedRow<T>(columnCount));
|
|
|
|
data[row].PushBack(v);
|
|
++count;
|
|
}
|
|
|
|
public bool TryGetValue(int row, int column, out T value)
|
|
{
|
|
value = default;
|
|
|
|
if (row < 0 || row >= data.Count) return false;
|
|
if (column < 0 || column >= data[row].Count) return false;
|
|
|
|
value = data[row][column];
|
|
return true;
|
|
}
|
|
|
|
public IEnumerator<FixedRow<T>> GetEnumerator() => data.GetEnumerator();
|
|
IEnumerator IEnumerable.GetEnumerator() => data.GetEnumerator();
|
|
}
|
|
}
|