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 
37 namespace Opm {
38 
45 template <typename ValueType, unsigned N>
47 {
48 public:
51  {
52  size_ = 0;
53  dataPtr_ = smallBuf_.data();
54  }
55 
57  explicit FastSmallVector(const size_t numElem)
58  {
59  init_(numElem);
60  }
61 
64  FastSmallVector(const size_t numElem, const ValueType value)
65  {
66  init_(numElem);
67 
68  std::fill(dataPtr_, dataPtr_ + size_, value);
69  }
70 
73  {
74  size_ = 0;
75  dataPtr_ = smallBuf_.data();
76 
77  (*this) = other;
78  }
79 
82  {
83  size_ = 0;
84  dataPtr_ = smallBuf_.data();
85 
86  (*this) = std::move(other);
87  }
88 
91  {
92  if (dataPtr_ != smallBuf_.data())
93  delete [] dataPtr_;
94  }
95 
96 
99  {
100  if (dataPtr_ != smallBuf_.data() && dataPtr_ != other.dataPtr_)
101  delete [] dataPtr_;
102 
103  size_ = other.size_;
104  if (size_ <= N) {
105  smallBuf_ = std::move(other.smallBuf_);
106  dataPtr_ = smallBuf_.data();
107  }
108  else
109  dataPtr_ = other.dataPtr_;
110 
111  other.dataPtr_ = nullptr;
112  other.size_ = 0;
113 
114  return (*this);
115  }
116 
119  {
120  size_ = other.size_;
121 
122  if (size_ <= N) {
123  smallBuf_ = other.smallBuf_;
124  dataPtr_ = smallBuf_.data();
125  }
126  else if (dataPtr_ != other.dataPtr_) {
127  if (dataPtr_ != smallBuf_.data())
128  delete[] dataPtr_;
129  dataPtr_ = new ValueType[size_];
130 
131  std::copy(other.dataPtr_, other.dataPtr_ + size_, dataPtr_);
132  }
133 
134  return (*this);
135  }
136 
138  ValueType& operator[](size_t idx)
139  { return dataPtr_[idx]; }
140 
142  const ValueType& operator[](size_t idx) const
143  { return dataPtr_[idx]; }
144 
146  size_t size() const
147  { return size_; }
148 
149 private:
150  void init_(size_t numElem)
151  {
152  size_ = numElem;
153 
154  if (size_ > N)
155  dataPtr_ = new ValueType[size_];
156  else
157  dataPtr_ = smallBuf_.data();
158  }
159 
160  std::array<ValueType, N> smallBuf_;
161  std::size_t size_;
162  ValueType* dataPtr_;
163 };
164 
165 } // namespace Opm
166 
167 #endif // OPM_FAST_SMALL_VECTOR_HPP
An implementation of vector/array based on small object optimization.
Definition: FastSmallVector.hpp:47
FastSmallVector()
default constructor
Definition: FastSmallVector.hpp:50
FastSmallVector & operator=(const FastSmallVector &other)
copy assignment
Definition: FastSmallVector.hpp:118
size_t size() const
number of the element
Definition: FastSmallVector.hpp:146
const ValueType & operator[](size_t idx) const
const access the idx th element
Definition: FastSmallVector.hpp:142
FastSmallVector(FastSmallVector &&other)
move constructor
Definition: FastSmallVector.hpp:81
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:64
~FastSmallVector()
destructor
Definition: FastSmallVector.hpp:90
FastSmallVector(const size_t numElem)
constructor based on the number of the element
Definition: FastSmallVector.hpp:57
FastSmallVector(const FastSmallVector &other)
copy constructor
Definition: FastSmallVector.hpp:72
FastSmallVector & operator=(FastSmallVector &&other)
move assignment
Definition: FastSmallVector.hpp:98
ValueType & operator[](size_t idx)
access the idx th element
Definition: FastSmallVector.hpp:138