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

📄 confusionmatrix.cpp

📁 粗糙集应用软件
💻 CPP
字号:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Revisions.....:
//===================================================================

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

#include <kernel/structures/confusionmatrix.h>

#include <kernel/utilities/iokit.h>
#include <kernel/utilities/creator.h>

#include <kernel/system/fstream.h>

//-------------------------------------------------------------------
// Static methods (file scope).
//===================================================================

//-------------------------------------------------------------------
// Method........: StaticInteger/StaticFloat
// Author........: Aleksander 豩rn
// Date..........:
// Description...: For fixed-width formatting.
// Comments......:
// Revisions.....:
//===================================================================

static String
StaticInteger(int i, int width) {

	String formatted;

	// Initialize.
	if (i == Undefined::Integer())
		formatted = Undefined::String();
	else
		formatted = String::Format(i);

	// Pad or truncate.
	formatted.Pad(' ', width);

	return formatted;

}

static String
StaticFloat(float f, int width) {

	String formatted;

	// Initialize.
	if (f == Undefined::Float())
		formatted = Undefined::String();
	else
		formatted = String::Format(100 * f) + '%';

	// Pad or truncate.
	formatted.Pad(' ', width);

	if (f == Undefined::Float())
		return formatted;

	if (formatted.Last() != ' ')
		formatted[formatted.GetLength() - 1] = '%';

	return formatted;

}

//-------------------------------------------------------------------
// Methods for class ConfusionMatrix.
//===================================================================

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

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

ConfusionMatrix::ConfusionMatrix(const ConfusionMatrix &in) : AnnotatedStructure(in) {

	// Duplicate matrix.
	if (!in.matrix_.IsNULL())
		matrix_ = dynamic_cast(DecisionTable *, in.matrix_->Duplicate());
	else
		matrix_ = NULL;

	// Duplicate map.
	if (in.map_ == NULL)
		map_ = NULL;
	else
		map_ = new IMap(*(in.map_));

}

//-------------------------------------------------------------------
// Method........: Constructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Annotation not needed, superstructure should have
//                 one.
// Revisions.....:
//===================================================================

ConfusionMatrix::ConfusionMatrix() {

	// Create the physical representation.
	matrix_    = Creator::DecisionTable();
	map_       = NULL;

	matrix_->SetDictionary(NULL);

	// No annotation needed.
	SetAnnotation(NULL);

}

//-------------------------------------------------------------------
// Method........: Constructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Annotation not needed, superstructure should have
//                 one.
// Revisions.....:
//===================================================================

ConfusionMatrix::ConfusionMatrix(int dimension) {

	// Create the physical representation.
	matrix_    = Creator::DecisionTable();
	map_       = NULL;

	matrix_->SetDictionary(NULL);

	// Adjust dimensionality.
	SetDimension(dimension);

	// No annotation needed.
	SetAnnotation(NULL);

  // Reset entries.
	Clear();

}

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

ConfusionMatrix::~ConfusionMatrix() {
	delete map_;
}

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

IMPLEMENTIDMETHODS(ConfusionMatrix, CONFUSIONMATRIX, AnnotatedStructure)

//-------------------------------------------------------------------
// Methods inherited from Persistent.
//===================================================================

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

bool
ConfusionMatrix::Load(ifstream &stream) {

	// Load data higher up in the hierarchy.
	if (!AnnotatedStructure::Load(stream))
		return false;

	// Reset old physical representation.
	matrix_ = NULL;
	matrix_ = Creator::DecisionTable();
	matrix_->SetDictionary(NULL);

	delete map_;
	map_ = NULL;

	int size, mapping;

	// Load map size.
	if (!IOKit::Load(stream, size))
		return false;

	int i;

	// Load map.
	for (i = 0; i < size; i++) {
		if (!IOKit::Load(stream, mapping))
			return false;
		if (!SetMap(i, mapping))
			return false;
	}

	// Load actual matrix.
	if (!matrix_->Load(stream))
		return false;

	return true;

}

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

bool
ConfusionMatrix::Save(ofstream &stream) const {

	// Save data higher up in the hierarchy.
	if (!AnnotatedStructure::Save(stream))
		return false;

	// Save size.
	if (!IOKit::Save(stream, GetDimension()))
		return false;

	if (!IOKit::Save(stream, '\n'))
		return false;

	int i;

	// Save map.
	for (i = 0; i < GetDimension(); i++) {
		if (!IOKit::Save(stream, GetValue(i)))
			return false;
	}

	if (!IOKit::Save(stream, '\n'))
		return false;

	// Save actual matrix.
	if (!matrix_->Save(stream))
		return false;

	return true;

}

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

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

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

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

void
ConfusionMatrix::Clear() {

	int i, j;

	// Reset all entries.
	for (i = 0; i < GetDimension(); i++) {
		for (j = 0; j < GetDimension(); j++) {
			SetEntry(i, j, 0);
		}
	}

	// Clear stuff higher up.
	AnnotatedStructure::Clear();

}

//-------------------------------------------------------------------
// Operators.
//===================================================================

//-------------------------------------------------------------------
// Operator......: =
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Assignment operator.
// Comments......:
// Revisions.....:
//===================================================================

ConfusionMatrix &
ConfusionMatrix::operator=(const ConfusionMatrix &in) {

	// Protect against self-assignment.
	if (this == &in)
		return *this;

	// Duplicate physical representation.
	if (in.matrix_.IsNULL())
		matrix_ = NULL;
	else
		matrix_ = dynamic_cast(DecisionTable *, in.matrix_->Duplicate());

	if (in.map_ == NULL)
		map_ = NULL;
	else
		map_ = new IMap(*(in.map_));

	// This should be done elsewhere...
	SetAnnotation(NULL);

  return *this;

}

//-------------------------------------------------------------------
// Operator......: ==
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....: A

⌨️ 快捷键说明

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