Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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
41namespace ogdf {
42namespace matching_blossom {
43
45template<class TWeight>
46TWeight infinity() {
47 return std::numeric_limits<TWeight>::has_infinity ? std::numeric_limits<TWeight>::infinity()
48 : std::numeric_limits<TWeight>::max();
49}
50
53template<class K, class V>
54V* 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
64template<class TWeight>
65TWeight getWeight(edge e, const EdgeArray<TWeight>& weights) {
66 return weights[e];
67}
68
70template<class TWeight>
71TWeight 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
83template<typename Key, typename Value>
84class MapKeyIterator : public std::unordered_map<Key, Value>::iterator {
85 using MapIterator = typename std::unordered_map<Key, Value>::iterator;
86
87public:
90
91 Key* operator->() { return (Key* const)&(MapIterator::operator->()->first); }
92
93 Key operator*() { return MapIterator::operator*().first; }
94};
95
97template<typename Key, typename Value>
98class MapValueIterator : public std::unordered_map<Key, Value>::iterator {
99 using MapIterator = typename std::unordered_map<Key, Value>::iterator;
100
101public:
104
105 Value* operator->() { return (Value* const)&(MapIterator::operator->()->second); }
106
107 Value operator*() { return MapIterator::operator*().second; }
108};
109
111template<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
117public:
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
127template<typename Key, typename Value>
129
130template<typename Key, typename Value>
132
133}
134}
Includes declaration of graph class.
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Class for the representation of edges.
Definition Graph_d.h:364
Stores additional attributes of a graph (like layout information).
int intWeight(edge e) const
Returns the (integer) weight of edge e.
double doubleWeight(edge e) const
Returns the (real number) weight of edge e.
bool has(long attr) const
Returns true iff all attributes in attr are available.
static const long edgeIntWeight
Corresponds to edge attribute intWeight(edge).
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
Dummy class for scoped iteration of a std::unordered_map.
Definition utils.h:112
BaseIteratorContainer(std::unordered_map< Key, Value > &map)
Definition utils.h:118
std::unordered_map< Key, Value > & m_map
Definition utils.h:115
Iterator to access the keys of a std::unordered_map.
Definition utils.h:84
typename std::unordered_map< Key, Value >::iterator MapIterator
Definition utils.h:85
Iterator to access the values of a std::unordered_map.
Definition utils.h:98
typename std::unordered_map< Key, Value >::iterator MapIterator
Definition utils.h:99
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
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
TWeight infinity()
Helper function to get the maximum value for a given weight type.
Definition utils.h:46
The namespace for all OGDF objects.