3

In C#, I want to have a data structure that maps (x,y) coordinates to (x,y). How can I do something like this?

I don't want to convert the x,y coordinate into a single value using a formula like y*w+x. Is there a way to have dictionary<key,key,(value,value)>.

If I put the key as Tuple, then its an object and Tuple(1,1) does not equal Tuple(1,1). So I don't think I can find keys in that sense.

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
omega
  • 40,311
  • 81
  • 251
  • 474

7 Answers7

5

If you use struct rather than class the keys will be compared based on the values not based on the reference, because the structs are value type

public struct Point
{
   public int x;
   public int y;
   public Point(int x, int y)
   {
      this.x = x;
      this.y = y;
   }
 }

Then using this struct

var dic = new Dictionary<Point,Point>();
dic.Add(new Point(1,1), new Point(1,2));

var f = dic[new Point(1,1)];
Console.WriteLine(f.x); //Output will be 1
Taher Rahgooy
  • 6,528
  • 3
  • 19
  • 30
3

Can't you just define (or use the pre-existing structure from the System.Drawing namespace) Point as a structure that holds (x, y) and then use that in your Dictionary<Point, Point>?

Cid
  • 14,968
  • 4
  • 30
  • 45
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1
var dict = new Dictionary<Point,string>();
dict[new Point(1,3)] = "asd";
Backs
  • 24,430
  • 5
  • 58
  • 85
1

You can use any kind of object for as a dictionary key, as long as you correctly override GetHashCode() for that type. This is what the dictionary will use to determine whether a specific key exists in it or not. Therefore you can create your own class and use that as a key.

Check this answer for more information: What is the best algorithm for an overridden System.Object.GetHashCode?

Community
  • 1
  • 1
Ioannis Karadimas
  • 7,746
  • 3
  • 35
  • 45
1

I'm writing this answer because the other ones seem to map to 1 string, and you need to map to 2 strings. You could try using Point to stores the x and y position and then create a dictionary of a Tuple.

var points = new Dictionary<Point,Tuple<string, string>>();
points[new Point(1,1)] = new Tuple<string, string>("2","2");
Shane
  • 336
  • 1
  • 11
0
public class Point
{
    public string X {get; protected set;}
    public string Y {get; protected set;}
    public Point(string x, string y)
    {
        X = x;
        Y = y;
    }

    public HashSet<string> GetSet()
    {
        HashSet<string> result = new HashSet<string>();
        result.Add(this.X);
        result.Add(this.Y);
        return result;
    }
}

CreateSetComparer allows the dictionary to use the values of the set

List<Point> pointSource = GetSet();
Dictionary<HashSet<string>, Point> points = new Dictionary<HashSet<string>, Point>(HashSet<string>.CreateSetComparer());

foreach(Point d in pointSource)
{
    points.Add(d.GetSet(), d);
}

And finding a point:

HashSet<string> key = new HashSet<string>();
key.Add("X");
key.Add("Y");
Point myPoint = points[key];

May this link is help full HashCode

Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28
-1
Dictionary<string, int> dictionary =  new Dictionary<string, int>();

dictionary.Add("cat", 2);
dictionary.Add("dog", 1);
dictionary.Add("llama", 0);
dictionary.Add("iguana", -1);


if (dictionary.ContainsKey("apple"))
{
    int value = dictionary["apple"];
    Console.WriteLine(value);
}