CMS 3D CMS Logo

VecArray.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_VecArray_h
2 #define FWCore_Utilities_VecArray_h
3 
4 #include <utility>
5 #include <stdexcept>
6 #include <string>
7 #include <cstddef>
8 
9 namespace edm {
27  template <typename T, unsigned int N>
28  class VecArray {
29  T data_[N];
30  unsigned int size_;
31 
32  public:
33  using value_type = T;
34  using size_type = unsigned int;
35  using difference_type = std::ptrdiff_t;
37  using const_reference = const value_type&;
38  using pointer = value_type*;
39  using const_pointer = const value_type*;
40  using iterator = value_type*;
41  using const_iterator = const value_type*;
42 
43  VecArray() : data_{}, size_{0} {}
44 
45  // Not range-checked, undefined behaviour if access beyond size()
46  reference operator[](size_type pos) { return data_[pos]; }
47  // Not range-checked, undefined behaviour if access beyond size()
48  const_reference operator[](size_type pos) const { return data_[pos]; }
49  // Undefined behaviour if size()==0
50  reference front() { return data_[0]; }
51  // Undefined behaviour if size()==0
52  const_reference front() const { return data_[0]; }
53 
54  // Undefined behaviour if size()==0
55  reference back() { return data_[size_ - 1]; }
56  // Undefined behaviour if size()==0
57  const_reference back() const { return data_[size_ - 1]; }
58  pointer data() { return data_; }
59  const_pointer data() const { return data_; }
60 
61  iterator begin() noexcept { return data_; }
62  const_iterator begin() const noexcept { return data_; }
63  const_iterator cbegin() const noexcept { return data_; }
64 
65  iterator end() noexcept { return begin() + size_; }
66  const_iterator end() const noexcept { return begin() + size_; }
67  const_iterator cend() const noexcept { return cbegin() + size_; }
68 
69  constexpr bool empty() const noexcept { return size_ == 0; }
70  constexpr size_type size() const noexcept { return size_; }
71  static constexpr size_type capacity() noexcept { return N; }
72 
73  void clear() { size_ = 0; }
74 
75  // Throws if size()==N
76  void push_back(const T& value) {
77  if (size_ >= N)
78  throw std::length_error("push_back on already-full VecArray (N=" + std::to_string(N) + ")");
79  push_back_unchecked(value);
80  }
81 
82  // Undefined behaviour if size()==N
83  void push_back_unchecked(const T& value) {
84  data_[size_] = value;
85  ++size_;
86  }
87 
88  // Throws if size()==N
89  template <typename... Args>
90  void emplace_back(Args&&... args) {
91  if (size_ >= N)
92  throw std::length_error("emplace_back on already-full VecArray (N=" + std::to_string(N) + ")");
93  emplace_back_unchecked(std::forward<Args>(args)...);
94  }
95 
96  // Undefined behaviour if size()==N
97  template <typename... Args>
98  void emplace_back_unchecked(Args&&... args) {
99  data_[size_] = T(std::forward<Args>(args)...);
100  ++size_;
101  }
102 
103  // Undefined behaviour if size()==0
104  void pop_back() { --size_; }
105 
106  void resize(unsigned int size) {
107  if (size > N)
108  throw std::length_error("Requesting size " + std::to_string(size) + " while maximum allowed is " +
109  std::to_string(N));
110 
111  while (size < size_)
112  pop_back();
113  size_ = size;
114  }
115 
116  void swap(VecArray& other) noexcept(noexcept(std::swap(data_, other.data_)) &&
117  noexcept(std::swap(size_, other.size_))) {
118  std::swap(data_, other.data_);
119  std::swap(size_, other.size_);
120  }
121  };
122 } // namespace edm
123 
124 #endif
std::pair< int, int > value_type
Definition: VecArray.h:33
void pop_back()
Definition: VecArray.h:104
unsigned int size_
Definition: VecArray.h:30
iterator end() noexcept
Definition: VecArray.h:65
const_reference back() const
Definition: VecArray.h:57
void swap(VecArray &other) noexcept(noexcept(std::swap(data_, other.data_))&& noexcept(std::swap(size_, other.size_)))
Definition: VecArray.h:116
const_iterator begin() const noexcept
Definition: VecArray.h:62
void resize(unsigned int size)
Definition: VecArray.h:106
const_iterator cend() const noexcept
Definition: VecArray.h:67
void emplace_back(Args &&...args)
Definition: VecArray.h:90
const_iterator end() const noexcept
Definition: VecArray.h:66
void push_back(const T &value)
Definition: VecArray.h:76
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
reference operator[](size_type pos)
Definition: VecArray.h:46
reference front()
Definition: VecArray.h:50
void clear()
Definition: VecArray.h:73
const_iterator cbegin() const noexcept
Definition: VecArray.h:63
Definition: value.py:1
constexpr size_type size() const noexcept
Definition: VecArray.h:70
const_pointer data() const
Definition: VecArray.h:59
pointer data()
Definition: VecArray.h:58
const_reference operator[](size_type pos) const
Definition: VecArray.h:48
#define noexcept
#define N
Definition: blowfish.cc:9
void emplace_back_unchecked(Args &&...args)
Definition: VecArray.h:98
constexpr bool empty() const noexcept
Definition: VecArray.h:69
HLT enums.
void push_back_unchecked(const T &value)
Definition: VecArray.h:83
reference back()
Definition: VecArray.h:55
long double T
const_reference front() const
Definition: VecArray.h:52
static constexpr size_type capacity() noexcept
Definition: VecArray.h:71
#define constexpr
T data_[N]
Definition: VecArray.h:29
iterator begin() noexcept
Definition: VecArray.h:61