📄 attribute.cpp
字号:
/************************************************************************/
/* AUTHOR: Leiming Hong */
/* INST.: South China Univ. of Tech. */
/* Date: 2005-10-25 */
/* FUNC: Attribute used for defining data column type */
/************************************************************************/
#pragma warning(disable: 4786) //: disable the warning for map<string,int>
#include "Attribute.h"
#include "Exception.h"
//: for testing
#include <iostream>
#include <ctime>
using namespace std;
Attribute::Attribute()
: _name(""),
_type(_At::numeric),
_nvs(0)
{
//: default mining attribute type
}
Attribute::Attribute(const string& name,_At type /* = _At::numeric */)
: _name(name),
_type(type),
_nvs(0)
{
if(_type == _At::nominal)
_nvs = new map<string,int>();
}
Attribute::Attribute(const Attribute& attr)
{
_name = attr._name;
_type = attr._type;
_nvs = NULL;
if(attr.is_nominal())
{
_nvs = new map<string,int>(*attr._nvs);
}
}
Attribute::~Attribute()
{
if(_nvs != NULL) delete _nvs;
}
string Attribute::get_name() const
{
return _name;
}
void Attribute::set_name(const string& name)
{
_name = name;
}
Attribute::_At Attribute::type() const
{
return _type;
}
bool Attribute::is_nominal() const
{
return _type == _At::nominal;
}
bool Attribute::is_numeric() const
{
return _type == _At::numeric;
}
int Attribute::nominal_count() const
{
if(!is_nominal())
throw new Exception("Attribute: nominal_count not support!");
return _nvs->size();
}
void Attribute::add_nominal(const string& nom)
{
if(!is_nominal())
throw new Exception("Attribute: add_nominal not support!");
map<string,int>::iterator pos;
pos = _nvs->find(nom);
if(pos == _nvs->end()) _nvs->insert(pair<string,int>(nom,_nvs->size()));
}
int Attribute::nominal_value(const string& nom)
{
if(!is_nominal())
throw new Exception("Attribute: nominal_value not support!");
map<string,int>::iterator pos = _nvs->find(nom);
if(pos == _nvs->end()) return -1;
return pos->second;
}
/************************************************************************
void main()
{
time_t start,end;
start = clock();
try
{
Attribute attr("test",Attribute::_At::nominal);
attr.add_nominal("A");
attr.add_nominal("B");
attr.add_nominal("C");
cout<<attr.nominal_value("C")<<endl;
}
catch(Exception* e)
{
cout<<e->message()<<endl;
delete e;
}
end = clock();
cout<<"Time eclipsed[s]: "<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
}
/************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -