Skip to content

Commit 96989e0

Browse files
committedJul 4, 2015
Craftdef: Use numbers instead of iterators
Use numbers instead of iterators to traverse various vectors.
1 parent 87b9cda commit 96989e0

File tree

1 file changed

+99
-102
lines changed

1 file changed

+99
-102
lines changed
 

Diff for: ‎src/craftdef.cpp

+99-102
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,9 @@ static std::vector<std::string> craftGetItemNames(
112112
const std::vector<std::string> &itemstrings, IGameDef *gamedef)
113113
{
114114
std::vector<std::string> result;
115-
for (std::vector<std::string>::const_iterator
116-
it = itemstrings.begin();
117-
it != itemstrings.end(); it++) {
118-
result.push_back(craftGetItemName(*it, gamedef));
115+
for (std::vector<std::string>::size_type i = 0;
116+
i < itemstrings.size(); i++) {
117+
result.push_back(craftGetItemName(itemstrings[i], gamedef));
119118
}
120119
return result;
121120
}
@@ -125,10 +124,9 @@ static std::vector<std::string> craftGetItemNames(
125124
const std::vector<ItemStack> &items, IGameDef *gamedef)
126125
{
127126
std::vector<std::string> result;
128-
for (std::vector<ItemStack>::const_iterator
129-
it = items.begin();
130-
it != items.end(); it++) {
131-
result.push_back(it->name);
127+
for (std::vector<ItemStack>::size_type i = 0;
128+
i < items.size(); i++) {
129+
result.push_back(items[i].name);
132130
}
133131
return result;
134132
}
@@ -138,10 +136,9 @@ static std::vector<ItemStack> craftGetItems(
138136
const std::vector<std::string> &items, IGameDef *gamedef)
139137
{
140138
std::vector<ItemStack> result;
141-
for (std::vector<std::string>::const_iterator
142-
it = items.begin();
143-
it != items.end(); it++) {
144-
result.push_back(ItemStack(std::string(*it), (u16)1,
139+
for (std::vector<std::string>::size_type i = 0;
140+
i < items.size(); i++) {
141+
result.push_back(ItemStack(std::string(items[i]), (u16)1,
145142
(u16)0, "", gamedef->getItemDefManager()));
146143
}
147144
return result;
@@ -156,11 +153,10 @@ static bool craftGetBounds(const std::vector<std::string> &items, unsigned int w
156153
bool success = false;
157154
unsigned int x = 0;
158155
unsigned int y = 0;
159-
for (std::vector<std::string>::const_iterator
160-
it = items.begin();
161-
it != items.end(); it++) {
156+
for (std::vector<std::string>::size_type i = 0;
157+
i < items.size(); i++) {
162158
// Is this an actual item?
163-
if (*it != "") {
159+
if (items[i] != "") {
164160
if (!success) {
165161
// This is the first nonempty item
166162
min_x = max_x = x;
@@ -187,11 +183,10 @@ static bool craftGetBounds(const std::vector<std::string> &items, unsigned int w
187183
// Removes 1 from each item stack
188184
static void craftDecrementInput(CraftInput &input, IGameDef *gamedef)
189185
{
190-
for (std::vector<ItemStack>::iterator
191-
it = input.items.begin();
192-
it != input.items.end(); it++) {
193-
if (it->count != 0)
194-
it->remove(1);
186+
for (std::vector<ItemStack>::size_type i = 0;
187+
i < input.items.size(); i++) {
188+
if (input.items[i].count != 0)
189+
input.items[i].remove(1);
195190
}
196191
}
197192

@@ -211,33 +206,33 @@ static void craftDecrementOrReplaceInput(CraftInput &input,
211206
// Make a copy of the replacements pair list
212207
std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs;
213208

214-
for (std::vector<ItemStack>::iterator
215-
it = input.items.begin();
216-
it != input.items.end(); it++) {
209+
for (std::vector<ItemStack>::size_type i = 0;
210+
i < input.items.size(); i++) {
211+
ItemStack &item = input.items[i];
217212
// Find an appropriate replacement
218213
bool found_replacement = false;
219214
for (std::vector<std::pair<std::string, std::string> >::iterator
220215
j = pairs.begin();
221-
j != pairs.end(); j++) {
222-
if (it->name == craftGetItemName(j->first, gamedef)) {
223-
if (it->count == 1) {
224-
it->deSerialize(j->second, gamedef->idef());
216+
j != pairs.end(); ++j) {
217+
if (item.name == craftGetItemName(j->first, gamedef)) {
218+
if (item.count == 1) {
219+
item.deSerialize(j->second, gamedef->idef());
225220
found_replacement = true;
226221
pairs.erase(j);
227222
break;
228223
} else {
229224
ItemStack rep;
230225
rep.deSerialize(j->second, gamedef->idef());
231-
it->remove(1);
226+
item.remove(1);
232227
found_replacement = true;
233228
output_replacements.push_back(rep);
234229
break;
235230
}
236231
}
237232
}
238233
// No replacement was found, simply decrement count by one
239-
if (!found_replacement && it->count > 0)
240-
it->remove(1);
234+
if (!found_replacement && item.count > 0)
235+
item.remove(1);
241236
}
242237
}
243238

@@ -246,18 +241,16 @@ static std::string craftDumpMatrix(const std::vector<std::string> &items,
246241
unsigned int width)
247242
{
248243
std::ostringstream os(std::ios::binary);
249-
os<<"{ ";
250-
unsigned int x = 0;
251-
for(std::vector<std::string>::const_iterator
252-
it = items.begin();
253-
it != items.end(); it++, x++) {
254-
if (x == width) {
255-
os<<"; ";
256-
x = 0;
257-
} else if (x != 0) {
258-
os<<",";
244+
os << "{ ";
245+
for(std::vector<std::string>::size_type i = 0;
246+
i < items.size(); i++) {
247+
if (i == width) {
248+
os << "; ";
249+
i = 0;
250+
} else if (i != 0) {
251+
os << ",";
259252
}
260-
os << '"' << (*it) << '"';
253+
os << '"' << items[i] << '"';
261254
}
262255
os << " }";
263256
return os.str();
@@ -269,17 +262,15 @@ std::string craftDumpMatrix(const std::vector<ItemStack> &items,
269262
{
270263
std::ostringstream os(std::ios::binary);
271264
os << "{ ";
272-
unsigned int x = 0;
273-
for (std::vector<ItemStack>::const_iterator
274-
it = items.begin();
275-
it != items.end(); it++, x++) {
276-
if (x == width) {
265+
for (std::vector<ItemStack>::size_type i = 0;
266+
i < items.size(); i++) {
267+
if (i == width) {
277268
os << "; ";
278-
x = 0;
279-
} else if (x != 0) {
280-
os<<",";
269+
i = 0;
270+
} else if (i != 0) {
271+
os << ",";
281272
}
282-
os << '"' << (it->getItemString()) << '"';
273+
os << '"' << (items[i].getItemString()) << '"';
283274
}
284275
os << " }";
285276
return os.str();
@@ -318,10 +309,12 @@ std::string CraftReplacements::dump() const
318309
std::ostringstream os(std::ios::binary);
319310
os<<"{";
320311
const char *sep = "";
321-
for (std::vector<std::pair<std::string, std::string> >::const_iterator
322-
it = pairs.begin();
323-
it != pairs.end(); it++) {
324-
os << sep << '"' << (it->first) << "\"=>\"" << (it->second) << '"';
312+
for (std::vector<std::pair<std::string, std::string> >::size_type i = 0;
313+
i < pairs.size(); i++) {
314+
const std::pair<std::string, std::string> &repl_p = pairs[i];
315+
os << sep
316+
<< '"' << (repl_p.first)
317+
<< "\"=>\"" << (repl_p.second) << '"';
325318
sep = ",";
326319
}
327320
os << "}";
@@ -479,11 +472,11 @@ bool CraftDefinitionShapeless::check(const CraftInput &input, IGameDef *gamedef)
479472

480473
// Filter empty items out of input
481474
std::vector<std::string> input_filtered;
482-
for (std::vector<ItemStack>::const_iterator
483-
it = input.items.begin();
484-
it != input.items.end(); it++) {
485-
if (it->name != "")
486-
input_filtered.push_back(it->name);
475+
for (std::vector<ItemStack>::size_type i = 0;
476+
i < input.items.size(); i++) {
477+
const ItemStack &item = input.items[i];
478+
if (item.name != "")
479+
input_filtered.push_back(item.name);
487480
}
488481

489482
// If there is a wrong number of items in input, no match
@@ -627,14 +620,14 @@ bool CraftDefinitionToolRepair::check(const CraftInput &input, IGameDef *gamedef
627620

628621
ItemStack item1;
629622
ItemStack item2;
630-
for (std::vector<ItemStack>::const_iterator
631-
it = input.items.begin();
632-
it != input.items.end(); it++) {
633-
if (!it->empty()) {
623+
for (std::vector<ItemStack>::size_type i = 0;
624+
i < input.items.size(); i++) {
625+
const ItemStack &item = input.items[i];
626+
if (!item.empty()) {
634627
if (item1.empty())
635-
item1 = *it;
628+
item1 = item;
636629
else if (item2.empty())
637-
item2 = *it;
630+
item2 = item;
638631
else
639632
return false;
640633
}
@@ -647,14 +640,14 @@ CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameD
647640
{
648641
ItemStack item1;
649642
ItemStack item2;
650-
for (std::vector<ItemStack>::const_iterator
651-
it = input.items.begin();
652-
it != input.items.end(); it++) {
653-
if (!it->empty()) {
643+
for (std::vector<ItemStack>::size_type i = 0;
644+
i < input.items.size(); i++) {
645+
const ItemStack &item = input.items[i];
646+
if (!item.empty()) {
654647
if (item1.empty())
655-
item1 = *it;
648+
item1 = item;
656649
else if (item2.empty())
657-
item2 = *it;
650+
item2 = item;
658651
}
659652
}
660653
ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
@@ -697,11 +690,11 @@ bool CraftDefinitionCooking::check(const CraftInput &input, IGameDef *gamedef) c
697690

698691
// Filter empty items out of input
699692
std::vector<std::string> input_filtered;
700-
for (std::vector<ItemStack>::const_iterator
701-
it = input.items.begin();
702-
it != input.items.end(); it++) {
703-
if (it->name != "")
704-
input_filtered.push_back(it->name);
693+
for (std::vector<ItemStack>::size_type i = 0;
694+
i < input.items.size(); i++) {
695+
const std::string &name = input.items[i].name;
696+
if (name != "")
697+
input_filtered.push_back(name);
705698
}
706699

707700
// 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
789782

790783
// Filter empty items out of input
791784
std::vector<std::string> input_filtered;
792-
for (std::vector<ItemStack>::const_iterator
793-
it = input.items.begin();
794-
it != input.items.end(); it++) {
795-
if (it->name != "")
796-
input_filtered.push_back(it->name);
785+
for (std::vector<ItemStack>::size_type i = 0;
786+
i < input.items.size(); i++) {
787+
const std::string &name = input.items[i].name;
788+
if (name != "")
789+
input_filtered.push_back(name);
797790
}
798791

799792
// If there is a wrong number of items in input, no match
@@ -889,10 +882,9 @@ class CCraftDefManager: public IWritableCraftDefManager
889882

890883
// If all input items are empty, abort.
891884
bool all_empty = true;
892-
for (std::vector<ItemStack>::const_iterator
893-
it = input.items.begin();
894-
it != input.items.end(); it++) {
895-
if (!it->empty()) {
885+
for (std::vector<ItemStack>::size_type i = 0;
886+
i < input.items.size(); i++) {
887+
if (!input.items[i].empty()) {
896888
all_empty = false;
897889
break;
898890
}
@@ -921,10 +913,9 @@ class CCraftDefManager: public IWritableCraftDefManager
921913
const std::vector<CraftDefinition*> &hash_collisions = col_iter->second;
922914
// Walk crafting definitions from back to front, so that later
923915
// definitions can override earlier ones.
924-
for (std::vector<CraftDefinition*>::const_reverse_iterator
925-
it = hash_collisions.rbegin();
926-
it != hash_collisions.rend(); it++) {
927-
CraftDefinition *def = *it;
916+
for (std::vector<CraftDefinition*>::size_type
917+
i = hash_collisions.size(); i > 0; i--) {
918+
CraftDefinition *def = hash_collisions[i - 1];
928919

929920
/*errorstream << "Checking " << input.dump() << std::endl
930921
<< " against " << def->dump() << std::endl;*/
@@ -957,11 +948,12 @@ class CCraftDefManager: public IWritableCraftDefManager
957948

958949
recipes.reserve(limit ? MYMIN(limit, vec.size()) : vec.size());
959950

960-
for (std::vector<CraftDefinition*>::const_reverse_iterator
961-
it = vec.rbegin(); it != vec.rend(); ++it) {
951+
for (std::vector<CraftDefinition*>::size_type i = vec.size();
952+
i > 0; i--) {
953+
CraftDefinition *def = vec[i - 1];
962954
if (limit && recipes.size() >= limit)
963955
break;
964-
recipes.push_back(*it);
956+
recipes.push_back(def);
965957
}
966958

967959
return recipes;
@@ -974,9 +966,12 @@ class CCraftDefManager: public IWritableCraftDefManager
974966
for (std::map<u64, std::vector<CraftDefinition*> >::const_iterator
975967
it = (m_craft_defs[type]).begin();
976968
it != (m_craft_defs[type]).end(); it++) {
977-
for (std::vector<CraftDefinition*>::const_iterator
978-
iit = it->second.begin(); iit != it->second.end(); iit++) {
979-
os << "type " << type << " hash " << it->first << (*iit)->dump() << "\n";
969+
for (std::vector<CraftDefinition*>::size_type i = 0;
970+
i < it->second.size(); i++) {
971+
os << "type " << type
972+
<< " hash " << it->first
973+
<< " def " << it->second[i]->dump()
974+
<< "\n";
980975
}
981976
}
982977
}
@@ -1000,7 +995,8 @@ class CCraftDefManager: public IWritableCraftDefManager
1000995
it = m_craft_defs[type].begin();
1001996
it != m_craft_defs[type].end(); it++) {
1002997
for (std::vector<CraftDefinition*>::iterator
1003-
iit = it->second.begin(); iit != it->second.end(); iit++) {
998+
iit = it->second.begin();
999+
iit != it->second.end(); ++iit) {
10041000
delete *iit;
10051001
}
10061002
it->second.clear();
@@ -1012,10 +1008,11 @@ class CCraftDefManager: public IWritableCraftDefManager
10121008
virtual void initHashes(IGameDef *gamedef)
10131009
{
10141010
// Move the CraftDefs from the unhashed layer into layers higher up.
1015-
for (std::vector<CraftDefinition*>::iterator
1016-
it = (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).begin();
1017-
it != (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).end(); it++) {
1018-
CraftDefinition *def = *it;
1011+
std::vector<CraftDefinition *> &unhashed =
1012+
m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0];
1013+
for (std::vector<CraftDefinition*>::size_type i = 0;
1014+
i < unhashed.size(); i++) {
1015+
CraftDefinition *def = unhashed[i];
10191016

10201017
// Initialize and get the definition's hash
10211018
def->initHash(gamedef);
@@ -1025,7 +1022,7 @@ class CCraftDefManager: public IWritableCraftDefManager
10251022
// Enter the definition
10261023
m_craft_defs[type][hash].push_back(def);
10271024
}
1028-
m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].clear();
1025+
unhashed.clear();
10291026
}
10301027
private:
10311028
//TODO: change both maps to unordered_map when c++11 can be used

0 commit comments

Comments
 (0)
Please sign in to comment.