Skip to content

Instantly share code, notes, and snippets.

@GuyInGrey
Created December 11, 2019 20:28
Show Gist options
  • Save GuyInGrey/a45ea421c1d61f8d946fd4eaf545cf05 to your computer and use it in GitHub Desktop.
Save GuyInGrey/a45ea421c1d61f8d946fd4eaf545cf05 to your computer and use it in GitHub Desktop.
public class Memory
{
public Dictionary<long, long> Storage = new Dictionary<long, long>();
public int Size => Storage.Count;
public Memory(long[] input)
{
for (var i = 0; i < input.Length; i++)
{
Storage.Add(i, input[i]);
}
}
public long Get(long index) =>
Storage.ContainsKey(index) ? Storage[index] : 0;
public void Set(long index, long value)
{
if (Storage.ContainsKey(index)) { Storage[index] = value; }
else { Storage.Add(index, value); }
}
public long[] SaveMemory()
{
var toReturn = new long[Storage.Keys.Max() + 1];
for (var i = (long)0; i < toReturn.Length; i++)
{
toReturn[i] = Get(i);
}
return toReturn;
}
public static Memory FromCSV(string s) =>
new Memory(s.Split(',').ToList().ConvertAll(a => long.Parse(a)).ToArray());
public static Memory FromPath(string s) =>
FromCSV(File.ReadAllText(s));
public static Memory FromArray(long[] input) =>
new Memory(input);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment