changed Subtypes map into a vector, hoping for some speed improvements.

This commit is contained in:
wagic.the.homebrew
2011-05-09 13:56:22 +00:00
parent 7447a451eb
commit b1ea63cd79
2 changed files with 22 additions and 15 deletions
+6 -6
View File
@@ -35,18 +35,18 @@ public:
protected: protected:
map<string, int> values; map<string, int> values;
vector<string> valuesById; vector<string> valuesById;
map<int,int> subtypesToType; vector<unsigned int> subtypesToType;
public: public:
static Subtypes * subtypesList; static Subtypes * subtypesList;
Subtypes(); Subtypes();
int find(string subtype, bool forceAdd = true); int find(string subtype, bool forceAdd = true);
string find(unsigned int id); string find(unsigned int id);
bool isSubtypeOfType(string subtype, string type); bool isSubtypeOfType(string subtype, string type);
bool isSubtypeOfType(int subtype, int type); bool isSubtypeOfType(unsigned int subtype, unsigned int type);
bool isSuperType(int type); bool isSuperType(unsigned int type);
bool isType(int type); bool isType(unsigned int type);
bool isSubType(int type); bool isSubType(unsigned int type);
int add(string value, int parentType); int add(string value, unsigned int parentType);
const vector<string> getValuesById(); const vector<string> getValuesById();
}; };
+15 -8
View File
@@ -29,6 +29,9 @@ Subtypes::Subtypes()
int Subtypes::find(string value, bool forceAdd) int Subtypes::find(string value, bool forceAdd)
{ {
if (!value.size())
return 0;
if (value[0] >= 97 && value[0] <= 122) value[0] -= 32; //Poor man's camelcase. We assume strings we get are either Camelcased or lowercase if (value[0] >= 97 && value[0] <= 122) value[0] -= 32; //Poor man's camelcase. We assume strings we get are either Camelcased or lowercase
map<string, int>::iterator it = values.find(value); map<string, int>::iterator it = values.find(value);
if (it != values.end()) return it->second; if (it != values.end()) return it->second;
@@ -42,11 +45,15 @@ int Subtypes::find(string value, bool forceAdd)
// Adds a subtype to the list, and associated it with a parent type. // Adds a subtype to the list, and associated it with a parent type.
//The association can happen only once, a subtype is then definitely associated to its parent type. //The association can happen only once, a subtype is then definitely associated to its parent type.
// If you associate "goblin" to "creature", trying to associate "goblin" to "land" afterwards will fail. "goblin" will stay associated to its first parent. // If you associate "goblin" to "creature", trying to associate "goblin" to "land" afterwards will fail. "goblin" will stay associated to its first parent.
int Subtypes::add(string value, int parentType) int Subtypes::add(string value, unsigned int parentType)
{ {
int subtype = find(value); unsigned int subtype = find(value);
if (parentType && isSubType(subtype) && !subtypesToType[subtype]) if (parentType && isSubType(subtype))
{
if ((unsigned int)(subtypesToType.size()) < subtype + 1)
subtypesToType.resize(1 + subtype * 2, 0); //multiplying by 2 to avoid resizing at every insertion
subtypesToType[subtype] = parentType; subtypesToType[subtype] = parentType;
}
return subtype; return subtype;
} }
@@ -62,17 +69,17 @@ bool Subtypes::isSubtypeOfType(string subtype, string type)
unsigned int typeInt = find(type); unsigned int typeInt = find(type);
return isSubtypeOfType(subtypeInt, typeInt); return isSubtypeOfType(subtypeInt, typeInt);
} }
bool Subtypes::isSubtypeOfType(int subtype, int type) bool Subtypes::isSubtypeOfType(unsigned int subtype, unsigned int type)
{ {
return (subtypesToType[subtype] == type); return (subtypesToType[subtype] == type);
} }
bool Subtypes::isSuperType(int type) bool Subtypes::isSuperType(unsigned int type)
{ {
return (type == TYPE_BASIC || type == TYPE_WORLD || type == TYPE_SNOW || type == TYPE_LEGENDARY); return (type == TYPE_BASIC || type == TYPE_WORLD || type == TYPE_SNOW || type == TYPE_LEGENDARY);
} }
bool Subtypes::isType(int type) bool Subtypes::isType(unsigned int type)
{ {
return ( return (
type == TYPE_CREATURE || type == TYPE_CREATURE ||
@@ -81,7 +88,7 @@ bool Subtypes::isType(int type)
type == TYPE_INSTANT || type == TYPE_INSTANT ||
type == TYPE_LAND || type == TYPE_LAND ||
type == TYPE_ARTIFACT || type == TYPE_ARTIFACT ||
type ==TYPE_PLANESWALKER || type == TYPE_PLANESWALKER ||
type == TYPE_TRIBAL || type == TYPE_TRIBAL ||
type == TYPE_PLANE || type == TYPE_PLANE ||
type == TYPE_SCHEME || type == TYPE_SCHEME ||
@@ -89,7 +96,7 @@ bool Subtypes::isType(int type)
); );
} }
bool Subtypes::isSubType(int type) bool Subtypes::isSubType(unsigned int type)
{ {
return (!isSuperType(type) && !isType(type)); return (!isSuperType(type) && !isType(type));
} }