Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to represent sets of 3 values

Assume that I have 100 sets of 3 values. first value is time, second value is x position and 3rd value is y position. this is going to be the xy position of a point over 100 seconds for example. I have a PHP background. in PHP I would do it with a 3D associative array like this :

position["time"]["x"]["y"]

so I can access x,y values at a certain time. How would you do this in c#? I think generics like list and dictionary would do that. but I don't know how to implement key values for a 3D set of data.

like image 331
MehdiB Avatar asked Mar 29 '17 09:03

MehdiB


3 Answers

In C# there is types concept, you would need to create a concrete custom type either struct (value type )or class (reference type)

  • Value Types
  • Refrence Types

You can create a new type in this case i am creating class which would be like:

public class Position
{
   public DateTime Time {get;set;}
   public int X {get;set;}
   public int Y {get;set;}
}

Now at some point you would have a instance of class position:

Position postion = new Position(); // object created
positon.Time = DateTime.Now;
position.X = 1;
position.Y =0;

and the same way you would get the values back from it :

DateTime time = position.Time;
int positionX = position.X;
int positionY = position.Y;

I would suggest to read basics of type from MSDN here

and for sets part, we have Framework provided collections in C# which includes Array,List which can be used to hold multiple objects of Position.

Hope It helps!

like image 78
Ehsan Sajjad Avatar answered Nov 15 '22 22:11

Ehsan Sajjad


It looks like you want to access an x/y position at a given time. You might consider a combination of the other answers along with a dictionary.

Define a simple struct which holds x & y positions. It is important that this object is immutable.

public struct Position
{
    public int X { get; }
    public int Y { get; }

    public Position(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }
}

Now you can store these in a dictionary with (perhaps) DateTime as the key.

Dictionary<DateTime, Position> positions = new Dictionary<DateTime, Position>();
positions.Add(new DateTime(2017,3,29,10,0,0), new Position(10,10));

And you can read your position like

var pos = positions[new DateTime(2017,3,29,10,0,0)];
Console.WriteLine("{0}:{1}",pos.X,pos.Y);

A neat little addition to this is that if, for example, you are getting a list of these object you wish to lookup by time you can easily turn them into a dictionary. Say your get a list of these (from other answer):

public class PositionEntity
{
   public DateTime Time {get;set;}
   public int X {get;set;}
   public int Y {get;set;}
}

As a list entities you can do this:

IEnumerable<PositionEntity> entities = .... loaded from somewhere
var dict = entities.ToDictionary(k => k.Time, v => new Position(v.X,v.Y));
like image 39
Jamiec Avatar answered Nov 15 '22 21:11

Jamiec


I suggest defining an immutable type for this purpose and use a dictionary to store values since an object can be in one place at a specific time.

public class Position
{
    public Position(double x, double y)
    {
        X = x;
        Y = y;
    }

    public double X { get; }
    public double Y { get; }
}

use it like this:

var data = new Dictionary<TimeSpan, Position>();

//add data in two different ways
data.Add(TimeSpan.FromMilliseconds(10), new Position(0.1, 1));
data[TimeSpan.FromMilliseconds(15)] = new Position(10, 15);

//accessing data
var pos = data[TimeSpan.FromMilliseconds(10)];

//check if data exist for specific time
if (data.ContainsKey(TimeSpan.FromMilliseconds(15)))
{
    //do what you want
}
like image 35
Hamid Pourjam Avatar answered Nov 15 '22 21:11

Hamid Pourjam