Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Extensions/CollectionExtensions.cs @ 17104

Last change on this file since 17104 was 15017, checked in by pkimmesw, 8 years ago

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 938 bytes
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Extensions {
4  using HeuristicLab.Core;
5  using HeuristicLab.Random;
6
7  public static class CollectionExtensions {
8    public static T Random<T>(this IReadOnlyList<T> source, IRandom random = null) {
9      random = random ?? new MersenneTwister();
10      var x = random.Next(source.Count);
11
12      return source[x];
13    }
14
15    public static int HashCode<T>(this IReadOnlyList<T> source) {
16      var hash = 19 * 31 + typeof(T).FullName.GetHashCode();
17
18      for (var i = 0; i < source.Count; i++)
19        hash = hash * 31 + source[i].GetHashCode();
20
21      return hash;
22    }
23
24    public static int HashCode<T>(this IEnumerable<T> source) {
25      var hash = 19 * 31 + typeof(T).FullName.GetHashCode();
26
27      foreach (var item in source)
28        hash = hash * 31 + item.GetHashCode();
29
30      return hash;
31    }
32  }
33}
Note: See TracBrowser for help on using the repository browser.