forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathie_algorithm.hpp
125 lines (114 loc) · 3.45 KB
/
ie_algorithm.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
/**
* @brief A header file with simple helper functions for STL containters
* @file ie_algorithm.hpp
*/
#pragma once
#include <algorithm>
#include <functional>
#include <numeric>
namespace InferenceEngine {
/**
* @brief A namespace with non-public Inference Engine Plugin API
* @ingroup ie_dev_api
*/
namespace details {
/**
* @brief Simple helper function to check element presence in container
* container must provede stl-compliant find member function
*
* @param container - Container to check
* @param element - element to check
*
* @return true if element present in container
*/
template <typename C, typename T>
bool contains(const C& container, const T& element) {
return container.find(element) != container.end();
}
/**
* @brief Associative containers doesnt work with remove_if algorithm
* @tparam ContainerT
* @tparam PredicateT
* @param data An associative container
* @param predicate A predicate to remove values conditionally
*/
template <typename Container, typename PredicateT>
inline void erase_if(Container& data, const PredicateT& predicate) {
for (auto it = std::begin(data); it != std::end(data);) {
if (predicate(*it)) {
it = data.erase(it);
} else {
++it;
}
}
}
/**
* @brief Multiplies container
*
* @param[in] beg The `begin` iterator
* @param[in] en The `end` iterator
*
* @tparam TIterator An iterator type
*
* @return A result of multiplication.
*/
template <typename TIterator>
auto product(TIterator beg, TIterator en) -> typename std::remove_reference<decltype(*beg)>::type {
return std::accumulate(beg,
en,
static_cast<typename std::remove_reference<decltype(*beg)>::type>(1),
std::multiplies<typename std::remove_reference<decltype(*beg)>::type>());
}
/**
* @brief Clips element to be in range `[min, max]`
*
* @param idx The pointer to element.
* @param[in] min The minimum value
* @param[in] max The maximum value
*/
inline void clipping(int* idx, const int min, const int max) {
(*idx) = ((*idx) > min) ? (*idx) : min;
(*idx) = ((*idx) < max) ? (*idx) : (max - 1);
}
/**
* @brief Set containers intersection
* @tparam Set
* @param lhs First set container
* @param rhs Second set container
* @return Set intersection
*/
template <typename Set>
static Set Intersection(const Set& lhs, const Set& rhs) {
Set result;
const auto& minSizeSet = (lhs.size() < rhs.size()) ? lhs : rhs;
const auto& maxSizeSet = (lhs.size() >= rhs.size()) ? lhs : rhs;
for (auto&& val : minSizeSet) {
if (InferenceEngine::details::contains(maxSizeSet, val)) {
result.insert(val);
}
}
return result;
}
/**
* @brief Check whether two sets intersect
* @tparam Set
* @param lhs First set container
* @param rhs Second set container
* @return true if two sets interesect false otherwise
*/
template <typename Set>
static bool Intersects(const Set& lhs, const Set& rhs) {
const auto& minSizeSet = (lhs.size() < rhs.size()) ? lhs : rhs;
const auto& maxSizeSet = (lhs.size() >= rhs.size()) ? lhs : rhs;
for (auto&& val : minSizeSet) {
if (InferenceEngine::details::contains(maxSizeSet, val)) {
return true;
}
}
return false;
}
} // namespace details
} // namespace InferenceEngine