NSVD Reader  0.0.1
dim_type.hpp
Go to the documentation of this file.
1 
6 /*
7  * These codes are licensed under CC0.
8  * http://creativecommons.org/publicdomain/zero/1.0/
9  */
10 #ifndef __NODAMUSHI_SVD_DIM_TYPE_HPP__
11 #define __NODAMUSHI_SVD_DIM_TYPE_HPP__
12 
13 # include <ostream>
14 # include <cstdint>
15 # include <string>
16 # include <vector>
19 # include "nodamushi/to_int.hpp"
20 
21 
22 namespace nodamushi{
23 namespace svd{
24 
29 using dim_type = uint64_t;
34 using dimInc_type = uint64_t;
35 
40 template<typename STR>using dimName = STR;
41 
47 {
49  dimIndex_value(const std::string& s,size_t f,size_t e):
50  str(s.data()+f,s.data()+e),original(),has_original(false){}
53  const std::string &s, size_t f, size_t e,size_t f2,size_t e2):
54  str(std::to_string(value)), original(), has_original(true)
55  {
56  original.reserve(e + e2 - f - f2 + 1);
57  original.append(s, f, e-f);
58  original.push_back('-');
59  original.append(s, f2,e2-f2);
60  }
63  str(std::to_string(value)), original(), has_original(true) {}
64 # if __cplusplus >= 201703
65  dimIndex_value(std::string_view s, size_t f, size_t e) :
67  str(s.data() + f, s.data() + e), original(), has_original(false) {}
69  dimIndex_value(int value,
70  std::string_view s, size_t f, size_t e, size_t f2, size_t e2) :
71  str(std::to_string(value)),original(), has_original(true)
72  {
73  original.reserve(e + e2 - f - f2 + 1);
74  original.append(s, f, e-f);
75  original.push_back('-');
76  original.append(s, f2,e2-f2);
77  }
78 # endif
79 
80  bool operator==(string_ref s)const noexcept{return str == s;}
81  bool operator!=(string_ref s)const noexcept{return str != s;}
82 
84  std::string str;
90  std::string original;
91  bool has_original;
92 };
93 
98 struct dimIndex
99 {
100  dimIndex():dims{}{}
101 
102 
103  static bool is_white_space(char c)
104  {
105  return c == ' ' || c == '\t'
106 # ifndef DISABLE_SVD_LINEBREAK_AS_WHITESPACE
107  ||c == '\r' || c == '\n'
108 # endif
109  ;
110  }
111 
112 
113  dimIndex& operator=(const char* str)
114  {
115 # if __cplusplus >= 201703
116  std::string_view s = str;
117 # else
118  std::string s = str;
119 #endif
120  return operator=(s);
121  }
122 # if __cplusplus >= 201703
123  dimIndex& operator=(const std::string &str)
124  {
125  std::string_view s = str;
126  return operator=(s);
127  }
128 
129  dimIndex& operator=(std::string_view text)
130 # else
131  dimIndex& operator=(const std::string &text)
132 # endif
133  {
134  // split dimIncrement text.
135  dims.clear();
136  size_t len = text.length();
137  size_t i = 0;
138 
139  while(i < len)
140  {
141  char c;
142  // skip white space
143  while(i < len && is_white_space(text[i])) i++;
144 
145  if(i == len) break;
146 
147  const size_t p = i;
148  size_t e = p; // to trim last white space
149  //
150  // number sequence
151  // n1 - n2
152  // ddd - ddd
153  // (* d = 0,1,2,3,4,5,6,7,8,9)
154  //
155  // state
156  // 0: error
157  // 1: no-init (start)
158  // 2: n1
159  // 3: white space before '-'
160  // 4: white space after '-'
161  // 5: n2 (last)
162  // 6: trimed spaces (last)
163  // (1) -> 1 input: white-space
164  // (1) -> 2 input: d
165  // 2 -> 2 input: d
166  // 2 -> 3 input: white-space
167  // 2 -> 4 input: -
168  // 3 -> 4 input: -
169  // 4 ->(5) input: d
170  // 5 ->(5) input: d
171  // 5 ->(6) input: white-space
172  // 6 ->(6) input: white-space
173  //
174  unsigned int numseq_state = 1;
175  size_t n1_begin_pos=0;
176  size_t n1_end_pos = 0;
177  size_t n2_begin_pos=0;
178 
179  while(i < len && ((c=text[i]) != ',')){
180 
181  if(is_white_space(c)){
182  switch(numseq_state){
183  case 2:
184  numseq_state=3;
185  n1_end_pos = i;
186  break;
187  case 5: numseq_state=6;break;
188  }
189  i++;
190  continue;
191  }
192 
193  e = i;
194  bool is_num = '0' <= c && c <= '9';
195  if(is_num){
196  switch(numseq_state){
197  case 0:
198  break;
199  case 1:
200  numseq_state =2;
201  n1_begin_pos =i;
202  break;
203  case 2:
204  break;
205  case 3:
206  numseq_state = 0;
207  break;
208  case 4:
209  numseq_state = 5;
210  n2_begin_pos = i;
211  break;
212  case 5:
213  break;
214  case 6:
215  numseq_state = 0;
216  break;
217  }
218  }else if ((numseq_state == 2 || numseq_state==3)&& c == '-'){
219  if(numseq_state == 2){
220  n1_end_pos = i;
221  }
222  numseq_state = 4;
223  }else{
224  numseq_state = 0;
225  }
226  i++;
227  }
228 
229  e++;
230  if(numseq_state == 5 || numseq_state == 6){
231  const int from = ::nodamushi::to_int<int>(text,n1_begin_pos,n1_end_pos);
232  const int end = ::nodamushi::to_int<int>(text,n2_begin_pos,e);
233  for(int k = from;k<=end;k++){
234  if(k == from)
235  dims.emplace_back(k, text, n1_begin_pos, n1_end_pos,n2_begin_pos,e);
236  else
237  dims.emplace_back(k);
238  }
239  }else{
240  dims.emplace_back(text ,p, e);
241  }
242  i++;
243  }
244  return * this;
245  }
246  const std::vector<dimIndex_value>& get()const{return dims;}
247  const std::string& at(int index)const{return dims.at(index).str;}
248  size_t length()const noexcept{return dims.size();}
249  size_t size()const noexcept{return dims.size();}
250  const std::string& operator[](size_t i)const noexcept{return dims[i].str;}
251  operator bool()const noexcept{return dims.size() != 0;}
258  void append(std::string& dst,size_t index)const{
259  if(index < size())dst.append(dims[index].str);
260  else dst.append(std::to_string(index));
261  }
266  bool contains(string_ref s)const noexcept{
267  for(auto& d:dims)
268  if(d == s)
269  return true;
270  return false;
271  }
272 
273 private:
274  std::vector<dimIndex_value> dims;
275 };
276 
277 
278 
279 template<>struct value_setter_helper<dimIndex>
280 {
281  static constexpr bool value=true;
282  static bool set(const std::string& src,dimIndex& dst)
283  {
284  dst = src;
285  bool b = dst;
286  return b;
287  }
288 # if __cplusplus >= 201703
289  static bool set(std::string_view src,dimIndex& dst)
290  {
291  dst = src;
292  bool b = dst;
293  return b;
294  }
295 # endif
296 };
297 
298 
299 }} // end namespace svd
300 
301 
302 
303 namespace std{
304 ostream& operator <<(ostream& o,const ::nodamushi::svd::dimIndex& d)
305 {
306  bool first = true;
307  for(const auto& v:d.get()){
308  if(v.has_original && v.original.empty())
309  continue;
310  if(first) first = false;
311  else o << ",";
312  if(v.has_original)
313  o << v.original;
314  else
315  o << v.str;
316  }
317  return o;
318 }
319 } // end namespace std
320 
321 #endif // __NODAMUSHI_SVD_DIM_TYPE_HPP__
ostream & operator<<(std::ostream &os, const ::nodamushi::svd::Access &value)
Definition: Access.hpp:144
const std::string & at(int index) const
Definition: dim_type.hpp:247
Definition: Access.hpp:143
dimIndex_value(int value, const std::string &s, size_t f, size_t e, size_t f2, size_t e2)
for first number
Definition: dim_type.hpp:52
const std::string & string_ref
Definition: string_type.hpp:44
const std::vector< dimIndex_value > & get() const
Definition: dim_type.hpp:246
uint64_t dim_type
Definition: dim_type.hpp:29
dimIndex & operator=(const std::string &text)
Definition: dim_type.hpp:131
dimIndex_value(const std::string &s, size_t f, size_t e)
value
Definition: dim_type.hpp:49
static bool is_white_space(char c)
Definition: dim_type.hpp:103
static bool set(const std::string &src, dimIndex &dst)
Definition: dim_type.hpp:282
bool operator!=(string_ref s) const noexcept
Definition: dim_type.hpp:81
size_t length() const noexcept
Definition: dim_type.hpp:248
std::string str
dimIndex text
Definition: dim_type.hpp:84
bool operator==(string_ref s) const noexcept
Definition: dim_type.hpp:80
This class reperesents SVD(xml) element / attribute.
Definition: value.hpp:53
void append(std::string &dst, size_t index) const
Definition: dim_type.hpp:258
bool has_original
flag for original.
Definition: dim_type.hpp:91
size_t size() const noexcept
Definition: dim_type.hpp:249
dimIndex_value(int value)
for number
Definition: dim_type.hpp:62
uint64_t dimInc_type
Definition: dim_type.hpp:34
dimIndex & operator=(const char *str)
Definition: dim_type.hpp:113
constexpr small library
const std::string & operator[](size_t i) const noexcept
Definition: dim_type.hpp:250
bool contains(string_ref s) const noexcept
Definition: dim_type.hpp:266