From fd16152121f0449cd98e01a295c2556f3b61b8b5 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 12 Nov 2019 18:38:09 +0100 Subject: [PATCH] Make scriptset return scripts in order of insertion, unit tests for ScriptSource --- .../ScriptHandling/ScriptAggregator.hpp | 19 ++-- src/Battling/ScriptHandling/ScriptSet.hpp | 23 ++-- .../ScriptTests/ScriptSourceTest.cpp | 104 ++++++++++++++++++ 3 files changed, 127 insertions(+), 19 deletions(-) create mode 100644 tests/BattleTests/ScriptTests/ScriptSourceTest.cpp diff --git a/src/Battling/ScriptHandling/ScriptAggregator.hpp b/src/Battling/ScriptHandling/ScriptAggregator.hpp index 2466600..8b195ec 100644 --- a/src/Battling/ScriptHandling/ScriptAggregator.hpp +++ b/src/Battling/ScriptHandling/ScriptAggregator.hpp @@ -13,26 +13,26 @@ namespace CreatureLib::Battling{ std::vector _scripts; int _index = 0; bool _isSetSet = false; - std::__detail::_Node_const_iterator, false, true> _setIterator; - std::__detail::_Node_const_iterator, false, true> _setEnd; + const std::vector* _setScripts; + int _setIndex; public: ScriptAggregator(std::vector scripts) : _scripts(std::move(scripts)){ }; bool HasNext(){ - return _index < _scripts.size() || (_isSetSet && _setIterator != _setEnd); + return _index < _scripts.size() || (_isSetSet && _setIndex < _setScripts->size()); } Script* GetNext(){ // We can probably do this in a cleaner version once C++ 20 drops with Coroutine support. if (_isSetSet){ - if (_setIterator == _setEnd){ + if (_setIndex >= _setScripts->size()){ _isSetSet = false; return GetNext(); } - auto s = _setIterator->second; - _setIterator.operator++(); - if (_setIterator == _setEnd){ + auto s = _setScripts->at(_setIndex); + _setIndex++; + if (_setIndex >= _setScripts->size()){ _isSetSet = false; } return s; @@ -51,10 +51,9 @@ namespace CreatureLib::Battling{ auto set = next.GetScriptSet(); if (set->Count() == 0) return GetNext(); - auto it = set->GetIterator(); - _setIterator = it->begin(); - _setEnd = it->end(); + _setScripts = set->GetIterator(); _isSetSet = true; + _setIndex = 0; return GetNext(); } throw NotReachableException(); diff --git a/src/Battling/ScriptHandling/ScriptSet.hpp b/src/Battling/ScriptHandling/ScriptSet.hpp index ea39e43..78b537e 100644 --- a/src/Battling/ScriptHandling/ScriptSet.hpp +++ b/src/Battling/ScriptHandling/ScriptSet.hpp @@ -7,27 +7,32 @@ namespace CreatureLib::Battling{ class ScriptSet{ - std::unordered_map _scripts; + std::vector _scripts; + std::unordered_map _lookup; public: void Add(Script* script){ - auto f = _scripts.find(script->GetName()); - if (f != _scripts.end()){ - f->second->Stack(); - } - else{ - _scripts.insert({script->GetName(), script}); + auto f = _lookup.find(script->GetName()); + if (f != _lookup.end()){ + _scripts[f.operator*().second]->Stack(); + return; } + _scripts.push_back(script); + _lookup.insert({script->GetName(), _scripts.size() - 1}); } void Remove(const std::string& key){ - _scripts.erase(key); + auto find = _lookup.find(key); + if (find != _lookup.end()){ + _scripts.erase(_scripts.begin() + find.operator*().second); + _lookup.erase(key); + } } size_t Count() const{ return _scripts.size(); } - const std::unordered_map * GetIterator() const{ + const std::vector