📄 reducts.cpp
字号:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Revisions.....:
//===================================================================
#include <stdafx.h> // Precompiled headers.
#include <copyright.h>
#include <kernel/structures/reducts.h>
#include <kernel/structures/reduct.h>
#include <kernel/structures/rules.h>
#include <kernel/system/stdlib.h>
#include <kernel/system/fstream.h>
#include <kernel/basic/algorithm.h>
#include <kernel/basic/message.h>
#include <kernel/utilities/iokit.h>
#include <kernel/utilities/creator.h>
#include <kernel/utilities/discerner.h>
//-------------------------------------------------------------------
// Static stuff (file scope). Hack to use STL sorting.
//===================================================================
static bool static_compare_ascending_ = true;
static int static_compare_property_ = 0;
//-------------------------------------------------------------------
// STL comparator methods.
//===================================================================
//-------------------------------------------------------------------
// Method........: Comparison operator
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Compares two reducts according to some specified
// reduct property.
// Comments......: Assumes static stuff has been properly set.
// Revisions.....:
//===================================================================
bool
Reducts::Compare::operator()(const Handle<Structure> &a, const Handle<Structure> &b) const {
if (a == b)
return false;
Handle<Reduct> reduct_a = dynamic_cast(Reduct *, const_cast(Structure *, a.GetPointer()));
Handle<Reduct> reduct_b = dynamic_cast(Reduct *, const_cast(Structure *, b.GetPointer()));
int property_a = 0;
int property_b = 0;
switch (static_compare_property_) {
case 0: // Support.
property_a = reduct_a->GetSupport();
property_b = reduct_b->GetSupport();
break;
case 1: // Length.
property_a = reduct_a->GetNoAttributes();
property_b = reduct_b->GetNoAttributes();
break;
default: return false;
}
if (static_compare_ascending_)
return (property_a < property_b);
else
return (property_a > property_b);
}
//-------------------------------------------------------------------
// Methods for class Reducts.
//===================================================================
//-------------------------------------------------------------------
// Constructors/destructor.
//===================================================================
Reducts::Reducts(const Reducts &in) : Structures(in) {
}
Reducts::Reducts() {
}
Reducts::~Reducts() {
}
//-------------------------------------------------------------------
// Methods inherited from Identifier.
//===================================================================
IMPLEMENTIDMETHODS(Reducts, REDUCTS, Structures)
//-------------------------------------------------------------------
// Methods inherited from Structure.
//===================================================================
Structure *
Reducts::Duplicate() const {
return new Reducts(*this);
}
//-------------------------------------------------------------------
// Method........: FindMember
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Eliminate this method with time -- should be
// implemented on a more general level.
// Revisions.....:
//===================================================================
int
Reducts::FindMember(const Structure *member, bool physical) const {
// Physical membership is implemented elsewhere.
if (physical)
return Structure::FindMember(member, physical);
if (member == NULL)
return Undefined::Integer();
if (!member->IsA(REDUCT))
return Undefined::Integer();
// Ugly, but safe.
Handle<Reduct> reduct = dynamic_cast(Reduct *, const_cast(Structure *, member));
int i, no_reducts = GetNoReducts();
// Check logical membership.
for (i = 0; i < no_reducts; i++) {
if (*reduct == *GetReduct(i))
return i;
}
return Undefined::Integer();
}
//-------------------------------------------------------------------
// Methods inherited from Structures.
//===================================================================
//-------------------------------------------------------------------
// Method........: InsertStructure
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Overloaded to check for consistency/homogenity.
// Ensures that the inserted structure really is a
// reduct.
// Comments......: AppendStructure is not necessary to overload, since
// Structures::AppendStructure is implemented via the
// InsertStructure method.
// Revisions.....:
//===================================================================
bool
Reducts::InsertStructure(Structure *structure, int i) {
if (structure == NULL) {
Message::Error("Cannot insert a NULL reduct.");
return false;
}
if (!structure->IsA(REDUCT)) {
Message::Error("Attempted to insert a non-reduct to a reduct set.");
return false;
}
return Structures::InsertStructure(structure, i);
}
//-------------------------------------------------------------------
// New methods.
//===================================================================
//-------------------------------------------------------------------
// Method........: GetNoReducts/GetReduct
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Convenience methods.
// Comments......: Made virtual due to the special nature of the
// wrapper for RSES reduct collections.
// Revisions.....:
//===================================================================
int
Reducts::GetNoReducts() const {
return GetNoStructures();
}
Reduct *
Reducts::GetReduct(int i) const {
return dynamic_cast(Reduct *, GetStructure(i));
}
//-------------------------------------------------------------------
// Method........: Reindex
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Reindexes all member reducts.
// Comments......:
// Revisions.....:
//===================================================================
bool
Reducts::Reindex(const Vector(int) &indices) {
int i, no_reducts = GetNoReducts();
for (i = 0; i < no_reducts; i++) {
if (!GetReduct(i)->Reindex(indices))
return false;
}
return true;
}
//-------------------------------------------------------------------
// Method........: Sort
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Sorts the reducts according to some specified
// property.
//
// 0 = Support
// 1 = Length
//
// Comments......:
// Revisions.....:
//===================================================================
bool
Reducts::Sort(int property, bool ascending) {
// Check property parameter.
if (property < 0 || property > 1)
return false;
// Set static stuff.
static_compare_property_ = property;
static_compare_ascending_ = ascending;
Compare comparator;
Message message;
message.Notify("Sorting reducts...");
// Sort.
std::sort(structures_.begin(), structures_.end(), comparator);
return true;
}
//-------------------------------------------------------------------
// Method........: PostCreation
// Author........: Aleksander 豩rn
// Date..........:
// Description...: This method can be overloaded by derived reduct
// set representations that need to have some kind of
// post-processing performed after thay have been
// created.
//
// This method is called after the set of reducts
// has been computed/created.
//
// Comments......:
// Revisions.....: A
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -