ChampSim
span.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_SPAN_H
18 #define UTIL_SPAN_H
19 
20 #include <algorithm>
21 #include <cassert>
22 #include <iterator>
23 #include <limits>
24 
25 namespace champsim
26 {
27 template <typename It>
28 std::pair<It, It> get_span(It begin, It end, typename std::iterator_traits<It>::difference_type sz)
29 {
30  assert(std::distance(begin, end) >= 0);
31  assert(sz >= 0);
32  auto distance = std::min(std::distance(begin, end), sz);
33  return {begin, std::next(begin, distance)};
34 }
35 
36 template <typename It, typename F>
37 std::pair<It, It> get_span_p(It begin, It end, typename std::iterator_traits<It>::difference_type sz, F&& func)
38 {
39  auto [span_begin, span_end] = get_span(begin, end, sz);
40  return {span_begin, std::find_if_not(span_begin, span_end, std::forward<F>(func))};
41 }
42 
43 template <typename It, typename F>
44 std::pair<It, It> get_span_p(It begin, It end, F&& func)
45 {
46  return get_span_p(begin, end, std::numeric_limits<typename std::iterator_traits<It>::difference_type>::max(), std::forward<F>(func));
47 }
48 } // namespace champsim
49 
50 #endif
Definition: champsim.h:24
std::pair< It, It > get_span(It begin, It end, typename std::iterator_traits< It >::difference_type sz)
Definition: span.h:28
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