CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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_; }
64 
65  iterator end() noexcept { return begin()+size_; }
68 
69  constexpr bool empty() noexcept { return size_ == 0; }
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  // Undefined behaviour if size()==0
91  void pop_back() {
92  --size_;
93  }
94 
95  void resize(unsigned int size) {
96  if(size > N)
97  throw std::length_error("Requesting size "+std::to_string(size)+" while maximum allowed is "+std::to_string(N));
98 
99  while(size < size_)
100  pop_back();
101  size_ = size;
102  }
103 
104  void swap(VecArray& other)
105  noexcept(noexcept(std::swap(data_, other.data_)) &&
106  noexcept(std::swap(size_, other.size_)))
107  {
108  std::swap(data_, other.data_);
109  std::swap(size_, other.size_);
110  }
111  };
112 }
113 
114 #endif
const_iterator cbegin() const noexcept
Definition: VecArray.h:63
std::pair< int, int > value_type
Definition: VecArray.h:33
void pop_back()
Definition: VecArray.h:91
unsigned int size_
Definition: VecArray.h:30
iterator end() noexcept
Definition: VecArray.h:65
constexpr size_type size() noexcept
Definition: VecArray.h:70
const_reference back() const
Definition: VecArray.h:57
void resize(unsigned int size)
Definition: VecArray.h:95
#define noexcept
const_iterator cend() const noexcept
Definition: VecArray.h:67
constexpr bool empty() noexcept
Definition: VecArray.h:69
#define constexpr
const_iterator end() const noexcept
Definition: VecArray.h:66
void push_back(const T &value)
Definition: VecArray.h:78
std::string to_string(const T &t)
Definition: Logger.cc:26
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
void swap(VecArray &other) noexcept(noexcept(std::swap(data_, other.data_))&&noexcept(std::swap(size_, other.size_)))
Definition: VecArray.h:104
reference operator[](size_type pos)
Definition: VecArray.h:46
reference front()
Definition: VecArray.h:50
void clear()
Definition: VecArray.h:73
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
const_iterator begin() const noexcept
Definition: VecArray.h:62
string const
Definition: compareJSON.py:14
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