#ifndef __JSET__ #define __JSET__ #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 set with one template parameter. */ template class JSetCollection : public std::set > {}; /** * Template set implementation of collection of elements. */ template class JSet : protected JSetCollection, 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 JComparator JComparator_t; typedef JSetCollection 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. */ JSet() : 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 = JContainer_t::lower_bound(x); if (i == JContainer_t::end() || operator()(x,*i)) i = JContainer_t::insert(i, value_type(x)); return const_cast(i->getValue()); } /** * 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(value_type(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(value_type(x)); } /** * Read template JSet from input. * * \param in JReader * \param buffer JSet * \return JReader */ friend inline JReader& operator>>(JReader& in, JSet& buffer) { int n; in >> n; typename JSet::value_type element; for (int i = 0; i != n; ++i) { in >> element; buffer.insert(element); } return in; } /** * Write template JSet to output. * * \param out JWriter * \param buffer JSet * \return JWriter */ friend inline JWriter& operator<<(JWriter& out, const JSet& buffer) { int n = buffer.size(); out << n; for (typename JSet::const_iterator i = buffer.begin(); i != buffer.end(); ++i) out << *i; return out; } }; /** * Template set implementation of map. */ template class JSetMap : public JSet< JMapElement > { public: typedef JKey_t key_type; typedef JValue_t mapped_type; typedef JMapElement value_type; typedef JSet 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. */ JSetMap() : JSet< JMapElement >() {} }; } #endif