A dictionary object that uses ranges of values for keys

前端 未结 8 821
情深已故
情深已故 2020-11-27 19:26

I have need of a sort of specialized dictionary. My use case is this: The user wants to specify ranges of values (the range could be a single point as well) and assign a v

8条回答
  •  自闭症患者
    2020-11-27 19:55

    I would make a little Interval class, which would something like that:

    public class Interval
    {
        public int Start {get; set;}
        public int End {get; set;}
        public int Step {get; set;}
        public double Value {get; set;}
    
        public WriteToDictionary(Dictionary dict)
        {
            for(int i = Start; i < End; i += Step)
            {
                dict.Add(i, Value);
            }
        }
    }
    

    So you still can a normal lookup within your dictionary. Maybe you should also perform some checks before calling Add() or implement some kind of rollback if any value is already within the dictionary.

提交回复
热议问题