[DEV] create a basic hach table
This commit is contained in:
parent
a09c3cf795
commit
c7ece69d4e
182
etk/Hach.h
182
etk/Hach.h
@ -0,0 +1,182 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
*
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
*
|
||||
* @license BSD v3 (see license file)
|
||||
*/
|
||||
|
||||
#ifndef __ETK_HACH_H__
|
||||
#define __ETK_HACH_H__
|
||||
|
||||
#include <etk/types.h>
|
||||
#include <etk/DebugInternal.h>
|
||||
#include <etk/Vector.h>
|
||||
#include <etk/UString.h>
|
||||
|
||||
#undef __class__
|
||||
#define __class__ "etk::Hash"
|
||||
|
||||
|
||||
namespace etk
|
||||
{
|
||||
template<class MY_TYPE> class HashData
|
||||
{
|
||||
public:
|
||||
etk::UString m_key; //!< name of the current hash
|
||||
MY_TYPE m_value; //!< data of the current Hash
|
||||
HashData(const etk::UString& _key, const MY_TYPE& _val) :
|
||||
m_key(_key),
|
||||
m_value(_val)
|
||||
{
|
||||
// nothing to do ...
|
||||
}
|
||||
};
|
||||
|
||||
template<class MY_TYPE> class Hash
|
||||
{
|
||||
private:
|
||||
etk::Vector<HashData<MY_TYPE>* > m_data; //!< Data of the hash ==> the Hash table is composed of pointer, this permit to have high speed when resize the vestor ...
|
||||
public:
|
||||
Hash(int32_t _count=0) :
|
||||
m_data(_count)
|
||||
{
|
||||
|
||||
}
|
||||
~Hash(void)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
/**
|
||||
* @brief Remove all entry in the Hash table
|
||||
*/
|
||||
void Clear(void)
|
||||
{
|
||||
for (int32_t iii=0; iii<m_data.Size(); iii++) {
|
||||
if (m_data[iii] != NULL) {
|
||||
delete(m_data[iii]);
|
||||
m_data[iii]=NULL;
|
||||
}
|
||||
}
|
||||
m_data.Clear();
|
||||
}
|
||||
/**
|
||||
* @brief Get a current element ID in the Hash table
|
||||
* @param[in] _key Name of the hash requested
|
||||
* @return Id of the element in the table or -1 of it does not existed
|
||||
*/
|
||||
int64_t GetId(const etk::UString& _key) const
|
||||
{
|
||||
for (int32_t iii=0; iii<m_data.Size(); iii++) {
|
||||
if (m_data[iii] != NULL) {
|
||||
if (m_data[iii]->m_key == _key) {
|
||||
return iii;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
/**
|
||||
*@brief Check if an element exist or not
|
||||
* @param[in] _key Name of the hash requested
|
||||
* @return true if the element exist
|
||||
*/
|
||||
bool Exist(const etk::UString& _name)
|
||||
{
|
||||
int64_t elementId = GetId(_name);
|
||||
if (elementId<0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* @brief Get a current element in the vector
|
||||
* @param[in] _key Name of the hash requested
|
||||
* @return Reference on the Element
|
||||
*/
|
||||
MY_TYPE& Get(const etk::UString& _key) const
|
||||
{
|
||||
static MY_TYPE g_error;
|
||||
int64_t elementId = GetId(_key);
|
||||
if (elementId<0) {
|
||||
TK_ERROR("try to acces at an unexistant hash element : " << _key);
|
||||
return g_error;
|
||||
}
|
||||
return m_data[elementId]->m_value;
|
||||
}
|
||||
/**
|
||||
* @brief Get an copy Element an a special position
|
||||
* @param[in] _key Name of the hash requested
|
||||
* @return An reference on the copy of selected element
|
||||
*/
|
||||
MY_TYPE& operator[] (const etk::UString& _key)
|
||||
{
|
||||
return Get(_key);
|
||||
}
|
||||
const MY_TYPE& operator[] (const etk::UString& _key) const
|
||||
{
|
||||
return Get(_key);
|
||||
}
|
||||
|
||||
void Add(const etk::UString& _key, const MY_TYPE& _value)
|
||||
{
|
||||
int64_t elementId = GetId(_key);
|
||||
if (elementId <0) {
|
||||
HashData<MY_TYPE>* tmp = new HashData<MY_TYPE>(_key, _value);
|
||||
if (NULL == tmp) {
|
||||
TK_ERROR("allocation error in Hash table : '" << _key << "'");
|
||||
return;
|
||||
}
|
||||
m_data.PushBack(tmp);
|
||||
return;
|
||||
}
|
||||
m_data[elementId]->m_value = _value;
|
||||
}
|
||||
void Set(const etk::UString& _key, const MY_TYPE& _value)
|
||||
{
|
||||
Add(_key, _value);
|
||||
}
|
||||
void Remove(const etk::UString& _key)
|
||||
{
|
||||
int64_t elementId = GetId(_key);
|
||||
if (elementId <0) {
|
||||
//nothing to do ==> not existed
|
||||
return;
|
||||
}
|
||||
delete(m_data[elementId]);
|
||||
m_data[elementId] = NULL;
|
||||
m_data.Remove(elementId);
|
||||
}
|
||||
/**
|
||||
* @brief Get the number of element in the hash table
|
||||
* @return number of elements
|
||||
*/
|
||||
esize_t Size(void) const
|
||||
{
|
||||
return m_data.Size();
|
||||
}
|
||||
const etk::UString& GetKey(esize_t _pos) const
|
||||
{
|
||||
// NOTE :Do not change log level, this generate error only in debug mode
|
||||
#if DEBUG_LEVEL > 2
|
||||
if(_pos>m_data.Size()){
|
||||
TK_CRITICAL("[CRITICAL] Access to an unexistant data in hach : " << _pos << "/ " << m_data.Size());
|
||||
}
|
||||
#endif
|
||||
return m_data[_pos]->m_key;
|
||||
}
|
||||
const MY_TYPE& GetValue(esize_t _pos) const
|
||||
{
|
||||
// NOTE :Do not change log level, this generate error only in debug mode
|
||||
#if DEBUG_LEVEL > 2
|
||||
if(_pos>m_data.Size()){
|
||||
TK_CRITICAL("[CRITICAL] Access to an unexistant data in hach : " << _pos << "/ " << m_data.Size());
|
||||
}
|
||||
#endif
|
||||
return m_data[_pos]->m_value;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -13,6 +13,6 @@ def Create(target):
|
||||
|
||||
myModule.AddModuleDepend(['etk'])
|
||||
|
||||
# add the currrent module at the
|
||||
# add the currrent module at the system
|
||||
return myModule
|
||||
|
||||
|
@ -57,7 +57,21 @@ void testUString(void)
|
||||
|
||||
void testHash(void)
|
||||
{
|
||||
|
||||
TK_INFO("==> Start test of Hach table");
|
||||
etk::Hash<etk::UString> testData;
|
||||
testData.Add("TEST", "testData");
|
||||
testData.Add("TEST", "testData333");
|
||||
testData.Add("TEST2", "22222222222222222");
|
||||
testData.Add("TEST4", "4444444444444444444");
|
||||
testData.Add("TEST3", "3333333333");
|
||||
testData.Add("TEST1", "11111111111");
|
||||
testData.Add("TEST55", "555555555555555((((5555");
|
||||
TK_INFO(" count =" << testData.Size());
|
||||
for (int32_t iii=0; iii< testData.Size(); iii++) {
|
||||
TK_INFO(" id=" << iii << " key='" << testData.GetKey(iii) << "' val='" << testData.GetValue(iii) << "'");
|
||||
}
|
||||
TK_INFO(" direct acces at the key key='TEST4' val='" << testData["TEST4"] << "'");
|
||||
TK_INFO("==> End test of Hach table");
|
||||
}
|
||||
|
||||
void testFSNode(void)
|
||||
@ -127,11 +141,11 @@ int main(int argc, const char *argv[])
|
||||
{
|
||||
// the only one init for etk:
|
||||
GeneralDebugSetLevel(etk::LOG_LEVEL_VERBOSE);
|
||||
testVector();
|
||||
testUniChar();
|
||||
testUString();
|
||||
//testVector();
|
||||
//testUniChar();
|
||||
//testUString();
|
||||
testHash();
|
||||
testFSNode();
|
||||
//testFSNode();
|
||||
//testDimension();
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user