Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/DistanceBase.cs @ 15018

Last change on this file since 15018 was 15018, checked in by gkronber, 7 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

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;
27
28namespace HeuristicLab.Algorithms.DataAnalysis {
29  [StorableType("b9ad40ed-4e7b-42d7-8bce-c78f5d5ea79d")]
30  public abstract class DistanceBase<T> : Item, IDistance<T> {
31
32    #region HLConstructors & Boilerplate
33    [StorableConstructor]
34    protected DistanceBase(StorableConstructorFlag 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
46    public double Get(object x, object y) {
47      return Get((T)x, (T)y);
48    }
49
50    public IComparer GetDistanceComparer(object item) {
51      return new DistanceComparer((T)item, this);
52    }
53
54    private class DistanceComparer : IComparer<T>, IComparer {
55      private readonly T item;
56      private readonly IDistance<T> dist;
57
58      public DistanceComparer(T item, IDistance<T> dist) {
59        this.dist = dist;
60        this.item = item;
61      }
62
63      public int Compare(T x, T y) {
64        return dist.Get(x, item).CompareTo(dist.Get(y, item));
65      }
66
67      public int Compare(object x, object y) {
68        return Compare((T)x, (T)y);
69      }
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.