Cantera  2.0
ckr_utils.h
Go to the documentation of this file.
1 /**
2  * @file ckr_utils.h
3  *
4  */
5 
6 // Copyright 2001 California Institute of Technology
7 
8 #ifndef CKR_UTILS_H
9 #define CKR_UTILS_H
10 
11 #include <math.h>
12 #include <string>
13 #include <map>
14 #include <vector>
15 
16 namespace ckr
17 {
18 
19 /**
20  * Fill vector 'keys' with the keys of map 'mp'
21  */
22 template<class K, class V>
23 void getMapKeys(const std::map<K,V>& mp, std::vector<K>& keys)
24 {
25  keys.clear();
26  typename std::map<K,V>::const_iterator i = mp.begin();
27  for (; i != mp.end(); ++i) {
28  keys.push_back(i->first);
29  }
30 }
31 
32 
33 /**
34  * Fill vector 'values' with the values of map 'mp'
35  */
36 template<class K, class V>
37 void getMapValues(const std::map<K,V>& mp, std::vector<V>& values)
38 {
39  values.clear();
40  typename std::map<K,V>::const_iterator i = mp.begin();
41  for (; i != mp.end(); ++i) {
42  values.push_back(i->second);
43  }
44 }
45 
46 
47 /**
48  *
49  * Template to compare two objects a and b, possibly of different
50  * types, and return the greater of the two, converted to the type of
51  * a. The '<' operator is used for the comparison, and must be
52  * defined for the two types in question.
53  *
54  */
55 
56 template<class T, class S>
57 inline T max(T a, S b)
58 {
59  return (a < b ? b : a);
60 }
61 
62 
63 
64 /**
65  *
66  * Template to compare two objects a and b, possibly of different
67  * types, and * return the lesser of the two, converted to the type
68  * of a. The '<' operator is used for the comparison, and must be
69  * defined for the two types in question.
70  *
71  */
72 
73 template<class T, class S>
74 inline T min(T a, S b)
75 {
76  return (a < b ? a : b);
77 }
78 
79 
80 /**
81  * Template to return a string equal to s, but padded with spaces on
82  * the right as necessary to make the length n.
83  */
84 template<class S>
85 inline S pad(const S& s, size_t n)
86 {
87  S output;
88  output.resize(max(n,s.size()),' ');
89  copy(s.begin(), s.end(), output.begin());
90  return output;
91 }
92 
93 /// Absolute value.
94 template<class T>
95 inline T absval(T x)
96 {
97  if (x < 0) {
98  return -x;
99  }
100  return x;
101 }
102 
103 /**
104  *
105  * Iterate through a list of objects that have a numeric member named
106  * 'valid', and return false if for any object this attribute is not
107  * greater than 0. Otherwise return true.
108  *
109  */
110 
111 template<class L>
112 inline bool valid(L& list)
113 {
114  size_t i;
115  for (i=0; i < list.size(); i++) if (list[i].valid <= 0) {
116  return false;
117  }
118  return true;
119 }
120 
121 
122 /// Remove all white space from string s.
123 void removeWhiteSpace(std::string& s);
124 
125 void getTokens(std::string& begin,
126  int n, std::vector<std::string>& toks, char delim=' ');
127 
128 
129 /**
130  * Perform a case-insensitive comparison of the first n2 characters
131  * of strings s1 and s2, where n2 is the length of s2. Typically, s1
132  * is an unknown string and s2 is the significant portion of a
133  * keyword. Returns true if a match is found, false otherwise. An asterisk
134  * in string s2 matches any character at that position.
135  *
136  * Example: if s1 = "elements", then match(s1, "ELEM") would return true.
137  */
138 bool match(const std::string& s1, const std::string& s2);
139 
140 /**
141  * Check whether string 'word' begins with a Chemkin keyword.
142  */
143 inline bool isKeyword(std::string word)
144 {
145  return (match(word, "ELEM") ||
146  match(word, "SPEC") ||
147  match(word, "THERM") ||
148  match(word, "REAC") ||
149  match(word, "END"));
150 }
151 
152 
153 bool extractSlashData(std::string& s, std::string& name, std::string& data);
154 std::string capitalize(const std::string& word);
155 
156 }
157 
158 #endif
159 
160 
161 
162 
163