#ifndef __JVECTOR__ #define __JVECTOR__ #include #include #include "JTools/JCollection.hh" #include "JTools/JMapElement.hh" #include "JIO/JSerialisable.hh" namespace JTOOLS { namespace { using JIO::JReader; using JIO::JWriter; } /** * Auxiliary class for vector with one template parameter. */ template class JVectorCollection : public std::vector {}; /** * Template vector implementation of collection of elements. */ template class JVector : protected JVectorCollection, public virtual JAbstractCollection { public: typedef typename JElement_t::key_type key_type; typedef typename JElement_t::mapped_type mapped_type; typedef typename JElement_t::value_type value_type; typedef JVectorCollection JContainer_t; typedef JAbstractCollection JCollection_t; 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; /** * Default constructor. */ JVector() : 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) { typename JContainer_t::iterator i = std::lower_bound(JContainer_t::begin(), JContainer_t::end(), x, this->getComparator()); if (i == JContainer_t::end() || operator()(x,*i)) i = JContainer_t::insert(i, value_type(x)); return i->getValue(); } /** * Insert element. * * \param element element */ virtual pair insert(const value_type& element) { typename JContainer_t::iterator i = std::lower_bound(JContainer_t::begin(), JContainer_t::end(), element, this->getComparator()); if (i == JContainer_t::end() || operator()(element,*i)) { i = JContainer_t::insert(i, element); return pair(i,true); } else return pair(JContainer_t::end(), false); } /** * Get first position of element i, where x >= i->getKey(). * * \param x key * \return position of corresponding element */ virtual const_iterator lower_bound(const key_type& x) const { return std::lower_bound(JContainer_t::begin(), JContainer_t::end(), x, this->getComparator()); } /** * Get first position of element i, where x >= i->getKey(). * * \param x key * \return position of corresponding element */ virtual iterator lower_bound(const key_type& x) { return std::lower_bound(JContainer_t::begin(), JContainer_t::end(), x, this->getComparator()); } /** * Read template JVector from input. * * \param in JReader * \param buffer JVector * \return JReader */ friend inline JReader& operator>>(JReader& in, JVector& buffer) { int n; in >> n; buffer.resize(n); for (typename JVector::iterator i = buffer.begin(); i != buffer.end(); ++i) in >> *i; return in; } /** * Write template JVector to output. * * \param out JWriter * \param buffer JVector * \return JWriter */ friend inline JWriter& operator<<(JWriter& out, const JVector& buffer) { int n = buffer.size(); out << n; for (typename JVector::const_iterator i = buffer.begin(); i != buffer.end(); ++i) out << *i; return out; } }; /** * Template vector implementation of map. */ template class JVectorMap : public JVector< JMapElement > { public: typedef JKey_t key_type; typedef JValue_t mapped_type; typedef JMapElement value_type; typedef JVector JCollection_t; 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; /** * Default constructor. */ JVectorMap() : JVector< JMapElement >() {} }; } #endif