bit_vector.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/util/bit_vector.h"
  15. #include <cassert>
  16. #include <iostream>
  17. namespace spvtools {
  18. namespace utils {
  19. void BitVector::ReportDensity(std::ostream& out) {
  20. uint32_t count = 0;
  21. for (BitContainer e : bits_) {
  22. while (e != 0) {
  23. if ((e & 1) != 0) {
  24. ++count;
  25. }
  26. e = e >> 1;
  27. }
  28. }
  29. out << "count=" << count
  30. << ", total size (bytes)=" << bits_.size() * sizeof(BitContainer)
  31. << ", bytes per element="
  32. << (double)(bits_.size() * sizeof(BitContainer)) / (double)(count);
  33. }
  34. bool BitVector::Or(const BitVector& other) {
  35. auto this_it = this->bits_.begin();
  36. auto other_it = other.bits_.begin();
  37. bool modified = false;
  38. while (this_it != this->bits_.end() && other_it != other.bits_.end()) {
  39. auto temp = *this_it | *other_it;
  40. if (temp != *this_it) {
  41. modified = true;
  42. *this_it = temp;
  43. }
  44. ++this_it;
  45. ++other_it;
  46. }
  47. if (other_it != other.bits_.end()) {
  48. modified = true;
  49. this->bits_.insert(this->bits_.end(), other_it, other.bits_.end());
  50. }
  51. return modified;
  52. }
  53. std::ostream& operator<<(std::ostream& out, const BitVector& bv) {
  54. out << "{";
  55. for (uint32_t i = 0; i < bv.bits_.size(); ++i) {
  56. BitVector::BitContainer b = bv.bits_[i];
  57. uint32_t j = 0;
  58. while (b != 0) {
  59. if (b & 1) {
  60. out << ' ' << i * BitVector::kBitContainerSize + j;
  61. }
  62. ++j;
  63. b = b >> 1;
  64. }
  65. }
  66. out << "}";
  67. return out;
  68. }
  69. } // namespace utils
  70. } // namespace spvtools