# HG changeset patch # User deba # Date 1111793517 0 # Node ID a93f94dbe3d30c141caebec086cf1f8b71362ca6 # Parent 74d616d081f0a4ca5f785a8f6a8148c9434305ef First version of iterable maps. diff -r 74d616d081f0 -r a93f94dbe3d3 src/lemon/array_map.h --- a/src/lemon/array_map.h Fri Mar 25 22:11:28 2005 +0000 +++ b/src/lemon/array_map.h Fri Mar 25 23:31:57 2005 +0000 @@ -18,6 +18,7 @@ #define LEMON_ARRAY_MAP_H #include +#include ///\ingroup graphmaps ///\file @@ -35,16 +36,16 @@ * the map. This map factory uses the allocators to implement * the container functionality. * - * The template parameter is the MapRegistry that the maps + * The template parameter is the AlterationNotifier that the maps * will belong to and the Value. */ template class ArrayMap : public AlterationNotifier<_Item>::ObserverBase { + typedef _Item Item; public: /// The graph type of the maps. @@ -54,29 +55,11 @@ typedef AlterationNotifier<_Item> Registry; - private: - /// The iterator to iterate on the keys. - typedef _ItemIt KeyIt; - /// The MapBase of the Map which imlements the core regisitry function. typedef typename Registry::ObserverBase Parent; - - public: - /// The value type of the map. typedef _Value Value; - /// The reference type of the map; - typedef Value& Reference; - /// The pointer type of the map; - typedef Value* Pointer; - - /// The const value type of the map. - typedef const Value ConstValue; - /// The const reference type of the map; - typedef const Value& ConstReference; - /// The pointer type of the map; - typedef const Value* ConstPointer; private: @@ -88,9 +71,10 @@ /** Graph and Registry initialized map constructor. */ ArrayMap(const Graph& _g) : graph(&_g) { - attach(_g.getNotifier(_Item())); + Item it; + attach(_g.getNotifier(Item())); allocate_memory(); - for (KeyIt it(*graph); it != INVALID; ++it) { + for (graph->first(it); it != INVALID; graph->next(it)) { int id = graph->id(it);; allocator.construct(&(values[id]), Value()); } @@ -101,9 +85,10 @@ /// It constrates a map and initialize all of the the map. ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) { + Item it; attach(_g.getNotifier(_Item())); allocate_memory(); - for (KeyIt it(*graph); it != INVALID; ++it) { + for (graph->first(it); it != INVALID; graph->next(it)) { int id = graph->id(it);; allocator.construct(&(values[id]), _v); } @@ -118,7 +103,8 @@ capacity = copy.capacity; if (capacity == 0) return; values = allocator.allocate(capacity); - for (KeyIt it(*graph); it != INVALID; ++it) { + Item it; + for (graph->first(it); it != INVALID; graph->next(it)) { int id = graph->id(it);; allocator.construct(&(values[id]), copy.values[id]); } @@ -146,7 +132,8 @@ values = allocator.allocate(capacity); } - for (KeyIt it(*graph); it != INVALID; ++it) { + Item it; + for (graph->first(it); it != INVALID; graph->next(it)) { int id = graph->id(it);; allocator.construct(&(values[id]), copy.values[id]); } @@ -168,7 +155,7 @@ * The subscript operator. The map can be subscripted by the * actual keys of the graph. */ - Reference operator[](const Key& key) { + Value& operator[](const Key& key) { int id = graph->id(key); return values[id]; } @@ -177,7 +164,7 @@ * The const subscript operator. The map can be subscripted by the * actual keys of the graph. */ - ConstReference operator[](const Key& key) const { + const Value& operator[](const Key& key) const { int id = graph->id(key); return values[id]; } @@ -199,7 +186,8 @@ new_capacity <<= 1; } Value* new_values = allocator.allocate(new_capacity); - for (KeyIt it(*graph); it != INVALID; ++it) { + Item it; + for (graph->first(it); it != INVALID; graph->next(it)) { int jd = graph->id(it);; if (id != jd) { allocator.construct(&(new_values[jd]), values[jd]); @@ -222,7 +210,8 @@ void build() { allocate_memory(); - for (KeyIt it(*graph); it != INVALID; ++it) { + Item it; + for (graph->first(it); it != INVALID; graph->next(it)) { int id = graph->id(it);; allocator.construct(&(values[id]), Value()); } @@ -230,7 +219,8 @@ void clear() { if (capacity != 0) { - for (KeyIt it(*graph); it != INVALID; ++it) { + Item it; + for (graph->first(it); it != INVALID; graph->next(it)) { int id = graph->id(it);; allocator.destroy(&(values[id])); } @@ -313,18 +303,6 @@ Value* values; Allocator allocator; - public: -// // STL compatibility typedefs. -// typedef Iterator iterator; -// typedef ConstIterator const_iterator; -// typedef typename Iterator::PairValue value_type; -// typedef typename Iterator::Key key_type; -// typedef typename Iterator::Value data_type; -// typedef typename Iterator::PairReference reference; -// typedef typename Iterator::PairPointer pointer; -// typedef typename ConstIterator::PairReference const_reference; -// typedef typename ConstIterator::PairPointer const_pointer; -// typedef int difference_type; }; template @@ -345,14 +323,15 @@ template - class NodeMap : public ArrayMap { + class NodeMap + : public IterableMapExtender > { public: typedef ArrayMappableGraphExtender<_Base> Graph; typedef typename Graph::Node Node; typedef typename Graph::NodeIt NodeIt; - typedef ArrayMap Parent; + typedef IterableMapExtender > Parent; //typedef typename Parent::Graph Graph; typedef typename Parent::Value Value; @@ -365,14 +344,15 @@ }; template - class EdgeMap : public ArrayMap { + class EdgeMap + : public IterableMapExtender > { public: typedef ArrayMappableGraphExtender<_Base> Graph; typedef typename Graph::Edge Edge; typedef typename Graph::EdgeIt EdgeIt; - typedef ArrayMap Parent; + typedef IterableMapExtender > Parent; //typedef typename Parent::Graph Graph; typedef typename Parent::Value Value; diff -r 74d616d081f0 -r a93f94dbe3d3 src/lemon/default_map.h --- a/src/lemon/default_map.h Fri Mar 25 22:11:28 2005 +0000 +++ b/src/lemon/default_map.h Fri Mar 25 23:31:57 2005 +0000 @@ -42,66 +42,66 @@ - template + template struct DefaultMapSelector { - typedef ArrayMap<_Graph, _Item, _ItemIt, _Value> Map; + typedef ArrayMap<_Graph, _Item, _Value> Map; }; // bool - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, bool> { + template + struct DefaultMapSelector<_Graph, _Item, bool> { typedef VectorMap<_Graph, _Item, bool> Map; }; // char - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, char> { + template + struct DefaultMapSelector<_Graph, _Item, char> { typedef VectorMap<_Graph, _Item, char> Map; }; - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, signed char> { + template + struct DefaultMapSelector<_Graph, _Item, signed char> { typedef VectorMap<_Graph, _Item, signed char> Map; }; - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, unsigned char> { + template + struct DefaultMapSelector<_Graph, _Item, unsigned char> { typedef VectorMap<_Graph, _Item, unsigned char> Map; }; // int - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, signed int> { + template + struct DefaultMapSelector<_Graph, _Item, signed int> { typedef VectorMap<_Graph, _Item, signed int> Map; }; - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, unsigned int> { + template + struct DefaultMapSelector<_Graph, _Item, unsigned int> { typedef VectorMap<_Graph, _Item, unsigned int> Map; }; // short - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, signed short> { + template + struct DefaultMapSelector<_Graph, _Item, signed short> { typedef VectorMap<_Graph, _Item, signed short> Map; }; - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, unsigned short> { + template + struct DefaultMapSelector<_Graph, _Item, unsigned short> { typedef VectorMap<_Graph, _Item, unsigned short> Map; }; // long - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, signed long> { + template + struct DefaultMapSelector<_Graph, _Item, signed long> { typedef VectorMap<_Graph, _Item, signed long> Map; }; - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, unsigned long> { + template + struct DefaultMapSelector<_Graph, _Item, unsigned long> { typedef VectorMap<_Graph, _Item, unsigned long> Map; }; @@ -109,42 +109,43 @@ // float - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, float> { + template + struct DefaultMapSelector<_Graph, _Item, float> { typedef VectorMap<_Graph, _Item, float> Map; }; // double - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, double> { + template + struct DefaultMapSelector<_Graph, _Item, double> { typedef VectorMap<_Graph, _Item, double> Map; }; // long double - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, long double> { + template + struct DefaultMapSelector<_Graph, _Item, long double> { typedef VectorMap<_Graph, _Item, long double> Map; }; // pointer - template - struct DefaultMapSelector<_Graph, _Item, _ItemIt, _Ptr*> { + template + struct DefaultMapSelector<_Graph, _Item, _Ptr*> { typedef VectorMap<_Graph, _Item, _Ptr*> Map; }; - template - class DefaultMap : public DefaultMapSelector<_Graph, _Item, _ItemIt, _Value>::Map { + template < + typename _Graph, + typename _Item, + typename _Value> + class DefaultMap + : public DefaultMapSelector<_Graph, _Item, _Value>::Map { public: - typedef typename DefaultMapSelector<_Graph, _Item, _ItemIt, _Value>::Map Parent; - typedef DefaultMap<_Graph, _Item, _ItemIt, _Value> Map; + typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; + typedef DefaultMap<_Graph, _Item, _Value> Map; typedef typename Parent::Graph Graph; typedef typename Parent::Value Value; @@ -170,10 +171,11 @@ template - class NodeMap : public DefaultMap { + class NodeMap + : public IterableMapExtender > { public: typedef DefaultMappableGraphExtender Graph; - typedef DefaultMap Parent; + typedef IterableMapExtender > Parent; NodeMap(const Graph& _g) : Parent(_g) {} @@ -182,10 +184,11 @@ }; template - class EdgeMap : public DefaultMap { + class EdgeMap + : public IterableMapExtender > { public: typedef DefaultMappableGraphExtender Graph; - typedef DefaultMap Parent; + typedef IterableMapExtender > Parent; EdgeMap(const Graph& _g) : Parent(_g) {} @@ -204,14 +207,14 @@ typedef DefaultMappableGraphExtender<_Base> Parent; typedef typename Parent::UndirEdge UndirEdge; - typedef typename Parent::UndirEdgeIt UndirEdgeIt; template - class UndirEdgeMap : - public DefaultMap { + class UndirEdgeMap + : public IterableMapExtender > { public: typedef MappableUndirGraphExtender Graph; - typedef DefaultMap Parent; + typedef IterableMapExtender< + DefaultMap > Parent; UndirEdgeMap(const Graph& _g) : Parent(_g) {} diff -r 74d616d081f0 -r a93f94dbe3d3 src/lemon/graph_utils.h --- a/src/lemon/graph_utils.h Fri Mar 25 22:11:28 2005 +0000 +++ b/src/lemon/graph_utils.h Fri Mar 25 23:31:57 2005 +0000 @@ -21,7 +21,6 @@ #include #include -#include ///\ingroup gutils ///\file @@ -34,8 +33,8 @@ namespace lemon { -/// \addtogroup gutils -/// @{ + /// \addtogroup gutils + /// @{ /// \brief Function to count the items in the graph. /// @@ -160,8 +159,8 @@ /// \bug Untested ... template typename Graph::Edge findEdge(const Graph &g, - typename Graph::Node u, typename Graph::Node v, - typename Graph::Edge prev = INVALID) + typename Graph::Node u, typename Graph::Node v, + typename Graph::Edge prev = INVALID) { typename Graph::OutEdgeIt e(g,prev); // if(prev==INVALID) g.first(e,u); @@ -225,46 +224,51 @@ edgeCopy(_d, _s, _nb, _eb); } - template < + template < typename _DestinationGraph, typename _SourceGraph, typename _NodeBijection =typename _SourceGraph::template NodeMap, typename _EdgeBijection - =typename _SourceGraph::template EdgeMap - > - class GraphCopy { - public: + = typename _SourceGraph::template EdgeMap + > + class GraphCopy { + public: + + typedef _DestinationGraph DestinationGraph; + typedef _SourceGraph SourceGraph; - typedef _DestinationGraph DestinationGraph; - typedef _SourceGraph SourceGraph; + typedef _NodeBijection NodeBijection; + typedef _EdgeBijection EdgeBijection; + + protected: + + NodeBijection node_bijection; + EdgeBijection edge_bijection; - typedef _NodeBijection NodeBijection; - typedef _EdgeBijection EdgeBijection; + public: + + GraphCopy(DestinationGraph& _d, const SourceGraph& _s) { + copyGraph(_d, _s, node_bijection, edge_bijection); + } + + const NodeBijection& getNodeBijection() const { + return node_bijection; + } - protected: + const EdgeBijection& getEdgeBijection() const { + return edge_bijection; + } + + }; - NodeBijection node_bijection; - EdgeBijection edge_bijection; - public: - - GraphCopy(DestinationGraph& _d, const SourceGraph& _s) { - copyGraph(_d, _s, node_bijection, edge_bijection); - } - - const NodeBijection& getNodeBijection() const { - return node_bijection; - } - - const EdgeBijection& getEdgeBijection() const { - return edge_bijection; - } - - }; + template + class ItemSetTraits { + }; template - class GraphNodeSet { + class ItemSetTraits<_Graph, typename _Graph::Node> { public: typedef _Graph Graph; @@ -283,14 +287,10 @@ : Parent(_graph, _value) {} }; - typedef IdMap IdMap; - - private: - Graph* graph; }; template - class GraphEdgeSet { + class ItemSetTraits<_Graph, typename _Graph::Edge> { public: typedef _Graph Graph; @@ -309,12 +309,29 @@ : Parent(_graph, _value) {} }; - typedef IdMap IdMap; - - private: - Graph* graph; }; + template + class ItemSetTraits<_Graph, typename _Graph::UndirEdge> { + public: + + typedef _Graph Graph; + + typedef typename Graph::UndirEdge Item; + typedef typename Graph::UndirEdgeIt ItemIt; + + template + class Map : public Graph::template UndirEdgeMap<_Value> { + public: + typedef typename Graph::template UndirEdgeMap<_Value> Parent; + typedef typename Parent::Value Value; + + Map(const Graph& _graph) : Parent(_graph) {} + Map(const Graph& _graph, const Value& _value) + : Parent(_graph, _value) {} + }; + + }; /// @} diff -r 74d616d081f0 -r a93f94dbe3d3 src/lemon/map_iterator.h --- a/src/lemon/map_iterator.h Fri Mar 25 22:11:28 2005 +0000 +++ b/src/lemon/map_iterator.h Fri Mar 25 23:31:57 2005 +0000 @@ -20,6 +20,7 @@ #include #include +#include ///\ingroup graphmaps ///\file @@ -35,39 +36,23 @@ * simple step functions and equality operators. */ - template + template < + typename _Graph, + typename _Item> class MapIteratorBase { - public: - - /// The key type of the iterator. - typedef typename Map::Key Key; - /// The iterator to iterate on the keys. - typedef typename Map::KeyIt KeyIt; - - /// The value type of the iterator. - typedef typename Map::Value Value; - /// The reference type of the iterator. - typedef typename Map::Reference Reference; - /// The pointer type of the iterator. - typedef typename Map::Pointer Pointer; - - /// The const value type of the iterator. - typedef typename Map::ConstValue ConstValue; - /// The const reference type of the iterator. - typedef typename Map::ConstReference ConstReference; - /// The pointer type of the iterator. - typedef typename Map::ConstPointer ConstPointer; - protected: - KeyIt it; + /// The key type of the iterator. + typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt; + + ItemIt it; /// Default constructor. MapIteratorBase() {} - /// KeyIt initialized MapIteratorBase constructor. - MapIteratorBase(const KeyIt pit) : it(pit) {} + /// ItemIt initialized MapIteratorBase constructor. + MapIteratorBase(const ItemIt _it) : it(_it) {} public: @@ -77,330 +62,333 @@ } /// The equality operator of the map. - bool operator==(const MapIteratorBase& pit) const { - return pit.it == it; + bool operator==(const MapIteratorBase& _it) const { + return _it.it == it; } /// The not-equality operator of the map. - bool operator!=(const MapIteratorBase& pit) const { - return !(*this == pit); + bool operator!=(const MapIteratorBase& _it) const { + return !(*this == _it); } }; - template class MapConstIterator; + + template < + typename _Graph, + typename _Item, + typename _Map> + class MapConstIterator; /** Compatible iterator with the stl maps' iterators. * It iterates on pairs of a key and a value. */ - template - class MapIterator : public MapIteratorBase { + template < + typename _Graph, + typename _Item, + typename _Map> + class MapIterator : public MapIteratorBase<_Graph, _Item> { - friend class MapConstIterator; + friend class MapConstIterator<_Graph, _Item, _Map>; public: /// The iterator base class. - typedef MapIteratorBase Base; + typedef MapIteratorBase<_Graph, _Item> Parent; - /// The key type of the iterator. - typedef typename Map::Key Key; - /// The iterator to iterate on the keys. - typedef typename Map::KeyIt KeyIt; + typedef _Item Item; + typedef _Map Map; + typedef _Graph Graph; - /// The value type of the iterator. - typedef typename Map::Value Value; - /// The reference type of the iterator. - typedef typename Map::Reference Reference; - /// The pointer type of the iterator. - typedef typename Map::Pointer Pointer; + protected: - /// The const value type of the iterator. - typedef typename Map::ConstValue ConstValue; - /// The const reference type of the iterator. - typedef typename Map::ConstReference ConstReference; - /// The pointer type of the iterator. - typedef typename Map::ConstPointer ConstPointer; + typedef typename Parent::ItemIt ItemIt; + + typedef typename ReferenceMapTraits<_Map>::Value MapValue; + typedef typename ReferenceMapTraits<_Map>::Reference MapReference; public: /// The value type of the iterator. - typedef extended_pair PairValue; + typedef extended_pair Value; /// The reference type of the iterator. - typedef extended_pair PairReference; + typedef extended_pair Reference; /// Default constructor. MapIterator() {} /// Constructor to initalize the iterators returned /// by the begin() and end(). - MapIterator(Map& pmap, const KeyIt& pit) : Base(pit), map(&pmap) {} + MapIterator(Map& _map, const ItemIt& _it) + : Parent(_it), map(&_map) {} /// Dereference operator for the iterator. - PairReference operator*() { - return PairReference(Base::it, (*map)[Base::it]); + Reference operator*() { + return Reference(Parent::it, (*map)[Parent::it]); } /// The pointer type of the iterator. - class PairPointer { + class Pointer { friend class MapIterator; - private: - PairReference data; - PairPointer(const Key& key, Reference val) - : data(key, val) {} + protected: + Reference data; + Pointer(const Item& item, MapReference val) + : data(item, val) {} public: - PairReference* operator->() {return &data;} + Reference* operator->() {return &data;} }; /// Arrow operator for the iterator. - PairPointer operator->() { - return PairPointer(Base::it, ((*map)[Base::it])); + Pointer operator->() { + return Pointer(Parent::it, (*map)[Parent::it]); } /// The pre increment operator of the iterator. MapIterator& operator++() { - Base::increment(); + Parent::increment(); return *this; } /// The post increment operator of the iterator. MapIterator operator++(int) { MapIterator tmp(*this); - Base::increment(); + Parent::increment(); return tmp; } - private: + protected: + Map* map; public: // STL compatibility typedefs. typedef std::forward_iterator_tag iterator_category; typedef int difference_type; - typedef PairValue value_type; - typedef PairReference reference; - typedef PairPointer pointer; + typedef Value value_type; + typedef Reference reference; + typedef Pointer pointer; }; /** Compatible iterator with the stl maps' iterators. * It iterates on pairs of a key and a value. */ - template - class MapConstIterator : public MapIteratorBase { + template < + typename _Graph, + typename _Item, + typename _Map> + class MapConstIterator : public MapIteratorBase<_Graph, _Item> { + + public: + + /// The iterator base class. + typedef MapIteratorBase<_Graph, _Item> Parent; + + typedef _Graph Graph; + typedef _Item Item; + typedef _Map Map; + + protected: + + typedef typename Parent::ItemIt ItemIt; + + typedef typename ReferenceMapTraits<_Map>::Value MapValue; + typedef typename ReferenceMapTraits<_Map>::ConstReference + MapReference; public: - /// The iterator base class. - typedef MapIteratorBase Base; + /// The value type of the iterator. + typedef extended_pair Value; - /// The key type of the iterator. - typedef typename Map::Key Key; - /// The iterator to iterate on the keys. - typedef typename Map::KeyIt KeyIt; - - /// The value type of the iterator. - typedef typename Map::Value Value; - /// The reference type of the iterator. - typedef typename Map::Reference Reference; - /// The pointer type of the iterator. - typedef typename Map::Pointer Pointer; - - /// The const value type of the iterator. - typedef typename Map::ConstValue ConstValue; - /// The const reference type of the iterator. - typedef typename Map::ConstReference ConstReference; - /// The pointer type of the iterator. - typedef typename Map::ConstPointer ConstPointer; - - public: + /// The reference type of the iterator. + typedef extended_pair Reference; /// Default constructor. MapConstIterator() {} - /// Constructor to initalize the the iterators returned - /// by the begin() and end(). - MapConstIterator(const Map& pmap, const KeyIt& pit) - : Base(pit), map(&pmap) {} - - /// Constructor to create const iterator from a non const. - MapConstIterator(const MapIterator& pit) { - Base::it = pit.Base::it; - map = pit.map; - } - - /// The value type of the iterator. - typedef extended_pair PairValue; - - /// The reference type of map. - typedef extended_pair PairReference; + /// Constructor to initalize the iterators returned + /// by the begin() and end(). + MapConstIterator(const Map& _map, const ItemIt& _it) + : Parent(_it), map(&_map) {} /// Dereference operator for the iterator. - PairReference operator*() { - return PairReference(Base::it, (*map)[Base::it]); + Reference operator*() { + return Reference(Parent::it, (*map)[Parent::it]); } /// The pointer type of the iterator. - class PairPointer { + class Pointer { friend class MapConstIterator; - private: - PairReference data; - PairPointer(const Key& key, ConstReference val) - : data(key, val) {} + protected: + Reference data; + Pointer(const Item& item, MapReference val) + : data(item, val) {} public: - PairReference* operator->() {return &data;} + Reference* operator->() {return &data;} }; /// Arrow operator for the iterator. - PairPointer operator->() { - return PairPointer(Base::it, (*map)[Base::it]); + Pointer operator->() { + return Pointer(Parent::it, ((*map)[Parent::it])); } - + /// The pre increment operator of the iterator. MapConstIterator& operator++() { - Base::increment(); + Parent::increment(); return *this; } /// The post increment operator of the iterator. MapConstIterator operator++(int) { MapConstIterator tmp(*this); - Base::increment(); + Parent::increment(); return tmp; } - private: + protected: const Map* map; public: // STL compatibility typedefs. - typedef std::input_iterator_tag iterator_category; + typedef std::forward_iterator_tag iterator_category; typedef int difference_type; - typedef PairValue value_type; - typedef PairReference reference; - typedef PairPointer pointer; + typedef Value value_type; + typedef Reference reference; + typedef Pointer pointer; }; - - /** The class makes the KeyIt to an stl compatible iterator + + /** The class makes the ItemIt to an stl compatible iterator * with dereferencing operator. */ - template - class MapKeyIterator : public MapIteratorBase { + template < + typename _Graph, + typename _Item> + class MapConstKeyIterator : public MapIteratorBase<_Graph, _Item> { public: /// The iterator base class. - typedef MapIteratorBase Base; + typedef MapIteratorBase<_Graph, _Item> Parent; - /// The key type of the iterator. - typedef typename Map::Key Key; + typedef _Graph Graph; + typedef _Item Item; + + protected: /// The iterator to iterate on the keys. - typedef typename Map::KeyIt KeyIt; + typedef typename Parent::ItemIt ItemIt; public: + typedef Item Value; + typedef const Item& Reference; + typedef const Item* Pointer; + /// Default constructor. - MapKeyIterator() {} + MapConstKeyIterator() {} - /// KeyIt initialized iterator. - MapKeyIterator(const KeyIt& pit) : Base(pit) {} + /// ItemIt initialized iterator. + MapConstKeyIterator(const ItemIt& pit) : Parent(pit) {} /// The pre increment operator of the iterator. - MapKeyIterator& operator++() { - Base::increment(); + MapConstKeyIterator& operator++() { + Parent::increment(); return *this; } /// The post increment operator of the iterator. - MapKeyIterator operator++(int) { - MapKeyIterator tmp(*this); - Base::increment(); + MapConstKeyIterator operator++(int) { + MapConstKeyIterator tmp(*this); + Parent::increment(); return tmp; } /// The dereferencing operator of the iterator. - Key operator*() const { - return static_cast(Base::it); + Item operator*() const { + return static_cast(Parent::it); } public: // STL compatibility typedefs. typedef std::input_iterator_tag iterator_category; typedef int difference_type; - typedef Key value_type; - typedef const Key& reference; - typedef const Key* pointer; + typedef Value value_type; + typedef Reference reference; + typedef Pointer pointer; }; - template class MapConstValueIterator; + template < + typename _Graph, + typename _Item, + typename _Map> + class MapConstValueIterator; /** MapValueIterator creates an stl compatible iterator * for the values. */ - template - class MapValueIterator : public MapIteratorBase { + template < + typename _Graph, + typename _Item, + typename _Map> + class MapValueIterator : public MapIteratorBase<_Graph, _Item> { - friend class MapConstValueIterator; + friend class MapConstValueIterator<_Graph, _Item, _Map>; public: /// The iterator base class. - typedef MapIteratorBase Base; + typedef MapIteratorBase<_Graph, _Item> Parent; - /// The key type of the iterator. - typedef typename Map::Key Key; + typedef _Graph Graph; + typedef _Item Item; + typedef _Map Map; + + protected: + /// The iterator to iterate on the keys. - typedef typename Map::KeyIt KeyIt; - + typedef typename Parent::ItemIt ItemIt; /// The value type of the iterator. - typedef typename Map::Value Value; + typedef typename ReferenceMapTraits::Value MapValue; /// The reference type of the iterator. - typedef typename Map::Reference Reference; + typedef typename ReferenceMapTraits::Reference MapReference; /// The pointer type of the iterator. - typedef typename Map::Pointer Pointer; - - /// The const value type of the iterator. - typedef typename Map::ConstValue ConstValue; - /// The const reference type of the iterator. - typedef typename Map::ConstReference ConstReference; - /// The pointer type of the iterator. - typedef typename Map::ConstPointer ConstPointer; - - private: - - Map* map; + typedef typename ReferenceMapTraits::Pointer MapPointer; public: + typedef MapValue Value; + typedef MapReference Reference; + typedef MapPointer Pointer; + /// Default constructor. MapValueIterator() {} - /// Map and KeyIt initialized iterator. - MapValueIterator(Map& pmap, const KeyIt& pit) - : Base(pit), map(&pmap) {} + /// Map and ItemIt initialized iterator. + MapValueIterator(Map& _map, const ItemIt& _it) + : Parent(_it), map(&_map) {} /// The pre increment operator of the iterator. MapValueIterator& operator++() { - Base::increment(); + Parent::increment(); return *this; } /// The post increment operator of the iterator. MapValueIterator operator++(int) { MapValueIterator tmp(*this); - Base::increment(); + Parent::increment(); return tmp; } /// The dereferencing operator of the iterator. Reference operator*() const { - return (*map)[Base::it]; + return (*map)[Parent::it]; } /// The arrow operator of the iterator. @@ -408,6 +396,10 @@ return &(operator*()); } + protected: + + Map* map; + public: // STL compatibility typedefs. typedef std::forward_iterator_tag iterator_category; @@ -418,133 +410,130 @@ }; /** MapValueIterator creates an stl compatible iterator - * for the const values. + * for the values. */ - - template - class MapConstValueIterator : public MapIteratorBase { + template < + typename _Graph, + typename _Item, + typename _Map> + class MapConstValueIterator : public MapIteratorBase<_Graph, _Item> { public: /// The iterator base class. - typedef MapIteratorBase Base; + typedef MapIteratorBase<_Graph, _Item> Parent; - /// The key type of the iterator. - typedef typename Map::Key Key; + typedef _Graph Graph; + typedef _Item Item; + typedef _Map Map; + + protected: + /// The iterator to iterate on the keys. - typedef typename Map::KeyIt KeyIt; + typedef typename Parent::ItemIt ItemIt; /// The value type of the iterator. - typedef typename Map::Value Value; + typedef typename ReferenceMapTraits::Value MapValue; /// The reference type of the iterator. - typedef typename Map::Reference Reference; + typedef typename ReferenceMapTraits::ConstReference MapReference; /// The pointer type of the iterator. - typedef typename Map::Pointer Pointer; - - /// The const value type of the iterator. - typedef typename Map::ConstValue ConstValue; - /// The const reference type of the iterator. - typedef typename Map::ConstReference ConstReference; - /// The pointer type of the iterator. - typedef typename Map::ConstPointer ConstPointer; - - private: - - const Map* map; + typedef typename ReferenceMapTraits::ConstPointer MapPointer; public: + typedef MapValue Value; + typedef MapReference Reference; + typedef MapPointer Pointer; + /// Default constructor. MapConstValueIterator() {} - /// Constructor to create const iterator from a non const. - MapConstValueIterator(const MapValueIterator& pit) { - Base::it = pit.Base::it; - map = pit.map; - } - - /// Map and KeyIt initialized iterator. - MapConstValueIterator(const Map& pmap, const KeyIt& pit) - : Base(pit), map(&pmap) {} + /// Map and ItemIt initialized iterator. + MapConstValueIterator(const Map& _map, const ItemIt& _it) + : Parent(_it), map(&_map) {} + /// The pre increment operator of the iterator. MapConstValueIterator& operator++() { - Base::increment(); + Parent::increment(); return *this; } /// The post increment operator of the iterator. MapConstValueIterator operator++(int) { MapConstValueIterator tmp(*this); - Base::increment(); + Parent::increment(); return tmp; } /// The dereferencing operator of the iterator. - ConstReference operator*() const { - return (*map)[Base::it]; + Reference operator*() const { + return (*map)[Parent::it]; } /// The arrow operator of the iterator. - ConstPointer operator->() const { + Pointer operator->() const { return &(operator*()); } + protected: + + const Map* map; + public: // STL compatibility typedefs. - typedef std::input_iterator_tag iterator_category; + typedef std::forward_iterator_tag iterator_category; typedef int difference_type; typedef Value value_type; - typedef ConstReference reference; - typedef ConstPointer pointer; + typedef Reference reference; + typedef Pointer pointer; }; /** This class makes from a map an iteratable set * which contains all the keys of the map. */ - template + template class MapConstKeySet { - const Map* map; - public: + typedef _Graph Graph; /// The key type of the iterator. - typedef typename Map::Key Key; + typedef _Item Item; /// The iterator to iterate on the keys. - typedef typename Map::KeyIt KeyIt; + protected: - /// The value type of the iterator. - typedef typename Map::Value Value; - /// The reference type of the iterator. - typedef typename Map::Reference Reference; - /// The pointer type of the iterator. - typedef typename Map::Pointer Pointer; + typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt; - /// The const value type of the iterator. - typedef typename Map::ConstValue ConstValue; - /// The const reference type of the iterator. - typedef typename Map::ConstReference ConstReference; - /// The pointer type of the iterator. - typedef typename Map::ConstPointer ConstPointer; + public: /// The map initialized const key set. - MapConstKeySet(const Map& pmap) : map(&pmap) {} + MapConstKeySet(const Graph& _graph) : graph(&_graph) {} /// The const iterator of the set. - typedef MapKeyIterator ConstIterator; + typedef MapConstKeyIterator<_Graph, _Item> ConstIterator; + + typedef typename ConstIterator::Value Value; + /// The reference type of the iterator. + typedef typename ConstIterator::Reference ConstReference; + /// The pointer type of the iterator. + typedef typename ConstIterator::Pointer ConstPointer; /// It gives back the const iterator pointed to the first element. ConstIterator begin() const { - return ConstIterator(KeyIt(*map->getGraph())); + return ConstIterator(ItemIt(*graph)); } /// It gives back the const iterator pointed to the first ivalid element. ConstIterator end() const { - return ConstIterator(KeyIt(INVALID)); + return ConstIterator(ItemIt(INVALID)); } + + protected: + + const Graph* graph; public: // STL compatibility typedefs. @@ -559,49 +548,48 @@ * which contains all the values of the map. * The values cannot be modified. */ - template + template class MapConstValueSet { - const Map* map; + public: + + typedef _Graph Graph; + typedef _Item Item; + typedef _Map Map; + + protected: + + /// The iterator to iterate on the keys. + typedef typename ItemSetTraits::ItemIt ItemIt; public: - /// The key type of the iterator. - typedef typename Map::Key Key; - /// The iterator to iterate on the keys. - typedef typename Map::KeyIt KeyIt; - - - /// The value type of the iterator. - typedef typename Map::Value Value; - /// The reference type of the iterator. - typedef typename Map::Reference Reference; - /// The pointer type of the iterator. - typedef typename Map::Pointer Pointer; - - /// The const value type of the iterator. - typedef typename Map::ConstValue ConstValue; - /// The const reference type of the iterator. - typedef typename Map::ConstReference ConstReference; - /// The pointer type of the iterator. - typedef typename Map::ConstPointer ConstPointer; - /// The map initialized const value set. - MapConstValueSet(const Map& pmap) : map(&pmap) {} + MapConstValueSet(const Graph& _graph, const Map& _map) + : graph(&_graph), map(&_map) {} /// The const iterator of the set. - typedef MapConstValueIterator ConstIterator; + typedef MapConstValueIterator<_Graph, _Item, _Map> ConstIterator; + + typedef typename ConstIterator::Value Value; + typedef typename ConstIterator::Reference ConstReference; + typedef typename ConstIterator::Pointer ConstPointer; /// It gives back the const iterator pointed to the first element. ConstIterator begin() const { - return ConstIterator(*map, KeyIt(*map->getGraph())); + return ConstIterator(*map, ItemIt(*graph)); } /// It gives back the const iterator pointed to the first invalid element. ConstIterator end() const { - return ConstIterator(*map, KeyIt(INVALID)); + return ConstIterator(*map, ItemIt(INVALID)); } + protected: + + const Map* map; + const Graph * graph; + public: // STL compatibility typedefs. typedef Value value_type; @@ -616,61 +604,137 @@ * which contains all the values of the map. * The values can be modified. */ - template + template class MapValueSet { - Map* map; + public: + + typedef _Graph Graph; + typedef _Item Item; + typedef _Map Map; + + protected: + + /// The iterator to iterate on the keys. + typedef typename ItemSetTraits::ItemIt ItemIt; public: - /// The key type of the iterator. - typedef typename Map::Key Key; - /// The iterator to iterate on the keys. - typedef typename Map::KeyIt KeyIt; - - - /// The value type of the iterator. - typedef typename Map::Value Value; - /// The reference type of the iterator. - typedef typename Map::Reference Reference; - /// The pointer type of the iterator. - typedef typename Map::Pointer Pointer; - - /// The const value type of the iterator. - typedef typename Map::ConstValue ConstValue; - /// The const reference type of the iterator. - typedef typename Map::ConstReference ConstReference; - /// The pointer type of the iterator. - typedef typename Map::ConstPointer ConstPointer; - - /// The map initialized value set. - MapValueSet(Map& pmap) : map(&pmap) {} + /// The map initialized const value set. + MapValueSet(const Graph& _graph, Map& _map) + : graph(&_graph), map(&_map) {} /// The const iterator of the set. - typedef MapConstValueIterator ConstIterator; + typedef MapValueIterator<_Graph, _Item, _Map> Iterator; + /// The const iterator of the set. + typedef MapConstValueIterator<_Graph, _Item, _Map> ConstIterator; + + typedef typename ConstIterator::Value Value; + typedef typename Iterator::Reference Reference; + typedef typename Iterator::Pointer Pointer; + typedef typename ConstIterator::Reference ConstReference; + typedef typename ConstIterator::Pointer ConstPointer; /// It gives back the const iterator pointed to the first element. ConstIterator begin() const { - return ConstIterator(*map, KeyIt(*map->getGraph())); + return ConstIterator(*map, ItemIt(*graph)); } /// It gives back the const iterator pointed to the first invalid element. ConstIterator end() const { - return ConstIterator(*map, KeyIt(INVALID)); + return ConstIterator(*map, ItemIt(INVALID)); } - /// The iterator of the set. - typedef MapValueIterator Iterator; - /// It gives back the iterator pointed to the first element. Iterator begin() { - return Iterator(*map, KeyIt(*map->getGraph())); + return Iterator(*map, ItemIt(*graph)); } /// It gives back the iterator pointed to the first invalid element. Iterator end() { - return Iterator(*map, KeyIt(INVALID)); + return Iterator(*map, ItemIt(INVALID)); } + + protected: + + Map* map; + const Graph * graph; + + public: + // STL compatibility typedefs. + typedef Value value_type; + typedef Iterator iterator; + typedef ConstIterator const_iterator; + typedef Reference reference; + typedef ConstReference const_reference; + typedef Pointer pointer; + typedef ConstPointer const_pointer; + typedef int difference_type; + + }; + + /** This class makes from a map an iteratable set + * which contains all the values of the map. + * The values can be modified. + */ + template < + typename _Graph, + typename _Item, + typename _Map + > + class MapSet { + public: + + typedef _Graph Graph; + typedef _Item Item; + typedef _Map Map; + + protected: + + typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt; + + public: + + /// The map initialized value set. + MapSet(const Graph& _graph, Map& _map) : graph(&_graph), map(&_map) {} + + /// The const iterator of the set. + typedef MapIterator<_Graph, _Item, _Map> Iterator; + typedef MapConstIterator<_Graph, _Item, _Map> ConstIterator; + + typedef typename ConstIterator::Value Value; + typedef typename Iterator::Reference Reference; + typedef typename Iterator::Pointer Pointer; + typedef typename ConstIterator::Reference ConstReference; + typedef typename ConstIterator::Pointer ConstPointer; + + + /// It gives back the const iterator pointed to the first element. + ConstIterator begin() const { + return ConstIterator(*map, ItemIt(*graph)); + } + + /// It gives back the const iterator pointed to the first invalid element. + ConstIterator end() const { + return ConstIterator(*map, ItemIt(INVALID)); + } + + /// The iterator of the set. + + /// It gives back the iterator pointed to the first element. + Iterator begin() { + return Iterator(*map, ItemIt(*graph)); + } + + /// It gives back the iterator pointed to the first invalid element. + Iterator end() { + return Iterator(*map, ItemIt(INVALID)); + } + + protected: + + const Graph* graph; + Map* map; public: // STL compatibility typedefs. @@ -685,6 +749,105 @@ }; + template < + typename _Graph, + typename _Item, + typename _Map + > + class ConstMapSet { + + typedef _Graph Graph; + typedef _Map Map; + + const Graph* graph; + const Map* map; + + public: + + typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt; + + + /// The map initialized value set. + ConstMapSet(const Graph& _graph, const Map& _map) + : graph(&_graph), map(&_map) {} + + /// The const iterator of the set. + typedef MapConstIterator<_Graph, _Item, _Map> ConstIterator; + + typedef typename ConstIterator::Value Value; + typedef typename ConstIterator::Reference ConstReference; + typedef typename ConstIterator::Pointer ConstPointer; + + + /// It gives back the const iterator pointed to the first element. + ConstIterator begin() const { + return ConstIterator(*map, ItemIt(*graph)); + } + + /// It gives back the const iterator pointed to the first invalid element. + ConstIterator end() const { + return ConstIterator(*map, ItemIt(INVALID)); + } + + public: + // STL compatibility typedefs. + typedef Value value_type; + typedef ConstIterator const_iterator; + typedef ConstReference const_reference; + typedef ConstPointer const_pointer; + typedef int difference_type; + + }; + + template + class IterableMapExtender : public _Map { + public: + + typedef _Map Parent; + typedef Parent Map; + typedef typename Map::Graph Graph; + typedef typename Map::Key Item; + typedef typename Map::Value Value; + + typedef MapSet MapSet; + + IterableMapExtender() : Parent() {} + + IterableMapExtender(const Graph& graph) : Parent(graph) {} + + IterableMapExtender(const Graph& graph, const Value& value) + : Parent(graph, value) {} + + MapSet mapSet() { + return MapSet(*Parent::getGraph(), *this); + } + + typedef ConstMapSet ConstMapSet; + + ConstMapSet mapSet() const { + return ConstMapSet(*Parent::getGraph(), *this); + } + + typedef MapConstKeySet ConstKeySet; + + ConstKeySet keySet() const { + return ConstKeySet(*Parent::getGraph()); + } + + typedef MapValueSet ValueSet; + + ValueSet valueSet() { + return ValueSet(*Parent::getGraph(), *this); + } + + typedef MapConstValueSet ConstValueSet; + + ConstValueSet valueSet() const { + return ConstValueSet(*Parent::getGraph(), *this); + } + + }; + /// @} } diff -r 74d616d081f0 -r a93f94dbe3d3 src/lemon/map_utils.h --- a/src/lemon/map_utils.h Fri Mar 25 22:11:28 2005 +0000 +++ b/src/lemon/map_utils.h Fri Mar 25 23:31:57 2005 +0000 @@ -25,11 +25,35 @@ #include #include +#include + namespace lemon { /// \addtogroup mutils /// @{ + + template + struct ReferenceMapTraits { + typedef typename Map::Value Value; + typedef typename Map::Value& Reference; + typedef const typename Map::Value& ConstReference; + typedef typename Map::Value* Pointer; + typedef const typename Map::Value* ConstPointer; + }; + + template + struct ReferenceMapTraits< + Map, + typename enable_if::type + > { + typedef typename Map::Value Value; + typedef typename Map::Reference Reference; + typedef typename Map::ConstReference ConstReference; + typedef typename Map::Pointer Pointer; + typedef typename Map::ConstPointer ConstPointer; + }; + /// \brief General inversable map type. /// This type provides simple inversable map functions. @@ -39,19 +63,23 @@ /// \param _Graph The graph type. /// \param _Map The map to extend with inversable functionality. template < - typename _Graph, - typename _Map + typename _Graph, + typename _Item, + typename _Value, + typename _Map + = typename ItemSetTraits<_Graph, _Item>::template Map<_Value> > class InversableMap : protected _Map { public: + + typedef _Map Map; typedef _Graph Graph; - - typedef _Map Map; - /// The key type of InversableMap (Node, Edge, UndirEdge). + /// The key type of InversableMap (Node, Edge, UndirEdge). typedef typename _Map::Key Key; /// The value type of the InversableMap. typedef typename _Map::Value Value; + typedef std::map InverseMap; typedef typename _Map::ConstReference ConstReference; @@ -64,7 +92,7 @@ /// \brief The setter function of the map. /// - /// It sets the map and the inverse map to given key-value pair. + void set(const Key& key, const Value& val) { Value oldval = Map::operator[](key); typename InverseMap::iterator it = invMap.find(oldval); @@ -140,7 +168,7 @@ template < typename _Graph, typename _Item, - typename _Map + typename _Map = typename ItemSetTraits<_Graph, _Item>::template Map > class DescriptorMap : protected _Map { @@ -237,6 +265,7 @@ typedef _Graph Graph; typedef int Value; typedef _Item Item; + typedef _Item Key; /// \brief The class represents the inverse of the map. /// diff -r 74d616d081f0 -r a93f94dbe3d3 src/lemon/vector_map.h --- a/src/lemon/vector_map.h Fri Mar 25 22:11:28 2005 +0000 +++ b/src/lemon/vector_map.h Fri Mar 25 23:31:57 2005 +0000 @@ -20,6 +20,8 @@ #include #include +#include +#include #include ///\ingroup graphmaps @@ -44,9 +46,11 @@ /// \author Balazs Dezso - template + template < + typename _Graph, + typename _Item, + typename _Value + > class VectorMap : public AlterationNotifier<_Item>::ObserverBase { public: @@ -83,6 +87,8 @@ /// The pointer type of the map; typedef typename Container::const_pointer ConstPointer; + typedef True FullTypeTag; + /// Constructor to attach the new map into the registry. /// It construates a map and attachs it into the registry. @@ -205,7 +211,7 @@ void clear() { container.clear(); } - + private: Container container; @@ -232,15 +238,15 @@ typedef typename Parent::EdgeNotifier EdgeObserverRegistry; - template - class NodeMap : public VectorMap { + class NodeMap : + public IterableMapExtender > { public: typedef VectorMappableGraphExtender<_Base> Graph; typedef typename Graph::Node Node; - typedef VectorMap Parent; + typedef IterableMapExtender > Parent; //typedef typename Parent::Graph Graph; typedef typename Parent::Value Value; @@ -253,13 +259,14 @@ }; template - class EdgeMap : public VectorMap { + class EdgeMap + : public IterableMapExtender > { public: typedef VectorMappableGraphExtender<_Base> Graph; typedef typename Graph::Edge Edge; - typedef VectorMap Parent; + typedef IterableMapExtender > Parent; //typedef typename Parent::Graph Graph; typedef typename Parent::Value Value; diff -r 74d616d081f0 -r a93f94dbe3d3 src/work/deba/iterator_test.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/deba/iterator_test.cpp Fri Mar 25 23:31:57 2005 +0000 @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace lemon; + +template +struct unary_compose { + typedef typename G::argument_type argument_type; + typedef typename F::result_type result_type; + + unary_compose(const F& _f, const G& _g) : f(_f), g(_g) {} + + result_type operator()(const argument_type& x) { + return f(g(x)); + } + +private: + F f; + G g; +}; + +template +unary_compose compose1(const F& f, const G& g) { + return unary_compose(f, g); +} + + + + +template +struct Second { + typedef T argument_type; + typedef typename T::second_type result_type; + + typename T::second_type operator()(const T& t) const { + return t.second; + } +}; + +template +struct First { + typedef T argument_type; + typedef typename T::first_type result_type; + typename T::first_type operator()(const T& t) const { + return t.first; + } +}; + + +int main() { + + typedef ListGraph Graph; + + typedef Graph::Edge Edge; + typedef Graph::Node Node; + typedef Graph::EdgeIt EdgeIt; + typedef Graph::NodeIt NodeIt; + typedef Graph::EdgeMap LengthMap; + + typedef IdMap EdgeIdMap; + + Graph graph; + LengthMap length(graph); + + readGraph(std::cin, graph, length); + + const LengthMap& constLength = length; + + copy(length.valueSet().begin(), length.valueSet().end(), + ostream_iterator(cout, " ")); + cout << endl; + + + copy(constLength.valueSet().begin(), constLength.valueSet().end(), + ostream_iterator(cout, " ")); + cout << endl; + + + transform(constLength.keySet().begin(), constLength.keySet().end(), + ostream_iterator(cout, " "), + MapFunctor(EdgeIdMap(graph))); + cout << endl; + + + transform(constLength.mapSet().begin(), constLength.mapSet().end(), + ostream_iterator(cout, " "), + Second()); + cout << endl; + + transform(constLength.mapSet().begin(), constLength.mapSet().end(), + ostream_iterator(cout, " "), + compose1(MapFunctor(EdgeIdMap(graph)), + First() )); + cout << endl; + + transform(length.mapSet().begin(), length.mapSet().end(), + ostream_iterator(cout, " "), + Second()); + cout << endl; + + transform(length.mapSet().begin(), length.mapSet().end(), + ostream_iterator(cout, " "), + compose1(MapFunctor(EdgeIdMap(graph)), + First() )); + cout << endl; + + return 0; +} diff -r 74d616d081f0 -r a93f94dbe3d3 src/work/deba/test.cpp --- a/src/work/deba/test.cpp Fri Mar 25 22:11:28 2005 +0000 +++ b/src/work/deba/test.cpp Fri Mar 25 23:31:57 2005 +0000 @@ -4,7 +4,7 @@ #include -using namespace std; +using namespace lemon; /* struct _EmptyList { void write() const {} @@ -57,31 +57,28 @@ class A { public: typedef int X; + typedef True XD; }; class C { }; -template -class TRUE { + +template +class B { +public: + static const bool state = false; +}; + +template +class B<_A, typename enable_if::type> { public: static const bool state = true; }; -template -class B { -public: - typedef enable_if state; -}; - -template -class B<_A, void> { -public: - static const bool state = true; -}; int main() { - printf("%s\n", B::state(), true ? "true" : "false"); - printf("%s\n", B::state(), true ? "true" : "false"); + printf("%s\n", B::state ? "true" : "false"); + printf("%s\n", B::state ? "true" : "false"); return 0; }