Скачиваний:
7
Добавлен:
01.05.2014
Размер:
2.38 Кб
Скачать
#if !defined(UTILS_H)
#define UTILS_H

#include "Hashable.h"
#include <sstream>
#include <iostream>
using namespace std;

// int to string conversion
template <class T>
inline string buildString(const T& t){
	stringstream ss;
	ss << t;
	return ss.str();
}

// hash builder
inline int buildHashCode(const string& s){
	unsigned int hashCode = 0;

    for (int i = 0; i < s.size(); i++){
        unsigned int temp = 0;
        hashCode = (hashCode << 4) + s[i];
        if ( temp = ( hashCode & 0xf0000000 ) )
        {
            hashCode = hashCode ^ ( temp >> 24 );
            hashCode = hashCode ^ temp;
        }
    }

	return hashCode;
}

// int wrapper that implements Hashable interface
class intKey: public Hashable{
	private: 
		int value;
	public:
		static unsigned long int count;
		intKey(): value(-1) {
			count++;
		};
		intKey(int v): value(v) {
			count++;
		};
		intKey(const intKey& o): value(o.value) {
			count++;
		};
		intKey(const intKey* o): value(o->value) {
			count++;
		};
		virtual ~intKey(){
			count--;
		};
		int hashCode() const{
			return value;
		};
		int equal(intKey o){
			return value == o.value;
		};
		int operator==(const intKey& o) const {
			return value == o.value;
		};
		friend ostream& operator<<(ostream& os, const intKey& o){
			return os << o.value;
		};
};


// int wrapper that implements Hashable interface
class stringKey: public Hashable{
	private: 
		string str;
	public:
		static unsigned long int count;
		stringKey(): str(""){
			count++;
		};
		stringKey(string s): str(s){
			count++;
		};
		stringKey(const stringKey& o): str(o.str) {
			count++;
		};
		stringKey(const stringKey* o): str(o->str) {
			count++;
		};
		string getKey(){
			return str;
		};
		virtual ~stringKey(){
			count--;
		};
		int hashCode() const{
			return buildHashCode(str);
		};
		int equal(stringKey o){
			return str == o.str;
		};
		int operator==(const stringKey& o) const {
			return str == o.str;
		};
		friend ostream& operator<<(ostream& os, const stringKey& o){
			return os << o.str;
		};

		virtual CArchive& save(CArchive& ar){
			CString s(str.c_str());
			ar	<< s;
			return ar;
		}

		virtual CArchive& load(CArchive& ar){
			CString s;
			ar	>> s;
			str = string(s);
			return ar;
		}
};

#endif 
Соседние файлы в папке OLEApp