1 #ifndef _BASIC_UTILITIES_ 7 #define _BASIC_UTILITIES_ 41 template<
typename Container>
42 bool LessThan(
const Container &c1,
const Container &c2)
44 typename Container::const_iterator c1i = c1.begin();
45 typename Container::const_iterator c2i = c2.begin();
46 while(c1i != c1.end() && c2i != c2.end()){
65 template<
typename Container>
68 unsigned int size = c1.size();
70 typename Container::const_iterator c1i = c1.begin();
71 for(
unsigned int iii = 0;iii < size;iii++)
81 template<
typename ContainerType>
82 void DumpContents(std::ostream &Ostr,
const ContainerType &c,std::string del =
"\n"){
84 typename ContainerType::const_iterator ci = c.begin();
106 template<
typename BufferDataType>
107 int PackBuffer(
const BufferDataType *sourceBuffer,
const std::vector<size_t> &sourceIndices,
108 BufferDataType *targetBuffer,
const std::vector<size_t> &targetIndices)
110 size_t numVal = sourceIndices.size();
111 if(numVal == 0 || (targetIndices.size() != numVal))
113 const size_t *sourceIndex = &sourceIndices[0];
114 const size_t *targetIndex = &targetIndices[0];
115 for(
size_t iVal = 0;iVal < numVal;iVal++)
116 targetBuffer[targetIndex[iVal]] = sourceBuffer[sourceIndex[iVal]];
120 template<
typename ContainerType,
typename T>
123 size_t datasize =
sizeof(T);
125 memcpy(&cont[0],src,count*datasize);
128 template<
typename OuterCont,
typename OutCont,
typename InnerCont,
typename MapType>
131 trg.resize(src.size());
132 typename OuterCont::iterator sci = src.begin();
133 typename OutCont::iterator oci = trg.begin();
134 while(sci != src.end()){
135 typename InnerCont::iterator ici = sci->begin();
136 while(ici != sci->end())
137 oci->push_back(m[*ici++]);
144 template<
typename ListContainerType,
typename ListType>
148 typename ListContainerType::const_iterator lci = fc.begin();
149 while(lci != fc.end()){
150 typename ListType::const_iterator li = lci->begin();
151 while(li != lci->end()){
163 template<
typename OuterCont,
typename InnerCont,
typename OutCont>
165 typename OuterCont::iterator cci = con.begin();
167 while(cci != con.end()){
168 typename InnerCont::iterator ccni = cci->begin();
169 while(ccni != cci->end())
170 ocon.push_back(*ccni++);
176 template<
typename OuterContType>
180 typename OuterContType::iterator ci = con.begin();
181 while(ci != con.end()){
182 total_size += ci->size();
188 template<
typename OuterContType,
typename InnerContType,
typename RetCont,
typename idxtype>
190 OuterContType &source)
193 xadj.resize(number_of_elements+1);
195 adj.reserve(number_entries);
196 typename OuterContType::iterator si = source.begin();
199 while(si != source.end()){
200 typename InnerContType::iterator ei = si->begin();
201 xadj[elm] =
static_cast<idxtype
>(si->size() + xadj[elm-1]);
203 while(ei != si->end())
204 adj.push_back(static_cast<idxtype>(*ei++-1));
213 template<
typename ListContainerType,
typename ListType>
218 nnodes = MaxNodeId<ListContainerType,ListType>(fc);
219 anodelist.resize(nnodes);
220 typename ListContainerType::iterator lci = fc.begin();
221 while(lci != fc.end())
223 typename ListType::iterator li = lci->begin();
224 while(li != lci->end()){
228 next_node = *(lci->begin());
231 anodelist[this_node].push_back(next_node);
232 anodelist[next_node-1].push_back(this_node+1);
238 anodelist[node].sort();
239 anodelist[node].unique();
249 template<
typename ListContainerType,
typename ListType>
250 void AdjEList(std::vector<std::list<primitive::IndexType> > &aelist,
251 ListContainerType &dual_con,
unsigned long nel=0)
254 nel = MaxNodeId<ListContainerType,ListType>(dual_con);
256 typename ListContainerType::iterator lci = dual_con.begin();
257 while(lci != dual_con.end())
259 typename ListType::iterator li = lci->begin();
260 while(li != lci->end()){
262 typename ListType::iterator li2 = li;
263 while(li2 != lci->end()){
265 aelist[this_node].push_back(nexnode+1);
266 aelist[nexnode].push_back(this_node+1);
274 aelist[node].unique();
278 template<
typename ConType,
typename IConType>
281 typename ConType::iterator ci = con.begin();
284 while(ci != con.end())
287 typename IConType::iterator eni = ci->begin();
288 while(eni != ci->end())
299 template<
typename ContainerType,
typename Icont>
306 template<
typename BufferDataType>
307 void ReportBufferStats(std::ostream &outStream,
size_t numValues,
const BufferDataType *dataBuffer)
309 size_t minLocation = 0;
310 size_t maxLocation = 0;
311 BufferDataType minValue = std::numeric_limits<BufferDataType>::max();
312 BufferDataType maxValue = std::numeric_limits<BufferDataType>::min();
313 double bufferMean = 0;
314 double bufferDev = 0;
315 for(
size_t iValue = 0;iValue < numValues;iValue++){
316 if(dataBuffer[iValue] < minValue){
317 minValue = dataBuffer[iValue];
318 minLocation = iValue;
320 if(dataBuffer[iValue] > maxValue){
321 maxValue = dataBuffer[iValue];
322 maxLocation = iValue;
324 bufferMean += dataBuffer[iValue];
325 bufferDev += dataBuffer[iValue]*dataBuffer[iValue];
327 bufferMean /= numValues;
328 bufferDev /= numValues;
329 bufferDev -= (bufferMean*bufferMean);
330 bufferDev = std::sqrt(bufferDev);
331 outStream <<
"Min: " << minValue <<
" @ " << minLocation << std::endl
332 <<
"Max: " << maxValue <<
" @ " << maxLocation << std::endl
333 <<
"Mean: " << bufferMean <<
" +/- " << bufferDev << std::endl;
343 template<
typename Container>
346 if(c1.size() != c2.size())
348 typename Container::iterator c1i = c1.begin();
349 typename Container::iterator c2i =
350 std::find(c2.begin(),c2.end(),*c1i);
353 while(c1i != c1.end()){
371 template<
typename Container>
374 if(c1.size() != c2.size())
376 typename Container::iterator c1i = c1.begin();
377 typename Container::reverse_iterator c2i =
378 std::find(c2.rbegin(),c2.rend(),*c1i);
381 while(c1i != c1.end()){
406 unsigned int size = values.size();
409 for(
unsigned int i=0; i < size; ++i)
419 while(ret.empty() && std::getline(In,line)){
420 line = line.substr(0,line.find(
"#"));
421 std::istringstream Istr(line);
424 if(!token.empty() && token[0] !=
'#')
432 const std::string &etag)
434 std::ostringstream Ostr;
436 while(std::getline(In,line) && ret.empty()){
437 line = line.substr(0,line.find(
"#"));
439 std::istringstream Istr(line);
448 Ostr << line << std::endl;
455 std::istringstream Istr(instr);
458 while(std::getline(Istr,line))
467 int rval = instr.size();
468 *buf =
new char [rval];
469 void *resl = std::memcpy(*buf,instr.c_str(),rval);
478 inline void Trim(std::string &instr,
bool preserve_newline =
false)
480 std::istringstream Istr(instr);
482 std::ostringstream Ostr;
483 while(std::getline(Istr,line)){
484 std::istringstream Line(line);
490 Ostr <<
" " << token;
500 inline const std::string
Trimmed(
const std::string &instr,
bool preserve_newline =
false)
502 std::istringstream Istr(instr);
504 std::ostringstream Ostr;
505 while(std::getline(Istr,line)){
506 std::istringstream Line(line);
512 Ostr <<
" " << token;
525 inline const std::string
529 std::string::size_type
x = pname.find(
"/");
530 if(x == std::string::npos)
532 return(pname.substr(pname.find_last_of(
"/")+1));
544 std::istringstream Istr(source);
547 tokens.push_back(token);
556 TokenizeString(std::vector<std::string> &tokens,
const std::string &source,
const char delim)
559 std::string::size_type ssize = source.size();
560 std::string::size_type
x = 0;
561 std::ostringstream Ostr;
564 if(source[x] != delim)
566 if((source[x] == delim) || (x == (ssize-1))){
567 tokens.push_back(token);
577 template<
typename NumType>
580 if(!stinter.empty()){
581 std::string::size_type
x = stinter.find(
":");
582 std::string t1s = stinter;
583 std::string t2s = stinter;
584 if(x != std::string::npos){
585 t1s = stinter.substr(0,x);
586 t2s = stinter.substr(x+1);
588 std::istringstream Istr(t2s);
596 std::istringstream Istr(t1s);
600 std::istringstream Istr(t2s);
610 inline int OpenFile(std::ifstream &Inf,
const std::string &filename)
612 Inf.open(filename.c_str());
620 Vectorize(std::vector<std::string> &retVal,
const char **in)
624 retVal.push_back(in[i++]);
628 Vectorize(std::vector<std::string> &retVal,
const char **in,
int n)
633 while((in[i] != NULL) && i < n)
634 retVal.push_back(in[i++]);
void FormGraph(const ContainerType &adjlist)
void Trim(std::string &instr, bool preserve_newline=false)
Creates space delimited tokens in place.
void Flatten(OuterCont &con, OutCont &ocon)
Populate OutCont with a flat list of entries from a multicontainer.
void TokenizeString(std::vector< std::string > &tokens, const std::string &source)
Tokenize string.
void DumpContents(std::ostream &Ostr, const ContainerType &c, std::string del="\)
Dump container contents.
int OpenFile(std::ifstream &Inf, const std::string &filename)
File opener.
void Vectorize(std::vector< std::string > &retVal, const char **in)
primitive::IndexType MaxNodeId(const ListContainerType &fc)
Return the maximum of all elements of a multicontainer.
void RenewStream(std::ostringstream &outStream)
std::string GetNextContent(std::istream &In)
Defines MPI-specific parallel global and program classes.
int PackBuffer(const BufferDataType *sourceBuffer, const std::vector< size_t > &sourceIndices, BufferDataType *targetBuffer, const std::vector< size_t > &targetIndices)
Pack subset of target buffer from subset of source buffer.
bool HaveSameOrientation(Container &c1, Container &c2)
Cyclic test.
void MultiContainer2CSR(RetCont &xadj, RetCont &adj, OuterContType &source)
primitive::IndexType NumberOfEdges(ConType &con)
void const size_t const size_t const size_t const double const double * x
void CreateAdjacentNodeList(std::vector< std::list< primitive::IndexType > > &anodelist, ListContainerType &fc, primitive::IndexType nnodes=0)
Given an array of adjacent node lists (like an array of face connectivities), this function will loop...
void AdjEList(std::vector< std::list< primitive::IndexType > > &aelist, ListContainerType &dual_con, unsigned long nel=0)
Given an array of adjacent node lists (like an array of face connectivities), this function will loop...
void ReportBufferStats(std::ostream &outStream, size_t numValues, const BufferDataType *dataBuffer)
Reports min/max/mean/stddev for data in buffer.
primitive::IndexType GetTotalSize(OuterContType &con)
Return the total number of entries in a multicontainer.
const std::string Trimmed(const std::string &instr, bool preserve_newline=false)
Returns space delimited tokens.
int String2Buf(const std::string &instr, void **buf)
void SortPermutation(const std::vector< T > &values, std::vector< unsigned int > &v)
void MapElements(OuterCont &src, OutCont &trg, MapType &m)
void size_t int size_t int size_t int int int int double int int double double *void size_t int size_t int int int int int double int size_t size_t size_t double double *void size_t int size_t int size_t size_t int double int double double *void size_t size_t size_t double * a
const std::string stripdirs(const std::string &pname)
Strip absolute path.
int NumLines(const std::string &instr)
bool operator()(int a, int b)
void ProcessRange(NumType &t1, NumType &t2, const std::string stinter)
Process a range in the format "t1:t2".
void GetContentUntil(std::istream &In, std::string ret, const std::string &etag)
CmpPairs(const std::vector< T > &v)
void CopyIntoContainer(ContainerType &cont, const T *src, size_t count)
void InvertContainer(const Container &c1, Container &c2)
bool HaveOppositeOrientation(Container &c1, Container &c2)
Anti-cyclic test.
bool LessThan(const Container &c1, const Container &c2)
CmpPairs< T > CreateCmpPairs(const std::vector< T > &v)