alicommit-malp / roundrobin

A simple implementation of round robin in c# with circular linked list

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Initialize weights via constructor

filiphajek opened this issue · comments

It would be nice to initialize RoundRobinList with weights.

Example:
Now i have to do this:

var dic = new Dictionary<string, int>()
{
    { "first", 1 },
    { "second", 4 },
    { "third", 5 }
};

var roundRobin = new RoundRobinList<string>(dic.Keys);
foreach (var (Name, Weight) in dic)
{
    roundRobin.IncreaseWeight(Name, Weight);
}

But if we have a constructor that takes for examle dictionary, it would be simply:

var roundRobin = new RoundRobinList<string>(dic);

Other way to achieve this is making RoundRobinData public.
And of course it would be nice to still support generics. :)

Hey :)
can you explain your usecase here , becasue at the moment the only difference to my eye is

var roundRobin = new RoundRobinList<string>(dic);
//vs 
var roundRobin = new RoundRobinList<string>(dic.Keys);

Yes
So what i meant by But if we have a constructor that takes for examle dictionary, it would be simply ... is this new constructor:

public RoundRobinList(IDictionary<T, int> dic)
{
    _linkedList = new LinkedList<RoundRobinData<T>>(RoundRobinData<T>.ToRoundRobinData(dic));
}

and then we call it like:

var roundRobin = new RoundRobinList<string>(dic);

Idea is that you could specify weights from constructor. It would be more ellegant and easier if we have some way to initialize RoundRobinList elements with weights. For example i want the first element to have weight 2 right at the beginning, second element to have weight 5 etc. Now it is little bit complicated, because there is no constructor that support initializing elements with different weight.
It does not have to be via dictionary .. it was only my first idea.

I got what you mean :)
I have facilitated your idea in version 2.2.1

var roundRobinList = new RoundRobinList<int>(
    new List<int>{
        1,2,3,4,5
    },new int[]{0,1,0,0,0}
);

for (var i = 0; i < 10; i++)
{
    Write($"{roundRobinList.Next()},");
}

//result
//1,2,2,3,4,5,1,2,2,3

I am trying to introdue as few new datatypes as possible that the flexebility will be at development side that's why I didn't introduce IDictionary and facilitate your idea just by introducing an optional array of weights

In your case it will be as following

var dic = new Dictionary<string, int>()
{
    { "first", 1 },
    { "second", 4 },
    { "third", 5 }
};

var roundRobin = new RoundRobinList<string>(dic.Keys, dic.values.ToArray());

Let me know your feedback please

Great! Works for me
Thank you