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