123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- //
- // Created by jovian on 20/08/17.
- //
- #include "Stock.h"
- Stock::Stock() {
- setZero();
- }
- int Stock::set(Resource res, int val) {
- int prev(m_tab[res]);
- m_tab[res] = val;
- return prev;
- }
- int Stock::add(Resource res, int val) {
- m_tab[res] += val;
- return m_tab[res];
- }
- int Stock::get(Resource res) const {
- return m_tab[res];
- }
- void Stock::setZero() {
- for (int& res : m_tab) {
- res = 0;
- }
- }
- void Stock::setPositive() {
- for (int& res : m_tab) {
- if (res < 0)
- res = 0;
- }
- }
- Stock &Stock::operator+=(Stock const &stock) {
- for (int id(0); id < RESOURCE_NUMBER; ++id) {
- m_tab[id] += stock.m_tab[id];
- }
- return *this;
- }
- Stock &Stock::operator-=(Stock const &stock) {
- for (int id(0); id < RESOURCE_NUMBER; ++id) {
- m_tab[id] -= stock.m_tab[id];
- }
- return *this;
- }
- Stock &Stock::operator*=(int const& multi) {
- for (int id(0); id < RESOURCE_NUMBER; ++id) {
- m_tab[id] *= multi;
- }
- return *this;
- }
- Stock &Stock::operator/=(int const& multi) {
- for (int id(0); id < RESOURCE_NUMBER; ++id) {
- m_tab[id] /= multi;
- }
- return *this;
- }
- Resource Stock::mostResource() const {
- int highest(0);
- Resource most(ENERGY);
- for (int res(0); res < RESOURCE_NUMBER; ++res) {
- if (m_tab[res] > highest) {
- highest = m_tab[res];
- most = (Resource)res;
- }
- }
- return most;
- }
- Stock operator+(Stock const &a, Stock const &b) {
- Stock copy(a);
- copy += b;
- return copy;
- }
- Stock operator-(Stock const &a, Stock const &b) {
- Stock copy(a);
- copy -= b;
- return copy;
- }
- Stock operator*(Stock const &a, int const &b) {
- Stock copy(a);
- copy *= b;
- return copy;
- }
- Stock operator/(Stock const &a, int const &b) {
- Stock copy(a);
- copy /= b;
- return copy;
- }
|