Interface Documentation
Version: invalid
offset.hh
Go to the documentation of this file.
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Triad National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
18 #include <cassert>
19 #include <cstddef>
20 #include <cstdint>
21 #include <utility>
22 
23 namespace flecsi {
24 namespace util {
25 
33 template<size_t COUNT_BITS>
34 class offset
35 {
36 public:
37  static_assert(COUNT_BITS <= 32, "COUNT_BITS max exceeded");
38 
46  static constexpr uint32_t count_mask = (1ul << COUNT_BITS) - 1;
47 
51  static constexpr uint64_t start_max = (1ul << (64 - COUNT_BITS)) - 1;
52 
53  offset() : o_(0ul) {}
54 
61  offset(uint64_t start, uint32_t count) : o_(start << COUNT_BITS | count) {
62  assert(count <= count_mask);
63  assert(start <= start_max);
64  }
65 
75  offset(const offset & prev, uint32_t count) : offset(prev.end(), count) {}
76 
82  uint64_t start() const {
83  return o_ >> COUNT_BITS;
84  }
85 
91  uint32_t count() const {
92  return o_ & count_mask;
93  }
94 
102  uint64_t end() const {
103  return start() + count();
104  }
105 
111  void set_count(uint32_t count) {
112  assert(count <= count_mask);
113  o_ = (o_ & ~count_mask) | count;
114  }
115 
121  void set_offset(uint64_t start) {
122  assert(start <= start_max);
123  o_ = (o_ & count_mask) | (start << COUNT_BITS);
124  }
125 
132  std::pair<size_t, size_t> range() const {
133  uint64_t s = start();
134  return {s, s + count()};
135  }
136 
137 private:
143  uint64_t o_;
144 };
145 
146 } // namespace util
147 } // namespace flecsi
uint32_t count() const
Get the count (number) of elements represented by this offset range.
Definition: offset.hh:91
std::pair< size_t, size_t > range() const
Get the range (start index to end index, inclusive) represented by this offset range.
Definition: offset.hh:132
offset represents an offset range (a start index plus a count of elements) in a single uint64_t...
Definition: offset.hh:34
void set_count(uint32_t count)
Set the count of this offset range.
Definition: offset.hh:111
uint64_t start() const
Get the start index of the offset range.
Definition: offset.hh:82
uint64_t end() const
Get the index pointing one element past the final element represented by this offset range...
Definition: offset.hh:102
static constexpr uint32_t count_mask
Bitmask used to get the count bits, this is also the maximum value for the count value.
Definition: offset.hh:46
offset(uint64_t start, uint32_t count)
Construct a new offset range from a start index and a count.
Definition: offset.hh:61
offset(const offset &prev, uint32_t count)
Construct a new offset range from a previous offset range and a count. The start index of this offset...
Definition: offset.hh:75
static constexpr uint64_t start_max
The maximum value of the start index value.
Definition: offset.hh:51
Definition: control.hh:31
void set_offset(uint64_t start)
Set the start index of this offset range.
Definition: offset.hh:121