const-integer-set-inl.h
Go to the documentation of this file.
1 // util/const-integer-set-inl.h
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #ifndef KALDI_UTIL_CONST_INTEGER_SET_INL_H_
22 #define KALDI_UTIL_CONST_INTEGER_SET_INL_H_
23 
24 // Do not include this file directly. It is included by const-integer-set.h
25 
26 
27 namespace kaldi {
28 
29 template<class I>
32  quick_set_.clear(); // just in case we previously had data.
33  if (slow_set_.size() == 0) {
34  lowest_member_=(I) 1;
35  highest_member_=(I) 0;
36  contiguous_ = false;
37  quick_ = false;
38  } else {
39  lowest_member_ = slow_set_.front();
40  highest_member_ = slow_set_.back();
41  size_t range = highest_member_ + 1 - lowest_member_;
42  if (range == slow_set_.size()) {
43  contiguous_ = true;
44  quick_= false;
45  } else {
46  contiguous_ = false;
47  // If it would be more compact to store as bool
48  if (range < slow_set_.size() * 8 * sizeof(I)) {
49  // (assuming 1 bit per element)...
50  quick_set_.resize(range, false);
51  for (size_t i = 0;i < slow_set_.size();i++)
52  quick_set_[slow_set_[i] - lowest_member_] = true;
53  quick_ = true;
54  } else {
55  quick_ = false;
56  }
57  }
58  }
59 }
60 
61 template<class I>
63  if (i < lowest_member_ || i > highest_member_) {
64  return 0;
65  } else {
66  if (contiguous_) return true;
67  if (quick_) {
68  return (quick_set_[i-lowest_member_] ? 1 : 0);
69  } else {
70  bool ans = std::binary_search(slow_set_.begin(), slow_set_.end(), i);
71  return (ans ? 1 : 0);
72  }
73  }
74 }
75 
76 template<class I>
77 void ConstIntegerSet<I>::Write(std::ostream &os, bool binary) const {
78  WriteIntegerVector(os, binary, slow_set_);
79 }
80 
81 template<class I>
82 void ConstIntegerSet<I>::Read(std::istream &is, bool binary) {
83  ReadIntegerVector(is, binary, &slow_set_);
84  InitInternal();
85 }
86 
87 
88 
89 } // end namespace kaldi
90 
91 #endif // KALDI_UTIL_CONST_INTEGER_SET_INL_H_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
Definition: kaldi-utils.h:133
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232
void Write(std::ostream &os, bool binary) const
void Read(std::istream &is, bool binary)
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
Definition: io-funcs-inl.h:198