C # Dictionary Search

I am currently trying to create a program that estimates location based on signal strength. The signal strength value is int, and then I need a search dictionary with ranges.

So, I would have something like:

Signal Strenth             Position
0-9                            1
10-19                          2
20-29                          3

and then I would like to see what position the signal strength is associated with, for example, 15 will refer to position 2.

I know that I can just load the if statements, but is there a good way to do this using some kind of search dictionary?

+5
source share
9 answers

If you have arbitrary but consistent ranges, you can use an array of upper bounds and do a binary search to get the position:

// Definition of ranges
int[] ranges = new int[] { 9, 19, 29 };

// Lookup
int position = Array.BinarySearch(ranges, 15);
if (position < 0)
    position = ~position;

// Definition of range names
string[] names = new string[] { "home", "street", "city", "far away" };

Console.WriteLine("Position is: {0}", names[position]);

Array.BinarySearch , ( ) , , .

+11

:

int position = signalStrength / 10 + 1;

,

Dan

+11

, , , - . KeyType KeyTypeRange (int int) KEyTypeSearch (int). KeyTypeSearch KeyTypeRange .

SortedDictionary<KeyType,int>  lookup = new Dictionary<int,int>();
lookup.Add( new KeyTypeRange(1,10),1);
lookup.Add( new KeyTypeRange(11,20),2);
lookup.Add( new KeyTypeRange(21,30),3);
lookup.TryGetValue( new KeyTypeSearch(15) );

. Overkill. BinarySearch.

+2

- . , , . , . , x% 10 + 1; , .

+1

, int - , int - . (, 0, 1, 1, 1 ..), .

- :

Dictionary<int, int> values;

values = new Dictionary<int, int>();

values[0] = 1;
values[1] = 1;
...
values[29] = 3;

, :

Console.WriteLine(values[27].ToString());
0

2 .  

dictionary<string,dictionary<int,int>>

, low med, high, foreach,

0

, , . (, ):

** . , , ... IEqualityComparer on Range, IndexOf :

public class Controller
{
   List m_positions = new List();

   public void LoadPositions()
   {
      m_positions.Add(new Range(0, 9));
      m_positions.Add(new Range(10, 19));
      m_positions.Add(new Range(20, 29));
   }

   public int GetPosition (int signal)
   {
      Range range = m_positions.Single(a => IsBetween(signal, a.Min, a.Max));

      return m_positions.IndexOf(range);
   }

   private static bool IsBetween (int target, int min, int max)
   {
      return min = target;
   }
}

, , , , Range:

public class Range
{
   public Range(int min, int max)
   {
      this.Min = min;
      this.Max = max;
   }

   public int Min
   {
      get;
      private set;
   }

   public int Max
   {
      get;
      private set;
   }
}
0

, , @agileguy.

, , :

class SignalStrengthPositionMapper
{
    private static readonly int[] signalStrength = { Int32.MinValue, 0, 5, 11, 15, 20, 27, 35 };
    public static int GetPosition(int strength)
    {
        return StrengthSearch(0, signalStrength.Length, strength);
    }

    // modified binary search
    private static int StrengthSearch(int start, int end, int strength)
    {
        int mid = 0;
        while (start <= end)
        {
            mid = (start + end) / 2;

            if (strength >= signalStrength[mid])         // lower bound check
            {
                start = mid + 1;
                if (strength < signalStrength[start])    // upper bound check
                    return mid;
            }
            else if (strength < signalStrength[mid])     // upper bound check
            {
                end = mid - 1;
                if (strength >= signalStrength[end])     // lower bound check
                    return mid;
            }
        }
        return 0;
    }
}
0

generics:

Dictionary<int,int>  lookup = new Dictionary<int,int>();
lookup.Add(0,1);
lookup.Add(1,1);
lookup.Add(2,1);
lookup.Add(3,1);
...
lookup.Add(9,1);
lookup.Add(10,2);
lookup.Add(11,2);

..

lookup [22] 3. "". O (1).

-1

All Articles