Skip to content

Instantly share code, notes, and snippets.

@jquintus
Created June 26, 2017 22:03
Show Gist options
  • Save jquintus/d46c79f1ad1adb49a5229d129e7dd46c to your computer and use it in GitHub Desktop.
Save jquintus/d46c79f1ad1adb49a5229d129e7dd46c to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
char[,] cars = new char[3, 4]
{
{'a', 'b', 'c', 'd' },
{'e', 'f', 'g', 'h' },
{'h', 'i', 'j', 'k' },
};
var arrs = new char[][]
{
new char[]{'a', 'b', 'c', 'd' },
new char[]{'e', 'f', 'g', 'h' },
new char[]{'h', 'i', 'j', 'k' },
};
var arrayOfList = new List<char>[]
{
new List<char>{'a', 'b', 'c', 'd' },
new List<char>{'e', 'f', 'g', 'h' },
new List<char>{'h', 'i', 'j', 'k' },
};
PrintIt("Double Array", cars.ToListOfLists());
PrintIt("Arrays", arrs);
PrintIt("arrayOfList", arrayOfList);
}
public static void PrintIt<T>(string name, IList<IList<T>> lists)
{
Console.WriteLine(name);
foreach (var row in lists)
{
foreach (var column in row)
{
Console.Write($" {column} ");
}
Console.WriteLine();
}
Console.WriteLine();
}
}
public static class ListListRevolutionHelper
{
public static IList<IList<T>> ToListOfLists<T>(this T[,] src)
{
return new ListListRevolution<T>(src);
}
}
public class ListListRevolution<T> : IList<IList<T>>
{
private class Row<T> : IList<T>
{
private readonly int _rowId;
private readonly T[,] _data;
public Row(T[,] data, int rowId)
{
_data = data;
_rowId = rowId;
}
public T this[int index]
{
get => _data[_rowId, index];
set => _data[_rowId, index] = value;
}
public int Count => _data.GetLength(1);
public bool IsReadOnly => true;
public void Add(T item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(T item)
{
for (int i = 0; i < Count; i++)
{
if (this[i].Equals(item)) return true;
}
return false;
}
public void CopyTo(T[] array, int arrayIndex)
{
for (int i = arrayIndex; i < Count; i++)
{
array[i - arrayIndex] = this[i];
}
}
public IEnumerator<T> GetEnumerator()
{
IEnumerable<T> LoopIt()
{
for (int i = 0; i < Count; i++)
{
yield return this[i];
}
}
return LoopIt().GetEnumerator();
}
public int IndexOf(T item)
{
for (int i = 0; i < Count; i++)
{
if (this[i].Equals(item)) return i;
}
return -1;
}
public void Insert(int index, T item)
{
throw new NotImplementedException();
}
public bool Remove(T item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
private T[,] _data;
public ListListRevolution(T[,] data)
{
_data = data;
}
public IList<T> this[int index]
{
get => new Row<T>(_data, index);
set => throw new NotImplementedException("Josh is too lazy to do this at the moment");
}
public int Count => _data.GetLength(0);
public bool IsReadOnly => true;
public void Add(IList<T> item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(IList<T> item)
{
throw new NotImplementedException("Josh lazy again");
}
public void CopyTo(IList<T>[] array, int arrayIndex)
{
throw new NotImplementedException("More lazy");
}
public IEnumerator<IList<T>> GetEnumerator()
{
IEnumerable<IList<T>> LoopIt()
{
for (int i = 0; i < Count; i++)
{
yield return this[i];
}
}
return LoopIt().GetEnumerator();
}
public int IndexOf(IList<T> item)
{
throw new NotImplementedException("More lazy");
}
public void Insert(int index, IList<T> item)
{
throw new NotImplementedException();
}
public bool Remove(IList<T> item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment