OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Intervals.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
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 
59 #pragma once
60 
61 #include <iterator>
62 #include <limits>
63 #include <type_traits>
64 
66 struct Interval {
67  T const begin;
68  T const end;
69  U const index; // [0, n_workers)
70  U size() const { return U(end) - U(begin); }
71 };
72 
74 class Intervals {
75  T const begin_;
76  U const total_size_;
77  U const quot_;
78  U const rem_;
79 
80  Intervals(T begin, T end, U n_workers)
81  : begin_(begin)
82  , total_size_(begin < end && n_workers ? end - begin : 0)
83  , quot_(n_workers ? total_size_ / n_workers : 0)
84  , rem_(n_workers ? total_size_ % n_workers : 0) {
85  static_assert(std::is_integral<T>::value);
86  }
87 
88  public:
89  class Iterator {
91  U const quot_;
92  U rem_;
93  U index{0};
94 
95  public:
96  using iterator_category = std::input_iterator_tag;
98  using difference_type = std::ptrdiff_t;
99  using pointer = value_type*;
101 
102  Iterator(T begin, U quot, U rem) : begin_(begin), quot_(quot), rem_(rem) {}
104  return {begin_, T(begin_ + quot_ + bool(rem_)), index};
105  }
106  void operator++() {
107  begin_ += quot_ + (rem_ && rem_--);
108  ++index;
109  }
110  bool operator==(Iterator const& rhs) const { return begin_ == rhs.begin_; }
111  bool operator!=(Iterator const& rhs) const { return begin_ != rhs.begin_; }
112  };
113 
114  bool empty() const { return !total_size_; }
115  Iterator begin() const { return {begin_, quot_, rem_}; }
116  Iterator end() const { return {static_cast<T>(begin_ + total_size_), quot_, 0}; }
117  template <typename T1, typename U1>
118  friend Intervals<T1> makeIntervals(T1 begin, T1 end, std::size_t n_workers);
119 };
120 
122 Intervals<T> makeIntervals(T begin, T end, std::size_t n_workers) {
123  if constexpr (sizeof(U) < sizeof(std::size_t)) { // NOLINT
124  if (std::numeric_limits<U>::max() < n_workers) {
125  n_workers = std::numeric_limits<U>::max();
126  }
127  }
128  return {begin, end, static_cast<U>(n_workers)};
129 }
U const index
Definition: Intervals.h:69
U size() const
Definition: Intervals.h:70
std::input_iterator_tag iterator_category
Definition: Intervals.h:96
bool operator!=(Iterator const &rhs) const
Definition: Intervals.h:111
T const begin
Definition: Intervals.h:67
Iterator begin() const
Definition: Intervals.h:115
T const begin_
Definition: Intervals.h:75
Intervals(T begin, T end, U n_workers)
Definition: Intervals.h:80
Interval< T > operator*() const
Definition: Intervals.h:103
U const total_size_
Definition: Intervals.h:76
T const end
Definition: Intervals.h:68
Iterator(T begin, U quot, U rem)
Definition: Intervals.h:102
bool operator==(Iterator const &rhs) const
Definition: Intervals.h:110
Iterator end() const
Definition: Intervals.h:116
U const rem_
Definition: Intervals.h:78
friend Intervals< T1 > makeIntervals(T1 begin, T1 end, std::size_t n_workers)
bool empty() const
Definition: Intervals.h:114
U const quot_
Definition: Intervals.h:77
std::ptrdiff_t difference_type
Definition: Intervals.h:98