annotation.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2015 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Justus Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * tensor.h
22  * Jun 16, 2015
23  *
24  */
25 
26 #ifndef TILEDARRAY_ANNOTATION_H__INCLUDED
27 #define TILEDARRAY_ANNOTATION_H__INCLUDED
28 #include <algorithm>
29 #include <cstring>
30 #include <string>
31 #include "TiledArray/error.h"
32 
33 namespace TiledArray::detail {
34 
35 inline std::string dummy_annotation(unsigned int n_outer_size,
36  unsigned int n_inner_size = 0) {
37  std::ostringstream oss;
38  if (n_outer_size > 0) oss << "i0";
39  for (unsigned int d = 1; d < n_outer_size; ++d) oss << ",i" << d;
40  if (n_inner_size == 0) return oss.str();
41  oss << ";i" << n_outer_size;
42  for (unsigned int d = 1; d < n_inner_size; ++d)
43  oss << ",i" << d + n_outer_size;
44  return oss.str();
45 }
46 
52 inline auto remove_whitespace(std::string s) {
53  s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
54  return s;
55 }
56 
81 inline auto tokenize_index(const std::string& s, char delim) {
82  if (s.size() == 0) return std::vector<std::string>{""};
83  std::vector<std::string> tokens;
84  std::stringstream ss(s);
85  std::string buffer;
86  while (std::getline(ss, buffer, delim))
87  tokens.emplace_back(std::move(buffer));
88 
89  // If the delimiter is the last element, we miss an empty string so add it
90  if (s[s.size() - 1] == delim) tokens.push_back(std::string{});
91  return tokens;
92 }
93 
122 inline bool is_valid_index(const std::string& idx) {
123  const std::string valid_chars =
124  "abcdefghijklmnopqrstuvwxyz"
125  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
126  "1234567890"
127  " _',;";
128  // Are valid characters
129  for (const auto& c : idx)
130  if (valid_chars.find(c) == std::string::npos) return false;
131 
132  // Is not only whitespace
133  auto no_ws = remove_whitespace(idx);
134  if (no_ws.size() == 0) return false;
135 
136  // At most one semicolon
137  auto split_on_semicolon = tokenize_index(no_ws, ';');
138  if (split_on_semicolon.size() > 2) return false;
139 
140  for (auto x : split_on_semicolon) {
141  auto indices = tokenize_index(x, ',');
142  for (const auto& idx : indices)
143  if (idx.size() == 0) return false;
144  }
145 
146  return true;
147 }
148 
160 inline bool is_tot_index(const std::string& idx) {
161  if (!is_valid_index(idx)) return false;
162  return idx.find(";") != std::string::npos;
163 }
164 
191 inline auto split_index(const std::string& idx) {
193  auto no_ws = remove_whitespace(idx);
194  if (!is_tot_index(no_ws)) {
195  return std::make_pair(tokenize_index(no_ws, ','),
196  std::vector<std::string>{});
197  }
198  auto tot_idx = tokenize_index(no_ws, ';');
199  return std::make_pair(tokenize_index(tot_idx[0], ','),
200  tokenize_index(tot_idx[1], ','));
201 }
202 
203 } // namespace TiledArray::detail
204 
205 #endif // TILEDARRAY_ANNOTATION_H__INCLUDED
std::string dummy_annotation(unsigned int n_outer_size, unsigned int n_inner_size=0)
Definition: annotation.h:35
auto remove_whitespace(std::string s)
Definition: annotation.h:52
auto split_index(const std::string &idx)
Definition: annotation.h:191
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
bool is_tot_index(const std::string &idx)
Definition: annotation.h:160
auto tokenize_index(const std::string &s, char delim)
Definition: annotation.h:81
bool is_valid_index(const std::string &idx)
Definition: annotation.h:122