20 #ifndef KALDI_UTIL_STL_UTILS_H_ 21 #define KALDI_UTIL_STL_UTILS_H_ 23 #include <unordered_map> 24 #include <unordered_set> 25 using std::unordered_map;
26 using std::unordered_set;
40 std::sort(vec->begin(), vec->end());
41 vec->erase(std::unique(vec->begin(), vec->end()), vec->end());
47 inline bool IsSorted(
const std::vector<T> &vec) {
48 typename std::vector<T>::const_iterator iter = vec.begin(), end = vec.end();
49 if (iter == end)
return true;
51 typename std::vector<T>::const_iterator next_iter = iter;
53 if (next_iter == end)
return true;
54 if (*next_iter < *iter)
return false;
64 typename std::vector<T>::const_iterator iter = vec.begin(), end = vec.end();
65 if (iter == end)
return true;
67 typename std::vector<T>::const_iterator next_iter = iter;
69 if (next_iter == end)
return true;
70 if (*next_iter <= *iter)
return false;
78 inline void Uniq(std::vector<T> *vec) {
81 vec->erase(std::unique(vec->begin(), vec->end()), vec->end());
91 typename std::set<T>::const_iterator siter = s.begin(), send = s.end();
92 typename std::vector<T>::iterator viter = v->begin();
93 for (; siter != send; ++siter, ++viter) {
102 typename unordered_set<T>::const_iterator siter = s.begin(), send = s.end();
103 typename std::vector<T>::iterator viter = v->begin();
104 for (; siter != send; ++siter, ++viter) {
111 template<
class A,
class B>
113 std::vector<std::pair<A, B> > *v) {
116 typename std::map<A, B>::const_iterator miter = m.begin(), mend = m.end();
117 typename std::vector<std::pair<A, B> >::iterator viter = v->begin();
118 for (; miter != mend; ++miter, ++viter) {
119 *viter = std::make_pair(miter->first, miter->second);
125 template<
class A,
class B>
129 typename std::map<A, B>::const_iterator miter = m.begin(), mend = m.end();
130 typename std::vector<A>::iterator viter = v->begin();
131 for (; miter != mend; ++miter, ++viter) {
132 *viter = miter->first;
137 template<
class A,
class B>
141 typename std::map<A, B>::const_iterator miter = m.begin(), mend = m.end();
142 typename std::vector<B>::iterator viter = v->begin();
143 for (; miter != mend; ++miter, ++viter) {
144 *viter = miter->second;
149 template<
class A,
class B>
153 typename std::map<A, B>::const_iterator miter = m.begin(), mend = m.end();
154 for (; miter != mend; ++miter) {
155 s->insert(s->end(), miter->first);
160 template<
class A,
class B>
164 typename std::map<A, B>::const_iterator miter = m.begin(), mend = m.end();
165 for (; miter != mend; ++miter)
166 s->insert(s->end(), miter->second);
175 typename std::vector<A>::const_iterator iter = v.begin(), end = v.end();
176 for (; iter != end; ++iter)
177 s->insert(s->end(), *iter);
186 typename std::vector<A*>::iterator iter = v->begin(), end = v->end();
187 for (; iter != end; ++iter) {
198 typename std::vector<A*>::const_iterator iter = v.begin(), end = v.end();
199 for (; iter != end; ++iter)
200 if (*iter == static_cast<A*> (NULL))
return true;
206 template<
typename A,
typename B>
209 vec_out->resize(vec_in.size());
210 for (
size_t i = 0;
i < vec_in.size();
i++)
211 (*vec_out)[
i] =
static_cast<B
> (vec_in[
i]);
215 template<
typename Int>
217 size_t operator()(
const std::vector<Int> &x)
const noexcept {
219 typename std::vector<Int>::const_iterator iter = x.begin(), end = x.end();
220 for (; iter != end; ++iter) {
234 template<
typename Int1,
typename Int2 = Int1>
236 size_t operator()(
const std::pair<Int1, Int2> &x)
const noexcept {
238 return x.first + x.second * 7853;
250 size_t ans = 0, len = str.length();
251 const char *c = str.c_str(), *end = c + len;
252 for (; c != end; c++) {
266 size_t sz = vec->size();
267 for (
size_t i = 0;
i < sz/2;
i++)
273 template<
class A,
class B>
276 const std::pair<A, B> &p2) {
277 return p1.first < p2.first;
287 template<
typename I,
typename F>
291 std::sort(vec->begin(), vec->end(), c);
292 typename std::vector<std::pair<I, F> >::iterator out = vec->begin(),
293 in = vec->begin(), end = vec->end();
296 while (in + 1 < end && in[0].first != in[1].first && in[0].second != 0.0) {
305 while (in < end && in->first == out->first) {
306 out->second += in->second;
309 if (out->second != static_cast<F>(0))
312 vec->erase(out, end);
317 #endif // KALDI_UTIL_STL_UTILS_H_ This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void CopySetToVector(const std::set< T > &s, std::vector< T > *v)
Copies the elements of a set to a vector.
void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
A hashing function-object for vectors.
void CopyMapValuesToSet(const std::map< A, B > &m, std::set< B > *s)
Copies the values in a map to a set.
void CopyMapKeysToVector(const std::map< A, B > &m, std::vector< A > *v)
Copies the keys in a map to a vector.
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
void CopyVectorToVector(const std::vector< A > &vec_in, std::vector< B > *vec_out)
Copies the contents a vector of one type to a vector of another type.
void Uniq(std::vector< T > *vec)
Removes duplicate elements from a sorted list.
Comparator object for pairs that compares only the first pair.
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
bool ContainsNullPointers(const std::vector< A *> &v)
Returns true if the vector of pointers contains NULL pointers.
void CopyVectorToSet(const std::vector< A > &v, std::set< A > *s)
Copies the contents of a vector to a set.
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq's (removes duplicates) from a vector.
A hashing function object for strings.
size_t operator()(const std::pair< Int1, Int2 > &x) const noexcept
size_t operator()(const std::string &str) const noexcept
void CopyMapKeysToSet(const std::map< A, B > &m, std::set< A > *s)
Copies the keys in a map to a set.
void CopyMapValuesToVector(const std::map< A, B > &m, std::vector< B > *v)
Copies the values in a map to a vector.
#define KALDI_PARANOID_ASSERT(cond)
void ReverseVector(std::vector< T > *vec)
Reverses the contents of a vector.
bool IsSorted(const std::vector< T > &vec)
Returns true if the vector is sorted.
#define KALDI_ASSERT(cond)
void MergePairVectorSumming(std::vector< std::pair< I, F > > *vec)
For a vector of pair<I, F> where I is an integer and F a floating-point or integer type...
bool IsSortedAndUniq(const std::vector< T > &vec)
Returns true if the vector is sorted and contains each element only once.
void CopyMapToVector(const std::map< A, B > &m, std::vector< std::pair< A, B > > *v)
Copies the (key, value) pairs in a map to a vector of pairs.
size_t operator()(const std::vector< Int > &x) const noexcept
A hashing function-object for pairs of ints.