Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/BinaryVectorComparers/LexicographicBinaryVectorComparer.cs @ 9385

Last change on this file since 9385 was 8083, checked in by mkommend, 12 years ago

#1877: Corrected errors detected in unit test in HeuristicLab.FitnessLandscapeAnalysis.

File size: 1.2 KB
Line 
1using System;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Encodings.BinaryVectorEncoding;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6
7namespace HeuristicLab.Problems.NK {
8
9  [Item("Lexicographic Binary Vector Comparer", "Compares two binary vectors lexicographically")]
10  [StorableClass]
11  public sealed class LexicographicBinaryVectorComparer : BinaryVectorComparer {
12
13    [StorableConstructor]
14    private LexicographicBinaryVectorComparer(bool deserializing) : base(deserializing) { }
15    private LexicographicBinaryVectorComparer(LexicographicBinaryVectorComparer original, Cloner cloner)
16      : base(original, cloner) {
17    }
18    public LexicographicBinaryVectorComparer() { }
19    public override IDeepCloneable Clone(Cloner cloner) {
20      return new LexicographicBinaryVectorComparer(this, cloner);
21    }
22
23    public override int Compare(BinaryVector x, BinaryVector y) {
24      for (int i = 0; i < Math.Min(x.Length, y.Length); i++) {
25        if (!x[i] && y[i])
26          return -1;
27        if (x[i] && !y[i])
28          return 1;
29      }
30      if (x.Length > y.Length)
31        return 1;
32      if (x.Length < y.Length)
33        return -1;
34      return 0;
35    }
36  }
37}
Note: See TracBrowser for help on using the repository browser.