COIN-OR::LEMON - Graph Library

Ticket #302: 302-maps-709cfcba8777.patch

File 302-maps-709cfcba8777.patch, 7.6 KB (added by Peter Kovacs, 15 years ago)
  • lemon/maps.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1248168789 -7200
    # Node ID 709cfcba87778b15e1cc8a483c6754f054acd6ea
    # Parent  9f529abcaebf13f19e61ba24fdd2c3631860af91
    Add tests for IdMap, RangeIdMap, CrossRefMap (#302)
    
    diff --git a/lemon/maps.h b/lemon/maps.h
    a b  
    18261826  /// Using this map you get access (i.e. can read) the inner id values of
    18271827  /// the items stored in the graph, which is returned by the \c id()
    18281828  /// function of the graph. This map can be inverted with its member
    1829   /// class \c InverseMap or with the \c operator() member.
     1829  /// class \c InverseMap or with the \c operator()() member.
    18301830  ///
    18311831  /// \tparam GR The graph type.
    18321832  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
     
    19021902  /// \brief General cross reference graph map type.
    19031903
    19041904  /// This class provides simple invertable graph maps.
    1905   /// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap"
    1906   /// and if a key is set to a new value then store it
    1907   /// in the inverse map.
     1905  /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
     1906  /// and if a key is set to a new value then stores it in the inverse map.
    19081907  ///
    19091908  /// The values of the map can be accessed
    19101909  /// with stl compatible forward iterator.
     
    20042003      if (it != _inv_map.end() && it->second == key) {
    20052004        _inv_map.erase(it);
    20062005      }
    2007       _inv_map.insert(make_pair(val, key));
     2006      _inv_map.insert(std::make_pair(val, key));
    20082007      Map::set(key, val);
    20092008    }
    20102009
     
    21032102
    21042103  };
    21052104
    2106   /// \brief Provides continuous and unique ID for the
     2105  /// \brief Provides continuous and unique id for the
    21072106  /// items of a graph.
    21082107  ///
    21092108  /// RangeIdMap provides a unique and continuous
    2110   /// ID for each item of a given type (\c Node, \c Arc or
     2109  /// id for each item of a given type (\c Node, \c Arc or
    21112110  /// \c Edge) in a graph. This id is
    21122111  ///  - \b unique: different items get different ids,
    21132112  ///  - \b continuous: the range of the ids is the set of integers
     
    21182117  /// Thus this id is not (necessarily) the same as what can get using
    21192118  /// the \c id() function of the graph or \ref IdMap.
    21202119  /// This map can be inverted with its member class \c InverseMap,
    2121   /// or with the \c operator() member.
     2120  /// or with the \c operator()() member.
    21222121  ///
    21232122  /// \tparam GR The graph type.
    21242123  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
  • test/maps_test.cc

    diff --git a/test/maps_test.cc b/test/maps_test.cc
    a b  
    2222#include <lemon/concept_check.h>
    2323#include <lemon/concepts/maps.h>
    2424#include <lemon/maps.h>
     25#include <lemon/list_graph.h>
    2526
    2627#include "test_tools.h"
    2728
     
    328329  // LoggerBoolMap
    329330  {
    330331    typedef std::vector<int> vec;
     332    checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >();
     333    checkConcept<WriteMap<int, bool>,
     334                 LoggerBoolMap<std::back_insert_iterator<vec> > >();
     335
    331336    vec v1;
    332337    vec v2(10);
    333338    LoggerBoolMap<std::back_insert_iterator<vec> >
     
    348353          it != map2.end(); ++it )
    349354      check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
    350355  }
     356 
     357  // IdMap, RangeIdMap
     358  {
     359    typedef ListDigraph Graph;
     360    DIGRAPH_TYPEDEFS(Graph);
     361
     362    checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >();
     363    checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >();
     364    checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >();
     365    checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >();
     366   
     367    Graph gr;
     368    IdMap<Graph, Node> nmap(gr);
     369    IdMap<Graph, Arc> amap(gr);
     370    RangeIdMap<Graph, Node> nrmap(gr);
     371    RangeIdMap<Graph, Arc> armap(gr);
     372   
     373    Node n0 = gr.addNode();
     374    Node n1 = gr.addNode();
     375    Node n2 = gr.addNode();
     376   
     377    Arc a0 = gr.addArc(n0, n1);
     378    Arc a1 = gr.addArc(n0, n2);
     379    Arc a2 = gr.addArc(n2, n1);
     380    Arc a3 = gr.addArc(n2, n0);
     381   
     382    check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap");
     383    check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap");
     384    check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap");
     385
     386    check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap");
     387    check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap");
     388    check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap");
     389    check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap");
     390
     391    check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap");
     392    check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap");
     393   
     394    check(nrmap.size() == 3 && armap.size() == 4,
     395          "Wrong RangeIdMap::size()");
     396
     397    check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap");
     398    check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap");
     399    check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap");
     400   
     401    check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap");
     402    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
     403    check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap");
     404    check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap");
     405
     406    check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
     407    check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
     408   
     409    gr.erase(n1);
     410   
     411    if (nrmap[n0] == 1) nrmap.swap(n0, n2);
     412    nrmap.swap(n2, n0);
     413    if (armap[a1] == 1) armap.swap(a1, a3);
     414    armap.swap(a3, a1);
     415   
     416    check(nrmap.size() == 2 && armap.size() == 2,
     417          "Wrong RangeIdMap::size()");
     418
     419    check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
     420    check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
     421   
     422    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
     423    check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
     424
     425    check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
     426    check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
     427  }
     428 
     429  // CrossRefMap
     430  {
     431    typedef ListDigraph Graph;
     432    DIGRAPH_TYPEDEFS(Graph);
     433
     434    checkConcept<ReadWriteMap<Node, int>,
     435                 CrossRefMap<Graph, Node, int> >();
     436    checkConcept<ReadWriteMap<Node, bool>,
     437                 CrossRefMap<Graph, Node, bool> >();
     438    checkConcept<ReadWriteMap<Node, double>,
     439                 CrossRefMap<Graph, Node, double> >();
     440   
     441    Graph gr;
     442    CrossRefMap<Graph, Node, char> map(gr);
     443   
     444    Node n0 = gr.addNode();
     445    Node n1 = gr.addNode();
     446    Node n2 = gr.addNode();
     447   
     448    map.set(n0, 'A');
     449    map.set(n1, 'B');
     450    map.set(n2, 'C');
     451   
     452    check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
     453          "Wrong CrossRefMap");
     454    check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
     455          "Wrong CrossRefMap");
     456    check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
     457          "Wrong CrossRefMap");
     458         
     459    map.set(n2, 'A');
     460
     461    check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
     462          "Wrong CrossRefMap");
     463    check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
     464    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
     465    check(map('C') == INVALID && map.inverse()['C'] == INVALID,
     466          "Wrong CrossRefMap");
     467
     468    map.set(n0, 'C');
     469
     470    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
     471          "Wrong CrossRefMap");
     472    check(map('A') == INVALID && map.inverse()['A'] == INVALID,
     473          "Wrong CrossRefMap");
     474    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
     475    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
     476  }
    351477
    352478  return 0;
    353479}