[Lemon-commits] Alpar Juttner: Merge
Lemon HG
hg at lemon.cs.elte.hu
Mon Aug 31 08:27:15 CEST 2009
details: http://lemon.cs.elte.hu/hg/lemon/rev/8dae88c5943e
changeset: 748:8dae88c5943e
user: Alpar Juttner <alpar [at] cs.elte.hu>
date: Mon Aug 31 08:25:33 2009 +0200
description:
Merge
diffstat:
lemon/maps.h | 910 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
test/maps_test.cc | 193 +++++++++++-
2 files changed, 1093 insertions(+), 10 deletions(-)
diffs (truncated from 1188 to 300 lines):
diff --git a/lemon/maps.h b/lemon/maps.h
--- a/lemon/maps.h
+++ b/lemon/maps.h
@@ -22,6 +22,7 @@
#include <iterator>
#include <functional>
#include <vector>
+#include <map>
#include <lemon/core.h>
@@ -29,8 +30,6 @@
///\ingroup maps
///\brief Miscellaneous property maps
-#include <map>
-
namespace lemon {
/// \addtogroup maps
@@ -1818,7 +1817,7 @@
/// \brief Provides an immutable and unique id for each item in a graph.
///
/// IdMap provides a unique and immutable id for each item of the
- /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
+ /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
/// - \b unique: different items get different ids,
/// - \b immutable: the id of an item does not change (even if you
/// delete other nodes).
@@ -2273,7 +2272,7 @@
}
/// \brief Gives back the item belonging to a \e RangeId
- ///
+ ///
/// Gives back the item belonging to a \e RangeId.
Item operator()(int id) const {
return _inv_map[id];
@@ -2330,6 +2329,903 @@
}
};
+ /// \brief Dynamic iterable \c bool map.
+ ///
+ /// This class provides a special graph map type which can store a
+ /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
+ /// For both \c true and \c false values it is possible to iterate on
+ /// the keys.
+ ///
+ /// This type is a reference map, so it can be modified with the
+ /// subscript operator.
+ ///
+ /// \tparam GR The graph type.
+ /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
+ /// \c GR::Edge).
+ ///
+ /// \see IterableIntMap, IterableValueMap
+ /// \see CrossRefMap
+ template <typename GR, typename K>
+ class IterableBoolMap
+ : protected ItemSetTraits<GR, K>::template Map<int>::Type {
+ private:
+ typedef GR Graph;
+
+ typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
+ typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
+
+ std::vector<K> _array;
+ int _sep;
+
+ public:
+
+ /// Indicates that the map is reference map.
+ typedef True ReferenceMapTag;
+
+ /// The key type
+ typedef K Key;
+ /// The value type
+ typedef bool Value;
+ /// The const reference type.
+ typedef const Value& ConstReference;
+
+ private:
+
+ int position(const Key& key) const {
+ return Parent::operator[](key);
+ }
+
+ public:
+
+ /// \brief Reference to the value of the map.
+ ///
+ /// This class is similar to the \c bool type. It can be converted to
+ /// \c bool and it provides the same operators.
+ class Reference {
+ friend class IterableBoolMap;
+ private:
+ Reference(IterableBoolMap& map, const Key& key)
+ : _key(key), _map(map) {}
+ public:
+
+ Reference& operator=(const Reference& value) {
+ _map.set(_key, static_cast<bool>(value));
+ return *this;
+ }
+
+ operator bool() const {
+ return static_cast<const IterableBoolMap&>(_map)[_key];
+ }
+
+ Reference& operator=(bool value) {
+ _map.set(_key, value);
+ return *this;
+ }
+ Reference& operator&=(bool value) {
+ _map.set(_key, _map[_key] & value);
+ return *this;
+ }
+ Reference& operator|=(bool value) {
+ _map.set(_key, _map[_key] | value);
+ return *this;
+ }
+ Reference& operator^=(bool value) {
+ _map.set(_key, _map[_key] ^ value);
+ return *this;
+ }
+ private:
+ Key _key;
+ IterableBoolMap& _map;
+ };
+
+ /// \brief Constructor of the map with a default value.
+ ///
+ /// Constructor of the map with a default value.
+ explicit IterableBoolMap(const Graph& graph, bool def = false)
+ : Parent(graph) {
+ typename Parent::Notifier* nf = Parent::notifier();
+ Key it;
+ for (nf->first(it); it != INVALID; nf->next(it)) {
+ Parent::set(it, _array.size());
+ _array.push_back(it);
+ }
+ _sep = (def ? _array.size() : 0);
+ }
+
+ /// \brief Const subscript operator of the map.
+ ///
+ /// Const subscript operator of the map.
+ bool operator[](const Key& key) const {
+ return position(key) < _sep;
+ }
+
+ /// \brief Subscript operator of the map.
+ ///
+ /// Subscript operator of the map.
+ Reference operator[](const Key& key) {
+ return Reference(*this, key);
+ }
+
+ /// \brief Set operation of the map.
+ ///
+ /// Set operation of the map.
+ void set(const Key& key, bool value) {
+ int pos = position(key);
+ if (value) {
+ if (pos < _sep) return;
+ Key tmp = _array[_sep];
+ _array[_sep] = key;
+ Parent::set(key, _sep);
+ _array[pos] = tmp;
+ Parent::set(tmp, pos);
+ ++_sep;
+ } else {
+ if (pos >= _sep) return;
+ --_sep;
+ Key tmp = _array[_sep];
+ _array[_sep] = key;
+ Parent::set(key, _sep);
+ _array[pos] = tmp;
+ Parent::set(tmp, pos);
+ }
+ }
+
+ /// \brief Set all items.
+ ///
+ /// Set all items in the map.
+ /// \note Constant time operation.
+ void setAll(bool value) {
+ _sep = (value ? _array.size() : 0);
+ }
+
+ /// \brief Returns the number of the keys mapped to \c true.
+ ///
+ /// Returns the number of the keys mapped to \c true.
+ int trueNum() const {
+ return _sep;
+ }
+
+ /// \brief Returns the number of the keys mapped to \c false.
+ ///
+ /// Returns the number of the keys mapped to \c false.
+ int falseNum() const {
+ return _array.size() - _sep;
+ }
+
+ /// \brief Iterator for the keys mapped to \c true.
+ ///
+ /// Iterator for the keys mapped to \c true. It works
+ /// like a graph item iterator, it can be converted to
+ /// the key type of the map, incremented with \c ++ operator, and
+ /// if the iterator leaves the last valid key, it will be equal to
+ /// \c INVALID.
+ class TrueIt : public Key {
+ public:
+ typedef Key Parent;
+
+ /// \brief Creates an iterator.
+ ///
+ /// Creates an iterator. It iterates on the
+ /// keys mapped to \c true.
+ /// \param map The IterableBoolMap.
+ explicit TrueIt(const IterableBoolMap& map)
+ : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
+ _map(&map) {}
+
+ /// \brief Invalid constructor \& conversion.
+ ///
+ /// This constructor initializes the iterator to be invalid.
+ /// \sa Invalid for more details.
+ TrueIt(Invalid) : Parent(INVALID), _map(0) {}
+
+ /// \brief Increment operator.
+ ///
+ /// Increment operator.
+ TrueIt& operator++() {
+ int pos = _map->position(*this);
+ Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
+ return *this;
+ }
+
+ private:
+ const IterableBoolMap* _map;
+ };
+
+ /// \brief Iterator for the keys mapped to \c false.
+ ///
+ /// Iterator for the keys mapped to \c false. It works
+ /// like a graph item iterator, it can be converted to
+ /// the key type of the map, incremented with \c ++ operator, and
+ /// if the iterator leaves the last valid key, it will be equal to
+ /// \c INVALID.
+ class FalseIt : public Key {
+ public:
+ typedef Key Parent;
+
+ /// \brief Creates an iterator.
+ ///
+ /// Creates an iterator. It iterates on the
+ /// keys mapped to \c false.
+ /// \param map The IterableBoolMap.
+ explicit FalseIt(const IterableBoolMap& map)
+ : Parent(map._sep < int(map._array.size()) ?
+ map._array.back() : INVALID), _map(&map) {}
+
+ /// \brief Invalid constructor \& conversion.
+ ///
+ /// This constructor initializes the iterator to be invalid.
+ /// \sa Invalid for more details.
+ FalseIt(Invalid) : Parent(INVALID), _map(0) {}
+
+ /// \brief Increment operator.
+ ///
+ /// Increment operator.
+ FalseIt& operator++() {
+ int pos = _map->position(*this);
+ Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
+ return *this;
+ }
+
+ private:
+ const IterableBoolMap* _map;
+ };
+
+ /// \brief Iterator for the keys mapped to a given value.
+ ///
+ /// Iterator for the keys mapped to a given value. It works
+ /// like a graph item iterator, it can be converted to
+ /// the key type of the map, incremented with \c ++ operator, and
+ /// if the iterator leaves the last valid key, it will be equal to
+ /// \c INVALID.
+ class ItemIt : public Key {
+ public:
+ typedef Key Parent;
+
+ /// \brief Creates an iterator with a value.
+ ///
+ /// Creates an iterator with a value. It iterates on the
+ /// keys mapped to the given value.
+ /// \param map The IterableBoolMap.
+ /// \param value The value.
More information about the Lemon-commits
mailing list