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.

157 lines
6.4 KiB

  1. #ifndef STORM_STORAGE_DISTRIBUTION_H_
  2. #define STORM_STORAGE_DISTRIBUTION_H_
  3. #include <vector>
  4. #include <ostream>
  5. #include <boost/container/flat_map.hpp>
  6. #include "storm/storage/sparse/StateType.h"
  7. namespace storm {
  8. namespace utility {
  9. template <typename ValueType>
  10. class ConstantsComparator;
  11. }
  12. namespace storage {
  13. template<typename ValueType, typename StateType = uint32_t>
  14. class Distribution {
  15. public:
  16. typedef boost::container::flat_map<StateType, ValueType> container_type;
  17. typedef typename container_type::iterator iterator;
  18. typedef typename container_type::const_iterator const_iterator;
  19. /*!
  20. * Creates an empty distribution.
  21. */
  22. Distribution();
  23. Distribution(Distribution const& other) = default;
  24. Distribution& operator=(Distribution const& other) = default;
  25. Distribution(Distribution&& other) = default;
  26. Distribution& operator=(Distribution&& other) = default;
  27. /*!
  28. * Adds the given distribution to the current one.
  29. */
  30. void add(Distribution const& other);
  31. /*!
  32. * Checks whether the two distributions specify the same probabilities to go to the same states.
  33. *
  34. * @param other The distribution with which the current distribution is to be compared.
  35. * @return True iff the two distributions are equal.
  36. */
  37. bool equals(Distribution<ValueType, StateType> const& other, storm::utility::ConstantsComparator<ValueType> const& comparator = storm::utility::ConstantsComparator<ValueType>()) const;
  38. /*!
  39. * Assigns the given state the given probability under this distribution.
  40. *
  41. * @param state The state to which to assign the probability.
  42. * @param probability The probability to assign.
  43. */
  44. void addProbability(StateType const& state, ValueType const& probability);
  45. /*!
  46. * Removes the given probability mass of going to the given state.
  47. *
  48. * @param state The state for which to remove the probability.
  49. * @param probability The probability to remove.
  50. * @param comparator A comparator that is used to determine if the remaining probability is zero. If so, the
  51. * entry is removed.
  52. */
  53. void removeProbability(StateType const& state, ValueType const& probability, storm::utility::ConstantsComparator<ValueType> const& comparator = storm::utility::ConstantsComparator<ValueType>());
  54. /*!
  55. * Removes the probability mass from one state and adds it to another.
  56. *
  57. * @param fromState The state from which to take the probability mass.
  58. * @param toState The state from which to which to add the probability mass.
  59. * @param probability The probability mass to shift.
  60. * @param comparator A comparator that is used to determine if the remaining probability is zero. If so, the
  61. * entry is removed.
  62. */
  63. void shiftProbability(StateType const& fromState, StateType const& toState, ValueType const& probability, storm::utility::ConstantsComparator<ValueType> const& comparator = storm::utility::ConstantsComparator<ValueType>());
  64. /*!
  65. * Retrieves an iterator to the elements in this distribution.
  66. *
  67. * @return The iterator to the elements in this distribution.
  68. */
  69. iterator begin();
  70. /*!
  71. * Retrieves an iterator to the elements in this distribution.
  72. *
  73. * @return The iterator to the elements in this distribution.
  74. */
  75. const_iterator begin() const;
  76. /*!
  77. * Retrieves an iterator to the elements in this distribution.
  78. *
  79. * @return The iterator to the elements in this distribution.
  80. */
  81. const_iterator cbegin() const;
  82. /*!
  83. * Retrieves an iterator past the elements in this distribution.
  84. *
  85. * @return The iterator past the elements in this distribution.
  86. */
  87. iterator end();
  88. /*!
  89. * Retrieves an iterator past the elements in this distribution.
  90. *
  91. * @return The iterator past the elements in this distribution.
  92. */
  93. const_iterator end() const;
  94. /*!
  95. * Retrieves an iterator past the elements in this distribution.
  96. *
  97. * @return The iterator past the elements in this distribution.
  98. */
  99. const_iterator cend() const;
  100. /*!
  101. * Scales the distribution by multiplying all the probabilities with 1/p where p is the probability of moving
  102. * to the given state and sets the probability of moving to the given state to zero. If the probability is
  103. * already zero, this operation has no effect.
  104. *
  105. * @param state The state whose associated probability is used to scale the distribution.
  106. */
  107. void scale(StateType const& state);
  108. /*!
  109. * Retrieves the size of the distribution, i.e. the size of the support set.
  110. */
  111. std::size_t size() const;
  112. bool less(Distribution<ValueType, StateType> const& other, storm::utility::ConstantsComparator<ValueType> const& comparator) const;
  113. private:
  114. // A list of states and the probabilities that are assigned to them.
  115. container_type distribution;
  116. };
  117. template<typename ValueType, typename StateType = uint32_t>
  118. std::ostream& operator<<(std::ostream& out, Distribution<ValueType, StateType> const& distribution);
  119. }
  120. }
  121. namespace std {
  122. template <typename ValueType>
  123. struct hash<storm::storage::Distribution<ValueType>> {
  124. std::size_t operator()(storm::storage::Distribution<ValueType> const& distribution) const {
  125. return (distribution.getHash());
  126. }
  127. };
  128. }
  129. #endif /* STORM_STORAGE_DISTRIBUTION_H_ */