Shift all values of Dictionary to a new key

时间:2016-07-11 22:29:58

标签: c# dictionary

I have a generic dictionary which stores a Tile definition for each coordinate in a map of my grid-based game.

Dictionary<IntVector2, Tile> tiles;

The map can be arbitrarily sized with this setup, since new coordinates can just be added without changing anything else. However, I would like to use the (0,0) coordinate as a map pivot for other calculations, which requires me to be able to change the the map center coordinate after a map was created.

Is there a cleaner and more performant way to shift all values (Tile) within the dictionary to new coordinates, create new keys if necessary and delete all unused ones afterwards?

So far I have this:

public void MovePivot(int xDelta, int yDelta)
{
    // Copy my existing tile map.
    Dictionary<IntVector2, Tile> tilesCopy = new Dictionary<IntVector2, Tile>(tiles);

    // Initialize a new empty one.
    tiles = new Dictionary<IntVector2, Tile>();

    // Copy all old values into the new one, but shift each coordinate.
    foreach (var tile in tilesCopy)
    {
        IntVector2 newKey = tile.Key + new IntVector2(xDelta, yDelta);
        tiles.Add(newKey, tile.Value);
    }
}

Would this be possible "in place" without copying my dictionary?

1 个答案:

答案 0 :(得分:0)

可以实现一种新的字典类型,用于保存当前移位的内存并在读/写期间执行移位。

示例用法:

AdjustableDictionary<int, string> map = new AdjustableDictionary<int, string>((key, adj) => key + adj);

这应该很接近。值与引用类型可能存在问题。

public class AdjustableDictionary<K, V> 
{
    public K CurrentAdjustment { get; set; }        
    public int Count { get { return _dictionary.Count; } }
    public ICollection<K> Keys { get { return _dictionary.Keys.Select(k => AdjustKey(k)).ToList(); } }

    private IDictionary<K, V> _dictionary;
    private Func<K, K, K> _adjustKey;

    public AdjustableDictionary(Func<K, K, K> keyAdjuster = null)
    {
        _dictionary = new Dictionary<K, V>();
        _adjustKey = keyAdjuster;
    }

    public void Add(K key, V value)
    {
        _dictionary.Add(AdjustKey(key), value);
    }

    public bool ContainsKey(K key)
    {
        return _dictionary.ContainsKey(AdjustKey(key));
    }

    public bool Remove(K key)
    {
        return _dictionary.Remove(AdjustKey(key));
    }

    public bool TryGetValue(K key, out V value)
    {
        return _dictionary.TryGetValue(AdjustKey(key), out value);
    }

    public ICollection<V> Values { get { return _dictionary.Values; } }

    public V this[K key] {
        get {
            return _dictionary[AdjustKey(key)];
        }
        set {
            _dictionary[AdjustKey(key)] = value;
        }
    }

    public void Clear()
    {
        _dictionary.Clear();
    }

    private K AdjustKey(K key)
    {
        return _adjustKey != null
            ? _adjustKey(key, CurrentAdjustment)
            : key;
    }
}

上述代码大部分都来自此VirtualDictionary answer