Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/DistanceBase.cs @ 15296

Last change on this file since 15296 was 15207, checked in by bwerth, 7 years ago

#2700 worked on TSNE (mostly removing unused code)

File size: 2.3 KB
Line 
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
22using System.Collections;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Algorithms.DataAnalysis {
29  [StorableClass]
30  public abstract class DistanceBase<T> : Item, IDistance<T> {
31
32    #region HLConstructors & Cloning
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
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 {
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      }
65
66      public int Compare(object x, object y) {
67        return Compare((T)x, (T)y);
68      }
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.