#ifndef __JMAP__ #define __JMAP__ #include #include "JTools/JCollection.hh" #include "JIO/JSerialisable.hh" namespace JTOOLS { namespace { using JIO::JReader; using JIO::JWriter; } /** * Auxiliary class for type definitions of an element of STL map. */ template class JElement { public: typedef std::map > JContainer_t; typedef typename JContainer_t::value_type value_type; typedef typename JContainer_t::key_type key_type; typedef typename JContainer_t::mapped_type mapped_type; }; /** * Auxiliary class for map with one template parameter. */ template class JMapCollection : public std::map > {}; /** * Template map implementation of collection of elements. */ template class JMap : protected JMapCollection< JElement >, public virtual JAbstractCollection, JMapCollection> { public: typedef JElement JElement_t; typedef JMapCollection JContainer_t; typedef JAbstractCollection JCollection_t; typedef typename JCollection_t::key_type key_type; typedef typename JCollection_t::mapped_type mapped_type; typedef typename JCollection_t::value_type value_type; typedef typename JCollection_t::const_iterator const_iterator; typedef typename JCollection_t::const_reverse_iterator const_reverse_iterator; typedef typename JCollection_t::iterator iterator; typedef typename JCollection_t::reverse_iterator reverse_iterator; typedef typename JCollection_t::pair pair; typedef typename JCollection_t::transformer_type transformer_type; /** * Default constructor. */ JMap() : JCollection_t(), JContainer_t () {} virtual const_iterator begin() const { return JContainer_t::begin(); } virtual const_iterator end() const { return JContainer_t::end(); } virtual const_reverse_iterator rbegin() const { return JContainer_t::rbegin(); } virtual const_reverse_iterator rend() const { return JContainer_t::rend(); } virtual iterator begin() { return JContainer_t::begin(); } virtual iterator end() { return JContainer_t::end(); } virtual reverse_iterator rbegin() { return JContainer_t::rbegin(); } virtual reverse_iterator rend() { return JContainer_t::rend(); } virtual size_t size() const { return JContainer_t::size(); } virtual bool empty() const { return JContainer_t::empty(); } virtual void clear() { return JContainer_t::clear(); } /** * Value assignment. * * \param x key * \return value */ virtual mapped_type& operator[](const key_type& x) { return JContainer_t::operator[](x); } /** * Insert element. * * \param element element */ virtual pair insert(const value_type& element) { return JContainer_t::insert(element); } /** * Get first position of element i, where x >= i->getKey(). * * \param x key * \return position of corresponding element */ const_iterator lower_bound(const key_type& x) const { return JContainer_t::lower_bound(x); } /** * Get first position of element i, where x >= i->getKey(). * * \param x key * \return position of corresponding element */ iterator lower_bound(const key_type& x) { return JContainer_t::lower_bound(x); } /** * Read template JMap from input. * * \param in JReader * \param buffer JMap * \return JReader */ friend inline JReader& operator>>(JReader& in, JMap& buffer) { int n; in >> n; JKey_t key; for (int i = 0; i != n; ++i) { in >> key; in >> buffer[key]; } return in; } /** * Write template JMap to output. * * \param out JWriter * \param buffer JMap * \return JWriter */ friend inline JWriter& operator<<(JWriter& out, const JMap& buffer) { int n = buffer.size(); out << n; for (typename JMap::const_iterator i = buffer.begin(); i != buffer.end(); ++i) { out << i->first; out << i->second; } return out; } }; } #endif