You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
10 KiB

  1. // util/stl-utils.h
  2. // Copyright 2009-2011 Microsoft Corporation; Saarland University
  3. // See ../../COPYING for clarification regarding multiple authors
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  13. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  14. // MERCHANTABLITY OR NON-INFRINGEMENT.
  15. // See the Apache 2 License for the specific language governing permissions and
  16. // limitations under the License.
  17. #ifndef KALDI_UTIL_STL_UTILS_H_
  18. #define KALDI_UTIL_STL_UTILS_H_
  19. #include <algorithm>
  20. #include <map>
  21. #include <set>
  22. #include <string>
  23. #include <unordered_map>
  24. #include <unordered_set>
  25. #include <utility>
  26. #include <vector>
  27. using std::unordered_map;
  28. using std::unordered_set;
  29. #include "base/kaldi-common.h"
  30. namespace kaldi {
  31. /// Sorts and uniq's (removes duplicates) from a vector.
  32. template <typename T>
  33. inline void SortAndUniq(std::vector<T>* vec) {
  34. std::sort(vec->begin(), vec->end());
  35. vec->erase(std::unique(vec->begin(), vec->end()), vec->end());
  36. }
  37. /// Returns true if the vector is sorted.
  38. template <typename T>
  39. inline bool IsSorted(const std::vector<T>& vec) {
  40. typename std::vector<T>::const_iterator iter = vec.begin(), end = vec.end();
  41. if (iter == end) return true;
  42. while (1) {
  43. typename std::vector<T>::const_iterator next_iter = iter;
  44. ++next_iter;
  45. if (next_iter == end) return true; // end of loop and nothing out of order
  46. if (*next_iter < *iter) return false;
  47. iter = next_iter;
  48. }
  49. }
  50. /// Returns true if the vector is sorted and contains each element
  51. /// only once.
  52. template <typename T>
  53. inline bool IsSortedAndUniq(const std::vector<T>& vec) {
  54. typename std::vector<T>::const_iterator iter = vec.begin(), end = vec.end();
  55. if (iter == end) return true;
  56. while (1) {
  57. typename std::vector<T>::const_iterator next_iter = iter;
  58. ++next_iter;
  59. if (next_iter == end) return true; // end of loop and nothing out of order
  60. if (*next_iter <= *iter) return false;
  61. iter = next_iter;
  62. }
  63. }
  64. /// Removes duplicate elements from a sorted list.
  65. template <typename T>
  66. inline void Uniq(std::vector<T>* vec) { // must be already sorted.
  67. KALDI_PARANOID_ASSERT(IsSorted(*vec));
  68. KALDI_ASSERT(vec);
  69. vec->erase(std::unique(vec->begin(), vec->end()), vec->end());
  70. }
  71. /// Copies the elements of a set to a vector.
  72. template <class T>
  73. void CopySetToVector(const std::set<T>& s, std::vector<T>* v) {
  74. // copies members of s into v, in sorted order from lowest to highest
  75. // (because the set was in sorted order).
  76. KALDI_ASSERT(v != NULL);
  77. v->resize(s.size());
  78. typename std::set<T>::const_iterator siter = s.begin(), send = s.end();
  79. typename std::vector<T>::iterator viter = v->begin();
  80. for (; siter != send; ++siter, ++viter) {
  81. *viter = *siter;
  82. }
  83. }
  84. template <class T>
  85. void CopySetToVector(const unordered_set<T>& s, std::vector<T>* v) {
  86. KALDI_ASSERT(v != NULL);
  87. v->resize(s.size());
  88. typename unordered_set<T>::const_iterator siter = s.begin(), send = s.end();
  89. typename std::vector<T>::iterator viter = v->begin();
  90. for (; siter != send; ++siter, ++viter) {
  91. *viter = *siter;
  92. }
  93. }
  94. /// Copies the (key, value) pairs in a map to a vector of pairs.
  95. template <class A, class B>
  96. void CopyMapToVector(const std::map<A, B>& m,
  97. std::vector<std::pair<A, B> >* v) {
  98. KALDI_ASSERT(v != NULL);
  99. v->resize(m.size());
  100. typename std::map<A, B>::const_iterator miter = m.begin(), mend = m.end();
  101. typename std::vector<std::pair<A, B> >::iterator viter = v->begin();
  102. for (; miter != mend; ++miter, ++viter) {
  103. *viter = std::make_pair(miter->first, miter->second);
  104. // do it like this because of const casting.
  105. }
  106. }
  107. /// Copies the keys in a map to a vector.
  108. template <class A, class B>
  109. void CopyMapKeysToVector(const std::map<A, B>& m, std::vector<A>* v) {
  110. KALDI_ASSERT(v != NULL);
  111. v->resize(m.size());
  112. typename std::map<A, B>::const_iterator miter = m.begin(), mend = m.end();
  113. typename std::vector<A>::iterator viter = v->begin();
  114. for (; miter != mend; ++miter, ++viter) {
  115. *viter = miter->first;
  116. }
  117. }
  118. /// Copies the values in a map to a vector.
  119. template <class A, class B>
  120. void CopyMapValuesToVector(const std::map<A, B>& m, std::vector<B>* v) {
  121. KALDI_ASSERT(v != NULL);
  122. v->resize(m.size());
  123. typename std::map<A, B>::const_iterator miter = m.begin(), mend = m.end();
  124. typename std::vector<B>::iterator viter = v->begin();
  125. for (; miter != mend; ++miter, ++viter) {
  126. *viter = miter->second;
  127. }
  128. }
  129. /// Copies the keys in a map to a set.
  130. template <class A, class B>
  131. void CopyMapKeysToSet(const std::map<A, B>& m, std::set<A>* s) {
  132. KALDI_ASSERT(s != NULL);
  133. s->clear();
  134. typename std::map<A, B>::const_iterator miter = m.begin(), mend = m.end();
  135. for (; miter != mend; ++miter) {
  136. s->insert(s->end(), miter->first);
  137. }
  138. }
  139. /// Copies the values in a map to a set.
  140. template <class A, class B>
  141. void CopyMapValuesToSet(const std::map<A, B>& m, std::set<B>* s) {
  142. KALDI_ASSERT(s != NULL);
  143. s->clear();
  144. typename std::map<A, B>::const_iterator miter = m.begin(), mend = m.end();
  145. for (; miter != mend; ++miter) s->insert(s->end(), miter->second);
  146. }
  147. /// Copies the contents of a vector to a set.
  148. template <class A>
  149. void CopyVectorToSet(const std::vector<A>& v, std::set<A>* s) {
  150. KALDI_ASSERT(s != NULL);
  151. s->clear();
  152. typename std::vector<A>::const_iterator iter = v.begin(), end = v.end();
  153. for (; iter != end; ++iter) s->insert(s->end(), *iter);
  154. // s->end() is a hint in case v was sorted. will work regardless.
  155. }
  156. /// Deletes any non-NULL pointers in the vector v, and sets
  157. /// the corresponding entries of v to NULL
  158. template <class A>
  159. void DeletePointers(std::vector<A*>* v) {
  160. KALDI_ASSERT(v != NULL);
  161. typename std::vector<A*>::iterator iter = v->begin(), end = v->end();
  162. for (; iter != end; ++iter) {
  163. if (*iter != NULL) {
  164. delete *iter;
  165. *iter = NULL; // set to NULL for extra safety.
  166. }
  167. }
  168. }
  169. /// Returns true if the vector of pointers contains NULL pointers.
  170. template <class A>
  171. bool ContainsNullPointers(const std::vector<A*>& v) {
  172. typename std::vector<A*>::const_iterator iter = v.begin(), end = v.end();
  173. for (; iter != end; ++iter)
  174. if (*iter == static_cast<A*>(NULL)) return true;
  175. return false;
  176. }
  177. /// Copies the contents a vector of one type to a vector
  178. /// of another type.
  179. template <typename A, typename B>
  180. void CopyVectorToVector(const std::vector<A>& vec_in, std::vector<B>* vec_out) {
  181. KALDI_ASSERT(vec_out != NULL);
  182. vec_out->resize(vec_in.size());
  183. for (size_t i = 0; i < vec_in.size(); i++)
  184. (*vec_out)[i] = static_cast<B>(vec_in[i]);
  185. }
  186. /// A hashing function-object for vectors.
  187. template <typename Int>
  188. struct VectorHasher { // hashing function for vector<Int>.
  189. size_t operator()(const std::vector<Int>& x) const noexcept {
  190. size_t ans = 0;
  191. typename std::vector<Int>::const_iterator iter = x.begin(), end = x.end();
  192. for (; iter != end; ++iter) {
  193. ans *= kPrime;
  194. ans += *iter;
  195. }
  196. return ans;
  197. }
  198. VectorHasher() { // Check we're instantiated with an integer type.
  199. KALDI_ASSERT_IS_INTEGER_TYPE(Int);
  200. }
  201. private:
  202. static const int kPrime = 7853;
  203. };
  204. /// A hashing function-object for pairs of ints
  205. template <typename Int1, typename Int2 = Int1>
  206. struct PairHasher { // hashing function for pair<int>
  207. size_t operator()(const std::pair<Int1, Int2>& x) const noexcept {
  208. // 7853 was chosen at random from a list of primes.
  209. return x.first + x.second * 7853;
  210. }
  211. PairHasher() { // Check we're instantiated with an integer type.
  212. KALDI_ASSERT_IS_INTEGER_TYPE(Int1);
  213. KALDI_ASSERT_IS_INTEGER_TYPE(Int2);
  214. }
  215. };
  216. /// A hashing function object for strings.
  217. struct StringHasher { // hashing function for std::string
  218. size_t operator()(const std::string& str) const noexcept {
  219. size_t ans = 0, len = str.length();
  220. const char *c = str.c_str(), *end = c + len;
  221. for (; c != end; c++) {
  222. ans *= kPrime;
  223. ans += *c;
  224. }
  225. return ans;
  226. }
  227. private:
  228. static const int kPrime = 7853;
  229. };
  230. /// Reverses the contents of a vector.
  231. template <typename T>
  232. inline void ReverseVector(std::vector<T>* vec) {
  233. KALDI_ASSERT(vec != NULL);
  234. size_t sz = vec->size();
  235. for (size_t i = 0; i < sz / 2; i++) std::swap((*vec)[i], (*vec)[sz - 1 - i]);
  236. }
  237. /// Comparator object for pairs that compares only the first pair.
  238. template <class A, class B>
  239. struct CompareFirstMemberOfPair {
  240. inline bool operator()(const std::pair<A, B>& p1, const std::pair<A, B>& p2) {
  241. return p1.first < p2.first;
  242. }
  243. };
  244. /// For a vector of pair<I, F> where I is an integer and F a floating-point or
  245. /// integer type, this function sorts a vector of type vector<pair<I, F> > on
  246. /// the I value and then merges elements with equal I values, summing these over
  247. /// the F component and then removing any F component with zero value. This
  248. /// is for where the vector of pairs represents a map from the integer to float
  249. /// component, with an "adding" type of semantics for combining the elements.
  250. template <typename I, typename F>
  251. inline void MergePairVectorSumming(std::vector<std::pair<I, F> >* vec) {
  252. KALDI_ASSERT_IS_INTEGER_TYPE(I);
  253. CompareFirstMemberOfPair<I, F> c;
  254. std::sort(vec->begin(), vec->end(), c); // sort on 1st element.
  255. typename std::vector<std::pair<I, F> >::iterator out = vec->begin(),
  256. in = vec->begin(),
  257. end = vec->end();
  258. // special case: while there is nothing to be changed, skip over
  259. // initial input (avoids unnecessary copying).
  260. while (in + 1 < end && in[0].first != in[1].first && in[0].second != 0.0) {
  261. in++;
  262. out++;
  263. }
  264. while (in < end) {
  265. // We reach this point only at the first element of
  266. // each stretch of identical .first elements.
  267. *out = *in;
  268. ++in;
  269. while (in < end && in->first == out->first) {
  270. out->second += in->second; // this is the merge operation.
  271. ++in;
  272. }
  273. if (out->second != static_cast<F>(0)) // Don't keep zero elements.
  274. out++;
  275. }
  276. vec->erase(out, end);
  277. }
  278. } // namespace kaldi
  279. #endif // KALDI_UTIL_STL_UTILS_H_