NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
MapTool.h
Go to the documentation of this file.
1//***************************************************************************************
4//***************************************************************************************
5
6#pragma once
7
8#include "NAGASH/Global.h"
9#include "NAGASH/Tool.h"
10
11namespace NAGASH
12{
16 class MapTool : public Tool
17 {
18 public:
20 MapTool(std::shared_ptr<MSGTool> MSG) : Tool(MSG) {}
21 unsigned int EditDistance(const TString &s1, const TString &s2) const;
22 unsigned int EditDistance(const std::string &s1, const std::string &s2) const;
23
24 template <typename T>
25 typename std::map<TString, T>::iterator FindMostSimilar(std::map<TString, T> &mymap, const TString &key);
26
27 template <typename T>
28 typename std::map<std::string, T>::iterator FindMostSimilar(std::map<std::string, T> &mymap, const std::string &key);
29
30 template <typename T1, typename T2>
31 std::vector<T1> GetListOfKeys(const std::map<T1, T2> &inputmap) const;
32
33 template <typename T1, typename T2>
34 std::vector<T2> GetListOfValues(const std::map<T1, T2> &inputmap) const;
35 };
36
42 template <typename T>
43 inline typename std::map<TString, T>::iterator MapTool::FindMostSimilar(std::map<TString, T> &mymap, const TString &key)
44 {
45 if (mymap.size() == 0)
46 {
47 MSGUser()->StartTitle("MapTool::FindMostSimilar");
48 MSGUser()->MSG_WARNING("The given map is empty, returning its end");
49 MSGUser()->EndTitle();
50 return std::end(mymap);
51 }
52
53 auto findresult = mymap.find(key);
54 if (findresult != mymap.end())
55 return findresult;
56 else
57 {
58 MSGUser()->StartTitle("MapTool::FindMostSimilar");
59 // find the most similiar key
60 typename std::map<TString, T>::iterator keypos;
61 int mindist = -1;
62 for (typename std::map<TString, T>::iterator it = mymap.begin(); it != mymap.end(); ++it)
63 {
64 if (mindist == -1)
65 {
66 mindist = EditDistance(it->first, key);
67 keypos = it;
68 }
69 else
70 {
71 int temp = EditDistance(it->first, key);
72 if (temp < mindist)
73 {
74 mindist = temp;
75 keypos = it;
76 }
77 }
78 }
79 MSGUser()->MSG(MSGLevel::WARNING, "No exact match of input string, turn to fuzzy search(the best match in the key). Key ", keypos->first, " is find (key ", key, " is the input)");
80 MSGUser()->EndTitle();
81 return keypos;
82 }
83 }
84
90 template <typename T>
91 inline typename std::map<std::string, T>::iterator MapTool::FindMostSimilar(std::map<std::string, T> &mymap, const std::string &key)
92 {
93 if (mymap.size() == 0)
94 {
95 MSGUser()->StartTitle("MapTool::FindMostSimilar");
96 MSGUser()->MSG_WARNING("The given map is empty, returning its end");
97 MSGUser()->EndTitle();
98 return std::end(mymap);
99 }
100
101 auto findresult = mymap.find(key);
102 if (findresult != mymap.end())
103 return findresult;
104 else
105 {
106 MSGUser()->StartTitle("MapTool::FindMostSimilar");
107 // find the most similiar key
108 typename std::map<std::string, T>::iterator keypos;
109 int mindist = -1;
110 for (typename std::map<std::string, T>::iterator it = mymap.begin(); it != mymap.end(); ++it)
111 {
112 if (mindist == -1)
113 {
114 mindist = EditDistance(it->first, key);
115 keypos = it;
116 }
117 else
118 {
119 int temp = EditDistance(it->first, key);
120 if (temp < mindist)
121 {
122 mindist = temp;
123 keypos = it;
124 }
125 }
126 }
127 MSGUser()->MSG(MSGLevel::WARNING, "No exact match of input string, turn to fuzzy search(the best match in the key). Key ", keypos->first, " is find (key ", key, " is the input)");
128 MSGUser()->EndTitle();
129 return keypos;
130 }
131 }
132
134 template <typename T1, typename T2>
135 inline std::vector<T1> MapTool::GetListOfKeys(const std::map<T1, T2> &inputmap) const
136 {
137 std::vector<T1> tempv;
138 for (auto &t : inputmap)
139 tempv.push_back(t.first);
140 return tempv;
141 }
142
144 template <typename T1, typename T2>
145 inline std::vector<T2> MapTool::GetListOfValues(const std::map<T1, T2> &inputmap) const
146 {
147 std::vector<T2> tempv;
148 for (auto &t : inputmap)
149 tempv.push_back(t.second);
150 return tempv;
151 }
152
153} // namespace NAGASH
Some global definitions.
Helper functions for std::map.
Definition MapTool.h:17
unsigned int EditDistance(const TString &s1, const TString &s2) const
Calculate the edit distance between two strings.
Definition MapTool.cxx:11
std::vector< T1 > GetListOfKeys(const std::map< T1, T2 > &inputmap) const
Get the vector of keys in a map.
Definition MapTool.h:135
std::vector< T2 > GetListOfValues(const std::map< T1, T2 > &inputmap) const
Get the vector of values in a map.
Definition MapTool.h:145
MapTool(std::shared_ptr< MSGTool > MSG)
Constructor.
Definition MapTool.h:20
std::map< TString, T >::iterator FindMostSimilar(std::map< TString, T > &mymap, const TString &key)
Find value to the most similar key in a map.
Definition MapTool.h:43
Provide interface for all tools in NAGASH.
Definition Tool.h:72
std::shared_ptr< MSGTool > MSGUser()
return the MSGTool inside.
Definition Tool.h:91