compiles on openSUSE 15.4 part 2
[engrid-github.git] / src / libengrid / containertricks.h
blob438cefd61fdd25a3da8854430ea933df6be640de
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
7 // + enGrid 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 // + enGrid 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 enGrid. If not, see <http://www.gnu.org/licenses/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #ifndef containertricks_h
22 #define containertricks_h
24 #include <iostream>
25 #include <fstream>
26 #include <string>
27 #include <vector>
28 #include <stdarg.h>
30 // vlinit
31 // ======
33 template <class C>
34 class vlinit_t
36 C *c;
38 public:
39 vlinit_t(C &a_c);
40 vlinit_t(const vlinit_t<C> &ci);
41 vlinit_t<C> add(typename C::value_type v);
42 vlinit_t<C> operator=(typename C::value_type v) { return add(v); };
43 vlinit_t<C> operator,(typename C::value_type v) { return add(v); };
46 template <class C>
47 inline vlinit_t<C>::vlinit_t(const vlinit_t<C> &ci)
49 c = ci.c;
52 template <class C>
53 inline vlinit_t<C>::vlinit_t(C &a_c)
55 c = &a_c;
58 template <class C>
59 inline vlinit_t<C> vlinit_t<C>::add(typename C::value_type v)
61 c->push_back(v);
62 return *this;
65 template <class C> inline vlinit_t<C> vlinit(C &c) { return vlinit_t<C>(c); };
68 // clinit
69 // ======
71 template <class C>
72 class clinit_t
74 C *c;
75 typename C::iterator i;
77 public:
78 clinit_t(C &a_c);
79 clinit_t(const clinit_t<C> &ci);
80 clinit_t<C> add(typename C::value_type v);
81 clinit_t<C> operator=(typename C::value_type v) { return add(v); };
82 clinit_t<C> operator,(typename C::value_type v) { return add(v); };
85 template <class C>
86 inline clinit_t<C>::clinit_t(const clinit_t<C> &ci)
88 c = ci.c;
89 i = ci.i;
92 template <class C>
93 inline clinit_t<C>::clinit_t(C &a_c)
95 c = &a_c;
96 i = c->begin();
99 template <class C>
100 inline clinit_t<C> clinit_t<C>::add(typename C::value_type v)
102 if (i == c->end()) {
103 cerr << "array bounds exceeded" << endl;
104 exit(EXIT_FAILURE);
106 *i = v;
107 i++;
108 return *this;
111 template <class C> inline clinit_t<C> clinit(C &c) { return clinit_t<C>(c); };
113 template <class C>
114 inline void clinit(C &c, typename C::value_type v, ...)
116 if (c.size() == 0) return;
117 typename C::iterator i = c.begin();
118 *i = v;
119 ++i;
120 va_list vl;
121 va_start(vl,v);
122 cout << v << ' ';
123 while ((v = va_arg(vl,typename C::value_type))) {
124 if (i == c.end()) {
125 cerr << "array bounds exceeded" << endl;
126 exit(EXIT_FAILURE);
128 *i = v;
129 cout << v << ' ';
130 ++i;
132 cout << endl;
136 // output tools
137 // ============
139 template <class C>
140 inline void simple_print(const C &c, ostream &s)
142 typename C::const_iterator i = c.begin();
143 s << '[';
144 while (i != c.end()) {
145 s << *i;
146 i++;
147 if (i != c.end()) s << ", ";
149 s << ']';
153 inline void print_table(vector<vector<double> > f, ostream &s)
155 size_t Nj = f[0].size();
156 for (size_t j = 0; j < Nj; j++) {
157 for (size_t i = 0; i < f.size(); i++) {
158 s.setf(iostream::scientific, iostream::floatfield);
159 s.precision(4);
160 s.width(11);
161 s << f[i][j] << ' ';
163 s << endl;
167 inline void print_table(vector<vector<double> > f, string file_name)
169 file_name += ".dat";
170 ofstream s(file_name.c_str());
171 print_table(f, s);
174 #endif