How would I look for a range of range values ​​using C #

I have a list of values ​​like this

1000, 20400
22200, 24444

Ranges do not overlap.

What I want to do is an aC # function that can store (loaded values ​​from db, then cache it locally), a relatively large list of these values, and then a method to find if the given value is in any of the ranges?

It makes sense?

Fastest solution required

+5
source share
8 answers

You provided values, but then talked about ranges.

HashSet<int>. ... , , , . , ? , ? , , ?

: , . ( ) , , , , .

EDIT: .

- List<Range>.Sort , .

public class Range : IComparable<Range>
{
      private readonly int bottom; // Add properties for these if you want
      private readonly int top;

      public Range(int bottom, int top)
      {
             this.bottom = bottom;
             this.top = top;
      }

      public int CompareTo(Range other)
      {
             if (bottom < other.bottom && top < other.top)
             {
                   return -1;
             }
             if (bottom > other.bottom && top > other.top)
             {
                   return 1;
             }
             if (bottom == other.bottom && top == other.top)
             {
                   return 0;
             }
             throw new ArgumentException("Incomparable values (overlapping)");
      }

      /// <summary>
      /// Returns 0 if value is in the specified range;
      /// less than 0 if value is above the range;
      /// greater than 0 if value is below the range.
      /// </summary>
      public int CompareTo(int value)
      {
             if (value < bottom)
             {
                   return 1;
             }
             if (value > top)
             {
                   return -1;
             }
             return 0;
      }
}

// Just an existence search
public static bool BinarySearch(IList<Range> ranges, int value)
{
    int min = 0;
    int max = ranges.Count-1;

    while (min <= max)
    {
        int mid = (min + max) / 2;
        int comparison = ranges[mid].CompareTo(value);
        if (comparison == 0)
        {
            return true;
        }
        if (comparison < 0)
        {
            min = mid+1;
        }
        else if (comparison > 0)
        {
            max = mid-1;
        }
    }
    return false;
}
+10

. , , ( , ). , .

+5

, .

class Range {
   int Lower { get; set; }
   int Upper { get; set; }
}

List<Range>.FirstOrDefault(r => i >= r.Lower && i <= r.Upper);
+4

, , . - SortedDictionary, - , O (log (n)). , , , . CompareTo, , , /, .

public struct Range : IComparable<Range>
{
    public int From;
    public int To;

    public Range(int point)
    {
        From = point;
        To = point;
    }

    public Range(int from, int to)
    {
        From = from;
        To = to;
    }

    public int CompareTo(Range other)
    {
        // If the ranges are overlapping, they are considered equal/matching
        if (From <= other.To && To >= other.From)
        {
            return 0;
        }

        // Since the ranges are not overlapping, we can compare either end
        return From.CompareTo(other.From);
    }
}

public class RangeDictionary
{
    private static SortedDictionary<Range, string> _ranges = new SortedDictionary<Range, string>();

    public RangeDictionary()
    {
        _ranges.Add(new Range(1, 1000), "Alice");
        _ranges.Add(new Range(1001, 2000), "Bob");
        _ranges.Add(new Range(2001, 3000), "Carol");
    }

    public string Lookup(int key)
    {
        /* We convert the value we want to lookup into a range,
         * so it can be compared with the other ranges */
        var keyAsRange = new Range(key);
        string value;
        if (_ranges.TryGetValue(keyAsRange, out value))
        {
            return value;
        }
        return null;
    }
}

var ranges = new RangeDictionary();
var value = ranges.Lookup(1356);

value "Bob", 1356 1001-2000.

, , . .

SortedList , ( ), / . ( ) . , # Dictionary GetHashCode Equals .

+1

, :

- > .

- > .

- > HashSet .

- > . :

- > (), : .

- > (), : .

0

, ? , , , foreach ValueRangeCollection .

    public struct ValueRange 
    { 
       public int LowVal; 
       public int HiVal; 
       public bool Contains (int CandidateValue) 
       { return CandidateValue >= LowVal && CandidateValue <= HiVal; } 
       public ValueRange(int loVal, int hiVal)
       {
          LowVal = loVal;
          HiVal = hiVal;
       }
   }

    public class ValueRangeCollection: SortedList<int, ValueRange> 
    { 
        public bool Contains(int candValue) 
        {  
            foreach ( ValueRange valRng in Values)
                if (valRng.Contains(candValue)) return true;
            return false; 
        }
        public void Add(int loValue, int hiValue)
        {
            Add(loValue, new ValueRange(loValue, hiValue));
        }
    }
0
class Range
{
   public int Start { get; set; }
   public int End { get; set; }

   static Dictionary<int, Range> values;
   static int[] arrToBinarySearchIn;
   public static void BuildRanges(IEnumerable<Range> ranges) { 
        values = new Dictionary<int, Range>();
        foreach (var item in ranges)
            values[item.Start] = item;
        arrToBinarySearchIn = values.Keys.ToArray();
        Array.Sort(arrToBinarySearchIn);
   }
   public static Range GetRange(int value)
   {
       int searchIndex = Array.BinarySearch(arrToBinarySearchIn, value);
       if (searchIndex < 0)
           searchIndex = ~searchIndex - 1;
       if (searchIndex < 0)
           return null;
       Range proposedRange = values[arrToBinarySearchIn[searchIndex]];
       if (proposedRange.End >= value)
           return proposedRange;
       return null;
   }
}
0
class Ranges
{
    int[] starts = new[] { 1000, 22200 };
    int[] ends = new[] { 20400, 24444 };

    public int RangeIndex(int test)
    {
        int index = -1;

        if (test >= starts[0] && test <= ends[ends.Length - 1])
        {
            index = Array.BinarySearch(ends, test);

            if (index <= 0)
            {
                index = ~index;
                if (starts[index] > test) index = -1;
            }
        }

        return index;
    }
}

, , . , DataTable .

0
source

All Articles