xview.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xoutput.h"
4 #include <set>
5 
6 namespace nmx {
7 
12 template<class CONTAINER>
13 class View
14 {
15 private:
16  //Zeiger auf einen Container
17  const CONTAINER *_data = nullptr;
18 
19  //sortierte Indexliste keine doppelten Einträge
20  std::set<size_t> _indexlist;
21 
22 public:
27  inline View(const CONTAINER &input)
28  : _data{ &input } {}
29 
34  inline View(const View &v)
35  : _data{ v._data }
36  , _indexlist{ v._indexlist } {}
37 
42  inline View(View &&v)
43  : _data{ nullptr } {
44  //vertausche Zeiger und Indexliste
45  std::swap(_data, v._data);
46  std::swap(_indexlist, v._indexlist);
47  //v._data ist ungültig und v._indexlist ist leer
48  }
49 
55  inline auto &operator=(const View &v) {
56  if (this != &v) {
57  _data = v._data;
58  _indexlist = v._indexlist;
59  }
60  return *this;
61  }
62 
68  inline auto &operator=(View &&v) {
69  if (this != &v) {
70  std::swap(_data, v._data);
71  std::swap(_indexlist, v._indexlist);
72  v._data = nullptr;
73  v._indexlist.clear();
74  }
75  return this;
76  }
77 
82  inline bool empty() const { return _indexlist.empty(); }
83 
88  inline bool is_valid() const { return _data != nullptr; }
89 
94  inline void add(size_t idx) { _indexlist.insert(idx); }
95 
100  template<class T>
101  void add(const T &idxlst) {
102  _indexlist.insert(idxlst.begin(), idxlst.end());
103  }
104 
110  inline void save(std::ofstream &ofs, Format fmt) const {
111  for (const auto idx : _indexlist) {
112  const auto &crow = _data->row(idx);
113  fmt.write_line(ofs, crow);
114  }
115  }
116 
123  template<class FN>
124  void save(std::ofstream &ofs, Format fmt, FN fn) const {
125  for (const auto idx : _indexlist) {
126  const auto crow = fn(_data->row(idx));
127  fmt.write_line(ofs, crow);
128  }
129  }
130 
136  inline auto column(size_t cidx) const {
137  //der Datentyp der Spalte wird über den Template-Parameter
138  //festgelegt. (reserviere Speicher)
139  typename CONTAINER::Column clmn(_indexlist.size());
140  auto clmnitr = clmn.begin();
141  for (const auto &idx : _indexlist) {
142  //idx : Reihe auf welche die View Zugriff hat
143  //cidx: die gewünschte Spalte
144  *clmnitr = _data->row(idx)[cidx];
145  //zeige auf nächstes Element
146  clmnitr++;
147  }
148  return clmn;
149  }
150 }; //View
151 
152 } // namespace nmx
void add(size_t idx)
add addiere Elemente
Definition: xview.h:94
void save(std::ofstream &ofs, Format fmt, FN fn) const
save Speichere Container auf den die View zeigt
Definition: xview.h:124
void add(const T &idxlst)
add addiere Elemente über Index-Liste
Definition: xview.h:101
void save(std::ofstream &ofs, Format fmt) const
save Speichere Container auf den die View zeigt
Definition: xview.h:110
View(const View &v)
View Kopierkonstruktor.
Definition: xview.h:34
View(View &&v)
View Move-Konstruktor.
Definition: xview.h:42
void write_line(std::ostream &ofs, const T &crow) const
write_line Daten in einem Array werden in einer Zeile ausgegeben
Definition: xfmt.h:66
auto & operator=(View &&v)
operator =
Definition: xview.h:68
The View class Sicht auf Teilmenge eines Containers.
Definition: xview.h:13
auto & operator=(const View &v)
operator = Zuweisungsoperator
Definition: xview.h:55
The Format class Formatierte Ausgabe von Arrays.
Definition: xfmt.h:10
auto column(size_t cidx) const
column Kopie einer Spalte
Definition: xview.h:136
bool empty() const
empty
Definition: xview.h:82
bool is_valid() const
is_valid
Definition: xview.h:88
View(const CONTAINER &input)
View Konstruktor.
Definition: xview.h:27