/** \file dynamic_set_impl.h */ // -*-c++-*- // Copyright (C) 2010 Daniel Burrows // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; see the file COPYING. If not, write to // the Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. #ifndef DYNAMIC_SET_IMPL_H #define DYNAMIC_SET_IMPL_H #include "dynamic_set.h" #include "enumerator.h" #include #include #include #include #include namespace aptitude { namespace util { /** \brief A dynamic set based on hash tables. * * Entries in the set must be compatible with boost::hash * and must support operator==. */ template class dynamic_set_impl : public boost::enable_shared_from_this >, public writable_dynamic_set { boost::unordered_set values; sigc::signal signal_inserted; sigc::signal signal_removed; public: /** \warning Should only be invoked by make_shared(). */ dynamic_set_impl(); /** \warning Should only be invoked by make_shared(). */ template dynamic_set_impl(const InputIter &begin, const InputIter &end); /** \brief Create an empty dynamic set. */ static boost::shared_ptr create(); /** \brief Create a dynamic set from a range of values. */ template static boost::shared_ptr create(const InputIter &begin, const InputIter &end); void insert(const T &t); void remove(const T &t); std::size_t size(); boost::shared_ptr > enumerate(); sigc::connection connect_inserted(const sigc::slot &slot); sigc::connection connect_removed(const sigc::slot &slot); }; template dynamic_set_impl::dynamic_set_impl() { } template template dynamic_set_impl::dynamic_set_impl(const InputIter &begin, const InputIter &end) : values(begin, end) { } template boost::shared_ptr > dynamic_set_impl::create() { return boost::make_shared(); } template template boost::shared_ptr > dynamic_set_impl::create(const InputIter &begin, const InputIter &end) { return boost::make_shared(begin, end); } template void dynamic_set_impl::insert(const T &t) { std::pair::iterator, bool> insert_result = values.insert(t); if(insert_result.second) signal_inserted(t); } template void dynamic_set_impl::remove(const T &t) { const std::size_t num_erased = values.erase(t); if(num_erased > 0) signal_removed(t); } template std::size_t dynamic_set_impl::size() { return values.size(); } template boost::shared_ptr > dynamic_set_impl::enumerate() { typedef typename boost::unordered_set::const_iterator Iter; typedef iterator_enumerator_with_keepalive > Enum; return boost::make_shared(values.begin(), values.end(), this->shared_from_this()); } template sigc::connection dynamic_set_impl::connect_inserted(const sigc::slot &slot) { return signal_inserted.connect(slot); } template sigc::connection dynamic_set_impl::connect_removed(const sigc::slot &slot) { return signal_removed.connect(slot); } } } #endif // DYNAMIC_SET_IMPL_H