JSN_Writer.h 11.2 KB
Newer Older
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
1
2
3
4
5
6
//-*-C++-*-

/** \ingroup DCA */
/*@{*/

/*! \file JSN_Writer.h
7
Author: Michael S. Summers
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
8
9
10
11
12
13

*/

#ifndef dca_JSN_Writer_H
#define dca_JSN_Writer_H

Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
14
15
#include "VectorLike.h"

Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
16
17
namespace dca {

Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
18
19
20
21
22
23
  using namespace psimag::VectorLike;

  template<typename T> class PrintArrayWidth         {public: enum {value=7 }; };
  template<>           class PrintArrayWidth<double> {public: enum {value=13}; };

  template<template<typename,typename> class MapType=std::map>
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
24
25
26
  class JSN {
  public:

Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
27
28
29
30
    typedef enum {First,Middle,Last} Position;
    typedef JSN<MapType> ThisType;

    //======================================================================
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
31

Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
32
33
34
35
    JSN(size_t offset_=0):
      offset          (offset_),
      precis          (10),
      printedFirstLine(false)
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
36
37
    {}
    
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
    //======================================================================

    class KeyReference {
    public:
      JSN& writer;
      std::string key;

      KeyReference(std::string k, JSN& w):
	writer(w),
	key   (k)
      {}

      template<typename T>
      void operator= (const T& val) {
	writer.add(key,val);
      }

      operator JSN& () {
	return writer;
      }

      KeyReference operator[](std::string subKey) { return writer.getComponentWriter(key)[subKey]; }
    };

    //======================================================================

    KeyReference operator[](std::string key) { return KeyReference(key,*this); }

    //======================================================================

    ThisType& getComponentWriter(std::string key) { return writers[key]; }

    //======================================================================

    mutable int  offset;
    int  precis;
    mutable bool printedFirstLine;

    MapType<std::string, bool>                          bools;
    MapType<std::string, std::string>                   strings;
    MapType<std::string, double>                        numbers;
    MapType<std::string, std::vector<double> >          vectors;
    MapType<std::string, std::vector<int> >             intvectors;
    MapType<std::string, std::vector<size_t> >          sizetvectors;
    MapType<std::string, std::vector<std::string> >     stringLists;
    MapType<std::string, const psimag::Matrix<int>*>    intMatrices;
    MapType<std::string, const psimag::Matrix<double>*> dblMatrices;
    MapType<std::string, const psimag::Matrix<std::complex<double> >*> cMatrices;

    MapType<std::string, ThisType>                   writers;
    
    //======================================================================

    void add(std::string key, const bool&          b)   { bools  [key] = b;    }
    void add(std::string key, const char*        str)   { strings[key] = str;  }
    void add(std::string key, const std::string& str)   { strings[key] = str;  }
    void add(std::string key, const size_t& val)              { numbers[key] = val;  }
    void add(std::string key, const int&    val)              { numbers[key] = val;  }
    void add(std::string key, const double& val)              { numbers[key] = val;  }

    void add(std::string key, const std::vector<double>&    vals) { vectors[key]     = vals; }
    void add(std::string key, const std::vector<int>&       vals) { intvectors[key]  = vals; }
    void add(std::string key, const std::vector<size_t>&    vals) { sizetvectors[key]= vals; }
    void add(std::string key, const psimag::Matrix<int>&    mat ) { intMatrices[key] = &mat; }
    void add(std::string key, const psimag::Matrix<double>& mat ) { dblMatrices[key] = &mat; }

    //======================================================================

    template<class T>
    void add(std::string key, const T& obj){ 
      obj.toJSN(getComponentWriter(key));
    }

    //======================================================================

    template<typename T>
    void add(std::string key, const std::map<std::string, T>& map) {
      typedef typename std::map<std::string, T>::const_iterator itr;
      ThisType& writer = writers[key]; 
      for(itr i=map.begin(); i!= map.end(); i++) 
	writer.add(i->first,i->second);
    }
    
    //======================================================================

    template<typename ValType>
    static
    int maxKeyWidth( const MapType<std::string, ValType>& map) {
      size_t result = 0;
      typedef typename MapType<std::string, ValType>::const_iterator itr;
      for(itr i=map.begin(); i!= map.end(); i++) 
	if( i->first.length() > result)
	  result = i->first.length();
      return result+2;
    }

    //======================================================================

    int maxKeyWidth() const {
      int result = 0;
      int maxKeyLen = 0; 

      maxKeyLen = maxKeyWidth(strings);
      if (maxKeyLen > result) result = maxKeyLen;

      maxKeyLen = maxKeyWidth(bools);
      if (maxKeyLen > result) result = maxKeyLen;

      maxKeyLen = maxKeyWidth(numbers);
      if (maxKeyLen > result) result = maxKeyLen;

      maxKeyLen = maxKeyWidth(vectors);
      if (maxKeyLen > result) result = maxKeyLen;

      maxKeyLen = maxKeyWidth(intvectors);
      if (maxKeyLen > result) result = maxKeyLen;

      maxKeyLen = maxKeyWidth(sizetvectors);
      if (maxKeyLen > result) result = maxKeyLen;

      maxKeyLen = maxKeyWidth(stringLists);
      if (maxKeyLen > result) result = maxKeyLen;

      maxKeyLen = maxKeyWidth(writers);
      if (maxKeyLen > result) result = maxKeyLen;

      maxKeyLen = maxKeyWidth(intMatrices);
      if (maxKeyLen > result) result = maxKeyLen;

      maxKeyLen = maxKeyWidth(dblMatrices);
      if (maxKeyLen > result) result = maxKeyLen;

      maxKeyLen = maxKeyWidth(cMatrices);
      if (maxKeyLen > result) result = maxKeyLen;

      return result;
    }

    //======================================================================

    template<typename ValType>
    void printLines(std::ostream& os,
		    const MapType<std::string, ValType>& map,
		    int offset, 
		    int keyWidth,
		    Position position=Middle) const {
      
      typedef typename MapType<std::string, ValType>::const_iterator itr;
      
      for(itr i=map.begin(); i!= map.end(); i++) {
	printLine(os,i->first,i->second,offset,keyWidth,printedFirstLine);
	printedFirstLine = true;
      }
    }

    //======================================================================
    
    template<typename ValType>
    static
    void printLine(std::ostream& os,
		   const std::string& key,
		   const ValType&     val,
		   int  offset, 
		   int  keyWidth,
		   bool printedFirstLine_,
		   bool printVal=true)  {

      if (printedFirstLine_) 
	os << ",\n" << std::string(offset,' ') ;
      
      os << std::setw(keyWidth) << std::left << quoted(key);
      os << " : ";
      if (printVal)
	valString(os,offset,keyWidth,val) ;
    }

    //======================================================================
    //======================================================================

    template<typename T>
    static 
    void valString(std::ostream& os,int offset, int keyWidth,
		   const T& val) {
      os << val;
    }

    template<typename T>
    static 
    void valString(std::ostream& os,int offset, int keyWidth,
		   const psimag::Matrix<T>* val) {

      const psimag::Matrix<T>& matrix(*val);

      int kWidth    = 6;
      int newOffset = offset + keyWidth + 4; 

      os << "{";
      printLine(os,"rows", matrix.n_row(), newOffset, kWidth, false);
      printLine(os,"cols", matrix.n_col(), newOffset, kWidth, true );
      printLine(os,"data", 0,              newOffset, kWidth, true, false);
      psimag::MatrixLike::printArray(matrix,os,PrintArrayWidth<T>::value,newOffset+8);
      os << "}";

    }

    static
    void valString(std::ostream& os, int offset, int keyWidth,
		   const std::string& val) {
      os << quoted(val);
    }

    static
    void valString(std::ostream& os,int offset, int keyWidth,
		   const ThisType& val) {
      val.print(os, offset + keyWidth + 4);
    }

    template<typename T>
    static
    void valString(std::ostream& os, int offset, int keyWidth,
		   const std::vector<T>& val) {
      size_t width = maxElementStringWidth(val);
      printList(val,os,',',width);
    }

    //======================================================================

    void print(std::ostream& os,int off=1,bool printLastBrace=true) const {

      offset           = off;
      printedFirstLine = false;

      int keyWidth   = maxKeyWidth();

      os << "{";
      printLines(os,strings,     offset, keyWidth);
      printLines(os,bools,       offset, keyWidth);
      printLines(os,numbers,     offset, keyWidth);
      printLines(os,vectors,     offset, keyWidth);
      printLines(os,sizetvectors,offset, keyWidth);
      printLines(os,intvectors,  offset, keyWidth);
      printLines(os,stringLists, offset, keyWidth);
      printLines(os,intMatrices, offset, keyWidth);
      printLines(os,dblMatrices, offset, keyWidth);
      printLines(os,cMatrices,   offset, keyWidth);
      printLines(os,writers,     offset, keyWidth);
      if (printLastBrace)
	os << "}";
    }

    //======================================================================
      
    static
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
291
    std::string toString(const std::map<std::string, std::string>& map) {
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
292

Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
293
294
295
296
297
298
299
300
301
302
303
304
305
      std::ostringstream buff;
      buff << "{";
      std::map<std::string,std::string>::const_iterator itr;
      for (itr = map.begin(); itr != map.end(); itr++) {
	if (itr != map.begin())
	  buff << ",";
	buff << " \"" << itr->first << "\": \"" << itr->second << "\"";
      }
      buff << "}";
      return buff.str();
    }

    template<typename T>
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
306
    static
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
    std::string toString(const std::map<std::string, T>& map) {
      typedef std::map<std::string,T> MapType;
      typedef typename MapType::const_iterator CITR;

      std::ostringstream buff;
      buff << "{";
      for (CITR itr = map.begin(); itr != map.end(); itr++) {
	if (itr != map.begin())
	  buff << ",";
	buff << " \"" << itr->first << "\": " << itr->second;
      }
      buff << "}";
      return buff.str();
    }

    template<typename T>
    std::string toString(const std::map<std::string, std::vector<T> >& map) {

      typedef std::map<std::string,T> MapType;
      typedef typename MapType::const_iterator CITR;

      std::ostringstream buff;
      buff << "{";

      for (CITR itr = map.begin(); itr != map.end(); itr++) {
	if (itr != map.begin())
	  buff << ",";
	buff << " \"" << itr->first << "\": " <<  psimag::VectorLike::toString(itr->second);
      }
      buff << "}";
      return buff.str();
    }

    template<class T>
    inline
    void printVector(std::vector<T> vec,
		     std::string title,
		     std::ostream& os)  {
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
345
      os.precision(precis);
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
346
347
348
349
350
351
352
353
      os << std::fixed; // scientific;
      os << "{ "
	 << " \'tile\': \'" << title   << "\', \n"
	 << " \'type\': \'Function1D', \n"
	 << " \'size\': " << vec.size() << ", \n";
      os << " \'data\': ";
      os << "array([[";
      for(size_t j=0; j<vec.size(); j++) 
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
354
	os << " " << std::setw(precis) << vec[j] << ",\n";
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
355
356
357
358
      os << "]])\n";
      
      os << "}";
    }
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
359
360
361
362
363
364
    //====================================================================== 
    static 
    std::string quoted(std::string str) {
      std::ostringstream result;
      result << "\"" << str << "\"";
      return result.str();
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
365
366
    }
    
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
367
368
    //======================================================================
   
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
369
370
371
372
373
374
375
  };
  
} // end namespace DCA


/*@}*/
#endif