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 <cctype>
8 #include <functional>
9 #include <map>
10 #include <set>
11 #include <unordered_map>
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), std::end(a), std::begin(b), std::end(b),
25  [](const char& cha, const char& chb) {
26  return std::tolower(cha) < std::tolower(chb);
27  });
28  }
29 };
30 
31 /**
32  * provides caseless eq for stl algorithms
33  * @tparam Key
34  */
35 template <class Key>
36 class CaselessEq : public std::binary_function<Key, Key, bool> {
37 public:
38  bool operator()(const Key& a, const Key& b) const noexcept {
39  return a.size() == b.size() &&
40  std::equal(std::begin(a), std::end(a), std::begin(b), [](const char& cha, const char& chb) {
41  return std::tolower(cha) == std::tolower(chb);
42  });
43  }
44 };
45 
46 /**
47  * To hash caseless
48  */
49 template <class T>
50 class CaselessHash : public std::hash<T> {
51 public:
52  size_t operator()(T __val) const noexcept {
53  T lc;
54  std::transform(std::begin(__val), std::end(__val), std::back_inserter(lc), [](typename T::value_type ch) {
55  return std::tolower(ch);
56  });
57  return std::hash<T>()(lc);
58  }
59 };
60 
61 template <class Key, class Value>
62 using caseless_unordered_map = std::unordered_map<Key, Value, CaselessHash<Key>, CaselessEq<Key>>;
63 
64 template <class Key, class Value>
65 using caseless_unordered_multimap = std::unordered_multimap<Key, Value, CaselessHash<Key>, CaselessEq<Key>>;
66 
67 template <class Key, class Value>
68 using caseless_map = std::map<Key, Value, CaselessLess<Key>>;
69 
70 template <class Key>
71 using caseless_set = std::set<Key, CaselessLess<Key>>;
72 
73 } // namespace details
74 } // namespace InferenceEngine
Inference Engine API.
Definition: ie_argmax_layer.hpp:11