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