Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

utils.h
Go to the documentation of this file.
1 
32 #pragma once
33 
35 
36 #include <unordered_map>
37 
38 namespace ogdf {
39 namespace matching_blossom {
40 
42 template<class TWeight>
43 TWeight infinity() {
44  return std::numeric_limits<TWeight>::has_infinity ? std::numeric_limits<TWeight>::infinity()
45  : std::numeric_limits<TWeight>::max();
46 }
47 
50 template<class K, class V>
51 V* tryGetPointerFromMap(const std::unordered_map<K, V*>& map, const K& key) {
52  auto it = map.find(key);
53  if (it != map.end()) {
54  return it->second;
55  } else {
56  return nullptr;
57  }
58 }
59 
61 template<class TWeight>
62 TWeight getWeight(edge e, const EdgeArray<TWeight>& weights) {
63  return weights[e];
64 }
65 
67 template<class TWeight>
68 TWeight getWeight(edge e, const GraphAttributes& GA) {
69  if (std::numeric_limits<TWeight>::is_integer) {
71  return GA.intWeight(e);
72  }
73  } else if (GA.has(GraphAttributes::edgeIntWeight)) {
74  return GA.doubleWeight(e);
75  }
76  return 1;
77 }
78 
80 template<typename Key, typename Value>
81 class MapKeyIterator : public std::unordered_map<Key, Value>::iterator {
82  using MapIterator = typename std::unordered_map<Key, Value>::iterator;
83 
84 public:
87 
88  Key* operator->() { return (Key* const)&(MapIterator::operator->()->first); }
89 
90  Key operator*() { return MapIterator::operator*().first; }
91 };
92 
94 template<typename Key, typename Value>
95 class MapValueIterator : public std::unordered_map<Key, Value>::iterator {
96  using MapIterator = typename std::unordered_map<Key, Value>::iterator;
97 
98 public:
101 
102  Value* operator->() { return (Value* const)&(MapIterator::operator->()->second); }
103 
104  Value operator*() { return MapIterator::operator*().second; }
105 };
106 
108 template<template<typename, typename> class Iterator, typename Key, typename Value>
110  using iterator = Iterator<Key, Value>;
111 
112  std::unordered_map<Key, Value>& m_map;
113 
114 public:
115  BaseIteratorContainer(std::unordered_map<Key, Value>& map) : m_map(map) { }
116 
117  iterator begin() { return iterator(m_map.begin()); }
118 
119  iterator end() { return iterator(m_map.end()); }
120 
121  size_t size() { return m_map.size(); }
122 };
123 
124 template<typename Key, typename Value>
126 
127 template<typename Key, typename Value>
129 
130 }
131 }
ogdf::GraphAttributes::edgeIntWeight
static const long edgeIntWeight
Corresponds to edge attribute intWeight(edge).
Definition: GraphAttributes.h:119
ogdf::matching_blossom::BaseIteratorContainer::m_map
std::unordered_map< Key, Value > & m_map
Definition: utils.h:112
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:66
GraphAttributes.h
Declaration of class GraphAttributes which extends a Graph by additional attributes.
ogdf::matching_blossom::BaseIteratorContainer
Dummy class for scoped iteration of a std::unordered_map.
Definition: utils.h:109
ogdf::matching_blossom::MapKeyIterator::operator*
Key operator*()
Definition: utils.h:90
ogdf::matching_blossom::infinity
TWeight infinity()
Helper function to get the maximum value for a given weight type.
Definition: utils.h:43
ogdf::matching_blossom::MapValueIterator::MapValueIterator
MapValueIterator(MapIterator it_)
Definition: utils.h:100
ogdf::matching_blossom::MapValueIterator::MapIterator
typename std::unordered_map< Key, Value >::iterator MapIterator
Definition: utils.h:96
ogdf::matching_blossom::MapValueIterator::operator->
Value * operator->()
Definition: utils.h:102
ogdf::matching_blossom::MapKeyIterator
Iterator to access the keys of a std::unordered_map.
Definition: utils.h:81
ogdf::matching_blossom::MapValueIterator
Iterator to access the values of a std::unordered_map.
Definition: utils.h:95
ogdf::matching_blossom::getWeight
TWeight getWeight(edge e, const EdgeArray< TWeight > &weights)
Helper function to get the edge weight of e from the EdgeArray weights.
Definition: utils.h:62
ogdf::matching_blossom::MapKeyIterator::MapIterator
typename std::unordered_map< Key, Value >::iterator MapIterator
Definition: utils.h:82
ogdf::matching_blossom::BaseIteratorContainer::end
iterator end()
Definition: utils.h:119
ogdf::matching_blossom::tryGetPointerFromMap
V * tryGetPointerFromMap(const std::unordered_map< K, V * > &map, const K &key)
Return the pointer belonging to key key int the given map map, or nullptr if key does not exist.
Definition: utils.h:51
ogdf::matching_blossom::BaseIteratorContainer::size
size_t size()
Definition: utils.h:121
ogdf::matching_blossom::MapKeyIterator::MapKeyIterator
MapKeyIterator(MapIterator it_)
Definition: utils.h:86
ogdf::matching_blossom::MapValueIterator::MapValueIterator
MapValueIterator()
Definition: utils.h:99
ogdf::matching_blossom::MapKeyIterator::operator->
Key * operator->()
Definition: utils.h:88
ogdf::matching_blossom::BaseIteratorContainer::begin
iterator begin()
Definition: utils.h:117
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
ogdf::matching_blossom::MapKeyIterator::MapKeyIterator
MapKeyIterator()
Definition: utils.h:85
ogdf::matching_blossom::MapValueIterator::operator*
Value operator*()
Definition: utils.h:104
ogdf::GraphAttributes::intWeight
int intWeight(edge e) const
Returns the (integer) weight of edge e.
Definition: GraphAttributes.h:768
ogdf::matching_blossom::BaseIteratorContainer::iterator
Iterator< Key, Value > iterator
Definition: utils.h:110
ogdf::GraphAttributes::doubleWeight
double doubleWeight(edge e) const
Returns the (real number) weight of edge e.
Definition: GraphAttributes.h:786
ogdf::matching_blossom::BaseIteratorContainer::BaseIteratorContainer
BaseIteratorContainer(std::unordered_map< Key, Value > &map)
Definition: utils.h:115
ogdf::GraphAttributes::has
bool has(long attr) const
Returns true iff all attributes in attr are available.
Definition: GraphAttributes.h:194
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709