1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/hugo/sym_map.h Wed Sep 29 14:02:14 2004 +0000
1.3 @@ -0,0 +1,144 @@
1.4 +/* -*- C++ -*-
1.5 + * src/hugo/sym_map.h - Part of HUGOlib, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef HUGO_SYM_MAP_H
1.21 +#define HUGO_SYM_MAP_H
1.22 +
1.23 +///\ingroup graphmaps
1.24 +///\file
1.25 +///\brief Graph maps that construates and destruates
1.26 +///their elements dynamically.
1.27 +
1.28 +namespace hugo {
1.29 +
1.30 +/// \addtogroup graphmaps
1.31 +/// @{
1.32 +
1.33 + /** The SymEdgeIt is wrapper class for the EdgeIt. It can be used to
1.34 + * iterate on the symmetric maps when all of the edge - reverse edge pair
1.35 + * has different parity.
1.36 + */
1.37 +
1.38 +
1.39 + template <typename Graph, typename Edge, typename EdgeIt>
1.40 + class SymEdgeIt : public EdgeIt {
1.41 + public:
1.42 +
1.43 + /** Default constructor.
1.44 + */
1.45 + SymEdgeIt()
1.46 + : EdgeIt() {}
1.47 +
1.48 + /** Graph initialized constructor.
1.49 + */
1.50 + SymEdgeIt(const Graph& graph)
1.51 + : EdgeIt(graph) {
1.52 + while ( (EdgeIt::n & 1) && EdgeIt::n != -1) {
1.53 + EdgeIt::operator++();
1.54 + }
1.55 + }
1.56 +
1.57 + /** Creating invelid SymEdgeIt.
1.58 + */
1.59 + SymEdgeIt(Invalid invalid)
1.60 + : EdgeIt(invalid) {}
1.61 +
1.62 + /** SymEdgeIt from the given Edge.
1.63 + */
1.64 + SymEdgeIt(const Graph& graph, const Edge& edge)
1.65 + : EdgeIt(graph, edge) {
1.66 + while ( (EdgeIt::n & 1) && EdgeIt::n != -1) {
1.67 + EdgeIt::operator++();
1.68 + }
1.69 + }
1.70 +
1.71 + /** Increase operator.
1.72 + */
1.73 + SymEdgeIt& operator++() {
1.74 + EdgeIt::operator++();
1.75 + while ( (EdgeIt::n & 1) && EdgeIt::n != -1) {
1.76 + EdgeIt::operator++();
1.77 + }
1.78 + return *this;
1.79 + }
1.80 + };
1.81 +
1.82 + /** The SymMap template class is graph map structure what
1.83 + * wraps an other map structure to use as symmetric map structure.
1.84 + *
1.85 + * The template parameter is the MapRegistry that the maps
1.86 + * will belong to and the ValueType.
1.87 + */
1.88 + template <template <typename, typename> class DynMap,
1.89 + typename MapRegistry, typename Value>
1.90 + class SymMap : public DynMap<MapRegistry, Value>{
1.91 +
1.92 + private:
1.93 +
1.94 + typedef DynMap<MapRegistry, Value> MapImpl;
1.95 +
1.96 + public:
1.97 +
1.98 + /// The graph type of the maps.
1.99 + typedef typename MapRegistry::Graph Graph;
1.100 +
1.101 + typedef typename MapImpl::KeyType KeyType;
1.102 +
1.103 + public:
1.104 +
1.105 +
1.106 + /** Graph and Registry initialized map constructor.
1.107 + */
1.108 + SymMap(const Graph& g, MapRegistry& r) : MapImpl(g, r) {}
1.109 +
1.110 + /** Constructor to use default value to initialize the map.
1.111 + */
1.112 + SymMap(const Graph& g, MapRegistry& r, const Value& v)
1.113 + : MapImpl(g, r, v) {}
1.114 +
1.115 + /** Constructor to copy a map of the same map type.
1.116 + */
1.117 + SymMap(const SymMap& copy)
1.118 + : MapImpl(static_cast<const MapImpl&>(copy)) {}
1.119 +
1.120 + /** Assign operator to copy a map of the same map type.
1.121 + */
1.122 + SymMap& operator=(const SymMap& copy) {
1.123 + MapImpl::operator=(static_cast<const MapImpl&>(copy));
1.124 + return *this;
1.125 + }
1.126 +
1.127 + /** Add a new key to the map. It called by the map registry.
1.128 + */
1.129 + void add(const KeyType& key) {
1.130 + int id = MapImpl::getGraph()->id(key);
1.131 + if (id & 1) return;
1.132 + MapImpl::add(key);
1.133 + }
1.134 +
1.135 + /** Erase a key from the map. It called by the map registry.
1.136 + */
1.137 + void erase(const KeyType& key) {
1.138 + int id = MapImpl::getGraph()->id(key);
1.139 + if (id & 1) return;
1.140 + MapImpl::add(key);
1.141 + }
1.142 + };
1.143 +
1.144 + /// @}
1.145 +}
1.146 +
1.147 +#endif