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