Hash.hpp
Go to the documentation of this file.
1 
7 #include <etk/types.hpp>
8 
9 #pragma once
10 
11 namespace etk {
17  template<class MY_TYPE> class HashData {
18  public:
19  std::string m_key;
20  MY_TYPE m_value;
21 
26  HashData(const std::string& _key, const MY_TYPE& _val) :
27  m_key(_key),
28  m_value(_val) {
29  // nothing to do ...
30  }
31  };
62  template<class MY_TYPE> class Hash {
63  private:
64  std::vector<HashData<MY_TYPE>* > m_data;
65  public:
70  Hash(int32_t _count = 0) :
71  m_data(_count) {
72  // nothing to do
73  }
77  ~Hash() {
78  clear();
79  }
84  void clear() {
85  for (auto &it : m_data) {
86  if (it != nullptr) {
87  delete(it);
88  it=nullptr;
89  }
90  }
91  m_data.clear();
92  }
98  int64_t getId(const std::string& _key) const {
99  for (size_t iii=0; iii<m_data.size(); iii++) {
100  if (m_data[iii] != nullptr) {
101  //TK_INFO("Compare key : '" << m_data[iii]->m_key << "' with '" << _key << "'" );
102  if (m_data[iii]->m_key == _key) {
103  return iii;
104  }
105  }
106  }
107  //TK_ERROR(" ==> not fund key '" << _key << "'" );
108  return -1;
109  }
115  bool exist(const std::string& _name) const {
116  int64_t elementId = getId(_name);
117  //TK_INFO(" Exist ? '" << _name << "' id=" << elementId );
118  if (elementId<0) {
119  //TK_INFO(" ==> return false" );
120  return false;
121  }
122  //TK_INFO(" ==> return true" );
123  return true;
124  }
130  MY_TYPE& get(const std::string& _key) const {
131  static MY_TYPE g_error;
132  int64_t elementId = getId(_key);
133  if (elementId<0) {
134  //TK_ERROR("try to acces at an unexistant hash element : " << _key);
135  return g_error;
136  }
137  return m_data[elementId]->m_value;
138  }
144  MY_TYPE& operator[] (const std::string& _key) {
145  return get(_key);
146  }
152  const MY_TYPE& operator[] (const std::string& _key) const {
153  return get(_key);
154  }
161  void add(const std::string& _key, const MY_TYPE& _value) {
162  int64_t elementId = getId(_key);
163  if (elementId <0) {
164  HashData<MY_TYPE>* tmp = new HashData<MY_TYPE>(_key, _value);
165  if (tmp == nullptr) {
166  //TK_ERROR("allocation error in Hash table : '" << _key << "'");
167  return;
168  }
169  m_data.push_back(tmp);
170  return;
171  }
172  m_data[elementId]->m_value = _value;
173  }
180  void set(const std::string& _key, const MY_TYPE& _value) {
181  add(_key, _value);
182  }
187  void remove(const std::string& _key) {
188  int64_t elementId = getId(_key);
189  if (elementId <0) {
190  //nothing to do ==> not existed
191  return;
192  }
193  delete(m_data[elementId]);
194  m_data[elementId] = nullptr;
195  m_data.erase(m_data.begin()+elementId);
196  }
201  int32_t size() const {
202  return m_data.size();
203  }
210  MY_TYPE& operator[] (size_t _pos) {
211  return getValue(_pos);
212  }
219  const MY_TYPE& operator[] (size_t _pos) const {
220  return getValue(_pos);
221  }
227  const std::string& getKey(size_t _pos) const {
228  // NOTE :Do not change log level, this generate error only in debug mode
229  #if DEBUG_LEVEL > 2
230  if(_pos>m_data.size()){
231  //TK_CRITICAL("Access to an unexistant data in hach : " << _pos << "/ " << m_data.size());
232  }
233  #endif
234  return m_data[_pos]->m_key;
235  }
240  std::vector<std::string> getKeys() const {
241  std::vector<std::string> keys;
242  for (auto &it : m_data) {
243  if (it != nullptr) {
244  keys.push_back(it->m_key);
245  }
246  }
247  return keys;
248  }
254  const MY_TYPE& getValue(size_t _pos) const {
255  // NOTE :Do not change log level, this generate error only in debug mode
256  #if DEBUG_LEVEL > 2
257  if(_pos>m_data.size()){
258  //TK_CRITICAL("Access to an unexistant data in hach : " << _pos << "/ " << m_data.size());
259  }
260  #endif
261  return m_data[_pos]->m_value;
262  }
266  MY_TYPE& getValue(size_t _pos) {
267  // NOTE :Do not change log level, this generate error only in debug mode
268  #if DEBUG_LEVEL > 2
269  if(_pos>m_data.size()){
270  //TK_CRITICAL("Access to an unexistant data in hach : " << _pos << "/ " << m_data.size());
271  }
272  #endif
273  return m_data[_pos]->m_value;
274  }
275  };
276 }
277 
Hash(int32_t _count=0)
Contructor of the Hach table.
Definition: Hash.hpp:70
int32_t size() const
Get the number of element in the hash table.
Definition: Hash.hpp:201
void clear()
Remove all entry in the Hash table.
Definition: Hash.hpp:84
std::string m_key
name of the current hash
Definition: Hash.hpp:19
basic namespace of the etk library. (it might contain all the etk fuctions/class/structures without m...
Definition: Archive.hpp:16
MY_TYPE m_value
data of the current Hash
Definition: Hash.hpp:20
bool exist(const std::string &_name) const
Check if an element exist or not.
Definition: Hash.hpp:115
int64_t getId(const std::string &_key) const
Get a current element ID in the Hash table.
Definition: Hash.hpp:98
void clear()
Clear all the matrix.
Definition: Matrix.hpp:520
MY_TYPE & getValue(size_t _pos)
Get a value of the hash table at a specific position. (size_t)
Definition: Hash.hpp:266
void add(const std::string &_key, const MY_TYPE &_value)
Add an element OR set an element value.
Definition: Hash.hpp:161
HashData(const std::string &_key, const MY_TYPE &_val)
Constructor of the data for hash table.
Definition: Hash.hpp:26
~Hash()
Destructor of the Hash table(clear all element in the table)
Definition: Hash.hpp:77
std::vector< std::string > getKeys() const
Get all the element name (keys).
Definition: Hash.hpp:240
Hash table tamplate is a simple classical hash interface. A hash table is a equivalent of the diction...
Definition: Hash.hpp:62
internel data of the [class[etk::hash]] class, it contain the name and the value of the hash vector...
Definition: Hash.hpp:17
const MY_TYPE & getValue(size_t _pos) const
Get a value of the hash table at a specific position.
Definition: Hash.hpp:254
const std::string & getKey(size_t _pos) const
Get the name of the element at a specific position.
Definition: Hash.hpp:227