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.

96 lines
2.9 KiB

  1. // util/const-integer-set.h
  2. // Copyright 2009-2011 Microsoft Corporation
  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_CONST_INTEGER_SET_H_
  18. #define KALDI_UTIL_CONST_INTEGER_SET_H_
  19. #include <algorithm>
  20. #include <cassert>
  21. #include <limits>
  22. #include <set>
  23. #include <vector>
  24. #include "util/stl-utils.h"
  25. /* ConstIntegerSet is a way to efficiently test whether something is in a
  26. supplied set of integers. It can be initialized from a vector or set, but
  27. never changed after that. It either uses a sorted vector or an array of
  28. bool, depending on the input. It behaves like a const version of an STL set,
  29. with only a subset of the functionality, except all the member functions are
  30. upper-case.
  31. Note that we could get rid of the member slow_set_, but we'd have to
  32. do more work to implement an iterator type. This would save memory.
  33. */
  34. namespace kaldi {
  35. template <class I>
  36. class ConstIntegerSet {
  37. public:
  38. ConstIntegerSet() : lowest_member_(1), highest_member_(0) {}
  39. void Init(const std::vector<I>& input) {
  40. slow_set_ = input;
  41. SortAndUniq(&slow_set_);
  42. InitInternal();
  43. }
  44. void Init(const std::set<I>& input) {
  45. CopySetToVector(input, &slow_set_);
  46. InitInternal();
  47. }
  48. explicit ConstIntegerSet(const std::vector<I>& input) : slow_set_(input) {
  49. SortAndUniq(&slow_set_);
  50. InitInternal();
  51. }
  52. explicit ConstIntegerSet(const std::set<I>& input) {
  53. CopySetToVector(input, &slow_set_);
  54. InitInternal();
  55. }
  56. explicit ConstIntegerSet(const ConstIntegerSet<I>& other)
  57. : slow_set_(other.slow_set_) {
  58. InitInternal();
  59. }
  60. int count(I i) const; // returns 1 or 0.
  61. typedef typename std::vector<I>::const_iterator iterator;
  62. iterator begin() const { return slow_set_.begin(); }
  63. iterator end() const { return slow_set_.end(); }
  64. size_t size() const { return slow_set_.size(); }
  65. bool empty() const { return slow_set_.empty(); }
  66. void Write(std::ostream& os, bool binary) const;
  67. void Read(std::istream& is, bool binary);
  68. private:
  69. I lowest_member_;
  70. I highest_member_;
  71. bool contiguous_;
  72. bool quick_;
  73. std::vector<bool> quick_set_;
  74. std::vector<I> slow_set_;
  75. void InitInternal();
  76. };
  77. } // end namespace kaldi
  78. #include "util/const-integer-set-inl.h"
  79. #endif // KALDI_UTIL_CONST_INTEGER_SET_H_