Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/DataSources/OneDimensional/EnumerableDataSource.cs @ 13792

Last change on this file since 13792 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 2.4 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Windows;
5
6namespace Microsoft.Research.DynamicDataDisplay.DataSources
7{
8  public class EnumerableDataSource<T> : EnumerableDataSourceBase<T>
9  {
10    public EnumerableDataSource(IEnumerable<T> data) : base(data) { }
11
12    public EnumerableDataSource(IEnumerable data) : base(data) { }
13
14    private readonly List<Mapping<T>> mappings = new List<Mapping<T>>();
15    private Func<T, double> xMapping;
16    private Func<T, double> yMapping;
17    private Func<T, Point> xyMapping;
18
19    public Func<T, double> XMapping
20    {
21      get { return xMapping; }
22      set { SetXMapping(value); }
23    }
24
25    public Func<T, double> YMapping
26    {
27      get { return yMapping; }
28      set { SetYMapping(value); }
29    }
30
31    public Func<T, Point> XYMapping
32    {
33      get { return xyMapping; }
34      set { SetXYMapping(value); }
35    }
36
37    public void SetXMapping(Func<T, double> mapping)
38    {
39      if (mapping == null)
40        throw new ArgumentNullException("mapping");
41
42      this.xMapping = mapping;
43      RaiseDataChanged();
44    }
45
46    public void SetYMapping(Func<T, double> mapping)
47    {
48      if (mapping == null)
49        throw new ArgumentNullException("mapping");
50
51      this.yMapping = mapping;
52      RaiseDataChanged();
53    }
54
55    public void SetXYMapping(Func<T, Point> mapping)
56    {
57      if (mapping == null)
58        throw new ArgumentNullException("mapping");
59
60      this.xyMapping = mapping;
61      RaiseDataChanged();
62    }
63
64    public void AddMapping(DependencyProperty property, Func<T, object> mapping)
65    {
66      if (property == null)
67        throw new ArgumentNullException("property");
68      if (mapping == null)
69        throw new ArgumentNullException("mapping");
70
71      mappings.Add(new Mapping<T> { Property = property, F = mapping });
72    }
73
74    public override IPointEnumerator GetEnumerator(DependencyObject context)
75    {
76      return new EnumerablePointEnumerator<T>(this);
77    }
78
79    internal void FillPoint(T elem, ref Point point)
80    {
81      if (xyMapping != null)
82      {
83        point = xyMapping(elem);
84      }
85      else
86      {
87        if (xMapping != null)
88        {
89          point.X = xMapping(elem);
90        }
91        if (yMapping != null)
92        {
93          point.Y = yMapping(elem);
94        }
95      }
96    }
97
98    internal void ApplyMappings(DependencyObject target, T elem)
99    {
100      if (target != null)
101      {
102        foreach (var mapping in mappings)
103        {
104          target.SetValue(mapping.Property, mapping.F(elem));
105        }
106      }
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.