small_vector.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // Copyright (c) 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_UTIL_SMALL_VECTOR_H_
  15. #define SOURCE_UTIL_SMALL_VECTOR_H_
  16. #include <array>
  17. #include <cassert>
  18. #include <cstddef>
  19. #include <iostream>
  20. #include <memory>
  21. #include <utility>
  22. #include <vector>
  23. #include "source/util/make_unique.h"
  24. namespace spvtools {
  25. namespace utils {
  26. // The |SmallVector| class is intended to be a drop-in replacement for
  27. // |std::vector|. The difference is in the implementation. A |SmallVector| is
  28. // optimized for when the number of elements in the vector are small. Small is
  29. // defined by the template parameter |small_size|.
  30. //
  31. // Note that |SmallVector| is not always faster than an |std::vector|, so you
  32. // should experiment with different values for |small_size| and compare to
  33. // using and |std::vector|.
  34. //
  35. // TODO: I have implemented the public member functions from |std::vector| that
  36. // I needed. If others are needed they should be implemented. Do not implement
  37. // public member functions that are not defined by std::vector.
  38. template <class T, size_t small_size>
  39. class SmallVector {
  40. public:
  41. using value_type = T;
  42. using iterator = T*;
  43. using const_iterator = const T*;
  44. SmallVector()
  45. : size_(0),
  46. small_data_(reinterpret_cast<T*>(buffer)),
  47. large_data_(nullptr) {}
  48. SmallVector(const SmallVector& that) : SmallVector() { *this = that; }
  49. SmallVector(SmallVector&& that) : SmallVector() { *this = std::move(that); }
  50. SmallVector(const std::vector<T>& vec) : SmallVector() {
  51. if (vec.size() > small_size) {
  52. large_data_ = MakeUnique<std::vector<T>>(vec);
  53. } else {
  54. size_ = vec.size();
  55. for (uint32_t i = 0; i < size_; i++) {
  56. new (small_data_ + i) T(vec[i]);
  57. }
  58. }
  59. }
  60. template <class InputIt>
  61. SmallVector(InputIt first, InputIt last) : SmallVector() {
  62. insert(end(), first, last);
  63. }
  64. SmallVector(std::vector<T>&& vec) : SmallVector() {
  65. if (vec.size() > small_size) {
  66. large_data_ = MakeUnique<std::vector<T>>(std::move(vec));
  67. } else {
  68. size_ = vec.size();
  69. for (uint32_t i = 0; i < size_; i++) {
  70. new (small_data_ + i) T(std::move(vec[i]));
  71. }
  72. }
  73. vec.clear();
  74. }
  75. SmallVector(std::initializer_list<T> init_list) : SmallVector() {
  76. if (init_list.size() < small_size) {
  77. for (auto it = init_list.begin(); it != init_list.end(); ++it) {
  78. new (small_data_ + (size_++)) T(std::move(*it));
  79. }
  80. } else {
  81. large_data_ = MakeUnique<std::vector<T>>(std::move(init_list));
  82. }
  83. }
  84. SmallVector(size_t s, const T& v) : SmallVector() { resize(s, v); }
  85. virtual ~SmallVector() {
  86. for (T* p = small_data_; p < small_data_ + size_; ++p) {
  87. p->~T();
  88. }
  89. }
  90. SmallVector& operator=(const SmallVector& that) {
  91. assert(small_data_);
  92. if (that.large_data_) {
  93. if (large_data_) {
  94. *large_data_ = *that.large_data_;
  95. } else {
  96. large_data_ = MakeUnique<std::vector<T>>(*that.large_data_);
  97. }
  98. } else {
  99. large_data_.reset(nullptr);
  100. size_t i = 0;
  101. // Do a copy for any element in |this| that is already constructed.
  102. for (; i < size_ && i < that.size_; ++i) {
  103. small_data_[i] = that.small_data_[i];
  104. }
  105. if (i >= that.size_) {
  106. // If the size of |this| becomes smaller after the assignment, then
  107. // destroy any extra elements.
  108. for (; i < size_; ++i) {
  109. small_data_[i].~T();
  110. }
  111. } else {
  112. // If the size of |this| becomes larger after the assignement, copy
  113. // construct the new elements that are needed.
  114. for (; i < that.size_; ++i) {
  115. new (small_data_ + i) T(that.small_data_[i]);
  116. }
  117. }
  118. size_ = that.size_;
  119. }
  120. return *this;
  121. }
  122. SmallVector& operator=(SmallVector&& that) {
  123. if (that.large_data_) {
  124. large_data_.reset(that.large_data_.release());
  125. } else {
  126. large_data_.reset(nullptr);
  127. size_t i = 0;
  128. // Do a move for any element in |this| that is already constructed.
  129. for (; i < size_ && i < that.size_; ++i) {
  130. small_data_[i] = std::move(that.small_data_[i]);
  131. }
  132. if (i >= that.size_) {
  133. // If the size of |this| becomes smaller after the assignment, then
  134. // destroy any extra elements.
  135. for (; i < size_; ++i) {
  136. small_data_[i].~T();
  137. }
  138. } else {
  139. // If the size of |this| becomes larger after the assignement, move
  140. // construct the new elements that are needed.
  141. for (; i < that.size_; ++i) {
  142. new (small_data_ + i) T(std::move(that.small_data_[i]));
  143. }
  144. }
  145. size_ = that.size_;
  146. }
  147. // Reset |that| because all of the data has been moved to |this|.
  148. that.DestructSmallData();
  149. return *this;
  150. }
  151. template <class OtherVector>
  152. friend bool operator==(const SmallVector& lhs, const OtherVector& rhs) {
  153. if (lhs.size() != rhs.size()) {
  154. return false;
  155. }
  156. auto rit = rhs.begin();
  157. for (auto lit = lhs.begin(); lit != lhs.end(); ++lit, ++rit) {
  158. if (*lit != *rit) {
  159. return false;
  160. }
  161. }
  162. return true;
  163. }
  164. // Avoid infinite recursion from rewritten operators in C++20
  165. #if __cplusplus <= 201703L
  166. friend bool operator==(const std::vector<T>& lhs, const SmallVector& rhs) {
  167. return rhs == lhs;
  168. }
  169. #endif
  170. friend bool operator!=(const SmallVector& lhs, const std::vector<T>& rhs) {
  171. return !(lhs == rhs);
  172. }
  173. friend bool operator!=(const std::vector<T>& lhs, const SmallVector& rhs) {
  174. return rhs != lhs;
  175. }
  176. T& operator[](size_t i) {
  177. if (!large_data_) {
  178. return small_data_[i];
  179. } else {
  180. return (*large_data_)[i];
  181. }
  182. }
  183. const T& operator[](size_t i) const {
  184. if (!large_data_) {
  185. return small_data_[i];
  186. } else {
  187. return (*large_data_)[i];
  188. }
  189. }
  190. size_t size() const {
  191. if (!large_data_) {
  192. return size_;
  193. } else {
  194. return large_data_->size();
  195. }
  196. }
  197. iterator begin() {
  198. if (large_data_) {
  199. return large_data_->data();
  200. } else {
  201. return small_data_;
  202. }
  203. }
  204. const_iterator begin() const {
  205. if (large_data_) {
  206. return large_data_->data();
  207. } else {
  208. return small_data_;
  209. }
  210. }
  211. const_iterator cbegin() const { return begin(); }
  212. iterator end() {
  213. if (large_data_) {
  214. return large_data_->data() + large_data_->size();
  215. } else {
  216. return small_data_ + size_;
  217. }
  218. }
  219. const_iterator end() const {
  220. if (large_data_) {
  221. return large_data_->data() + large_data_->size();
  222. } else {
  223. return small_data_ + size_;
  224. }
  225. }
  226. const_iterator cend() const { return end(); }
  227. T* data() { return begin(); }
  228. const T* data() const { return cbegin(); }
  229. T& front() { return (*this)[0]; }
  230. const T& front() const { return (*this)[0]; }
  231. iterator erase(const_iterator pos) { return erase(pos, pos + 1); }
  232. iterator erase(const_iterator first, const_iterator last) {
  233. if (large_data_) {
  234. size_t start_index = first - large_data_->data();
  235. size_t end_index = last - large_data_->data();
  236. auto r = large_data_->erase(large_data_->begin() + start_index,
  237. large_data_->begin() + end_index);
  238. return large_data_->data() + (r - large_data_->begin());
  239. }
  240. // Since C++11, std::vector has |const_iterator| for the parameters, so I
  241. // follow that. However, I need iterators to modify the current container,
  242. // which is not const. This is why I cast away the const.
  243. iterator f = const_cast<iterator>(first);
  244. iterator l = const_cast<iterator>(last);
  245. iterator e = end();
  246. size_t num_of_del_elements = last - first;
  247. iterator ret = f;
  248. if (first == last) {
  249. return ret;
  250. }
  251. // Move |last| and any elements after it their earlier position.
  252. while (l != e) {
  253. *f = std::move(*l);
  254. ++f;
  255. ++l;
  256. }
  257. // Destroy the elements that were supposed to be deleted.
  258. while (f != l) {
  259. f->~T();
  260. ++f;
  261. }
  262. // Update the size.
  263. size_ -= num_of_del_elements;
  264. return ret;
  265. }
  266. void push_back(const T& value) {
  267. if (!large_data_ && size_ == small_size) {
  268. MoveToLargeData();
  269. }
  270. if (large_data_) {
  271. large_data_->push_back(value);
  272. return;
  273. }
  274. new (small_data_ + size_) T(value);
  275. ++size_;
  276. }
  277. void push_back(T&& value) {
  278. if (!large_data_ && size_ == small_size) {
  279. MoveToLargeData();
  280. }
  281. if (large_data_) {
  282. large_data_->push_back(std::move(value));
  283. return;
  284. }
  285. new (small_data_ + size_) T(std::move(value));
  286. ++size_;
  287. }
  288. void pop_back() {
  289. if (large_data_) {
  290. large_data_->pop_back();
  291. } else {
  292. --size_;
  293. small_data_[size_].~T();
  294. }
  295. }
  296. template <class InputIt>
  297. iterator insert(iterator pos, InputIt first, InputIt last) {
  298. size_t element_idx = (pos - begin());
  299. size_t num_of_new_elements = std::distance(first, last);
  300. size_t new_size = size_ + num_of_new_elements;
  301. if (!large_data_ && new_size > small_size) {
  302. MoveToLargeData();
  303. }
  304. if (large_data_) {
  305. typename std::vector<T>::iterator new_pos =
  306. large_data_->begin() + element_idx;
  307. large_data_->insert(new_pos, first, last);
  308. return begin() + element_idx;
  309. }
  310. // Move |pos| and all of the elements after it over |num_of_new_elements|
  311. // places. We start at the end and work backwards, to make sure we do not
  312. // overwrite data that we have not moved yet.
  313. for (iterator i = begin() + new_size - 1, j = end() - 1; j >= pos;
  314. --i, --j) {
  315. if (i >= begin() + size_) {
  316. new (i) T(std::move(*j));
  317. } else {
  318. *i = std::move(*j);
  319. }
  320. }
  321. // Copy the new elements into position.
  322. iterator p = pos;
  323. for (; first != last; ++p, ++first) {
  324. if (p >= small_data_ + size_) {
  325. new (p) T(*first);
  326. } else {
  327. *p = *first;
  328. }
  329. }
  330. // Update the size.
  331. size_ += num_of_new_elements;
  332. return pos;
  333. }
  334. bool empty() const {
  335. if (large_data_) {
  336. return large_data_->empty();
  337. }
  338. return size_ == 0;
  339. }
  340. void clear() {
  341. if (large_data_) {
  342. large_data_->clear();
  343. } else {
  344. DestructSmallData();
  345. }
  346. }
  347. template <class... Args>
  348. void emplace_back(Args&&... args) {
  349. if (!large_data_ && size_ == small_size) {
  350. MoveToLargeData();
  351. }
  352. if (large_data_) {
  353. large_data_->emplace_back(std::forward<Args>(args)...);
  354. } else {
  355. new (small_data_ + size_) T(std::forward<Args>(args)...);
  356. ++size_;
  357. }
  358. }
  359. void resize(size_t new_size, const T& v) {
  360. if (!large_data_ && new_size > small_size) {
  361. MoveToLargeData();
  362. }
  363. if (large_data_) {
  364. large_data_->resize(new_size, v);
  365. return;
  366. }
  367. // If |new_size| < |size_|, then destroy the extra elements.
  368. for (size_t i = new_size; i < size_; ++i) {
  369. small_data_[i].~T();
  370. }
  371. // If |new_size| > |size_|, the copy construct the new elements.
  372. for (size_t i = size_; i < new_size; ++i) {
  373. new (small_data_ + i) T(v);
  374. }
  375. // Update the size.
  376. size_ = new_size;
  377. }
  378. private:
  379. // Moves all of the element from |small_data_| into a new std::vector that can
  380. // be access through |large_data|.
  381. void MoveToLargeData() {
  382. assert(!large_data_);
  383. large_data_ = MakeUnique<std::vector<T>>();
  384. for (size_t i = 0; i < size_; ++i) {
  385. large_data_->emplace_back(std::move(small_data_[i]));
  386. }
  387. DestructSmallData();
  388. }
  389. // Destroys all of the elements in |small_data_| that have been constructed.
  390. void DestructSmallData() {
  391. for (size_t i = 0; i < size_; ++i) {
  392. small_data_[i].~T();
  393. }
  394. size_ = 0;
  395. }
  396. // The number of elements in |small_data_| that have been constructed.
  397. size_t size_;
  398. // A type with the same alignment and size as T, but will is POD.
  399. struct alignas(T) PodType {
  400. std::array<int8_t, sizeof(T)> data;
  401. };
  402. // The actual data used to store the array elements. It must never be used
  403. // directly, but must only be accessed through |small_data_|.
  404. PodType buffer[small_size];
  405. // The pointed used to access the array of elements when the number of
  406. // elements is small.
  407. T* small_data_;
  408. // A pointer to a vector that is used to store the elements of the vector when
  409. // this size exceeds |small_size|. If |large_data_| is nullptr, then the data
  410. // is stored in |small_data_|. Otherwise, the data is stored in
  411. // |large_data_|.
  412. std::unique_ptr<std::vector<T>> large_data_;
  413. }; // namespace utils
  414. } // namespace utils
  415. } // namespace spvtools
  416. #endif // SOURCE_UTIL_SMALL_VECTOR_H_