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.

87 lines
2.6 KiB

  1. // util/const-integer-set-inl.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_INL_H_
  18. #define KALDI_UTIL_CONST_INTEGER_SET_INL_H_
  19. // Do not include this file directly. It is included by const-integer-set.h
  20. namespace kaldi {
  21. template <class I>
  22. void ConstIntegerSet<I>::InitInternal() {
  23. KALDI_ASSERT_IS_INTEGER_TYPE(I);
  24. quick_set_.clear(); // just in case we previously had data.
  25. if (slow_set_.size() == 0) {
  26. lowest_member_ = (I)1;
  27. highest_member_ = (I)0;
  28. contiguous_ = false;
  29. quick_ = false;
  30. } else {
  31. lowest_member_ = slow_set_.front();
  32. highest_member_ = slow_set_.back();
  33. size_t range = highest_member_ + 1 - lowest_member_;
  34. if (range == slow_set_.size()) {
  35. contiguous_ = true;
  36. quick_ = false;
  37. } else {
  38. contiguous_ = false;
  39. // If it would be more compact to store as bool
  40. if (range < slow_set_.size() * 8 * sizeof(I)) {
  41. // (assuming 1 bit per element)...
  42. quick_set_.resize(range, false);
  43. for (size_t i = 0; i < slow_set_.size(); i++)
  44. quick_set_[slow_set_[i] - lowest_member_] = true;
  45. quick_ = true;
  46. } else {
  47. quick_ = false;
  48. }
  49. }
  50. }
  51. }
  52. template <class I>
  53. int ConstIntegerSet<I>::count(I i) const {
  54. if (i < lowest_member_ || i > highest_member_) {
  55. return 0;
  56. } else {
  57. if (contiguous_) return true;
  58. if (quick_) {
  59. return (quick_set_[i - lowest_member_] ? 1 : 0);
  60. } else {
  61. bool ans = std::binary_search(slow_set_.begin(), slow_set_.end(), i);
  62. return (ans ? 1 : 0);
  63. }
  64. }
  65. }
  66. template <class I>
  67. void ConstIntegerSet<I>::Write(std::ostream& os, bool binary) const {
  68. WriteIntegerVector(os, binary, slow_set_);
  69. }
  70. template <class I>
  71. void ConstIntegerSet<I>::Read(std::istream& is, bool binary) {
  72. ReadIntegerVector(is, binary, &slow_set_);
  73. InitInternal();
  74. }
  75. } // end namespace kaldi
  76. #endif // KALDI_UTIL_CONST_INTEGER_SET_INL_H_