aboutsummaryrefslogtreecommitdiffstats
path: root/data_sets/shared_table_data_set.hpp
blob: 7a80a74f4e37bebcaf7429cb73f0688fa00e4afb (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
   \file shared_table_data_set.hpp
   \brief A data set with high copy efficiency
   \author Junhua Gu
 */

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


namespace opt_utilities
{

  /**
     \brief shared_table implement of the data set
     When the shared_table_data_set clones self, it doesn't
     allocate a new instance, but returns a pointer to self.
     \tparam Ty type of y
     \tparam Tx type of x
   */
  template <typename Ty,typename Tx>
  class shared_table_data_set
    :public data_set<Ty,Tx>
  {
    //  private:
  public:
    std::vector<data<Ty,Tx> > data_vec;
    
    /**
       Only returns a pointer to self
     */
    data_set<Ty,Tx>* do_clone()const
    {
      return (data_set<Ty,Tx>*)(this);
    }

    /**
       We do nothing here.
     */
    void do_destroy()
    {
    }


    const data<Ty,Tx>& do_get_data(size_t i)const
    {
      return data_vec.at(i);
    }

    void do_set_data(size_t i,const data<Ty,Tx>& d)
    {
      data_vec.at(i)=d;
    }
  
    size_t do_size()const
    {
      return data_vec.size();
    }
  
    void do_add_data(const data<Ty,Tx>& d)
    {
      data_vec.push_back(d);
    }
  
    void do_clear()
    {
      data_vec.clear();
    }

    bool insert_data(int idx,const data<Ty,Tx>& d)
    {
      if(idx<0||idx>data_vec.size())
	{
	  return false;
	}
      data_vec.insert(data_vec.begin()+idx,d);
      return true;
    }

    bool insert_data(int idx,size_t n,const data<Ty,Tx>& d)
    {
      if(idx<0||idx>data_vec.size())
	{
	  return false;
	}
      data_vec.insert(data_vec.begin()+idx,n,d);
      return true;
    }
    

    bool erase_data(size_t idx)
    {
      if(idx>=0&&idx<data_vec.size())
	{
	  data_vec.erase(data_vec.begin()+idx);
	  return true;
	}
      return false;
    }

    bool erase_data(size_t beg,size_t end)
    {
      if(beg>=0&&beg<=end&&end<=data_vec.size())
	{
	  data_vec.erase(data_vec.begin()+beg,data_vec.begin()+end);
	  return true;
	}
      return false;
    }

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

    shared_table_data_set(const data_set<Ty,Tx>& rhs)
    {
      data_vec.resize(rhs.size());
      for(int i=0;i<data_vec.size();++i)
	{
	  data_vec[i](rhs.get_data(i));
	}
    }

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

    shared_table_data_set& operator=(const data_set<Ty,Tx>& rhs)
    {
      data_vec.resize(rhs.size());
      for(int i=0;i<data_vec.size();++i)
	{
	  data_vec[i](rhs.get_data(i));
	}
      return *this;
    }

    void reserve(size_t n)
    {
      data_vec.reserve(n);
    }

    
  };
}

#endif
//EOF