Jishan
Jishan

Reputation: 1684

List <T> Add Value Only If It Is Minimum Of All Elements

I have 2 statements as follows to plot a graph.

public static List<KeyValuePair<int, double>> entries = new List<KeyValuePair<int, double>>();
entries.Add(new KeyValuePair<int, double>(j, min));

What I want is that the min value only gets added to the List if it is less than or equal to every other min values in the list. How can this be achieved? Thanks.

Upvotes: 2

Views: 188

Answers (4)

Dbl
Dbl

Reputation: 5904

If performance matters you should be using this instead:

public class MinFloatKeyValuePairList : Collection<KeyValuePair<int, float>>
{
    private float _lowest = float.MaxValue;

    protected override void InsertItem(int index, KeyValuePair<int, float> item)
    {
        if (item.Value <= _lowest || this.Count == 0)
        {
            _lowest = item.Value;
            base.InsertItem(index, item);
        }
    }
}

Upvotes: 2

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

Just calculate min value of all entries and check if it's bigger than your min value:

if (!entries.Any() || min <= entries.Min(e => e.Value))
   entries.Add(new KeyValuePair<int, double>(j, min));

UPDATE: you also need to check if there is any elements in entries list, before trying to find minimal value.

HINT: If you always use this rule for adding items and don't modify collection in other way (i.e. remove items or insert at some index) then you can simply check last item's value - it will always be less or equal to other values.

UPDATE: Instead of usage KeyValuePair for your data, you can create custom class with descriptive names for properties and class itself (you can consider even better names):

public class Point
{
    public Point(int iteration, double value)
    {
        Iteration = iteration;
        Value = value;
    }

    public int Iteration { get; private set; }
    public double Value { get; private set; }
}

And then create class which encapsulates points and behavior related to these points. From your description it could be something like:

public class DecresingGraph
{
    private List<Point> points = new List<Point>();

    public void Add(Point point)
    {
        if (!points.Any())
        {
            points.Add(point);
            return;
        }

        if (point.Value <= points.Last().Value)
            points.Add(point);
    }

    public IEnumerable<Point> Points 
    {
        get { return points; }
    }
}

Upvotes: 5

IEatBagels
IEatBagels

Reputation: 843

You could create a collection that overrides InsertItem

public class YourCollection : Collection<KeyValuePair<int, double>>
{
   protected override void InsertItem(int index, KeyValuePair<int, double> item)
   {
      if (!this.Any() || item.Value <= this.Min(e => e.Value))
         base.InsertItem(index, item);
   }
}

Then you can just add your item in the collection, and the item will be added in the list if it fits your requirement.

YourCollection collection = new YourCollection();
collection.Add(new KeyValuePair<int, double>(j, min));

Upvotes: 1

Habib
Habib

Reputation: 223282

Use Enumerable.Min to calculate minimum value and then compare it with the current value like:

var minValue = entries.Min(r => r.Value);
if (min <= minValue)
{
    entries.Add(new KeyValuePair<int, double>(j, min));
}

Upvotes: 2

Related Questions