1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 
     3  * This file is a part of LEMON, a generic C++ optimization library.
 
     5  * Copyright (C) 2003-2009
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     9  * Permission to use, modify and distribute this software is granted
 
    10  * provided that this copyright notice appears in all copies. For
 
    11  * precise terms see the accompanying LICENSE file.
 
    13  * This software is provided "AS IS" with no warranty of any kind,
 
    14  * express or implied, and with no claim as to its suitability for any
 
    19 #ifndef LEMON_BITS_MAP_EXTENDER_H
 
    20 #define LEMON_BITS_MAP_EXTENDER_H
 
    24 #include <lemon/bits/traits.h>
 
    26 #include <lemon/concept_check.h>
 
    27 #include <lemon/concepts/maps.h>
 
    30 //\brief Extenders for iterable maps.
 
    36   // \brief Extender for maps
 
    37   template <typename _Map>
 
    38   class MapExtender : public _Map {
 
    40     typedef typename Parent::GraphType GraphType;
 
    44     typedef MapExtender Map;
 
    45     typedef typename Parent::Key Item;
 
    47     typedef typename Parent::Key Key;
 
    48     typedef typename Parent::Value Value;
 
    49     typedef typename Parent::Reference Reference;
 
    50     typedef typename Parent::ConstReference ConstReference;
 
    56     friend class ConstMapIt;
 
    60     MapExtender(const GraphType& graph)
 
    63     MapExtender(const GraphType& graph, const Value& value)
 
    64       : Parent(graph, value) {}
 
    67     MapExtender& operator=(const MapExtender& cmap) {
 
    68       return operator=<MapExtender>(cmap);
 
    71     template <typename CMap>
 
    72     MapExtender& operator=(const CMap& cmap) {
 
    73       Parent::operator=(cmap);
 
    78     class MapIt : public Item {
 
    83       typedef typename Map::Value Value;
 
    87       MapIt(Invalid i) : Parent(i) { }
 
    89       explicit MapIt(Map& _map) : map(_map) {
 
    90         map.notifier()->first(*this);
 
    93       MapIt(const Map& _map, const Item& item)
 
    94         : Parent(item), map(_map) {}
 
    97         map.notifier()->next(*this);
 
   101       typename MapTraits<Map>::ConstReturnValue operator*() const {
 
   105       typename MapTraits<Map>::ReturnValue operator*() {
 
   109       void set(const Value& value) {
 
   110         map.set(*this, value);
 
   118     class ConstMapIt : public Item {
 
   123       typedef typename Map::Value Value;
 
   127       ConstMapIt(Invalid i) : Parent(i) { }
 
   129       explicit ConstMapIt(Map& _map) : map(_map) {
 
   130         map.notifier()->first(*this);
 
   133       ConstMapIt(const Map& _map, const Item& item)
 
   134         : Parent(item), map(_map) {}
 
   136       ConstMapIt& operator++() {
 
   137         map.notifier()->next(*this);
 
   141       typename MapTraits<Map>::ConstReturnValue operator*() const {
 
   149     class ItemIt : public Item {
 
   156       ItemIt(Invalid i) : Parent(i) { }
 
   158       explicit ItemIt(Map& _map) : map(_map) {
 
   159         map.notifier()->first(*this);
 
   162       ItemIt(const Map& _map, const Item& item)
 
   163         : Parent(item), map(_map) {}
 
   165       ItemIt& operator++() {
 
   166         map.notifier()->next(*this);
 
   176   // \ingroup graphbits
 
   178   // \brief Extender for maps which use a subset of the items.
 
   179   template <typename _Graph, typename _Map>
 
   180   class SubMapExtender : public _Map {
 
   182     typedef _Graph GraphType;
 
   186     typedef SubMapExtender Map;
 
   187     typedef typename Parent::Key Item;
 
   189     typedef typename Parent::Key Key;
 
   190     typedef typename Parent::Value Value;
 
   191     typedef typename Parent::Reference Reference;
 
   192     typedef typename Parent::ConstReference ConstReference;
 
   198     friend class ConstMapIt;
 
   202     SubMapExtender(const GraphType& _graph)
 
   203       : Parent(_graph), graph(_graph) {}
 
   205     SubMapExtender(const GraphType& _graph, const Value& _value)
 
   206       : Parent(_graph, _value), graph(_graph) {}
 
   209     SubMapExtender& operator=(const SubMapExtender& cmap) {
 
   210       return operator=<MapExtender>(cmap);
 
   213     template <typename CMap>
 
   214     SubMapExtender& operator=(const CMap& cmap) {
 
   215       checkConcept<concepts::ReadMap<Key, Value>, CMap>();
 
   217       for (graph.first(it); it != INVALID; graph.next(it)) {
 
   218         Parent::set(it, cmap[it]);
 
   224     class MapIt : public Item {
 
   228       typedef typename Map::Value Value;
 
   232       MapIt(Invalid i) : Parent(i) { }
 
   234       explicit MapIt(Map& _map) : map(_map) {
 
   235         map.graph.first(*this);
 
   238       MapIt(const Map& _map, const Item& item)
 
   239         : Parent(item), map(_map) {}
 
   241       MapIt& operator++() {
 
   242         map.graph.next(*this);
 
   246       typename MapTraits<Map>::ConstReturnValue operator*() const {
 
   250       typename MapTraits<Map>::ReturnValue operator*() {
 
   254       void set(const Value& value) {
 
   255         map.set(*this, value);
 
   263     class ConstMapIt : public Item {
 
   268       typedef typename Map::Value Value;
 
   272       ConstMapIt(Invalid i) : Parent(i) { }
 
   274       explicit ConstMapIt(Map& _map) : map(_map) {
 
   275         map.graph.first(*this);
 
   278       ConstMapIt(const Map& _map, const Item& item)
 
   279         : Parent(item), map(_map) {}
 
   281       ConstMapIt& operator++() {
 
   282         map.graph.next(*this);
 
   286       typename MapTraits<Map>::ConstReturnValue operator*() const {
 
   294     class ItemIt : public Item {
 
   301       ItemIt(Invalid i) : Parent(i) { }
 
   303       explicit ItemIt(Map& _map) : map(_map) {
 
   304         map.graph.first(*this);
 
   307       ItemIt(const Map& _map, const Item& item)
 
   308         : Parent(item), map(_map) {}
 
   310       ItemIt& operator++() {
 
   311         map.graph.next(*this);
 
   322     const GraphType& graph;