public OrderedSparseLookupReader(PosData <T>[] data, bool hasEqualValueCompression = true) { this.data = data; this.hasEqualValueCompression = hasEqualValueCompression; current = nullPosData; nextIdx = 0; }
/// <summary> /// Use <paramref name="compressEqualValues"/> to produce a smaller lookup which won't work with <see cref="PosData.LookupExact"/> /// When using <paramref name="compressEqualValues"/> without <paramref name="insertDefaultEntries"/>, /// unspecified positions will default to the value of the previous specified position /// </summary> /// <param name="capacity">Defaults to 1M entries to reduce reallocations. Final built collection will be smaller. </param> /// <param name="compressEqualValues">Reduces the size of the map, but gives unspecified positions a value.</param> /// <param name="insertDefaultEntries">Ensures unspecified positions are assigned a default value when used with <paramref name="compressEqualValues"/></param> public OrderedSparseLookupBuilder(int capacity = 1048576, bool compressEqualValues = true, bool insertDefaultEntries = false) { list = new List <PosData <T> >(capacity); last = nullPosData; this.compressEqualValues = compressEqualValues; this.insertDefaultEntries = insertDefaultEntries; }
public T Get(int pos) { if (pos <= current.pos) { throw new ArgumentException($"Must read in ascending index order. Prev: {current.pos}, pos: {pos}"); } while (nextIdx < data.Length && data[nextIdx].pos <= pos) { current = data[nextIdx++]; } if (!hasEqualValueCompression && current.pos != pos) { throw new KeyNotFoundException($"Position does not exist in map. {pos} (X: {current.X}, Y: {current.Y})"); } return(current.value); }
public void Add(int pos, T value) { if (pos <= last.pos) { throw new ArgumentException($"Must build in ascending index order. Prev: {last.pos}, pos: {pos}"); } if (compressEqualValues) { if (insertDefaultEntries && pos >= last.pos + 2) { // make sure the values between last.pos and pos are 'empty' by ensuring at two positions later than last. // note that this won't make a new entry if last.value is default, and will update last if it does make a new value Add(last.pos + 1, default); } if (EqualityComparer <T> .Default.Equals(value, last.value)) { return; } } list.Add(last = new PosData <T>(pos, value)); }
public T Get(int x, int y) => Get(PosData.CoordsToPos(x, y));
public void Add(int x, int y, T value) => Add(PosData.CoordsToPos(x, y), value);
public PosData(int x, int y, T value) : this(PosData.CoordsToPos(x, y), value) { }