Skip to content

Commit

Permalink
Bugfix in OffsetGrid and add DictionaryGenerator.StoreGeneratedValues
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixill committed Dec 18, 2023
1 parent 921bf0f commit 7308eeb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,62 @@ public class DictionaryGenerator<K, V> : IDictionary<K, V>
/// </summary>
public IDictionary<K, V> Dict { get; }

/// <summary>
/// Whether or not to store newly generated values when a key is not
/// found in the dictionary during a get operation. Does not affect
/// previously generated values, whether they were or weren't stored.
/// </summary>
public bool StoreGeneratedValues = true;

/// <summary>
/// Creates a new, empty Dictionary&lt;K, V&gt; with a
/// DefaultGenerator.
/// </summary>
public DictionaryGenerator()
public DictionaryGenerator(bool storeValues = true)
{
Dict = new Dictionary<K, V>();
Generator = new DefaultGenerator<K, V>();
StoreGeneratedValues = storeValues;
}

/// <summary>
/// Creates a new, empty Dictionary&lt;K, V&gt; and wraps it with the
/// provided Generator.
/// </summary>
public DictionaryGenerator(Generator<K, V> gen)
public DictionaryGenerator(Generator<K, V> gen, bool storeValues = true)
{
Dict = new Dictionary<K, V>();
Generator = gen;
StoreGeneratedValues = storeValues;
}

/// <summary>
/// Wraps an existing IDictionary with the provided Generator.
/// </summary>
/// <param name="dict">The Dictionary to use.</param>
/// <param name="gen">The Generator to use.</param>
public DictionaryGenerator(IDictionary<K, V> dict, Generator<K, V> gen)
public DictionaryGenerator(IDictionary<K, V> dict, Generator<K, V> gen, bool storeValues = true)
{
Dict = dict;
Generator = gen;
StoreGeneratedValues = storeValues;
}

/// <summary>
/// Gets or sets the value associated with the specified key.
///
/// If the specified key is not found, a get operation automatically
/// generates a value, sets it to that key, and returns it.
/// generates a value, sets it to that key (if the DictionaryGenerator
/// is set to do so), and returns it.
/// </summary>
/// <param name="key">The key of the value to get or set.</param>
public V this[K key]
{
get
{
if (Dict.ContainsKey(key)) return Dict[key];
else return Add(key);
else if (StoreGeneratedValues) return Add(key);
else return Generator.Generate(key);
}
set => Dict[key] = value;
}
Expand All @@ -90,7 +102,7 @@ public V Add(K key)
}
else if (Dict.ContainsKey(key))
{
throw new ArgumentException("Key " + key.ToString() + " already exists in map.");
throw new ArgumentException("Key " + key.ToString() + " already exists in the Dictionary.");
}
else
{
Expand Down
20 changes: 10 additions & 10 deletions CSharp.Nixill/src/Grid/OffsetGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,31 +191,31 @@ public GridReference IndexOfTransposed(T item)

public void InsertColumnShiftLeft(int before)
{
BackingGrid.InsertColumn(before);
BackingGrid.InsertColumn(before + ColumnOffset);
ColumnOffset += 1;
}

public void InsertColumnShiftLeft<U>(int before, IEnumerable<U> column) where U : T
{
BackingGrid.InsertColumn(before, column);
BackingGrid.InsertColumn(before + ColumnOffset, column);
ColumnOffset += 1;
}

public void InsertColumnShiftLeft(int before, T columnItem)
{
BackingGrid.InsertColumn(before, columnItem);
BackingGrid.InsertColumn(before + ColumnOffset, columnItem);
ColumnOffset += 1;
}

public void InsertColumnShiftLeft(int before, Func<T> columnItemFunc)
{
BackingGrid.InsertColumn(before, columnItemFunc);
BackingGrid.InsertColumn(before + ColumnOffset, columnItemFunc);
ColumnOffset += 1;
}

public void InsertColumnShiftLeft(int before, Func<int, T> columnItemFunc)
{
BackingGrid.InsertColumn(before, columnItemFunc);
BackingGrid.InsertColumn(before + ColumnOffset, columnItemFunc);
ColumnOffset += 1;
}

Expand All @@ -227,31 +227,31 @@ public void InsertColumnShiftLeft(int before, Func<int, T> columnItemFunc)

public void InsertRowShiftUp(int before)
{
BackingGrid.InsertRow(before);
BackingGrid.InsertRow(before + RowOffset);
RowOffset += 1;
}

public void InsertRowShiftUp<U>(int before, IEnumerable<U> row) where U : T
{
BackingGrid.InsertRow(before, row);
BackingGrid.InsertRow(before + RowOffset, row);
RowOffset += 1;
}

public void InsertRowShiftUp(int before, T rowItem)
{
BackingGrid.InsertRow(before, rowItem);
BackingGrid.InsertRow(before + RowOffset, rowItem);
RowOffset += 1;
}

public void InsertRowShiftUp(int before, Func<T> rowItemFunc)
{
BackingGrid.InsertRow(before, rowItemFunc);
BackingGrid.InsertRow(before + RowOffset, rowItemFunc);
RowOffset += 1;
}

public void InsertRowShiftUp(int before, Func<int, T> rowItemFunc)
{
BackingGrid.InsertRow(before, rowItemFunc);
BackingGrid.InsertRow(before + RowOffset, rowItemFunc);
RowOffset += 1;
}

Expand Down

0 comments on commit 7308eeb

Please sign in to comment.