Make scriptset return scripts in order of insertion, unit tests for ScriptSource
This commit is contained in:
@@ -13,26 +13,26 @@ namespace CreatureLib::Battling{
|
||||
std::vector<ScriptWrapper> _scripts;
|
||||
int _index = 0;
|
||||
bool _isSetSet = false;
|
||||
std::__detail::_Node_const_iterator<std::pair<const std::string, Script *>, false, true> _setIterator;
|
||||
std::__detail::_Node_const_iterator<std::pair<const std::string, Script *>, false, true> _setEnd;
|
||||
const std::vector<Script*>* _setScripts;
|
||||
int _setIndex;
|
||||
public:
|
||||
ScriptAggregator(std::vector<ScriptWrapper> 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();
|
||||
|
||||
@@ -7,27 +7,32 @@
|
||||
|
||||
namespace CreatureLib::Battling{
|
||||
class ScriptSet{
|
||||
std::unordered_map<std::string, Script*> _scripts;
|
||||
std::vector<Script*> _scripts;
|
||||
std::unordered_map<std::string, size_t> _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<std::string, Script *> * GetIterator() const{
|
||||
const std::vector<Script *> * GetIterator() const{
|
||||
return &_scripts;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user