Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

utils.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Graph.h>
36 
37 #include <cstddef>
38 #include <limits>
39 #include <unordered_map>
40 
41 namespace ogdf {
42 namespace matching_blossom {
43 
45 template<class TWeight>
46 TWeight infinity() {
47  return std::numeric_limits<TWeight>::has_infinity ? std::numeric_limits<TWeight>::infinity()
48  : std::numeric_limits<TWeight>::max();
49 }
50 
53 template<class K, class V>
54 V* tryGetPointerFromMap(const std::unordered_map<K, V*>& map, const K& key) {
55  auto it = map.find(key);
56  if (it != map.end()) {
57  return it->second;
58  } else {
59  return nullptr;
60  }
61 }
62 
64 template<class TWeight>
65 TWeight getWeight(edge e, const EdgeArray<TWeight>& weights) {
66  return weights[e];
67 }
68 
70 template<class TWeight>
71 TWeight getWeight(edge e, const GraphAttributes& GA) {
72  if (std::numeric_limits<TWeight>::is_integer) {
74  return GA.intWeight(e);
75  }
76  } else if (GA.has(GraphAttributes::edgeIntWeight)) {
77  return GA.doubleWeight(e);
78  }
79  return 1;
80 }
81 
83 template<typename Key, typename Value>
84 class MapKeyIterator : public std::unordered_map<Key, Value>::iterator {
85  using MapIterator = typename std::unordered_map<Key, Value>::iterator;
86 
87 public:
90 
91  Key* operator->() { return (Key* const)&(MapIterator::operator->()->first); }
92 
93  Key operator*() { return MapIterator::operator*().first; }
94 };
95 
97 template<typename Key, typename Value>
98 class MapValueIterator : public std::unordered_map<Key, Value>::iterator {
99  using MapIterator = typename std::unordered_map<Key, Value>::iterator;
100 
101 public:
104 
105  Value* operator->() { return (Value* const)&(MapIterator::operator->()->second); }
106 
107  Value operator*() { return MapIterator::operator*().second; }
108 };
109 
111 template<template<typename, typename> class Iterator, typename Key, typename Value>
113  using iterator = Iterator<Key, Value>;
114 
115  std::unordered_map<Key, Value>& m_map;
116 
117 public:
118  BaseIteratorContainer(std::unordered_map<Key, Value>& map) : m_map(map) { }
119 
120  iterator begin() { return iterator(m_map.begin()); }
121 
122  iterator end() { return iterator(m_map.end()); }
123 
124  size_t size() { return m_map.size(); }
125 };
126 
127 template<typename Key, typename Value>
129 
130 template<typename Key, typename Value>
132 
133 }
134 }
ogdf::GraphAttributes::edgeIntWeight
static const long edgeIntWeight
Corresponds to edge attribute intWeight(edge).
Definition: GraphAttributes.h:125
ogdf::matching_blossom::BaseIteratorContainer::m_map
std::unordered_map< Key, Value > & m_map
Definition: utils.h:115
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:72
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:112
Graph.h
Includes declaration of graph class.
ogdf::matching_blossom::MapKeyIterator::operator*
Key operator*()
Definition: utils.h:93
ogdf::matching_blossom::infinity
TWeight infinity()
Helper function to get the maximum value for a given weight type.
Definition: utils.h:46
ogdf::matching_blossom::MapValueIterator::MapValueIterator
MapValueIterator(MapIterator it_)
Definition: utils.h:103
ogdf::matching_blossom::MapValueIterator::MapIterator
typename std::unordered_map< Key, Value >::iterator MapIterator
Definition: utils.h:99
ogdf::matching_blossom::MapValueIterator::operator->
Value * operator->()
Definition: utils.h:105
ogdf::matching_blossom::MapKeyIterator
Iterator to access the keys of a std::unordered_map.
Definition: utils.h:84
ogdf::matching_blossom::MapValueIterator
Iterator to access the values of a std::unordered_map.
Definition: utils.h:98
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:65
ogdf::matching_blossom::MapKeyIterator::MapIterator
typename std::unordered_map< Key, Value >::iterator MapIterator
Definition: utils.h:85
ogdf::matching_blossom::BaseIteratorContainer::end
iterator end()
Definition: utils.h:122
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:54
ogdf::matching_blossom::BaseIteratorContainer::size
size_t size()
Definition: utils.h:124
ogdf::matching_blossom::MapKeyIterator::MapKeyIterator
MapKeyIterator(MapIterator it_)
Definition: utils.h:89
ogdf::matching_blossom::MapValueIterator::MapValueIterator
MapValueIterator()
Definition: utils.h:102
ogdf::matching_blossom::MapKeyIterator::operator->
Key * operator->()
Definition: utils.h:91
ogdf::matching_blossom::BaseIteratorContainer::begin
iterator begin()
Definition: utils.h:120
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
ogdf::matching_blossom::MapKeyIterator::MapKeyIterator
MapKeyIterator()
Definition: utils.h:88
ogdf::matching_blossom::MapValueIterator::operator*
Value operator*()
Definition: utils.h:107
ogdf::GraphAttributes::intWeight
int intWeight(edge e) const
Returns the (integer) weight of edge e.
Definition: GraphAttributes.h:774
ogdf::matching_blossom::BaseIteratorContainer::iterator
Iterator< Key, Value > iterator
Definition: utils.h:113
ogdf::GraphAttributes::doubleWeight
double doubleWeight(edge e) const
Returns the (real number) weight of edge e.
Definition: GraphAttributes.h:792
ogdf::matching_blossom::BaseIteratorContainer::BaseIteratorContainer
BaseIteratorContainer(std::unordered_map< Key, Value > &map)
Definition: utils.h:118
ogdf::GraphAttributes::has
bool has(long attr) const
Returns true iff all attributes in attr are available.
Definition: GraphAttributes.h:200
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716