public static bool SetOrUpdateRatings(ref SimpleRating currentRating, SimpleRating newRating) { if (newRating.IsEmpty) { return(false); } else if (newRating.RatingValue < 0) { return(false); } else if (currentRating.IsEmpty) { currentRating = newRating; return(true); } else if (!currentRating.VoteCount.HasValue && newRating.VoteCount.HasValue) { currentRating = newRating; return(true); } else if (currentRating.VoteCount.HasValue && newRating.VoteCount.HasValue && currentRating.VoteCount.Value < newRating.VoteCount.Value) { currentRating = newRating; return(true); } return(false); }
public IEnumerable <IRating> RecommendSubjects(IRater rater, IEnumerable <ISubject> subjects, int take = -1, int skip = 0) { var cachedResults = new ConcurrentBag <IRating>(); var notCachedSubjects = new ConcurrentBag <ISubject>(); var options = new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }; Parallel.ForEach(subjects, options, subject => { var key = CreateCacheKey(rater, subject); var value = 0D; if (_cache.TryGetValue(key, out value)) { var ranking = new SimpleRating(rater, subject, value); cachedResults.Add(ranking); } else { notCachedSubjects.Add(subject); } }); var partial = _algorithm.RecommendSubjects(rater, notCachedSubjects); var results = partial.ToList(); results.AddRange(cachedResults); var sorted = results.OrderByDescending(p => p.Value).AsEnumerable(); if (skip > 0) { sorted = sorted.Skip(skip); } if (take > 0) { sorted = sorted.Take(take); } return(sorted); }
public static bool SetOrUpdateList <T>(List <T> currentList, List <T> newList, bool addMissing, out bool itemWasAdded, bool overwriteShorterStrings = true) { itemWasAdded = false; bool changed = false; if (newList == null) { return(false); } if (currentList == null) { currentList = new List <T>(); } for (int iNew = 0; iNew < newList.Count; iNew++) { int iCurrent = currentList.IndexOf(newList[iNew]); if (iCurrent >= 0) { object currentObj = (object)currentList[iCurrent]; object newObj = (object)newList[iNew]; if (currentObj == null) { continue; } if (currentObj is BaseInfo info) { ((BaseInfo)currentObj).MergeWith((BaseInfo)newObj); } else if (currentObj is string && newObj is string) { string currentVal = (string)currentObj; string newVal = (string)newObj; changed |= SetOrUpdateString(ref currentVal, newVal, overwriteShorterStrings); } else if (currentObj is SimpleTitle && newObj is string) { SimpleTitle currentVal = (SimpleTitle)currentObj; string newVal = (string)newObj; changed |= SetOrUpdateString(ref currentVal, newVal, overwriteShorterStrings); } else if (currentObj is string && newObj is SimpleTitle) { string currentVal = (string)currentObj; SimpleTitle newVal = (SimpleTitle)newObj; changed |= SetOrUpdateString(ref currentVal, newVal, overwriteShorterStrings); } else if (currentObj is SimpleTitle && newObj is SimpleTitle) { SimpleTitle currentVal = (SimpleTitle)currentObj; SimpleTitle newVal = (SimpleTitle)newObj; changed |= SetOrUpdateString(ref currentVal, newVal, overwriteShorterStrings); } else if (currentObj is SimpleRating && newObj is SimpleRating) { SimpleRating currentVal = (SimpleRating)currentObj; SimpleRating newVal = (SimpleRating)newObj; changed |= SetOrUpdateRatings(ref currentVal, newVal); } else if (currentObj is SimpleRating && newObj is double) { SimpleRating currentVal = (SimpleRating)currentObj; SimpleRating newVal = new SimpleRating((double)newObj); changed |= SetOrUpdateRatings(ref currentVal, newVal); } else if (currentObj is byte[]) { byte[] currentVal = (byte[])currentObj; byte[] newVal = (byte[])newObj; changed |= SetOrUpdateValue(ref currentVal, newVal); } else if (currentObj is IList) { IList currentVal = (IList)currentObj; IList newVal = (IList)newObj; Type listElementType = currentVal.GetType().GetGenericArguments().Single(); bool itemAdded = false; if (listElementType == typeof(long)) { changed |= SetOrUpdateList((List <long>)currentVal, (List <long>)newVal, true, out itemAdded); } else if (listElementType == typeof(int)) { changed |= SetOrUpdateList((List <int>)currentVal, (List <int>)newVal, true, out itemAdded); } else if (listElementType == typeof(short)) { changed |= SetOrUpdateList((List <short>)currentVal, (List <short>)newVal, true, out itemAdded); } else if (listElementType == typeof(ulong)) { changed |= SetOrUpdateList((List <ulong>)currentVal, (List <ulong>)newVal, true, out itemAdded); } else if (listElementType == typeof(uint)) { changed |= SetOrUpdateList((List <uint>)currentVal, (List <uint>)newVal, true, out itemAdded); } else if (listElementType == typeof(ushort)) { changed |= SetOrUpdateList((List <ushort>)currentVal, (List <ushort>)newVal, true, out itemAdded); } else if (listElementType == typeof(double)) { changed |= SetOrUpdateList((List <double>)currentVal, (List <double>)newVal, true, out itemAdded); } else if (listElementType == typeof(float)) { changed |= SetOrUpdateList((List <float>)currentVal, (List <float>)newVal, true, out itemAdded); } else if (listElementType == typeof(string)) { changed |= SetOrUpdateList((List <string>)currentVal, (List <string>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(PersonInfo)) { changed |= SetOrUpdateList((List <PersonInfo>)currentVal, (List <PersonInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(CompanyInfo)) { changed |= SetOrUpdateList((List <CompanyInfo>)currentVal, (List <CompanyInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(CharacterInfo)) { changed |= SetOrUpdateList((List <CharacterInfo>)currentVal, (List <CharacterInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(SeasonInfo)) { changed |= SetOrUpdateList((List <SeasonInfo>)currentVal, (List <SeasonInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(EpisodeInfo)) { changed |= SetOrUpdateList((List <EpisodeInfo>)currentVal, (List <EpisodeInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(MovieInfo)) { changed |= SetOrUpdateList((List <MovieInfo>)currentVal, (List <MovieInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(TrackInfo)) { changed |= SetOrUpdateList((List <TrackInfo>)currentVal, (List <TrackInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else { throw new ArgumentException("SetOrUpdateList: IList is of an unsupported type"); } if (itemAdded) { itemWasAdded = true; } } else { object currentVal = currentObj; object newVal = newObj; if (Nullable.GetUnderlyingType(currentVal.GetType()) != null) { if (currentVal == null && newVal != null) { currentObj = newVal; changed = true; } } else { changed |= SetOrUpdateValue(ref currentVal, newVal); } } } else if (addMissing) { currentList.Add(newList[iNew]); itemWasAdded = true; changed = true; } } currentList.Sort(); return(changed); }
public static bool SetOrUpdateList <T>(List <T> currentList, List <T> newList, bool addMissing, out bool itemWasAdded, bool overwriteShorterStrings = true) { itemWasAdded = false; bool changed = false; if (newList == null) { return(false); } if (currentList == null) { currentList = new List <T>(); } for (int iNew = 0; iNew < newList.Count; iNew++) { int iCurrent = currentList.IndexOf(newList[iNew]); if (iCurrent >= 0) { object currentObj = (object)currentList[iCurrent]; object newObj = (object)newList[iNew]; if (currentObj == null) { continue; } Type objType = currentObj.GetType(); if (objType.IsClass) { FieldInfo[] fields = currentObj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public); if (fields != null) { foreach (FieldInfo field in fields) { if (field.Name.EndsWith("Id", StringComparison.InvariantCultureIgnoreCase)) { object currentVal = field.GetValue(currentObj); object newVal = field.GetValue(newObj); changed |= SetOrUpdateId(ref currentVal, newVal); field.SetValue(currentObj, currentVal); } else if (field.GetValue(currentObj) is string && field.GetValue(newObj) is string) { string currentVal = (string)field.GetValue(currentObj); string newVal = (string)field.GetValue(newObj); changed |= SetOrUpdateString(ref currentVal, newVal, overwriteShorterStrings); field.SetValue(currentObj, currentVal); } else if (field.GetValue(currentObj) is SimpleTitle && field.GetValue(newObj) is string) { SimpleTitle currentVal = (SimpleTitle)field.GetValue(currentObj); string newVal = (string)field.GetValue(newObj); changed |= SetOrUpdateString(ref currentVal, newVal, overwriteShorterStrings); field.SetValue(currentObj, currentVal); } else if (field.GetValue(currentObj) is string && field.GetValue(newObj) is SimpleTitle) { string currentVal = (string)field.GetValue(currentObj); SimpleTitle newVal = (SimpleTitle)field.GetValue(newObj); changed |= SetOrUpdateString(ref currentVal, newVal, overwriteShorterStrings); field.SetValue(currentObj, currentVal); } else if (field.GetValue(currentObj) is SimpleTitle && field.GetValue(newObj) is SimpleTitle) { SimpleTitle currentVal = (SimpleTitle)field.GetValue(currentObj); SimpleTitle newVal = (SimpleTitle)field.GetValue(newObj); changed |= SetOrUpdateString(ref currentVal, newVal, overwriteShorterStrings); field.SetValue(currentObj, currentVal); } else if (field.GetValue(currentObj) is SimpleRating && field.GetValue(newObj) is SimpleRating) { SimpleRating currentVal = (SimpleRating)field.GetValue(currentObj); SimpleRating newVal = (SimpleRating)field.GetValue(newObj); changed |= SetOrUpdateRatings(ref currentVal, newVal); field.SetValue(currentObj, currentVal); } else if (field.GetValue(currentObj) is SimpleRating && field.GetValue(newObj) is double) { SimpleRating currentVal = (SimpleRating)field.GetValue(currentObj); SimpleRating newVal = new SimpleRating((double)field.GetValue(newObj)); changed |= SetOrUpdateRatings(ref currentVal, newVal); field.SetValue(currentObj, currentVal); } else if (field.GetValue(currentObj) is byte[]) { byte[] currentVal = (byte[])field.GetValue(currentObj); byte[] newVal = (byte[])field.GetValue(newObj); changed |= SetOrUpdateValue(ref currentVal, newVal); field.SetValue(currentObj, currentVal); } else if (field.GetValue(currentObj) is IList) { IList currentVal = (IList)field.GetValue(currentObj); IList newVal = (IList)field.GetValue(newObj); Type listElementType = currentVal.GetType().GetGenericArguments().Single(); bool itemAdded = false; if (listElementType == typeof(long)) { changed |= SetOrUpdateList((List <long>)currentVal, (List <long>)newVal, true, out itemAdded); } else if (listElementType == typeof(int)) { changed |= SetOrUpdateList((List <int>)currentVal, (List <int>)newVal, true, out itemAdded); } else if (listElementType == typeof(short)) { changed |= SetOrUpdateList((List <short>)currentVal, (List <short>)newVal, true, out itemAdded); } else if (listElementType == typeof(ulong)) { changed |= SetOrUpdateList((List <ulong>)currentVal, (List <ulong>)newVal, true, out itemAdded); } else if (listElementType == typeof(uint)) { changed |= SetOrUpdateList((List <uint>)currentVal, (List <uint>)newVal, true, out itemAdded); } else if (listElementType == typeof(ushort)) { changed |= SetOrUpdateList((List <ushort>)currentVal, (List <ushort>)newVal, true, out itemAdded); } else if (listElementType == typeof(double)) { changed |= SetOrUpdateList((List <double>)currentVal, (List <double>)newVal, true, out itemAdded); } else if (listElementType == typeof(float)) { changed |= SetOrUpdateList((List <float>)currentVal, (List <float>)newVal, true, out itemAdded); } else if (listElementType == typeof(string)) { changed |= SetOrUpdateList((List <string>)currentVal, (List <string>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(PersonInfo)) { changed |= SetOrUpdateList((List <PersonInfo>)currentVal, (List <PersonInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(CompanyInfo)) { changed |= SetOrUpdateList((List <CompanyInfo>)currentVal, (List <CompanyInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(CharacterInfo)) { changed |= SetOrUpdateList((List <CharacterInfo>)currentVal, (List <CharacterInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(SeasonInfo)) { changed |= SetOrUpdateList((List <SeasonInfo>)currentVal, (List <SeasonInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(EpisodeInfo)) { changed |= SetOrUpdateList((List <EpisodeInfo>)currentVal, (List <EpisodeInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(MovieInfo)) { changed |= SetOrUpdateList((List <MovieInfo>)currentVal, (List <MovieInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else if (listElementType == typeof(TrackInfo)) { changed |= SetOrUpdateList((List <TrackInfo>)currentVal, (List <TrackInfo>)newVal, true, out itemAdded, overwriteShorterStrings); } else { throw new ArgumentException("SetOrUpdateList: IList is of an unsupported type"); } field.SetValue(currentObj, currentVal); if (itemAdded) { itemWasAdded = true; } } else { object currentVal = field.GetValue(currentObj); object newVal = field.GetValue(newObj); if (Nullable.GetUnderlyingType(field.FieldType) != null) { if (currentVal == null && newVal != null) { field.SetValue(currentObj, newVal); changed = true; } } else { changed |= SetOrUpdateValue(ref currentVal, newVal); field.SetValue(currentObj, currentVal); } } } } } } else if (addMissing) { currentList.Add(newList[iNew]); itemWasAdded = true; changed = true; } } currentList.Sort(); return(changed); }