aboutsummaryrefslogtreecommitdiffstats
path: root/data_sets/sorted_data_set.hpp
blob: 034a442d021904101532d5e3ba1ebf883265cd02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
   \file sorted_data_set.hpp
*/

#ifndef SORTED_DATA_SET
#define SORTED_DATA_SET
#define OPT_HEADER
#include "core/fitter.hpp"
#include <vector>
#include <algorithm>

namespace opt_utilities
{
  
  template <typename Ty,typename Tx>
  bool comp_data(const data<Ty,Tx>& d1,const data<Ty,Tx>& d2)
  {
    return d1.get_x()<d2.get_x();
  }



  /**
     \brief automatically sorting data set
     \tparam Ty type of y
     \tparam Tx type of x
   */
  template <typename Ty,typename Tx>
  class sorted_data_set
    :public data_set<Ty,Tx>
  {
  private:
    std::vector<data<Ty,Tx> > data_vec;
    
    data_set<Ty,Tx>* do_clone()const
    {
      return new sorted_data_set<Ty,Tx>(*this);
    }
    
    
    const data<Ty,Tx>& do_get_data(size_t i)const
    {
      return data_vec.at(i);
    }
    
    size_t do_size()const
    {
      return data_vec.size();
    }
    
    void do_add_data(const data<Ty,Tx>& d)
    {
      typename std::vector<data<Ty,Tx> >::iterator p
	=std::lower_bound(data_vec.begin(),data_vec.end(),d,comp_data<Ty,Tx>);
      //data_vec.push_back(d);
      data_vec.insert(p,d);
    }
    
    void do_clear()
    {
      data_vec.clear();
    }
    
  public:
    sorted_data_set()
    {}

    sorted_data_set(const sorted_data_set<Ty,Tx>& rhs)
      :data_vec(rhs.data_vec)
    {}

    sorted_data_set& operator=(const sorted_data_set<Ty,Tx>& rhs)
    {
      data_vec=rhs.data_vec;
      return *this;
    }

    sorted_data_set(const data_set<Ty,Tx>& rhs)
    {
      for(int i=0;i<rhs.size();++i)
	{
	  typename std::vector<data<Ty,Tx> >::iterator p
	    =std::lower_bound(data_vec.begin(),data_vec.end(),rhs.get_data(i),comp_data<Ty,Tx>);
	  //data_vec.push_back(d);
	  data_vec.insert(p,rhs.get_data(i));
	}
    }

    sorted_data_set& operator=(const data_set<Ty,Tx>& rhs)
    {
      data_vec.clear();
      for(int i=0;i<rhs.size();++i)
	{
	  typename std::vector<data<Ty,Tx> >::iterator p
	    =std::lower_bound(data_vec.begin(),data_vec.end(),rhs.get_data(i),comp_data<Ty,Tx>);
	  //data_vec.push_back(d);
	  data_vec.insert(p,rhs.get_data(i));
	}
      return *this;
    }
    
  };
}

#endif
//EOF