/// <p> /// Thanks to tsmorton for suggesting this functionality and writing part of the code. /// </p> /// /// @see GenericItemSimilarity#GenericItemSimilarity(Iterable, int) /// @see GenericItemSimilarity#GenericItemSimilarity(NReco.CF.Taste.Similarity.ItemSimilarity, /// NReco.CF.Taste.Model.DataModel, int) public static List <GenericItemSimilarity.ItemItemSimilarity> GetTopItemItemSimilarities( int howMany, IEnumerator <GenericItemSimilarity.ItemItemSimilarity> allSimilarities) { var topSimilarities = new SortedSet <GenericItemSimilarity.ItemItemSimilarity>(); bool full = false; double lowestTopValue = Double.NegativeInfinity; while (allSimilarities.MoveNext()) { GenericItemSimilarity.ItemItemSimilarity similarity = allSimilarities.Current; double value = similarity.getValue(); if (!Double.IsNaN(value) && (!full || value > lowestTopValue)) { topSimilarities.Add(similarity); if (full) { topSimilarities.Remove(topSimilarities.Max); //poll(); } else if (topSimilarities.Count > howMany) { full = true; topSimilarities.Remove(topSimilarities.Max);//topSimilarities.poll(); } lowestTopValue = topSimilarities.Max.getValue(); } } int size = topSimilarities.Count; var result = new List <GenericItemSimilarity.ItemItemSimilarity>(size); result.AddRange(topSimilarities); return(result); }
public static List <GenericItemSimilarity.ItemItemSimilarity> getTopItemItemSimilarities(int howMany, IEnumerator <GenericItemSimilarity.ItemItemSimilarity> allSimilarities) { SortedSet <GenericItemSimilarity.ItemItemSimilarity> collection = new SortedSet <GenericItemSimilarity.ItemItemSimilarity>(); bool flag = false; double negativeInfinity = double.NegativeInfinity; while (allSimilarities.MoveNext()) { GenericItemSimilarity.ItemItemSimilarity current = allSimilarities.Current; double d = current.getValue(); if (!double.IsNaN(d) && (!flag || (d > negativeInfinity))) { collection.Add(current); if (flag) { collection.Remove(collection.Max); } else if (collection.Count > howMany) { flag = true; collection.Remove(collection.Max); } negativeInfinity = collection.Max.getValue(); } } List <GenericItemSimilarity.ItemItemSimilarity> list = new List <GenericItemSimilarity.ItemItemSimilarity>(collection.Count); list.AddRange(collection); return(list); }