ChampSim
algorithm.h
Go to the documentation of this file.
1 /*
2  * Copyright 2023 The ChampSim Contributors
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 #ifndef UTIL_ALGORITHM_H
18 #define UTIL_ALGORITHM_H
19 
20 #include <algorithm>
21 
22 #include "util/span.h"
23 
24 namespace champsim
25 {
26 template <typename InputIt, typename OutputIt, typename F>
27 auto extract_if(InputIt begin, InputIt end, OutputIt d_begin, F func)
28 {
29  begin = std::find_if(begin, end, func);
30  for (auto i = begin; i != end; ++i) {
31  if (func(*i))
32  *d_begin++ = std::move(*i);
33  else
34  *begin++ = std::move(*i);
35  }
36  return std::pair{begin, d_begin};
37 }
38 
39 template <typename R, typename Output, typename F, typename G>
40 long int transform_while_n(R& queue, Output out, long int sz, F&& test_func, G&& transform_func)
41 {
42  auto [begin, end] = champsim::get_span_p(std::begin(queue), std::end(queue), sz, std::forward<F>(test_func));
43  auto retval = std::distance(begin, end);
44  std::transform(begin, end, out, std::forward<G>(transform_func));
45  queue.erase(begin, end);
46  return retval;
47 }
48 } // namespace champsim
49 
50 #endif
Definition: champsim.h:24
auto extract_if(InputIt begin, InputIt end, OutputIt d_begin, F func)
Definition: algorithm.h:27
std::pair< It, It > get_span_p(It begin, It end, typename std::iterator_traits< It >::difference_type sz, F &&func)
Definition: span.h:37
long int transform_while_n(R &queue, Output out, long int sz, F &&test_func, G &&transform_func)
Definition: algorithm.h:40