Добавил:
Upload
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:12пми / Template / Разреженная матрица / HashSparseArray
.h
#ifndef HASHSPARSEARRAY_H_
#define HASHSPARSEARRAY_H_
#include <iostream>
using namespace std;
template <typename T>
class HashSparseArray
{
public:
HashSparseArray(const unsigned long long size);
virtual ~HashSparseArray();
bool get(const unsigned long long key, T& value);
bool set(const unsigned long long key, const T value);
void printLog();
private:
class Element
{
public:
Element(const unsigned long long key, const T value);
virtual ~Element();
bool set(const unsigned long long key, const T value);
bool get(const unsigned long long key, T& value);
void printLog();
private:
unsigned long long mKey;
T mValue;
Element *mNext;
};
unsigned long long mSize;
Element **mArray;
unsigned long long hashFunction(const unsigned long long key);
};
template <typename T>
HashSparseArray<T>::HashSparseArray(const unsigned long long size)
{
mSize = size;
mArray = new Element*[size];
for(int i=0;i<size;i++)
{
mArray[i]=NULL;
}
}
template <typename T>
HashSparseArray<T>::~HashSparseArray()
{
if (mArray != NULL)
{
for (int i = 0; i < mSize; i++)
{
if (mArray[i] != NULL)
{
cout << "delete index " << i << endl;
delete mArray[i];
mArray[i] = NULL;
}
}
delete[] mArray;
mArray = NULL;
}
}
template <typename T>
bool HashSparseArray<T>::set(const unsigned long long key, const T value)
{
const unsigned long long index = hashFunction(key);
if (mArray[index] == NULL)
{
mArray[index] = new Element(key, value);
} else
{
return mArray[index]->set(key, value);
}
return true;
}
template <typename T>
bool HashSparseArray<T>::get(const unsigned long long key, T& value)
{
const unsigned long long index = hashFunction(key);
if (mArray[index] == NULL)
{
return false;
} else
{
return mArray[index]->get(key, value);
}
}
template <typename T>
void HashSparseArray<T>::printLog()
{
if (mArray != NULL)
{
for (int i = 0; i < mSize; i++)
{
if (mArray[i] != NULL)
{
cout << "printLog index " << i << endl;
mArray[i]->printLog();
}
}
}
}
template <typename T>
unsigned long long HashSparseArray<T>::hashFunction(const unsigned long long key)
{
return key%mSize;
}
template <typename T>
HashSparseArray<T>::Element::Element(const unsigned long long key, const T value):mKey(key), mValue(value), mNext(NULL)
{
}
template <typename T>
HashSparseArray<T>::Element::~Element()
{
cout << "delete key " << mKey << endl;
if (mNext != NULL)
{
delete mNext;
mNext = NULL;
}
}
template <typename T>
bool HashSparseArray<T>::Element::set(const unsigned long long key, const T value)
{
if (mKey == key)
{
mValue = value;
} else
{
if (mNext == NULL)
{
mNext = new Element(key, value);
} else
{
return mNext->set(key, value);
}
}
return true;
}
template <typename T>
bool HashSparseArray<T>::Element::get(const unsigned long long key, T& value)
{
if (mKey == key)
{
value = mValue;
return true;
} else
{
if (mNext == NULL)
{
return false;
} else
{
return mNext->get(key, value);
}
}
}
template <typename T>
void HashSparseArray<T>::Element::printLog()
{
cout << "printLog key " << mKey << endl;
if (mNext != NULL)
{
mNext->printLog();
}
}
#endif /* HASHSPARSEARRAY_H_ */
Соседние файлы в папке Разреженная матрица