My Project
FastSmallVector.hpp
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 
19  Consult the COPYING file in the top-level source directory of this
20  module for the precise wording of the license and the list of
21  copyright holders.
22 */
31 #ifndef OPM_FAST_SMALL_VECTOR_HPP
32 #define OPM_FAST_SMALL_VECTOR_HPP
33 
34 #include <array>
35 #include <algorithm>
36 #include <vector>
37 
38 namespace Opm {
39 
46 template <typename ValueType, unsigned N>
48 {
49 public:
52  {
53  size_ = 0;
54  dataPtr_ = smallBuf_.data();
55  }
56 
58  explicit FastSmallVector(const size_t numElem)
59  {
60  init_(numElem);
61  }
62 
65  FastSmallVector(const size_t numElem, const ValueType value)
66  {
67  init_(numElem);
68 
69  std::fill(dataPtr_, dataPtr_ + size_, value);
70  }
71 
74  {
75  size_ = 0;
76  dataPtr_ = smallBuf_.data();
77 
78  (*this) = other;
79  }
80 
83  {
84  size_ = 0;
85  dataPtr_ = smallBuf_.data();
86 
87  (*this) = std::move(other);
88  }
89 
92  {
93  }
94 
95 
98  {
99  size_ = other.size_;
100  if (size_ <= N) {
101  smallBuf_ = std::move(other.smallBuf_);
102  dataPtr_ = smallBuf_.data();
103  }
104  else {
105  data_ = std::move(other.data_);
106  dataPtr_ = data_.data();
107  }
108 
109  other.dataPtr_ = nullptr;
110  other.size_ = 0;
111 
112  return (*this);
113  }
114 
117  {
118  size_ = other.size_;
119 
120  if (size_ <= N) {
121  smallBuf_ = other.smallBuf_;
122  dataPtr_ = smallBuf_.data();
123  }
124  else if (dataPtr_ != other.dataPtr_) {
125  data_ = other.data_;
126  dataPtr_ = data_.data();
127  }
128 
129  return (*this);
130  }
131 
133  ValueType& operator[](size_t idx)
134  { return dataPtr_[idx]; }
135 
137  const ValueType& operator[](size_t idx) const
138  { return dataPtr_[idx]; }
139 
141  size_t size() const
142  { return size_; }
143 
144 private:
145  void init_(size_t numElem)
146  {
147  size_ = numElem;
148 
149  if (size_ > N) {
150  data_.resize(size_);
151  dataPtr_ = data_.data();
152  } else
153  dataPtr_ = smallBuf_.data();
154  }
155 
156  std::array<ValueType, N> smallBuf_;
157  std::vector<ValueType> data_;
158  std::size_t size_;
159  ValueType* dataPtr_;
160 };
161 
162 } // namespace Opm
163 
164 #endif // OPM_FAST_SMALL_VECTOR_HPP
An implementation of vector/array based on small object optimization.
Definition: FastSmallVector.hpp:48
FastSmallVector()
default constructor
Definition: FastSmallVector.hpp:51
FastSmallVector & operator=(const FastSmallVector &other)
copy assignment
Definition: FastSmallVector.hpp:116
size_t size() const
number of the element
Definition: FastSmallVector.hpp:141
const ValueType & operator[](size_t idx) const
const access the idx th element
Definition: FastSmallVector.hpp:137
FastSmallVector(FastSmallVector &&other)
move constructor
Definition: FastSmallVector.hpp:82
FastSmallVector(const size_t numElem, const ValueType value)
constructor based on the number of the element, and all the elements will have the same value
Definition: FastSmallVector.hpp:65
~FastSmallVector()
destructor
Definition: FastSmallVector.hpp:91
FastSmallVector(const size_t numElem)
constructor based on the number of the element
Definition: FastSmallVector.hpp:58
FastSmallVector(const FastSmallVector &other)
copy constructor
Definition: FastSmallVector.hpp:73
FastSmallVector & operator=(FastSmallVector &&other)
move assignment
Definition: FastSmallVector.hpp:97
ValueType & operator[](size_t idx)
access the idx th element
Definition: FastSmallVector.hpp:133