Craftdef: Use numbers instead of iterators

Use numbers instead of iterators to traverse various vectors.
This commit is contained in:
est31 2015-07-04 16:21:37 +02:00
parent 87b9cdab07
commit 96989e0a6a

@ -112,10 +112,9 @@ static std::vector<std::string> craftGetItemNames(
const std::vector<std::string> &itemstrings, IGameDef *gamedef) const std::vector<std::string> &itemstrings, IGameDef *gamedef)
{ {
std::vector<std::string> result; std::vector<std::string> result;
for (std::vector<std::string>::const_iterator for (std::vector<std::string>::size_type i = 0;
it = itemstrings.begin(); i < itemstrings.size(); i++) {
it != itemstrings.end(); it++) { result.push_back(craftGetItemName(itemstrings[i], gamedef));
result.push_back(craftGetItemName(*it, gamedef));
} }
return result; return result;
} }
@ -125,10 +124,9 @@ static std::vector<std::string> craftGetItemNames(
const std::vector<ItemStack> &items, IGameDef *gamedef) const std::vector<ItemStack> &items, IGameDef *gamedef)
{ {
std::vector<std::string> result; std::vector<std::string> result;
for (std::vector<ItemStack>::const_iterator for (std::vector<ItemStack>::size_type i = 0;
it = items.begin(); i < items.size(); i++) {
it != items.end(); it++) { result.push_back(items[i].name);
result.push_back(it->name);
} }
return result; return result;
} }
@ -138,10 +136,9 @@ static std::vector<ItemStack> craftGetItems(
const std::vector<std::string> &items, IGameDef *gamedef) const std::vector<std::string> &items, IGameDef *gamedef)
{ {
std::vector<ItemStack> result; std::vector<ItemStack> result;
for (std::vector<std::string>::const_iterator for (std::vector<std::string>::size_type i = 0;
it = items.begin(); i < items.size(); i++) {
it != items.end(); it++) { result.push_back(ItemStack(std::string(items[i]), (u16)1,
result.push_back(ItemStack(std::string(*it), (u16)1,
(u16)0, "", gamedef->getItemDefManager())); (u16)0, "", gamedef->getItemDefManager()));
} }
return result; return result;
@ -156,11 +153,10 @@ static bool craftGetBounds(const std::vector<std::string> &items, unsigned int w
bool success = false; bool success = false;
unsigned int x = 0; unsigned int x = 0;
unsigned int y = 0; unsigned int y = 0;
for (std::vector<std::string>::const_iterator for (std::vector<std::string>::size_type i = 0;
it = items.begin(); i < items.size(); i++) {
it != items.end(); it++) {
// Is this an actual item? // Is this an actual item?
if (*it != "") { if (items[i] != "") {
if (!success) { if (!success) {
// This is the first nonempty item // This is the first nonempty item
min_x = max_x = x; min_x = max_x = x;
@ -187,11 +183,10 @@ static bool craftGetBounds(const std::vector<std::string> &items, unsigned int w
// Removes 1 from each item stack // Removes 1 from each item stack
static void craftDecrementInput(CraftInput &input, IGameDef *gamedef) static void craftDecrementInput(CraftInput &input, IGameDef *gamedef)
{ {
for (std::vector<ItemStack>::iterator for (std::vector<ItemStack>::size_type i = 0;
it = input.items.begin(); i < input.items.size(); i++) {
it != input.items.end(); it++) { if (input.items[i].count != 0)
if (it->count != 0) input.items[i].remove(1);
it->remove(1);
} }
} }
@ -211,24 +206,24 @@ static void craftDecrementOrReplaceInput(CraftInput &input,
// Make a copy of the replacements pair list // Make a copy of the replacements pair list
std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs; std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs;
for (std::vector<ItemStack>::iterator for (std::vector<ItemStack>::size_type i = 0;
it = input.items.begin(); i < input.items.size(); i++) {
it != input.items.end(); it++) { ItemStack &item = input.items[i];
// Find an appropriate replacement // Find an appropriate replacement
bool found_replacement = false; bool found_replacement = false;
for (std::vector<std::pair<std::string, std::string> >::iterator for (std::vector<std::pair<std::string, std::string> >::iterator
j = pairs.begin(); j = pairs.begin();
j != pairs.end(); j++) { j != pairs.end(); ++j) {
if (it->name == craftGetItemName(j->first, gamedef)) { if (item.name == craftGetItemName(j->first, gamedef)) {
if (it->count == 1) { if (item.count == 1) {
it->deSerialize(j->second, gamedef->idef()); item.deSerialize(j->second, gamedef->idef());
found_replacement = true; found_replacement = true;
pairs.erase(j); pairs.erase(j);
break; break;
} else { } else {
ItemStack rep; ItemStack rep;
rep.deSerialize(j->second, gamedef->idef()); rep.deSerialize(j->second, gamedef->idef());
it->remove(1); item.remove(1);
found_replacement = true; found_replacement = true;
output_replacements.push_back(rep); output_replacements.push_back(rep);
break; break;
@ -236,8 +231,8 @@ static void craftDecrementOrReplaceInput(CraftInput &input,
} }
} }
// No replacement was found, simply decrement count by one // No replacement was found, simply decrement count by one
if (!found_replacement && it->count > 0) if (!found_replacement && item.count > 0)
it->remove(1); item.remove(1);
} }
} }
@ -247,17 +242,15 @@ static std::string craftDumpMatrix(const std::vector<std::string> &items,
{ {
std::ostringstream os(std::ios::binary); std::ostringstream os(std::ios::binary);
os << "{ "; os << "{ ";
unsigned int x = 0; for(std::vector<std::string>::size_type i = 0;
for(std::vector<std::string>::const_iterator i < items.size(); i++) {
it = items.begin(); if (i == width) {
it != items.end(); it++, x++) {
if (x == width) {
os << "; "; os << "; ";
x = 0; i = 0;
} else if (x != 0) { } else if (i != 0) {
os << ","; os << ",";
} }
os << '"' << (*it) << '"'; os << '"' << items[i] << '"';
} }
os << " }"; os << " }";
return os.str(); return os.str();
@ -269,17 +262,15 @@ std::string craftDumpMatrix(const std::vector<ItemStack> &items,
{ {
std::ostringstream os(std::ios::binary); std::ostringstream os(std::ios::binary);
os << "{ "; os << "{ ";
unsigned int x = 0; for (std::vector<ItemStack>::size_type i = 0;
for (std::vector<ItemStack>::const_iterator i < items.size(); i++) {
it = items.begin(); if (i == width) {
it != items.end(); it++, x++) {
if (x == width) {
os << "; "; os << "; ";
x = 0; i = 0;
} else if (x != 0) { } else if (i != 0) {
os << ","; os << ",";
} }
os << '"' << (it->getItemString()) << '"'; os << '"' << (items[i].getItemString()) << '"';
} }
os << " }"; os << " }";
return os.str(); return os.str();
@ -318,10 +309,12 @@ std::string CraftReplacements::dump() const
std::ostringstream os(std::ios::binary); std::ostringstream os(std::ios::binary);
os<<"{"; os<<"{";
const char *sep = ""; const char *sep = "";
for (std::vector<std::pair<std::string, std::string> >::const_iterator for (std::vector<std::pair<std::string, std::string> >::size_type i = 0;
it = pairs.begin(); i < pairs.size(); i++) {
it != pairs.end(); it++) { const std::pair<std::string, std::string> &repl_p = pairs[i];
os << sep << '"' << (it->first) << "\"=>\"" << (it->second) << '"'; os << sep
<< '"' << (repl_p.first)
<< "\"=>\"" << (repl_p.second) << '"';
sep = ","; sep = ",";
} }
os << "}"; os << "}";
@ -479,11 +472,11 @@ bool CraftDefinitionShapeless::check(const CraftInput &input, IGameDef *gamedef)
// Filter empty items out of input // Filter empty items out of input
std::vector<std::string> input_filtered; std::vector<std::string> input_filtered;
for (std::vector<ItemStack>::const_iterator for (std::vector<ItemStack>::size_type i = 0;
it = input.items.begin(); i < input.items.size(); i++) {
it != input.items.end(); it++) { const ItemStack &item = input.items[i];
if (it->name != "") if (item.name != "")
input_filtered.push_back(it->name); input_filtered.push_back(item.name);
} }
// If there is a wrong number of items in input, no match // If there is a wrong number of items in input, no match
@ -627,14 +620,14 @@ bool CraftDefinitionToolRepair::check(const CraftInput &input, IGameDef *gamedef
ItemStack item1; ItemStack item1;
ItemStack item2; ItemStack item2;
for (std::vector<ItemStack>::const_iterator for (std::vector<ItemStack>::size_type i = 0;
it = input.items.begin(); i < input.items.size(); i++) {
it != input.items.end(); it++) { const ItemStack &item = input.items[i];
if (!it->empty()) { if (!item.empty()) {
if (item1.empty()) if (item1.empty())
item1 = *it; item1 = item;
else if (item2.empty()) else if (item2.empty())
item2 = *it; item2 = item;
else else
return false; return false;
} }
@ -647,14 +640,14 @@ CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameD
{ {
ItemStack item1; ItemStack item1;
ItemStack item2; ItemStack item2;
for (std::vector<ItemStack>::const_iterator for (std::vector<ItemStack>::size_type i = 0;
it = input.items.begin(); i < input.items.size(); i++) {
it != input.items.end(); it++) { const ItemStack &item = input.items[i];
if (!it->empty()) { if (!item.empty()) {
if (item1.empty()) if (item1.empty())
item1 = *it; item1 = item;
else if (item2.empty()) else if (item2.empty())
item2 = *it; item2 = item;
} }
} }
ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef); ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
@ -697,11 +690,11 @@ bool CraftDefinitionCooking::check(const CraftInput &input, IGameDef *gamedef) c
// Filter empty items out of input // Filter empty items out of input
std::vector<std::string> input_filtered; std::vector<std::string> input_filtered;
for (std::vector<ItemStack>::const_iterator for (std::vector<ItemStack>::size_type i = 0;
it = input.items.begin(); i < input.items.size(); i++) {
it != input.items.end(); it++) { const std::string &name = input.items[i].name;
if (it->name != "") if (name != "")
input_filtered.push_back(it->name); input_filtered.push_back(name);
} }
// If there is a wrong number of items in input, no match // If there is a wrong number of items in input, no match
@ -789,11 +782,11 @@ bool CraftDefinitionFuel::check(const CraftInput &input, IGameDef *gamedef) cons
// Filter empty items out of input // Filter empty items out of input
std::vector<std::string> input_filtered; std::vector<std::string> input_filtered;
for (std::vector<ItemStack>::const_iterator for (std::vector<ItemStack>::size_type i = 0;
it = input.items.begin(); i < input.items.size(); i++) {
it != input.items.end(); it++) { const std::string &name = input.items[i].name;
if (it->name != "") if (name != "")
input_filtered.push_back(it->name); input_filtered.push_back(name);
} }
// If there is a wrong number of items in input, no match // If there is a wrong number of items in input, no match
@ -889,10 +882,9 @@ public:
// If all input items are empty, abort. // If all input items are empty, abort.
bool all_empty = true; bool all_empty = true;
for (std::vector<ItemStack>::const_iterator for (std::vector<ItemStack>::size_type i = 0;
it = input.items.begin(); i < input.items.size(); i++) {
it != input.items.end(); it++) { if (!input.items[i].empty()) {
if (!it->empty()) {
all_empty = false; all_empty = false;
break; break;
} }
@ -921,10 +913,9 @@ public:
const std::vector<CraftDefinition*> &hash_collisions = col_iter->second; const std::vector<CraftDefinition*> &hash_collisions = col_iter->second;
// Walk crafting definitions from back to front, so that later // Walk crafting definitions from back to front, so that later
// definitions can override earlier ones. // definitions can override earlier ones.
for (std::vector<CraftDefinition*>::const_reverse_iterator for (std::vector<CraftDefinition*>::size_type
it = hash_collisions.rbegin(); i = hash_collisions.size(); i > 0; i--) {
it != hash_collisions.rend(); it++) { CraftDefinition *def = hash_collisions[i - 1];
CraftDefinition *def = *it;
/*errorstream << "Checking " << input.dump() << std::endl /*errorstream << "Checking " << input.dump() << std::endl
<< " against " << def->dump() << std::endl;*/ << " against " << def->dump() << std::endl;*/
@ -957,11 +948,12 @@ public:
recipes.reserve(limit ? MYMIN(limit, vec.size()) : vec.size()); recipes.reserve(limit ? MYMIN(limit, vec.size()) : vec.size());
for (std::vector<CraftDefinition*>::const_reverse_iterator for (std::vector<CraftDefinition*>::size_type i = vec.size();
it = vec.rbegin(); it != vec.rend(); ++it) { i > 0; i--) {
CraftDefinition *def = vec[i - 1];
if (limit && recipes.size() >= limit) if (limit && recipes.size() >= limit)
break; break;
recipes.push_back(*it); recipes.push_back(def);
} }
return recipes; return recipes;
@ -974,9 +966,12 @@ public:
for (std::map<u64, std::vector<CraftDefinition*> >::const_iterator for (std::map<u64, std::vector<CraftDefinition*> >::const_iterator
it = (m_craft_defs[type]).begin(); it = (m_craft_defs[type]).begin();
it != (m_craft_defs[type]).end(); it++) { it != (m_craft_defs[type]).end(); it++) {
for (std::vector<CraftDefinition*>::const_iterator for (std::vector<CraftDefinition*>::size_type i = 0;
iit = it->second.begin(); iit != it->second.end(); iit++) { i < it->second.size(); i++) {
os << "type " << type << " hash " << it->first << (*iit)->dump() << "\n"; os << "type " << type
<< " hash " << it->first
<< " def " << it->second[i]->dump()
<< "\n";
} }
} }
} }
@ -1000,7 +995,8 @@ public:
it = m_craft_defs[type].begin(); it = m_craft_defs[type].begin();
it != m_craft_defs[type].end(); it++) { it != m_craft_defs[type].end(); it++) {
for (std::vector<CraftDefinition*>::iterator for (std::vector<CraftDefinition*>::iterator
iit = it->second.begin(); iit != it->second.end(); iit++) { iit = it->second.begin();
iit != it->second.end(); ++iit) {
delete *iit; delete *iit;
} }
it->second.clear(); it->second.clear();
@ -1012,10 +1008,11 @@ public:
virtual void initHashes(IGameDef *gamedef) virtual void initHashes(IGameDef *gamedef)
{ {
// Move the CraftDefs from the unhashed layer into layers higher up. // Move the CraftDefs from the unhashed layer into layers higher up.
for (std::vector<CraftDefinition*>::iterator std::vector<CraftDefinition *> &unhashed =
it = (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).begin(); m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0];
it != (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).end(); it++) { for (std::vector<CraftDefinition*>::size_type i = 0;
CraftDefinition *def = *it; i < unhashed.size(); i++) {
CraftDefinition *def = unhashed[i];
// Initialize and get the definition's hash // Initialize and get the definition's hash
def->initHash(gamedef); def->initHash(gamedef);
@ -1025,7 +1022,7 @@ public:
// Enter the definition // Enter the definition
m_craft_defs[type][hash].push_back(def); m_craft_defs[type][hash].push_back(def);
} }
m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].clear(); unhashed.clear();
} }
private: private:
//TODO: change both maps to unordered_map when c++11 can be used //TODO: change both maps to unordered_map when c++11 can be used