Скачиваний:
12
Добавлен:
01.05.2014
Размер:
1.97 Кб
Скачать
#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){
	int hashCode = 0;
	for (int i = 0; i < s.size(); i++)
		hashCode = hashCode * 31 + s[i];
	return hashCode;

/*
h += ~(h << 9);
h ^=  (h >>> 14);
h +=  (h << 4);
h ^=  (h >>> 10);
*/
}

// 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++;
		};
		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;
		};
};

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