You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

counter.h 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CORE_UTILS_COUNTER_H_
  17. #define MINDSPORE_CORE_UTILS_COUNTER_H_
  18. #include <functional>
  19. #include "utils/ordered_map.h"
  20. namespace mindspore {
  21. template <typename T, class Hash = std::hash<T>, class Equal = std::equal_to<T>>
  22. class Counter {
  23. using counter_type = Counter<T, Hash, Equal>;
  24. public:
  25. Counter() = default;
  26. ~Counter() = default;
  27. Counter(const Counter &other) { data = other.data; }
  28. Counter &operator=(const Counter &other) {
  29. if (this != &other) {
  30. data = other.data;
  31. }
  32. return *this;
  33. }
  34. int &operator[](const T &t) { return data[t]; }
  35. counter_type operator-(const counter_type &other) {
  36. counter_type new_counter;
  37. for (auto iter = begin(); iter != end(); ++iter) {
  38. auto key = iter->first;
  39. int value = iter->second;
  40. auto item = other.data.find(key);
  41. if (item != other.data.end()) {
  42. int o_value = item->second;
  43. if (value - o_value > 0) {
  44. new_counter[key] = value - o_value;
  45. }
  46. } else {
  47. new_counter[key] = value;
  48. }
  49. }
  50. return new_counter;
  51. }
  52. counter_type operator+(const counter_type &other) {
  53. counter_type new_counter;
  54. for (auto iter = begin(); iter != end(); ++iter) {
  55. auto key = iter->first;
  56. int value = iter->second;
  57. auto item = other.data.find(key);
  58. if (item != other.data.end()) {
  59. new_counter[key] = iter->second + item->second;
  60. } else {
  61. new_counter[key] = value;
  62. }
  63. }
  64. for (auto iter = other.cbegin(); iter != other.cend(); ++iter) {
  65. auto key = iter->first;
  66. int value = iter->second;
  67. if (!new_counter.contains(key)) {
  68. new_counter[key] = value;
  69. }
  70. }
  71. return new_counter;
  72. }
  73. std::size_t size() const { return data.size(); }
  74. bool contains(const T &t) const { return data.find(t) != data.end(); }
  75. typename OrderedMap<T, int, Hash, Equal>::iterator begin() { return data.begin(); }
  76. typename OrderedMap<T, int, Hash, Equal>::iterator end() { return data.end(); }
  77. typename OrderedMap<T, int, Hash, Equal>::const_iterator cbegin() const { return data.cbegin(); }
  78. typename OrderedMap<T, int, Hash, Equal>::const_iterator cend() const { return data.cend(); }
  79. private:
  80. OrderedMap<T, int, Hash, Equal> data;
  81. };
  82. } // namespace mindspore
  83. #endif // MINDSPORE_CORE_UTILS_COUNTER_H_