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() {
74  size_ = 0;
75  }
76 
77  // Throws if size()==N
78  void push_back(const T& value) {
79  if(size_ >= N)
80  throw std::length_error("push_back on already-full VecArray (N="+std::to_string(N)+")");
81  push_back_unchecked(value);
82  }
83 
84  // Undefined behaviour if size()==N
85  void push_back_unchecked(const T& value) {
86  data_[size_] = value;
87  ++size_;
88  }
89 
90  // Throws if size()==N
91  template <typename ...Args>
92  void emplace_back(Args&&... args) {
93  if(size_ >= N)
94  throw std::length_error("emplace_back on already-full VecArray (N="+std::to_string(N)+")");
95  emplace_back_unchecked(std::forward<Args>(args)...);
96  }
97 
98  // Undefined behaviour if size()==N
99  template <typename ...Args>
100  void emplace_back_unchecked(Args&&... args) {
101  data_[size_] = T(std::forward<Args>(args)...);
102  ++size_;
103  }
104 
105  // Undefined behaviour if size()==0
106  void pop_back() {
107  --size_;
108  }
109 
110  void resize(unsigned int size) {
111  if(size > N)
112  throw std::length_error("Requesting size "+std::to_string(size)+" while maximum allowed is "+std::to_string(N));
113 
114  while(size < size_)
115  pop_back();
116  size_ = size;
117  }
118 
120  noexcept(noexcept(std::swap(data_, other.data_)) &&
121  noexcept(std::swap(size_, other.size_)))
122  {
123  std::swap(data_, other.data_);
124  std::swap(size_, other.size_);
125  }
126  };
127 }
128 
129 #endif
std::pair< int, int > value_type
Definition: VecArray.h:33
void pop_back()
Definition: VecArray.h:106
unsigned int size_
Definition: VecArray.h:30
iterator end() noexcept
Definition: VecArray.h:65
const_reference back() const
Definition: VecArray.h:57
const_iterator begin() const noexcept
Definition: VecArray.h:62
void resize(unsigned int size)
Definition: VecArray.h:110
#define noexcept
const_iterator cend() const noexcept
Definition: VecArray.h:67
void swap(VecArray &other) noexcept(noexcept(std::swap(data_, other.data_))&& noexcept(std::swap(size_, other.size_)))
Definition: VecArray.h:119
void emplace_back(Args &&...args)
Definition: VecArray.h:92
const_iterator end() const noexcept
Definition: VecArray.h:66
#define constexpr
void push_back(const T &value)
Definition: VecArray.h:78
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 N
Definition: blowfish.cc:9
void emplace_back_unchecked(Args &&...args)
Definition: VecArray.h:100
constexpr bool empty() const noexcept
Definition: VecArray.h:69
HLT enums.
void push_back_unchecked(const T &value)
Definition: VecArray.h:85
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
T data_[N]
Definition: VecArray.h:29
iterator begin() noexcept
Definition: VecArray.h:61