#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections;
using System.Collections.Generic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Algorithms.DataAnalysis {
[StorableClass]
public abstract class DistanceBase : Item, IDistance {
#region HLConstructors & Cloning
[StorableConstructor]
protected DistanceBase(bool deserializing) : base(deserializing) { }
protected DistanceBase(DistanceBase original, Cloner cloner) : base(original, cloner) { }
protected DistanceBase() { }
#endregion
public abstract double Get(T a, T b);
public IComparer GetDistanceComparer(T item) {
return new DistanceComparer(item, this);
}
public double Get(object x, object y) {
return Get((T) x, (T) y);
}
public IComparer GetDistanceComparer(object item) {
return new DistanceComparer((T) item, this);
}
internal class DistanceComparer : IComparer, IComparer {
private readonly T item;
private readonly IDistance dist;
public DistanceComparer(T item, IDistance dist) {
this.dist = dist;
this.item = item;
}
public int Compare(T x, T y) {
return dist.Get(x, item).CompareTo(dist.Get(y, item));
}
public int Compare(object x, object y) {
return Compare((T) x, (T) y);
}
}
}
}