Xpetra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Xpetra_BlockReorderManager.cpp
Go to the documentation of this file.
1 
3 
4 namespace Xpetra {
5 
6 void BlockReorderManager::SetBlock(int blockIndex, int reorder) {
7  TEUCHOS_ASSERT(blockIndex < (int)children_.size());
8  Teuchos::RCP<BlockReorderManager> child = Teuchos::rcp(new BlockReorderLeaf(reorder));
9  children_[blockIndex] = child;
10 }
11 
12 void BlockReorderManager::SetBlock(int blockIndex, const Teuchos::RCP<BlockReorderManager>& reorder) {
13  TEUCHOS_ASSERT(blockIndex < (int)children_.size());
14  children_[blockIndex] = reorder;
15 }
16 
17 // this function tokenizes a string, breaking out whitespace but saving the
18 // brackets [,] as special tokens.
19 void tokenize(std::string srcInput, std::string whitespace, std::string prefer, std::vector<std::string>& tokens) {
20  std::string input = srcInput;
21  std::vector<std::string> wsTokens;
22  std::size_t endPos = input.length() - 1;
23  while (endPos < input.length()) {
24  std::size_t next = input.find_first_of(whitespace);
25 
26  // get the sub string
27  std::string s;
28  if (next != std::string::npos) {
29  s = input.substr(0, next);
30 
31  // break out the old substring
32  input = input.substr(next + 1, endPos);
33  } else {
34  s = input;
35  input = "";
36  }
37 
38  endPos = input.length() - 1;
39 
40  // add it to the WS tokens list
41  if (s == "") continue;
42  wsTokens.push_back(s);
43  }
44 
45  for (unsigned int i = 0; i < wsTokens.size(); i++) {
46  // get string to break up
47  input = wsTokens[i];
48 
49  endPos = input.length() - 1;
50  while (endPos < input.length()) {
51  std::size_t next = input.find_first_of(prefer);
52 
53  std::string s = input;
54  if (next > 0 && next < input.length()) {
55  // get the sub string
56  s = input.substr(0, next);
57 
58  input = input.substr(next, endPos);
59  } else if (next == 0) {
60  // get the sub string
61  s = input.substr(0, next + 1);
62 
63  input = input.substr(next + 1, endPos);
64  } else
65  input = "";
66 
67  // break out the old substring
68  endPos = input.length() - 1;
69 
70  // add it to the tokens list
71  tokens.push_back(s);
72  }
73  }
74 }
75 
76 // this function takes a set of tokens and returns the first "block", i.e. those
77 // values (including) brackets that correspond to the first block
78 std::vector<std::string>::const_iterator buildSubBlock(
79  std::vector<std::string>::const_iterator begin,
80  std::vector<std::string>::const_iterator end,
81  std::vector<std::string>& subBlock) {
82  std::stack<std::string> matched;
83  std::vector<std::string>::const_iterator itr;
84  for (itr = begin; itr != end; ++itr) {
85  subBlock.push_back(*itr);
86 
87  // push/pop brackets as they are discovered
88  if (*itr == "[")
89  matched.push("[");
90  else if (*itr == "]")
91  matched.pop();
92 
93  // found all matching brackets
94  if (matched.empty())
95  return itr;
96  }
97 
98  TEUCHOS_ASSERT(matched.empty());
99 
100  return itr - 1;
101 }
102 
103 // This function takes a tokenized vector and converts it to a block reorder manager
104 Teuchos::RCP<Xpetra::BlockReorderManager> blockedReorderFromTokens(const std::vector<std::string>& tokens) {
105  // base case
106  if (tokens.size() == 1)
107  return Teuchos::rcp(new Xpetra::BlockReorderLeaf(Teuchos::StrUtils::atoi(tokens[0])));
108 
109  // check first and last character
110  TEUCHOS_ASSERT(*(tokens.begin()) == "[")
111  TEUCHOS_ASSERT(*(tokens.end() - 1) == "]");
112 
113  std::vector<Teuchos::RCP<Xpetra::BlockReorderManager> > vecRMgr;
114  std::vector<std::string>::const_iterator itr = tokens.begin() + 1;
115  while (itr != tokens.end() - 1) {
116  // figure out which tokens are relevant for this block
117  std::vector<std::string> subBlock;
118  itr = buildSubBlock(itr, tokens.end() - 1, subBlock);
119 
120  // build the child block reorder manager
121  vecRMgr.push_back(blockedReorderFromTokens(subBlock));
122 
123  // move the iterator one more
124  itr++;
125  }
126 
127  // build the parent reorder manager
128  Teuchos::RCP<Xpetra::BlockReorderManager> rMgr = Teuchos::rcp(new Xpetra::BlockReorderManager());
129  rMgr->SetNumBlocks(vecRMgr.size());
130  for (unsigned int i = 0; i < vecRMgr.size(); i++)
131  rMgr->SetBlock(i, vecRMgr[i]);
132 
133  return rMgr;
134 }
135 
147 Teuchos::RCP<const Xpetra::BlockReorderManager> blockedReorderFromString(std::string reorder) {
148  // vector of tokens to use
149  std::vector<std::string> tokens;
150 
151  // manager to be returned
152 
153  // build tokens vector
154  tokenize(reorder, " \t\n", "[]", tokens);
155 
156  // parse recursively and build reorder manager
157  Teuchos::RCP<Xpetra::BlockReorderManager> mgr = blockedReorderFromTokens(tokens);
158 
159  return mgr;
160 }
161 
162 } // namespace Xpetra
std::vector< std::string >::const_iterator buildSubBlock(std::vector< std::string >::const_iterator begin, std::vector< std::string >::const_iterator end, std::vector< std::string > &subBlock)
Teuchos::RCP< const Xpetra::BlockReorderManager > blockedReorderFromString(std::string reorder)
Convert a string to a block reorder manager object.
void tokenize(std::string srcInput, std::string whitespace, std::string prefer, std::vector< std::string > &tokens)
virtual void SetBlock(int blockIndex, int reorder)
Sets the subblock to a specific index value.
Teuchos::RCP< Xpetra::BlockReorderManager > blockedReorderFromTokens(const std::vector< std::string > &tokens)
std::vector< Teuchos::RCP< BlockReorderManager > > children_
definitions of the subblocks