Doxygen
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 <unordered_map>
21#include <vector>
22#include <string>
23#include <utility>
24#include <cassert>
25
26//! Class implementing a symbol map that maps symbol names to objects.
27//! Symbol names do not have to be unique.
28//! Supports adding symbols with add(), removing symbols with remove(), and
29//! finding symbols with find().
30template<class T>
32{
33 public:
34 using Ptr = T *;
35 using VectorPtr = std::vector<Ptr>;
36 using Map = std::unordered_map<std::string,VectorPtr>;
37 using iterator = typename Map::iterator;
38 using const_iterator = typename Map::const_iterator;
39
40 //! Add a symbol \a def into the map under key \a name
41 void add(const QCString &name,Ptr def)
42 {
43 auto it = m_map.find(name.str());
44 if (it!=m_map.end())
45 {
46 it->second.push_back(def);
47 }
48 else
49 {
50 m_map.emplace(name.str(),VectorPtr({def}));
51 }
52 }
53
54 //! Remove a symbol \a def from the map that was stored under key \a name
55 void remove(const QCString &name,Ptr def)
56 {
57 auto it1 = m_map.find(name.str());
58 if (it1!=m_map.end())
59 {
60 VectorPtr &v = it1->second;
61 auto it2 = std::find(v.begin(),v.end(),def);
62 if (it2!=v.end())
63 {
64 v.erase(it2);
65 if (v.empty())
66 {
67 m_map.erase(it1);
68 }
69 }
70 }
71 }
72
73 //! Find the list of symbols stored under key \a name
74 //! Returns a pair of iterators pointing to the start and end of the range of matching symbols
75 const VectorPtr &find(const QCString &name)
76 {
77 assert(m_noMatch.empty());
78 auto it = m_map.find(name.str());
79 return it==m_map.end() ? m_noMatch : it->second;
80 }
81
82 iterator begin() { return m_map.begin(); }
83 iterator end() { return m_map.end(); }
84 const_iterator begin() const { return m_map.cbegin(); }
85 const_iterator end() const { return m_map.cend(); }
86 bool empty() const { return m_map.empty(); }
87
88 private:
91};
92
93#endif
This is an alternative implementation of QCString.
Definition qcstring.h:101
const std::string & str() const
Definition qcstring.h:537
Class implementing a symbol map that maps symbol names to objects.
Definition symbolmap.h:32
void remove(const QCString &name, Ptr def)
Remove a symbol def from the map that was stored under key name.
Definition symbolmap.h:55
VectorPtr m_noMatch
Definition symbolmap.h:90
typename Map::const_iterator const_iterator
Definition symbolmap.h:38
void add(const QCString &name, Ptr def)
Add a symbol def into the map under key name.
Definition symbolmap.h:41
iterator end()
Definition symbolmap.h:83
const_iterator begin() const
Definition symbolmap.h:84
const VectorPtr & find(const QCString &name)
Find the list of symbols stored under key name Returns a pair of iterators pointing to the start and ...
Definition symbolmap.h:75
std::vector< Ptr > VectorPtr
Definition symbolmap.h:35
typename Map::iterator iterator
Definition symbolmap.h:37
iterator begin()
Definition symbolmap.h:82
std::unordered_map< std::string, VectorPtr > Map
Definition symbolmap.h:36
const_iterator end() const
Definition symbolmap.h:85
Map m_map
Definition symbolmap.h:89
bool empty() const
Definition symbolmap.h:86