caseless.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @file
7  */
8 
9 #pragma once
10 #include <algorithm>
11 #include <cctype>
12 #include <functional>
13 #include <map>
14 #include <set>
15 #include <unordered_map>
16 
17 namespace InferenceEngine {
18 namespace details {
19 
20 /**
21  * @brief Provides caseless comparison for STL algorithms
22  *
23  * @tparam Key type, usually std::string
24  */
25 template <class Key>
26 class CaselessLess {
27 public:
28  bool operator()(const Key& a, const Key& b) const noexcept {
29  return std::lexicographical_compare(std::begin(a), std::end(a), std::begin(b), std::end(b),
30  [](const char& cha, const char& chb) {
31  return std::tolower(cha) < std::tolower(chb);
32  });
33  }
34 };
35 
36 /**
37  * provides caseless eq for stl algorithms
38  * @tparam Key
39  */
40 template <class Key>
41 class CaselessEq {
42 public:
43  bool operator()(const Key& a, const Key& b) const noexcept {
44  return a.size() == b.size() &&
45  std::equal(std::begin(a), std::end(a), std::begin(b), [](const char& cha, const char& chb) {
46  return std::tolower(cha) == std::tolower(chb);
47  });
48  }
49 };
50 
51 /**
52  * To hash caseless
53  */
54 template <class T>
55 class CaselessHash : public std::hash<T> {
56 public:
57  size_t operator()(T __val) const noexcept {
58  T lc;
59  std::transform(std::begin(__val), std::end(__val), std::back_inserter(lc), [](typename T::value_type ch) {
60  return std::tolower(ch);
61  });
62  return std::hash<T>()(lc);
63  }
64 };
65 
66 template <class Key, class Value>
67 using caseless_unordered_map = std::unordered_map<Key, Value, CaselessHash<Key>, CaselessEq<Key>>;
68 
69 template <class Key, class Value>
70 using caseless_unordered_multimap = std::unordered_multimap<Key, Value, CaselessHash<Key>, CaselessEq<Key>>;
71 
72 template <class Key, class Value>
73 using caseless_map = std::map<Key, Value, CaselessLess<Key>>;
74 
75 template <class Key>
76 using caseless_set = std::set<Key, CaselessLess<Key>>;
77 
78 } // namespace details
79 } // namespace InferenceEngine
Inference Engine API.
Definition: ie_argmax_layer.hpp:15