[14414] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[14911] | 22 | using System.Collections;
|
---|
[14414] | 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
[14512] | 28 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[14414] | 29 | [StorableClass]
|
---|
| 30 | public abstract class DistanceBase<T> : Item, IDistance<T> {
|
---|
| 31 |
|
---|
[15207] | 32 | #region HLConstructors & Cloning
|
---|
[14414] | 33 | [StorableConstructor]
|
---|
| 34 | protected DistanceBase(bool deserializing) : base(deserializing) { }
|
---|
| 35 | protected DistanceBase(DistanceBase<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
| 36 | protected DistanceBase() { }
|
---|
| 37 | #endregion
|
---|
| 38 |
|
---|
| 39 | public abstract double Get(T a, T b);
|
---|
| 40 |
|
---|
| 41 | public IComparer<T> GetDistanceComparer(T item) {
|
---|
| 42 | return new DistanceComparer(item, this);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[14911] | 45 | public double Get(object x, object y) {
|
---|
| 46 | return Get((T)x, (T)y);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public IComparer GetDistanceComparer(object item) {
|
---|
| 50 | return new DistanceComparer((T)item, this);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | private class DistanceComparer : IComparer<T>, IComparer {
|
---|
[14414] | 54 | private readonly T item;
|
---|
| 55 | private readonly IDistance<T> dist;
|
---|
| 56 |
|
---|
| 57 | public DistanceComparer(T item, IDistance<T> dist) {
|
---|
| 58 | this.dist = dist;
|
---|
| 59 | this.item = item;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public int Compare(T x, T y) {
|
---|
| 63 | return dist.Get(x, item).CompareTo(dist.Get(y, item));
|
---|
| 64 | }
|
---|
[14911] | 65 |
|
---|
| 66 | public int Compare(object x, object y) {
|
---|
| 67 | return Compare((T)x, (T)y);
|
---|
| 68 | }
|
---|
[14414] | 69 | }
|
---|
| 70 | }
|
---|
| 71 | } |
---|