views:

551

answers:

4

I need to look up a value in a "table" where a table can be an array or whatever really.
On paper it looks like this (reduced and generalized):

Size      500     750    1000    1250   1500 (speed)
--------------------------------------------
6x5       0.1     0.5     0.55   0.58   0.8
6x4       0.01    0.1     0.4    0.5    0.9
8x5       0.5     0.9     1.1    1.5    2.0
10x5      1.2     1.5     2.0    2.7    3.0
12x6      2.6     3.0     4.4    5.1    7.0  (pressure)

I need to somehow extract the pressure when I have a variable size and speed.
I right now I have put each row in a separate array, but I would like to avoid a bunch of if else's, but I don't really know a better way. Thanks for the help.

A: 

At the top of my head, you could use a DataTable.

Size      Speed     Pressure
--------------------------------------------
6x5       500       0.1
6x5       750       0.5
6x5       1000      0.55
6x5       1250      0.58
6x5       1500      0.8
6x4       500       0.01
shahkalpesh
+1  A: 

if the size is unique, make it the key to a Dictionary and then you can use it to get the other elements...

Ali Shafai
A: 

Create a struct to keep the size and speed pair:

public struct SizeSpeedKey
{
public string Size;
public int Speed;

public SizeSpeedKey(string size, int speed)
{
  Size = size;
  Speed = speed;
}
}

This would then be the lookup code:

using System;
using System.Collections.Generic;

namespace LookupTable
{
  internal class Program
  {
    private static readonly Dictionary<SizeSpeedKey, double> lookupTable =
      new Dictionary<SizeSpeedKey, double>
      {
        {new SizeSpeedKey("6x5", 500), 0.1},
        {new SizeSpeedKey("6x5", 750), 0.5},
        {new SizeSpeedKey("6x4", 500), 0.01},
        {new SizeSpeedKey("6x4", 750), 0.1},
        {new SizeSpeedKey("8x5", 500), 0.5},
        {new SizeSpeedKey("8x5", 750), 0.9},
      };

    private static void Main(string[] args)
    {
      // these will of course need to be read from the user
      var size = "6x4";
      var speed = 500;

      Console.WriteLine("For size = {0} and speed = {1}, the pressure will be {2}", size, speed, lookupTable[new SizeSpeedKey(size, speed)]);
      Console.ReadLine();
    }
  }
}
Marcel Popescu
This wouldn't work. SizeSpeedKey does not implement IEquatable<SizeSpeedKey> so how can the dictionary compare key values?
Ronald Wildenberg
Yes and no. Dictionaries can use arbitrary objects as keys (the all provide .Equals and .GetHashCode). Without overriding those methods, identity is used to assess equality. Now with structs being copied all the time, finding "equal" value could actually be tricky ^^You at least have to override .Equals like in @rwwilden's example.
SealedSun
Hmm... true, that. +1 for rwwilden :)
Marcel Popescu
+4  A: 

Assuming your size and speed are always specific values and do not fall between the values specified in your example (so no size 780 or 598 for example), the fastest way to perform lookups based on speed and size is to have a Dictionary<SizeAndSpeed, double> where SizeAndSpeed is a class like this:

public class SizeAndSpeed : IEquatable<SizeAndSpeed>
{
    public string Size { get; set; }
    public int Speed { get; set; }
    public bool Equals(SizeAndSpeed other)
    {
        return Size == other.Size && Speed == other.Speed;
    }
}

I'm assuming that Size can be a string but of course a more complex object could be used as well.

Ronald Wildenberg