• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <algorithm>
20 #include <tuple>
21 #include <utility>
22 #include <vector>
23 
24 namespace aidl::android::hardware::audio::effect {
25 
26 template <typename T>
isInRange(const T & value,const T & low,const T & high)27 bool isInRange(const T& value, const T& low, const T& high) {
28     return (value >= low) && (value <= high);
29 }
30 
31 template <typename T, std::size_t... Is>
isTupleInRange(const T & test,const T & min,const T & max,std::index_sequence<Is...>)32 bool isTupleInRange(const T& test, const T& min, const T& max, std::index_sequence<Is...>) {
33     return (isInRange(std::get<Is>(test), std::get<Is>(min), std::get<Is>(max)) && ...);
34 }
35 
36 template <typename T, std::size_t TupSize = std::tuple_size_v<T>>
isTupleInRange(const T & test,const T & min,const T & max)37 bool isTupleInRange(const T& test, const T& min, const T& max) {
38     return isTupleInRange(test, min, max, std::make_index_sequence<TupSize>{});
39 }
40 
41 template <typename T, typename F>
isTupleInRange(const std::vector<T> & cfgs,const T & min,const T & max,const F & func)42 bool isTupleInRange(const std::vector<T>& cfgs, const T& min, const T& max, const F& func) {
43     auto minT = func(min), maxT = func(max);
44     return std::all_of(cfgs.cbegin(), cfgs.cend(),
45                        [&](const T& cfg) { return isTupleInRange(func(cfg), minT, maxT); });
46 }
47 
48 }  // namespace aidl::android::hardware::audio::effect
49