📄 dictionary.cpp
字号:
// Do the removal.
attributes_.erase(attributes_.begin() + i);
return true;
}
//-------------------------------------------------------------------
// Method........: RemoveAllAttributes
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Dictionary::RemoveAllAttributes() {
attributes_.erase(attributes_.begin(), attributes_.end());
return true;
}
//-------------------------------------------------------------------
// Method........: SwapAttributes
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Swaps attributes i and j.
// Comments......:
// Revisions.....:
//===================================================================
bool
Dictionary::SwapAttributes(int i, int j) {
if (i == j)
return true;
#ifdef _DEBUG
// Index in range?
if ((i < 0) || (i >= GetNoAttributes()) || (j < 0) || (j >= GetNoAttributes())) {
Message::Error("Attribute index in dictionary out of range.");
return false;
}
#endif
Handle<Attribute> tmp;
tmp = attributes_[i];
attributes_[i] = attributes_[j];
attributes_[j] = tmp;
return true;
}
//-------------------------------------------------------------------
// Mapping methods.
//===================================================================
//-------------------------------------------------------------------
// Method........: GetEntry
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Maps from real-world value to coded value.
// Comments......:
// Revisions.....:
//===================================================================
int
Dictionary::GetEntry(int attribute_no, const String &text) const {
#ifdef _DEBUG
// Index in range?
if ((attribute_no < 0) || (attribute_no >= GetNoAttributes())) {
Message::Error("Attribute index in dictionary out of range.");
return Undefined::Integer();
}
#endif
return GetAttribute(attribute_no)->GetEntry(text);
}
//-------------------------------------------------------------------
// Method........: GetEntry
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Maps from coded value to real-world value.
// Comments......:
// Revisions.....:
//===================================================================
const String &
Dictionary::GetEntry(int attribute_no, int value) const {
#ifdef _DEBUG
// Index in range?
if ((attribute_no < 0) || (attribute_no >= GetNoAttributes())) {
Message::Error("Attribute index in dictionary out of range.");
return Undefined::String();
}
#endif
return GetAttribute(attribute_no)->GetEntry(value);
}
//-------------------------------------------------------------------
// Method........: SuggestEntry
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Use this method in preference to the corresponding
// method on the attribute level, as this method also
// makes some "global" considerations.
// Revisions.....:
//===================================================================
int
Dictionary::SuggestEntry(int attribute_no, const String &text) const {
#ifdef _DEBUG
// Index in range?
if ((attribute_no < 0) || (attribute_no >= GetNoAttributes())) {
Message::Error("Attribute index in dictionary out of range.");
return Undefined::Integer();
}
#endif
// Get the attribute.
Handle<Attribute> attribute = GetAttribute(attribute_no);
// Is the attribute of integer or float type?
if (attribute->IsA(INTEGERATTRIBUTE) || attribute->IsA(FLOATATTRIBUTE))
return attribute->SuggestEntry(text);
// Is the real-world value already present?
int coded = attribute->GetEntry(text);
if (coded != Undefined::Integer())
return coded;
int i;
// Is a similar real-world value present in any of the other string attribute maps?
for (i = 0; i < GetNoAttributes(); i++) {
if (i == attribute_no)
continue;
if (!GetAttribute(i)->IsA(STRINGATTRIBUTE))
continue;
coded = GetEntry(i, text);
if (coded != Undefined::Integer())
break;
}
// Is the coded entry available?
String realworld;
if (coded != Undefined::Integer()) {
realworld = attribute->GetEntry(coded);
if (realworld == Undefined::String())
return coded;
}
// Come up with a default local suggestion.
return attribute->SuggestEntry(text);
}
//-------------------------------------------------------------------
// Method........: SetEntry
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Assigns a mapping between a real-world value and
// a coded value.
// Comments......:
// Revisions.....:
//===================================================================
bool
Dictionary::SetEntry(int attribute_no, int value, const String &text) {
#ifdef _DEBUG
// Index in range?
if ((attribute_no < 0) || (attribute_no >= GetNoAttributes())) {
Message::Error("Attribute index in dictionary out of range.");
return false;
}
#endif
return GetAttribute(attribute_no)->SetEntry(value, text);
}
//-------------------------------------------------------------------
// Operators.
//===================================================================
//-------------------------------------------------------------------
// Operator......: ==
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Dictionary::operator==(const Dictionary &in) const {
// Are the two dictionaries physically equal?
if (&in == this)
return true;
int i, no_attributes = GetNoAttributes();
// Same number of attributes?
if (in.GetNoAttributes() != no_attributes)
return false;
// Are all attributes equal?
for (i = 0; i < no_attributes; i++) {
if (*(in.GetAttribute(i)) != *(GetAttribute(i)))
return false;
}
return true;
}
//-------------------------------------------------------------------
// Operator......: !=
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Dictionary::operator!=(const Dictionary &in) const {
return !(*this == in);
}
//-------------------------------------------------------------------
// Operator......: <=
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Determines if one dictionary "covers" or is more
// general than another dictionary.
// Comments......:
// Revisions.....:
//===================================================================
bool
Dictionary::operator<=(const Dictionary &in) const {
// Are the two dictionaries physically equal?
if (&in == this)
return true;
int i, no_attributes = GetNoAttributes();
// Same number of attributes?
if (in.GetNoAttributes() != no_attributes)
return false;
// Compare attributes.
for (i = 0; i < no_attributes; i++) {
if (!(*(in.GetAttribute(i)) <= *(GetAttribute(i)))) {
String name = GetAttribute(i)->GetName();
return false;
}
}
return true;
}
//-------------------------------------------------------------------
// Method........: IsCompatible
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Returns true if the two dictionaries are deemed
// compatible.
// Comments......:
// Revisions.....:
//===================================================================
bool
Dictionary::IsCompatible(const Dictionary &in) const {
// Are the two dictionaries physically equal?
if (&in == this)
return true;
int i, no_attributes = GetNoAttributes();
// Same number of attributes?
if (in.GetNoAttributes() != no_attributes)
return false;
// For compatibility, one attribute should "cover" the other.
for (i = 0; i < no_attributes; i++) {
if (!(*(in.GetAttribute(i)) <= *(GetAttribute(i))) && !(*(GetAttribute(i)) <= *(in.GetAttribute(i))))
return false;
}
// The dictionaries are compatible.
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -