const-integer-set.h
Go to the documentation of this file.
1 // util/const-integer-set.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_H_
22 #define KALDI_UTIL_CONST_INTEGER_SET_H_
23 #include <vector>
24 #include <set>
25 #include <algorithm>
26 #include <limits>
27 #include <cassert>
28 #include "util/stl-utils.h"
29 
30  /* ConstIntegerSet is a way to efficiently test whether something is in a
31  supplied set of integers. It can be initialized from a vector or set, but
32  never changed after that. It either uses a sorted vector or an array of
33  bool, depending on the input. It behaves like a const version of an STL set, with
34  only a subset of the functionality, except all the member functions are
35  upper-case.
36 
37  Note that we could get rid of the member slow_set_, but we'd have to
38  do more work to implement an iterator type. This would save memory.
39  */
40 
41 namespace kaldi {
42 
43 template<class I> class ConstIntegerSet {
44  public:
46 
47  void Init(const std::vector<I> &input) {
48  slow_set_ = input;
50  InitInternal();
51  }
52 
53  void Init(const std::set<I> &input) {
54  CopySetToVector(input, &slow_set_);
55  InitInternal();
56  }
57 
58  explicit ConstIntegerSet(const std::vector<I> &input): slow_set_(input) {
60  InitInternal();
61  }
62  explicit ConstIntegerSet(const std::set<I> &input) {
63  CopySetToVector(input, &slow_set_);
64  InitInternal();
65  }
66  explicit ConstIntegerSet(const ConstIntegerSet<I> &other):
67  slow_set_(other.slow_set_) {
68  InitInternal();
69  }
70 
71  int count(I i) const; // returns 1 or 0.
72 
73  typedef typename std::vector<I>::const_iterator iterator;
74  iterator begin() const { return slow_set_.begin(); }
75  iterator end() const { return slow_set_.end(); }
76  size_t size() const { return slow_set_.size(); }
77  bool empty() const { return slow_set_.empty(); }
78 
79  void Write(std::ostream &os, bool binary) const;
80  void Read(std::istream &is, bool binary);
81 
82  private:
86  bool quick_;
87  std::vector<bool> quick_set_;
88  std::vector<I> slow_set_;
89  void InitInternal();
90 };
91 
92 } // end namespace kaldi
93 
95 
96 #endif // KALDI_UTIL_CONST_INTEGER_SET_H_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void CopySetToVector(const std::set< T > &s, std::vector< T > *v)
Copies the elements of a set to a vector.
Definition: stl-utils.h:86
ConstIntegerSet(const std::vector< I > &input)
void Init(const std::set< I > &input)
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq&#39;s (removes duplicates) from a vector.
Definition: stl-utils.h:39
ConstIntegerSet(const ConstIntegerSet< I > &other)
std::vector< I > slow_set_
void Write(std::ostream &os, bool binary) const
std::vector< bool > quick_set_
void Read(std::istream &is, bool binary)
void Init(const std::vector< I > &input)
iterator begin() const
ConstIntegerSet(const std::set< I > &input)
std::vector< I >::const_iterator iterator