2
* To change this license header, choose License Headers in Project Properties.
3
* To change this template file, choose Tools | Templates
4
* and open the template in the editor.
11
* Created on February 20, 2018, 10:37 AM
19
#include "PetriEngine/Colored/Multiset.h"
21
namespace PetriEngine {
23
Multiset::Multiset() : _set(), type(nullptr) {
26
Multiset::Multiset(const Multiset& orig) {
31
Multiset::Multiset(std::vector<std::pair<const Color*,uint32_t>>& colors)
32
: _set(), type(nullptr)
34
for (auto& c : colors) {
35
(*this)[c.first] += c.second;
39
Multiset::~Multiset() = default;
41
Multiset Multiset::operator +(const Multiset& other) const {
47
Multiset Multiset::operator -(const Multiset& other) const {
53
Multiset Multiset::operator *(uint32_t scalar) const {
59
void Multiset::operator +=(const Multiset& other) {
60
if (type == nullptr) {
63
if (other.type != nullptr && type->getId() != other.type->getId()) {
64
throw "You cannot add Multisets over different sets";
66
for (auto c : other._set) {
67
const Color* color = DotConstant::dotConstant();
69
color = &((*type)[c.first]);
70
(*this)[color] += c.second;
74
void Multiset::operator -=(const Multiset& other) {
75
if (type == nullptr) {
78
if (other.type != nullptr && type->getId() != other.type->getId()) {
79
throw "You cannot add Multisets over different sets";
82
const Color* color = DotConstant::dotConstant();
84
color = &((*type)[c.first]);
85
(*this)[color] = std::min(c.second - other[color], c.second);
89
void Multiset::operator *=(uint32_t scalar) {
95
uint32_t Multiset::operator [](const Color* color) const {
96
if (type != nullptr && type->getId() == color->getColorType()->getId()) {
98
if (c.first == color->getId())
106
uint32_t& Multiset::operator [](const Color* color) {
107
if (type == nullptr) {
108
type = color->getColorType();
110
if (color->getColorType() != nullptr && type->getId() != color->getColorType()->getId()) {
111
throw "You cannot access a Multiset with a color from a different color type";
113
for (auto & i : _set) {
114
if (i.first == color->getId())
118
_set.emplace_back(color->getId(), 0);
119
return _set.back().second;
122
bool Multiset::empty() const {
126
void Multiset::clean() {
127
if (std::find_if(_set.begin(), _set.end(), [&](auto elem) { return elem.second == 0; }) == _set.end())
130
_set.erase(std::remove_if(_set.begin(), _set.end(), [&](auto elem) {
131
return elem.second == 0;
135
Multiset::Iterator Multiset::begin() {
136
return Iterator(this, 0);
139
Multiset::Iterator Multiset::end() {
140
return Iterator(this, _set.size());
144
/** Multiset iterator implementation */
145
bool Multiset::Iterator::operator==(Multiset::Iterator &other) {
146
return ms == other.ms && index == other.index;
149
bool Multiset::Iterator::operator!=(Multiset::Iterator &other) {
150
return !(*this == other);
153
Multiset::Iterator &Multiset::Iterator::operator++() {
158
std::pair<const Color *, uint32_t &> Multiset::Iterator::operator++(int) {
159
std::pair<const Color*, uint32_t&> old = **this;
164
std::pair<const Color *, uint32_t &> Multiset::Iterator::operator*() {
165
auto& item = ms->_set[index];
166
auto color = DotConstant::dotConstant();
167
if (ms->type != nullptr)
168
color = &(*ms->type)[item.first];
169
return { color, item.second };
172
std::string Multiset::toString() const {
173
std::ostringstream oss;
174
for (size_t i = 0; i < _set.size(); ++i) {
175
oss << _set[i].second << "'(" << (*type)[_set[i].first].toString() << ")";
176
if (i < _set.size() - 1) {
184
size_t Multiset::size() const {
186
for (auto item : _set) {