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

📄 tools.cc

📁 tools.03b.zip
💻 CC
📖 第 1 页 / 共 2 页
字号:
// Tools Library//// Copyright (C) 2004  Navel Ltd.//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA//// Contact information://  Mailing address://    Marios Hadjieleftheriou//    University of California, Riverside//    Department of Computer Science//    Surge Building, Room 310//    Riverside, CA 92521////  Email://    marioh@cs.ucr.edu#include <Tools.h>#include "ExternalSort.h"const std::string VERSION = "0.3b";const std::string DATE = "06 June 2004";Tools::IndexOutOfBoundsException::IndexOutOfBoundsException(int i){	std::ostringstream s;	s << "Invalid index " << i;	m_error = s.str();}std::string Tools::IndexOutOfBoundsException::what(){	return "IndexOutOfBoundsException: " + m_error;}Tools::IllegalArgumentException::IllegalArgumentException(std::string s) : m_error(s){}std::string Tools::IllegalArgumentException::what(){	return "IllegalArgumentException: " + m_error;}Tools::IllegalStateException::IllegalStateException(std::string s) : m_error(s){}std::string Tools::IllegalStateException::what(){	return "IllegalStateException: " + m_error + "\nPlease contact " + EMAIL;}
Tools::EndOfStreamException::EndOfStreamException(std::string s) : m_error(s){}std::string Tools::EndOfStreamException::what(){	return "EndOfStreamException: " + m_error;}Tools::ResourceLockedException::ResourceLockedException(std::string s) : m_error(s){}std::string Tools::ResourceLockedException::what(){	return "ResourceLockedException: " + m_error;}Tools::InvalidPageException::InvalidPageException(long id){	std::ostringstream s;	s << "Unknown page id " << id;	m_error = s.str();}std::string Tools::InvalidPageException::what(){	return "InvalidPageException: " + m_error;}Tools::NotSupportedException::NotSupportedException(std::string s) : m_error(s){}std::string Tools::NotSupportedException::what(){	return "NotSupportedException: " + m_error;}Tools::ParseErrorException::ParseErrorException(std::string s) : m_error(s){}std::string Tools::ParseErrorException::what(){	return "ParseErrorException: " + m_error;}Tools::Variant::Variant() : m_varType(VT_EMPTY){}unsigned long Tools::PropertySet::getByteArraySize(){	unsigned long size = sizeof(unsigned long);	for (std::map<std::string, Variant>::iterator it = m_propertySet.begin(); it != m_propertySet.end(); it++)	{		switch ((*it).second.m_varType)		{		case VT_LONG:			size += sizeof(long);			break;		case VT_BYTE:			size += sizeof(byte);			break;		case VT_SHORT:			size += sizeof(short);			break;		case VT_FLOAT:			size += sizeof(float);			break;		case VT_DOUBLE:			size += sizeof(double);			break;		case VT_CHAR:			size += sizeof(char);			break;		case VT_USHORT:			size += sizeof(unsigned short);			break;		case VT_ULONG:			size += sizeof(unsigned long);			break;		case VT_INT:			size += sizeof(int);			break;		case VT_UINT:			size += sizeof(unsigned int);			break;		case VT_BOOL:			size += sizeof(byte);			break;		default:			throw NotSupportedException("Cannot serialize a variant of this type.");		}		size += (*it).first.size() + sizeof(unsigned long) + sizeof(VariantType);	}	return size;}unsigned long Tools::PropertySet::loadFromByteArray(byte* const data)
{
	byte* ptr = data;	unsigned long numberOfProperties;	memcpy(&numberOfProperties, ptr, sizeof(unsigned long));	ptr += sizeof(unsigned long);	unsigned long strSize;	char* str;	Variant v;	for (unsigned long cIndex = 0; cIndex < numberOfProperties; cIndex++)	{		memcpy(&strSize, ptr, sizeof(unsigned long));		ptr += sizeof(unsigned long);		str = new char[strSize + 1]; str[strSize] = 0;		memcpy(str, ptr, strSize);		ptr += strSize;		memcpy(&(v.m_varType), ptr, sizeof(VariantType));
		ptr += sizeof(VariantType);		switch (v.m_varType)		{		case VT_LONG:			long l;			memcpy(&l, ptr, sizeof(long));			ptr += sizeof(long);			v.m_val.lVal = l;			break;		case VT_BYTE:			byte b;			memcpy(&b, ptr, sizeof(byte));			ptr += sizeof(byte);			v.m_val.bVal = b;			break;		case VT_SHORT:			short s;			memcpy(&s, ptr, sizeof(short));			ptr += sizeof(short);			v.m_val.iVal = s;			break;		case VT_FLOAT:			float f;			memcpy(&f, ptr, sizeof(float));			ptr += sizeof(float);			v.m_val.fltVal = f;			break;		case VT_DOUBLE:			double d;			memcpy(&d, ptr, sizeof(double));			ptr += sizeof(double);			v.m_val.dblVal = d;			break;		case VT_CHAR:			char c;			memcpy(&c, ptr, sizeof(char));			ptr += sizeof(char);			v.m_val.cVal = c;			break;		case VT_USHORT:			unsigned short us;			memcpy(&us, ptr, sizeof(unsigned short));			ptr += sizeof(unsigned short);			v.m_val.uiVal = us;			break;		case VT_ULONG:			unsigned long ul;			memcpy(&ul, ptr, sizeof(unsigned long));			ptr += sizeof(unsigned long);			v.m_val.ulVal = ul;			break;		case VT_INT:			int i;			memcpy(&i, ptr, sizeof(int));			ptr += sizeof(int);			v.m_val.intVal = i;			break;		case VT_UINT:			unsigned int ui;			memcpy(&ui, ptr, sizeof(unsigned int));			ptr += sizeof(unsigned int);			v.m_val.uintVal = ui;			break;		case VT_BOOL:			byte bl;			memcpy(&bl, ptr, sizeof(byte));			ptr += sizeof(byte);			v.m_val.blVal = static_cast<bool>(bl);			break;		default:			throw NotSupportedException("Cannot serialize a variant of this type.");		}		m_propertySet.insert(std::pair<std::string, Variant>(std::string(str), v));		delete[] str;	}	return getByteArraySize();
}
void Tools::PropertySet::storeToByteArray(unsigned long& len, byte** data)
{	len = getByteArraySize();	*data = new byte[len];	byte* ptr = *data;	unsigned long numberOfProperties = m_propertySet.size();	memcpy(ptr, &numberOfProperties, sizeof(unsigned long));	ptr += sizeof(unsigned long);	for (std::map<std::string, Variant>::iterator it = m_propertySet.begin(); it != m_propertySet.end(); it++)	{		unsigned long strSize = (*it).first.size();		memcpy(ptr, &strSize, sizeof(unsigned long));		ptr += sizeof(unsigned long);		memcpy(ptr, (*it).first.c_str(), strSize);		ptr += strSize;		memcpy(ptr, &((*it).second.m_varType), sizeof(VariantType));		ptr += sizeof(VariantType);		switch ((*it).second.m_varType)		{		case VT_LONG:			memcpy(ptr, &((*it).second.m_val.lVal), sizeof(long));			ptr += sizeof(long);			break;		case VT_BYTE:			memcpy(ptr, &((*it).second.m_val.bVal), sizeof(byte));			ptr += sizeof(byte);			break;		case VT_SHORT:			memcpy(ptr, &((*it).second.m_val.iVal), sizeof(short));			ptr += sizeof(short);			break;		case VT_FLOAT:			memcpy(ptr, &((*it).second.m_val.fltVal), sizeof(float));			ptr += sizeof(float);			break;		case VT_DOUBLE:			memcpy(ptr, &((*it).second.m_val.dblVal), sizeof(double));			ptr += sizeof(double);			break;		case VT_CHAR:			memcpy(ptr, &((*it).second.m_val.cVal), sizeof(char));			ptr += sizeof(char);			break;		case VT_USHORT:			memcpy(ptr, &((*it).second.m_val.uiVal), sizeof(unsigned short));			ptr += sizeof(unsigned short);			break;		case VT_ULONG:			memcpy(ptr, &((*it).second.m_val.ulVal), sizeof(unsigned long));			ptr += sizeof(unsigned long);			break;		case VT_INT:			memcpy(ptr, &((*it).second.m_val.intVal), sizeof(int));			ptr += sizeof(int);			break;		case VT_UINT:			memcpy(ptr, &((*it).second.m_val.uintVal), sizeof(unsigned int));			ptr += sizeof(unsigned int);			break;		case VT_BOOL:			byte bl;			bl = (*it).second.m_val.blVal;			memcpy(ptr, &bl, sizeof(byte));			ptr += sizeof(byte);			break;		default:			throw NotSupportedException("Cannot serialize a variant of this type.");		}	}}
Tools::Variant Tools::PropertySet::getProperty(std::string property){	std::map<std::string, Variant>::iterator it = m_propertySet.find(property);	if (it != m_propertySet.end()) return (*it).second;	else return Variant();}void Tools::PropertySet::setProperty(std::string property, Variant& v){	m_propertySet.insert(std::pair<std::string, Variant>(property, v));}void Tools::PropertySet::removeProperty(std::string property){	std::map<std::string, Variant>::iterator it = m_propertySet.find(property);	if (it != m_propertySet.end()) m_propertySet.erase(it);}Tools::IObjectStream* Tools::externalSort(IObjectStream& source, unsigned long bufferSize)
{
	return new ExternalSort(source, bufferSize);}
Tools::IObjectStream* Tools::externalSort(IObjectStream& source, IObjectComparator& comp, unsigned long bufferSize)
{	return new ExternalSort(source, comp, bufferSize);
}
Tools::Interval::Interval() : m_type(IT_RIGHTOPEN), m_low(0.0), m_high(0.0){}Tools::Interval::Interval(IntervalType t, double l, double h) : m_type(t), m_low(l), m_high(h){	assert(l < h);}Tools::Interval::Interval(double l, double h) : m_type(IT_RIGHTOPEN), m_low(l), m_high(h){	assert(l < h);}Tools::Interval::Interval(const Interval& iv){	m_low = iv.m_low;	m_high = iv.m_high;	m_type = iv.m_type;}Tools::IInterval& Tools::Interval::operator=(const IInterval& iv){	if (this != &iv)	{		m_low = iv.getLowerBound();		m_high = iv.getUpperBound();		m_type = iv.getIntervalType();	}	return *this;}Tools::Interval& Tools::Interval::operator=(const Interval& iv){	*this = *(static_cast<const IInterval*>(&iv));	return *this;}double Tools::Interval::getLowerBound() const{	return m_low;}double Tools::Interval::getUpperBound() const{	return m_high;}void Tools::Interval::setBounds(double l, double h){	assert(l <= h);	m_low = l;	m_high = h;}bool Tools::Interval::intersectsInterval(const IInterval& i) const{	return intersectsInterval(i.getIntervalType(), i.getLowerBound(), i.getUpperBound());}bool Tools::Interval::intersectsInterval(IntervalType type, const double low, const double high) const{	if (m_high < m_low) throw IllegalStateException("intersectsInterval: high boundary is smaller than low boundary.");	if (m_low > high || m_high < low) return false;	if ((m_low > low && m_low < high) || (m_high > low && m_high < high)) return true;	switch (m_type)	{	case IT_CLOSED:		if (m_low == high)		{			if (type == IT_CLOSED || type == IT_LEFTOPEN) return true;			else return false;		}		else if (m_high == low)		{			if (type == IT_CLOSED || type == IT_RIGHTOPEN) return true;			else return false;		}		break;	case IT_OPEN:		if (m_low == high || m_high == low) return false;		break;

⌨️ 快捷键说明

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