⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ksdecisiontable.cpp

📁 ROSETTA C++库是一个C++类库和例程集合
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Revisions.....:
//===================================================================

#include <stdafx.h> // Precompiled headers.
#include <copyright.h>

#include <kernel/structures/ksdecisiontable.h>

//-------------------------------------------------------------------
// Methods for class KSDecisionTable.
//===================================================================

//-------------------------------------------------------------------
// Constructors/destructor.
//===================================================================

//-------------------------------------------------------------------
// Method........: Copy constructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

KSDecisionTable::KSDecisionTable(const KSDecisionTable &in) : DecisionTable(in) {
	no_objects_           = in.no_objects_;
	no_attributes_        = in.no_attributes_;
	table_                = in.table_;
	attribute_masks_      = in.attribute_masks_;
	attribute_map_        = in.attribute_map_;
}

//-------------------------------------------------------------------
// Method........: Constructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

KSDecisionTable::KSDecisionTable() {
	no_objects_           = 0;
	no_attributes_        = 0;
}

//-------------------------------------------------------------------
// Method........: Destructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

KSDecisionTable::~KSDecisionTable() {
}

//-------------------------------------------------------------------
// Methods inherited from Identifier.
//===================================================================

IMPLEMENTIDMETHODS(KSDecisionTable, KSDECISIONTABLE, DecisionTable)

//-------------------------------------------------------------------
// Methods inherited from Structure.
//===================================================================

//-------------------------------------------------------------------
// Method........: Duplicate
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

Structure *
KSDecisionTable::Duplicate() const {
  return new KSDecisionTable(*this);
}

//-------------------------------------------------------------------
// Method........: Clear
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

void
KSDecisionTable::Clear() {
	DecisionTable::Clear();
}

//-------------------------------------------------------------------
// Methods inherited from DecisionTable.
//===================================================================

//-------------------------------------------------------------------
// Method........: GetNoObjects
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Returns the number of objects in the table.
// Comments......: Object masking not implemented yet.
// Revisions.....:
//===================================================================

int
KSDecisionTable::GetNoObjects(bool /*masked*/) const {
	return no_objects_;
}

//-------------------------------------------------------------------
// Method........: GetNoAttributes
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Returns the number of attributes in the table.
// Comments......:
// Comments......:
// Revisions.....:
//===================================================================

int
KSDecisionTable::GetNoAttributes(bool masked) const {

	if (masked)
		return attribute_map_.size();
	else
		return no_attributes_;

}

//-------------------------------------------------------------------
// Method........: Reserve
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Reserves space, if relevant for the physical
//                 implementation, for a decision table of the
//                 specified size.
// Comments......:
// Revisions.....:
//===================================================================

bool
KSDecisionTable::Reserve(int no_objects, int no_attributes) {

	table_.reserve(no_objects);

	int i;

	for (i = 0; i < table_.size(); i++)
		table_[i].reserve(no_attributes);

	attribute_masks_.reserve(no_attributes);

	return true;

}

//-------------------------------------------------------------------
// Method........: GetEntry
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

int
KSDecisionTable::GetEntry(int object_no, int attribute_no, bool masked) const {

#ifdef _DEBUG
	// Are the indices legal?
	if ((object_no < 0) || (object_no >= GetNoObjects(masked)) || (attribute_no < 0) || (attribute_no >= GetNoAttributes(masked))) {
		Message::Error("Entry indices out of range.");
		return Undefined::Integer();
	}
#endif

	int object_no_unmasked    = (masked) ? GetUnmaskedObject(object_no)       : object_no;
	int attribute_no_unmasked = (masked) ? GetUnmaskedAttribute(attribute_no) : attribute_no;

	return table_[object_no_unmasked][attribute_no_unmasked];

}

//-------------------------------------------------------------------
// Method........: SetEntry
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

bool
KSDecisionTable::SetEntry(int object_no, int attribute_no, int value, bool masked) {

#ifdef _DEBUG
	// Are the indices legal?
	if ((object_no < 0) || (object_no >= GetNoObjects(masked)) || (attribute_no < 0) || (attribute_no >= GetNoAttributes(masked))) {
		Message::Error("Entry indices out of range.");
		return false;
	}
#endif

	int object_no_unmasked    = (masked) ? GetUnmaskedObject(object_no)       : object_no;
	int attribute_no_unmasked = (masked) ? GetUnmaskedAttribute(attribute_no) : attribute_no;

	table_[object_no_unmasked][attribute_no_unmasked] = value;

	return true;

}

//-------------------------------------------------------------------
// Method........: InsertObject
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

bool
KSDecisionTable::InsertObject(int object_no, bool masked) {

#ifdef _DEBUG
	// Is the index legal?
	if ((object_no < 0) || (object_no > GetNoObjects(masked))) {
		Message::Error("Object index out of range.");
		return false;
	}
#endif

	Vector(int) inf;
	inf.reserve(no_attributes_);

	int i;

	// Prepare an information vector with default values.
	for (i = 0; i < no_attributes_; i++)
		inf.push_back(Undefined::Integer());

	int object_no_unmasked = (masked) ? GetUnmaskedObject(object_no) : object_no;

	// Insert it into the table.
	table_.insert(table_.begin() + object_no_unmasked, inf);

	// Update size variables.
	no_objects_++;

	return true;

}

//-------------------------------------------------------------------
// Method........: RemoveObject
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

bool
KSDecisionTable::RemoveObject(int object_no, bool masked) {

#ifdef _DEBUG
	// Is the index legal?
	if ((object_no < 0) || (object_no >= GetNoObjects(masked))) {
		Message::Error("Object index out of range.");
		return false;
	}
#endif

	int object_no_unmasked = (masked) ? GetUnmaskedObject(object_no) : object_no;

	table_.erase(table_.begin() + object_no_unmasked);

	// Update size variables.
	no_objects_--;

	return true;

}

//-------------------------------------------------------------------
// Method........: InsertAttribute
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

bool
KSDecisionTable::InsertAttribute(int attribute_no, const Attribute *attribute, bool masked) {

#ifdef _DEBUG
	// Is the index legal?
	if ((attribute_no < 0) || (attribute_no > GetNoAttributes(masked))) {
		Message::Error("Attribute index out of range.");
		return false;
	}
#endif

	// Does the decision table have any children?
	if (HasChildren()) {
		Message::Error("Cannot insert attribute, the decision table has children.");
		return false;
	}

	int i, attribute_no_unmasked = (masked) ? GetUnmaskedAttribute(attribute_no) : attribute_no;

	// Insert values into table.
	for (i = 0; i < no_objects_; i++) {
		table_[i].insert(table_[i].begin() + attribute_no_unmasked, Undefined::Integer());
	}

	// Update size variables.
	no_attributes_++;

	// Deal with dictionary stuff higher up.
	if (!DecisionTable::InsertAttribute(attribute_no, attribute, masked))
		return false;

	attribute_masks_.insert(attribute_masks_.begin() + attribute_no_unmasked, DecisionTable::MASK_ENABLED);

	if (!BuildAttributeMap(attribute_no_unmasked))
		return false;

	return true;

}

//-------------------------------------------------------------------
// Method........: RemoveAttribute
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

bool
KSDecisionTable::RemoveAttribute(int attribute_no, bool masked) {

#ifdef _DEBUG
	// Is the index legal?

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -