How would I search a range of ranged values using C#

后端 未结 8 1562
攒了一身酷
攒了一身酷 2021-02-10 20:24

I have a list of values like this

1000, 20400
22200, 24444

The ranges don\'t overlap.

What I want to do is have a c# function that can

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-10 21:01

    Is this functionally what you're after? If so, and you just want it to be more performant, than change the foreach in the ValueRangeCollection to a binary search..

        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 
        { 
            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));
            }
        }
    

提交回复
热议问题