Doxygen
Toggle main menu visibility
Loading...
Searching...
No Matches
symbolmap.h
Go to the documentation of this file.
1
/******************************************************************************
2
*
3
* Copyright (C) 1997-2020 by Dimitri van Heesch.
4
*
5
* Permission to use, copy, modify, and distribute this software and its
6
* documentation under the terms of the GNU General Public License is hereby
7
* granted. No representations are made about the suitability of this software
8
* for any purpose. It is provided "as is" without express or implied warranty.
9
* See the GNU General Public License for more details.
10
*
11
* Documents produced by Doxygen are derivative works derived from the
12
* input used in their production; they are not affected by this license.
13
*
14
*/
15
16
#ifndef SYMBOLMAP_H
17
#define SYMBOLMAP_H
18
19
#include <algorithm>
20
#include <string>
21
#include <unordered_map>
22
#include <utility>
23
#include <vector>
24
25
//! Class implementing a symbol map that maps symbol names to objects.
26
//! Symbol names do not have to be unique.
27
//! Supports adding symbols with add(), removing symbols with remove(), and
28
//! finding symbols with find().
29
template
<
class
T>
30
class
SymbolMap
31
{
32
public
:
33
using
Ptr
= T *;
34
using
VectorPtr
= std::vector<Ptr>;
35
using
Map
= std::unordered_map<std::string,VectorPtr>;
36
using
iterator
=
typename
Map::iterator;
37
using
const_iterator
=
typename
Map::const_iterator;
38
39
//! Add a symbol \a def into the map under key \a name
40
void
add
(
const
DString
&name,
Ptr
def)
41
{
42
auto
it =
m_map
.find(name.
str
());
43
if
(it!=
m_map
.end())
44
{
45
it->second.push_back(def);
46
}
47
else
48
{
49
m_map
.emplace(name.
str
(),
VectorPtr
({def}));
50
}
51
}
52
53
//! Remove a symbol \a def from the map that was stored under key \a name
54
void
remove
(
const
DString
&name,
Ptr
def)
55
{
56
auto
it1 =
m_map
.find(name.
str
());
57
if
(it1!=
m_map
.end())
58
{
59
VectorPtr
&v = it1->second;
60
auto
it2 = std::find(v.begin(),v.end(),def);
61
if
(it2!=v.end())
62
{
63
v.erase(it2);
64
if
(v.empty())
65
{
66
m_map
.erase(it1);
67
}
68
}
69
}
70
}
71
72
//! Find the list of symbols stored under key \a name
73
//! Returns a pair of iterators pointing to the start and end of the range of matching symbols
74
const
VectorPtr
&
find
(
const
DString
&name)
75
{
76
ASSERT
(
m_noMatch
.empty());
77
auto
it =
m_map
.find(name.
str
());
78
return
it==
m_map
.end() ?
m_noMatch
: it->second;
79
}
80
81
iterator
begin
() {
return
m_map
.begin(); }
82
iterator
end
() {
return
m_map
.end(); }
83
const_iterator
begin
()
const
{
return
m_map
.cbegin(); }
84
const_iterator
end
()
const
{
return
m_map
.cend(); }
85
bool
empty
()
const
{
return
m_map
.empty(); }
86
87
private
:
88
Map
m_map
;
89
VectorPtr
m_noMatch
;
90
};
91
92
#endif
DString
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition
dstring.h:84
DString::str
const std::string & str() const
Definition
dstring.h:645
SymbolMap
Definition
symbolmap.h:31
SymbolMap::find
const VectorPtr & find(const DString &name)
Definition
symbolmap.h:74
SymbolMap::m_noMatch
VectorPtr m_noMatch
Definition
symbolmap.h:89
SymbolMap::const_iterator
typename Map::const_iterator const_iterator
Definition
symbolmap.h:37
SymbolMap::end
iterator end()
Definition
symbolmap.h:82
SymbolMap::begin
const_iterator begin() const
Definition
symbolmap.h:83
SymbolMap::remove
void remove(const DString &name, Ptr def)
Remove a symbol def from the map that was stored under key name.
Definition
symbolmap.h:54
SymbolMap::VectorPtr
std::vector< Ptr > VectorPtr
Definition
symbolmap.h:34
SymbolMap::iterator
typename Map::iterator iterator
Definition
symbolmap.h:36
SymbolMap::begin
iterator begin()
Definition
symbolmap.h:81
SymbolMap::Map
std::unordered_map< std::string, VectorPtr > Map
Definition
symbolmap.h:35
SymbolMap::end
const_iterator end() const
Definition
symbolmap.h:84
SymbolMap::add
void add(const DString &name, Ptr def)
Add a symbol def into the map under key name.
Definition
symbolmap.h:40
SymbolMap::m_map
Map m_map
Definition
symbolmap.h:88
SymbolMap::empty
bool empty() const
Definition
symbolmap.h:85
SymbolMap::Ptr
T * Ptr
Definition
symbolmap.h:33
ASSERT
#define ASSERT(x)
Definition
message.h:142
src
symbolmap.h
Generated by
1.19.0