Commit 0685530e authored by Nicolas CHEVAUGEON's avatar Nicolas CHEVAUGEON

[DataManager] added functionalities to the DataManager famillies.

class concerned :
xAttachedDataManagerAOMDGeneric
xGeneralUnorderedMapDataManager
xUnorderedMapDataManager

- added public type alias :
  using value_type = DATATYPE;
  using key_type = KEYTYPE;
  Rational : usefull for generic programming with datamanagers.

- added member function contains :
bool contains(const KEYTYPE &key) const;
return true if the key is in the datamanager.
rational : when need to know if a key exist in the data manager, calling contains is easier and faster than call getData(key) and check if null.
The name contains is  standard in stl associative container starting at c++20. It complement the old count.

- added member function rangeKey :
xtool::xRange<c_iterKey_t> rangeKey() const;
return the range of key in the container.
rational : avoid calling beginKey() and endKey() in some contexts. Make Datamanagers in sync with other containers of Xfiles.
parent 5f4ffd7b
......@@ -8,6 +8,7 @@
#include "mAOMD.h"
#include "mAttachableDataContainer.h"
#include "mEntity.h"
#include "xIteratorTools.h"
namespace xinterface
{
......@@ -22,6 +23,8 @@ template <typename KEYTYPE, typename DATATYPE>
class xAttachedDataManagerAOMDGeneric
{
public:
using value_type = DATATYPE;
using key_type = KEYTYPE;
typedef std::list<KEYTYPE *> key_container_t;
typedef typename key_container_t::const_iterator c_iterKey_t;
inline xAttachedDataManagerAOMDGeneric();
......@@ -34,6 +37,8 @@ class xAttachedDataManagerAOMDGeneric
inline DATATYPE *getData(const KEYTYPE &e);
inline const DATATYPE &at(const KEYTYPE &key) const;
inline DATATYPE &at(const KEYTYPE &key);
/// return true if the key exist in the DataManager
inline bool contains(const KEYTYPE &key) const;
/*! return reference to object DATATYPE associated to e. If already exist return the existing one,
otherwise default construct a DATATYPE and associate it to e, before returning the ref
*/
......@@ -51,6 +56,9 @@ class xAttachedDataManagerAOMDGeneric
inline c_iterKey_t beginKey() const;
inline c_iterKey_t endKey() const;
/// return the range of keys known by the container
inline xtool::xRange<c_iterKey_t> rangeKey() const;
inline bool empty() const { return garbage.empty(); }
inline size_t size() const { return garbage.size(); }
......@@ -144,6 +152,12 @@ inline DATATYPE *xAttachedDataManagerAOMDGeneric<KEYTYPE, DATATYPE>::getData(con
return nullptr;
}
template <typename KEYTYPE, typename DATATYPE>
inline bool xAttachedDataManagerAOMDGeneric<KEYTYPE, DATATYPE>::contains(const KEYTYPE &key) const
{
return key.getData(tag);
}
/// return a const ref to the data associated to key or throw if there is no data associated to key
template <typename KEYTYPE, typename DATATYPE>
inline const DATATYPE &xAttachedDataManagerAOMDGeneric<KEYTYPE, DATATYPE>::at(const KEYTYPE &key) const
......@@ -255,6 +269,13 @@ inline auto xAttachedDataManagerAOMDGeneric<KEYTYPE, DATATYPE>::endKey() const -
else
return garbage.cend();
}
template <typename KEYTYPE, typename DATATYPE>
inline auto xAttachedDataManagerAOMDGeneric<KEYTYPE, DATATYPE>::rangeKey() const -> xtool::xRange<c_iterKey_t>
{
return xtool::xRange<c_iterKey_t>(beginKey(), endKey());
}
template <typename KEYTYPE, typename DATATYPE>
inline void xAttachedDataManagerAOMDGeneric<KEYTYPE, DATATYPE>::setTag()
{
......
......@@ -9,6 +9,8 @@
#include <iterator>
#include <unordered_map>
#include "xIteratorTools.h"
namespace xtool
{
/// \brief This class is a template implementation of an abstract DATAMANAGER interface (or concept) dedicated to key that
......@@ -21,14 +23,18 @@ namespace xtool
/// concept of attaching an information to a fictitious object remain. A key define a unique fictitious object that will uniquely
/// be identified by the xGeneralUnorderedMapDataManager not mater that used concrete object are different.
//
template <typename KEY, typename DATATYPE, typename HASH_KEY = std::hash<KEY>, typename EQUAL_KEY = std::equal_to<KEY>>
template <typename KEYTYPE, typename DATATYPE, typename HASH_KEYTYPE = std::hash<KEYTYPE>,
typename EQUAL_KEYTYPE = std::equal_to<KEYTYPE>>
class xGeneralUnorderedMapDataManager
{
private:
typedef std::unordered_map<KEY, DATATYPE *, HASH_KEY, EQUAL_KEY> data_container_t;
typedef std::unordered_map<KEYTYPE, DATATYPE *, HASH_KEYTYPE, EQUAL_KEYTYPE> data_container_t;
data_container_t data_map;
public:
using value_type = DATATYPE;
using key_type = KEYTYPE;
/// Default constructor
inline xGeneralUnorderedMapDataManager() = default;
/// Copy constructor
......@@ -42,7 +48,7 @@ class xGeneralUnorderedMapDataManager
/// create a new data associated to key, and return a reference to it. If a data associated to key already exist in the
/// xGeneralUnorderedMapDataManager instance, return a reference to the existing data. (Note: the semantic is the same as the
/// operator[] of stl's associative container )
inline DATATYPE &setData(const KEY &key)
inline DATATYPE &setData(const KEYTYPE &key)
{
DATATYPE *data = getData(key);
if (!data)
......@@ -56,7 +62,7 @@ class xGeneralUnorderedMapDataManager
otherwise construct a DATATYPE by calling new DATATYPE(a) and associate it to e, before returning the ref
*/
template <class... A>
inline DATATYPE &emplaceSetData(const KEY &key, A &&... a)
inline DATATYPE &emplaceSetData(const KEYTYPE &key, A &&... a)
{
DATATYPE *data = getData(key);
if (!data)
......@@ -67,7 +73,7 @@ class xGeneralUnorderedMapDataManager
return *data;
}
/// return a const pointer to the data associated to key
inline const DATATYPE *getData(const KEY &key) const
inline const DATATYPE *getData(const KEYTYPE &key) const
{
const DATATYPE *data = nullptr;
auto it = data_map.find(key);
......@@ -75,7 +81,7 @@ class xGeneralUnorderedMapDataManager
return data;
}
/// return a pointer to the data associated to key
inline DATATYPE *getData(const KEY &key)
inline DATATYPE *getData(const KEYTYPE &key)
{
DATATYPE *data = nullptr;
auto it = data_map.find(key);
......@@ -83,12 +89,18 @@ class xGeneralUnorderedMapDataManager
return data;
}
/// return a const ref to the data associated to key or throw if there is no data associated to key
inline const DATATYPE &at(const KEY &key) const { return *data_map.at(const_cast<KEY *>(&key)); }
inline const DATATYPE &at(const KEYTYPE &key) const { return *data_map.at(const_cast<KEYTYPE *>(&key)); }
/// return a ref to the data associated to key or throw if there is no data associated to key
inline DATATYPE &at(const KEY &key) { return *data_map.at(const_cast<KEY *>(&key)); }
inline DATATYPE &at(const KEYTYPE &key) { return *data_map.at(&key); }
/// return true if the key exist in the DataManager
inline bool contains(const KEYTYPE &key) const
{
return data_map.count(&key);
//! note : could use .contains in c++20.
}
/// delete data associated to key. if no data is associated to key, deleteData does nothing.
/// all references to the data associated to key keeped by the user become invalide
inline void deleteData(const KEY &key)
inline void deleteData(const KEYTYPE &key)
{
auto it = data_map.find(key);
if (it != data_map.end())
......@@ -157,6 +169,7 @@ class xGeneralUnorderedMapDataManager
auto it = data_map.end();
return c_iterKey_t(it);
}
inline xRange<c_iterKey_t> rangeKey() const { return xRange<c_iterKey_t>(beginKey(), endKey()); }
inline bool empty() const { return data_map.empty(); }
inline size_t size() const { return data_map.size(); }
};
......
......@@ -12,6 +12,8 @@
#include <sstream>
#include <unordered_map>
#include "xIteratorTools.h"
namespace xtool
{
/// \brief This class is an implementation of an abstract DATAMANAGER interface (or concept).
......@@ -43,7 +45,7 @@ namespace xtool
/// xinterface::aomd::xAttachedDataManagerAOMD.h) \n Note that this implementation store pointers to the keys. 2 keys are
/// considered equal if they have the same address. It means that a copy of a key is NOT the same key from the
/// xUnorderedMapDataManager point of view.
template <class KEYTYPE, class DATATYPE>
template <typename KEYTYPE, typename DATATYPE>
class xUnorderedMapDataManager
{
private:
......@@ -51,6 +53,9 @@ class xUnorderedMapDataManager
data_container_t data_map;
public:
using value_type = DATATYPE;
using key_type = KEYTYPE;
/// Default constructor
inline xUnorderedMapDataManager() = default;
/// Copy constructor
......@@ -109,9 +114,16 @@ class xUnorderedMapDataManager
/// return a const ref to the data associated to key or throw if there is no data associated to key
inline const DATATYPE &at(const KEYTYPE &key) const { return *data_map.at(const_cast<KEYTYPE *>(&key)); }
/// return a ref to the data associated to key or throw if there is no data associated to key
inline DATATYPE &at(const KEYTYPE &key) { return *data_map.at(const_cast<KEYTYPE *>(&key)); }
inline DATATYPE &at(const KEYTYPE &key) { return *data_map.at(&key); }
/// return true if the key exist in the DataManager
inline bool contains(const KEYTYPE &key) const
{
return data_map.count(&key);
//! note : could use .contains in c++20.
}
/// delete data associated to key. if no data is associated to key, deleteData does nothing.
/// all references to the data associated to key keeped by the user become invalide
inline void deleteData(const KEYTYPE &key)
{
auto it = data_map.find(const_cast<KEYTYPE *>(&key));
......@@ -181,6 +193,8 @@ class xUnorderedMapDataManager
auto it = data_map.end();
return c_iterKey_t(it);
}
/// return the range of keys known by the container
inline xRange<c_iterKey_t> rangeKey() const { return xRange<c_iterKey_t>(beginKey(), endKey()); }
inline bool empty() const { return data_map.empty(); }
inline size_t size() const { return data_map.size(); }
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment