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

📄 metadata.cpp

📁 一个简单灵活的数据挖掘实验平台
💻 CPP
字号:
/************************************************************************/
/*		AUTHOR:   Leiming Hong                                          */
/*		INST.:    South China Univ. of Tech.							*/
/*      Date:     2005-10-25                                            */
/*      FUNC:     Attribute used for defining data column type          */   
/************************************************************************/
#include "MetaData.h"
#include "Exception.h"
//: for test
#include <iostream>
#include <ctime>
using namespace std;
MetaData::MetaData()
	: _name(""),
	  _attributes(),
	  _target()
{
}
MetaData::MetaData(const MetaData& md)
{
	_name = md._name;
	_attributes = md._attributes;
	_target = md._target;
}
MetaData::~MetaData()
{
}
string MetaData::get_name() const
{
	return _name;
}
void MetaData::set_name(const string& rname)
{
	_name = rname;
}
int MetaData::size() const
{
	return _attributes.size();
}
void MetaData::append(Attribute* attr)
{
	_attributes.push_back(attr);
}
void MetaData::insert(Attribute* attr,int index)
{
	if(index < 0 || index > _attributes.size()) 
		throw new Exception("MetaData::insert, index out of bound!");
	
	vector<Attribute*>::iterator it = _attributes.begin();
	int i=0;
	while(i++<index)
	{
		if(*it == attr) return; //: same attribute exists
		it++;
	}
	_attributes.insert(it,attr);
}
Attribute MetaData::operator [](int index) const
{
	if(index<0 || index>=_attributes.size())
		throw new Exception("MetaData::operator[] index out of bound!");
	return *(_attributes[index]);
}
vector<Attribute*> MetaData::target() const
{
	return _target;
}
void MetaData::add_target(Attribute* attr)
{
	vector<Attribute*>::iterator it;
	bool found = false;
	int index = 0;
	for(it=_attributes.begin(); it!=_attributes.end();it++)
	{
		if(*it == attr) found = true;
		index++;
	}
	if(!found)
	{
		throw new Exception("MetaData::add_target, attribute not belongs to the attributes exists!");
	}
	for(it = _target.begin();it!=_target.end();it++)
	{
		if(*it == attr) return; //: same attribute added
	}
	_target.push_back(attr);
}
void MetaData::add_target(int index)
{
	if(index < 0 || index >= _attributes.size())
	{
		throw new Exception("MetaData::add_target, attribute index out of bound!");
	}
	Attribute* attr = _attributes[index];

	vector<Attribute*>::iterator it;
	for(it=_target.begin();it!=_target.end();it++)
	{
		if(*it == attr) return;//: same attribute index added
	}
	_target.push_back(attr);
}
/************************************************************************
// code for testing
void main()
{
	time_t start,end;
	start = clock();
	try
	{
		MetaData md;
		Attribute attr1("attr1",Attribute::_At::nominal);
		Attribute attr2("attr2",Attribute::_At::numeric);
		Attribute attr3("attr3",Attribute::_At::nominal);

//		md.insert(&attr1,0);
// 		md.insert(&attr2,1);
		
		md.append(&attr1);
		md.append(&attr2);
		md.add_target(&attr2);
		
		cout<<md.size()<<endl;

		vector<Attribute*> target = md.target();
		cout<<"target size: "<<target.size()<<endl;
		vector<Attribute*>::iterator it;
		for(it=target.begin();it!=target.end();it++)
		{
			cout<<(*it)->type()<<endl;
		}
		Attribute attr = md[1];
		cout<<attr.get_name()<<endl;
	}
	catch(Exception* e)
	{
		cout<<e->message()<<endl;
		delete e;
 	}
	end = clock();
	cout<<"\nTime eclipsed[s]: "<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
}
/************************************************************************/

⌨️ 快捷键说明

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