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

View File

@@ -35,18 +35,18 @@ public:
protected:
map<string, int> values;
vector<string> valuesById;
map<int,int> subtypesToType;
vector<unsigned int> subtypesToType;
public:
static Subtypes * subtypesList;
Subtypes();
int find(string subtype, bool forceAdd = true);
string find(unsigned int id);
bool isSubtypeOfType(string subtype, string type);
bool isSubtypeOfType(int subtype, int type);
bool isSuperType(int type);
bool isType(int type);
bool isSubType(int type);
int add(string value, int parentType);
bool isSubtypeOfType(unsigned int subtype, unsigned int type);
bool isSuperType(unsigned int type);
bool isType(unsigned int type);
bool isSubType(unsigned int type);
int add(string value, unsigned int parentType);
const vector<string> getValuesById();
};

View File

@@ -29,6 +29,9 @@ Subtypes::Subtypes()
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
map<string, int>::iterator it = values.find(value);
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.
//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.
int Subtypes::add(string value, int parentType)
int Subtypes::add(string value, unsigned int parentType)
{
int subtype = find(value);
if (parentType && isSubType(subtype) && !subtypesToType[subtype])
unsigned int subtype = find(value);
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;
}
return subtype;
}
@@ -62,26 +69,26 @@ bool Subtypes::isSubtypeOfType(string subtype, string type)
unsigned int typeInt = find(type);
return isSubtypeOfType(subtypeInt, typeInt);
}
bool Subtypes::isSubtypeOfType(int subtype, int type)
bool Subtypes::isSubtypeOfType(unsigned int subtype, unsigned int 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);
}
bool Subtypes::isType(int type)
bool Subtypes::isType(unsigned int type)
{
return (
type == TYPE_CREATURE ||
type == TYPE_ENCHANTMENT ||
type == TYPE_SORCERY ||
type == TYPE_INSTANT ||
type == TYPE_LAND ||
type == TYPE_LAND ||
type == TYPE_ARTIFACT ||
type ==TYPE_PLANESWALKER ||
type == TYPE_PLANESWALKER ||
type == TYPE_TRIBAL ||
type == TYPE_PLANE ||
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));
}