.gitignore changes
[engrid.git] / containertricks.h
blobf578f250adb370d4234efa68257d8138bd29b26c
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008,2009 Oliver Gloth +
7 // + +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
12 // + +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
17 // + +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #ifndef containertricks_h
24 #define containertricks_h
26 #include <iostream>
27 #include <fstream>
28 #include <string>
29 #include <vector>
30 #include <stdarg.h>
32 // vlinit
33 // ======
35 template <class C>
36 class vlinit_t
38 C *c;
40 public:
41 vlinit_t(C &a_c);
42 vlinit_t(const vlinit_t<C> &ci);
43 vlinit_t<C> add(typename C::value_type v);
44 vlinit_t<C> operator=(typename C::value_type v) { return add(v); };
45 vlinit_t<C> operator,(typename C::value_type v) { return add(v); };
48 template <class C>
49 inline vlinit_t<C>::vlinit_t(const vlinit_t<C> &ci)
51 c = ci.c;
54 template <class C>
55 inline vlinit_t<C>::vlinit_t(C &a_c)
57 c = &a_c;
60 template <class C>
61 inline vlinit_t<C> vlinit_t<C>::add(typename C::value_type v)
63 c->push_back(v);
64 return *this;
67 template <class C> inline vlinit_t<C> vlinit(C &c) { return vlinit_t<C>(c); };
70 // clinit
71 // ======
73 template <class C>
74 class clinit_t
76 C *c;
77 typename C::iterator i;
79 public:
80 clinit_t(C &a_c);
81 clinit_t(const clinit_t<C> &ci);
82 clinit_t<C> add(typename C::value_type v);
83 clinit_t<C> operator=(typename C::value_type v) { return add(v); };
84 clinit_t<C> operator,(typename C::value_type v) { return add(v); };
87 template <class C>
88 inline clinit_t<C>::clinit_t(const clinit_t<C> &ci)
90 c = ci.c;
91 i = ci.i;
94 template <class C>
95 inline clinit_t<C>::clinit_t(C &a_c)
97 c = &a_c;
98 i = c->begin();
101 template <class C>
102 inline clinit_t<C> clinit_t<C>::add(typename C::value_type v)
104 if (i == c->end()) {
105 cerr << "array bounds exceeded" << endl;
106 exit(EXIT_FAILURE);
108 *i = v;
109 i++;
110 return *this;
113 template <class C> inline clinit_t<C> clinit(C &c) { return clinit_t<C>(c); };
115 template <class C>
116 inline void clinit(C &c, typename C::value_type v, ...)
118 if (c.size() == 0) return;
119 typename C::iterator i = c.begin();
120 *i = v;
121 ++i;
122 va_list vl;
123 va_start(vl,v);
124 cout << v << ' ';
125 while (v = va_arg(vl,typename C::value_type)) {
126 if (i == c.end()) {
127 cerr << "array bounds exceeded" << endl;
128 exit(EXIT_FAILURE);
130 *i = v;
131 cout << v << ' ';
132 ++i;
134 cout << endl;
138 // output tools
139 // ============
141 template <class C>
142 inline void simple_print(const C &c, ostream &s)
144 typename C::const_iterator i = c.begin();
145 s << '[';
146 while (i != c.end()) {
147 s << *i;
148 i++;
149 if (i != c.end()) s << ", ";
151 s << ']';
155 inline void print_table(vector<vector<double> > f, ostream &s)
157 size_t Nj = f[0].size();
158 for (size_t j = 0; j < Nj; j++) {
159 for (size_t i = 0; i < f.size(); i++) {
160 s.setf(iostream::scientific, iostream::floatfield);
161 s.precision(4);
162 s.width(11);
163 s << f[i][j] << ' ';
165 s << endl;
169 inline void print_table(vector<vector<double> > f, string file_name)
171 file_name += ".dat";
172 ofstream s(file_name.c_str());
173 print_table(f, s);
176 #endif