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  }
34  else {
35  s = input;
36  input = "";
37  }
38 
39  endPos = input.length()-1;
40 
41  // add it to the WS tokens list
42  if(s=="") continue;
43  wsTokens.push_back(s);
44  }
45 
46  for(unsigned int i=0;i<wsTokens.size();i++) {
47  // get string to break up
48  input = wsTokens[i];
49 
50  endPos = input.length()-1;
51  while(endPos<input.length()) {
52  std::size_t next = input.find_first_of(prefer);
53 
54  std::string s = input;
55  if(next>0 && next<input.length()) {
56 
57  // get the sub string
58  s = input.substr(0,next);
59 
60  input = input.substr(next,endPos);
61  }
62  else if(next==0) {
63  // get the sub string
64  s = input.substr(0,next+1);
65 
66  input = input.substr(next+1,endPos);
67  }
68  else input = "";
69 
70  // break out the old substring
71  endPos = input.length()-1;
72 
73  // add it to the tokens list
74  tokens.push_back(s);
75  }
76  }
77 }
78 
79 // this function takes a set of tokens and returns the first "block", i.e. those
80 // values (including) brackets that correspond to the first block
81 std::vector<std::string>::const_iterator buildSubBlock(
82  std::vector<std::string>::const_iterator begin,
83  std::vector<std::string>::const_iterator end,
84  std::vector<std::string> & subBlock) {
85  std::stack<std::string> matched;
86  std::vector<std::string>::const_iterator itr;
87  for(itr=begin;itr!=end;++itr) {
88 
89  subBlock.push_back(*itr);
90 
91  // push/pop brackets as they are discovered
92  if(*itr=="[") matched.push("[");
93  else if(*itr=="]") matched.pop();
94 
95  // found all matching brackets
96  if(matched.empty())
97  return itr;
98  }
99 
100  TEUCHOS_ASSERT(matched.empty());
101 
102  return itr-1;
103 }
104 
105 // This function takes a tokenized vector and converts it to a block reorder manager
106 Teuchos::RCP<Xpetra::BlockReorderManager> blockedReorderFromTokens(const std::vector<std::string> & tokens)
107 {
108  // base case
109  if(tokens.size()==1)
110  return Teuchos::rcp(new Xpetra::BlockReorderLeaf(Teuchos::StrUtils::atoi(tokens[0])));
111 
112  // check first and last character
113  TEUCHOS_ASSERT(*(tokens.begin())=="[")
114  TEUCHOS_ASSERT(*(tokens.end()-1)=="]");
115 
116  std::vector<Teuchos::RCP<Xpetra::BlockReorderManager> > vecRMgr;
117  std::vector<std::string>::const_iterator itr = tokens.begin()+1;
118  while(itr!=tokens.end()-1) {
119  // figure out which tokens are relevant for this block
120  std::vector<std::string> subBlock;
121  itr = buildSubBlock(itr,tokens.end()-1,subBlock);
122 
123  // build the child block reorder manager
124  vecRMgr.push_back(blockedReorderFromTokens(subBlock));
125 
126  // move the iterator one more
127  itr++;
128  }
129 
130  // build the parent reorder manager
131  Teuchos::RCP<Xpetra::BlockReorderManager> rMgr = Teuchos::rcp(new Xpetra::BlockReorderManager());
132  rMgr->SetNumBlocks(vecRMgr.size());
133  for(unsigned int i=0;i<vecRMgr.size();i++)
134  rMgr->SetBlock(i,vecRMgr[i]);
135 
136  return rMgr;
137 }
138 
150 Teuchos::RCP<const Xpetra::BlockReorderManager> blockedReorderFromString(std::string reorder)
151 {
152  // vector of tokens to use
153  std::vector<std::string> tokens;
154 
155  // manager to be returned
156 
157  // build tokens vector
158  tokenize(reorder," \t\n","[]",tokens);
159 
160  // parse recursively and build reorder manager
161  Teuchos::RCP<Xpetra::BlockReorderManager> mgr = blockedReorderFromTokens(tokens);
162 
163  return mgr;
164 }
165 
166 } // end namespace
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