vector_n1962.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. // Copyright (C) 2008-2018 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  3. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  4. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  5. //[n1962_vector_n1962
  6. // Extra spaces, newlines, etc. for visual alignment with this library code.
  7. #include <boost/algorithm/cxx11/all_of.hpp>
  8. #include <boost/type_traits/has_equal_to.hpp>
  9. #include <boost/next_prior.hpp>
  10. #include <vector>
  11. #include <iterator>
  12. #include <memory>
  13. template< class T, class Allocator = std::allocator<T> >
  14. class vector {
  15. invariant {
  16. empty() == (size() == 0);
  17. std::distance(begin(), end()) == int(size());
  18. std::distance(rbegin(), rend()) == int(size());
  19. size() <= capacity();
  20. capacity() <= max_size();
  21. }
  22. public:
  23. typedef typename std::vector<T, Allocator>::allocator_type allocator_type;
  24. typedef typename std::vector<T, Allocator>::pointer pointer;
  25. typedef typename std::vector<T, Allocator>::const_pointer const_pointer;
  26. typedef typename std::vector<T, Allocator>::reference reference;
  27. typedef typename std::vector<T, Allocator>::const_reference const_reference;
  28. typedef typename std::vector<T, Allocator>::value_type value_type;
  29. typedef typename std::vector<T, Allocator>::iterator iterator;
  30. typedef typename std::vector<T, Allocator>::const_iterator const_iterator;
  31. typedef typename std::vector<T, Allocator>::size_type size_type;
  32. typedef typename std::vector<T, Allocator>::difference_type difference_type;
  33. typedef typename std::vector<T, Allocator>::reverse_iterator
  34. reverse_iterator;
  35. typedef typename std::vector<T, Allocator>::const_reverse_iterator
  36. const_reverse_iterator;
  37. vector()
  38. postcondition {
  39. empty();
  40. }
  41. : vect_()
  42. {}
  43. explicit vector(Allocator const& alloc)
  44. postcondition {
  45. empty();
  46. get_allocator() == alloc;
  47. }
  48. : vect_(alloc)
  49. {}
  50. explicit vector(size_type count)
  51. postcondition {
  52. size() == count;
  53. if constexpr(boost::has_equal_to<T>::value) {
  54. boost::algorithm::all_of_equal(begin(), end(), T());
  55. }
  56. }
  57. : vect_(count)
  58. {}
  59. vector(size_type count, T const& value)
  60. postcondition {
  61. size() == count;
  62. if constexpr(boost::has_equal_to<T>::value) {
  63. boost::algorithm::all_of_equal(begin(), end(), value);
  64. }
  65. }
  66. : vect_(count, value)
  67. {}
  68. vector(size_type count, T const& value, Allocator const& alloc)
  69. postcondition {
  70. size() == count;
  71. if constexpr(boost::has_equal_to<T>::value) {
  72. boost::algorithm::all_of_equal(begin(), end(), value);
  73. }
  74. get_allocator() == alloc;
  75. }
  76. : vect_(count, value, alloc)
  77. {}
  78. template<typename InputIter>
  79. vector(InputIter first, InputIter last)
  80. postcondition {
  81. std::distance(first, last) == int(size());
  82. }
  83. : vect_(first, last)
  84. {}
  85. template<typename InputIter>
  86. vector(InputIter first, InputIter last, Allocator const& alloc)
  87. postcondition {
  88. std::distance(first, last) == int(size());
  89. get_allocator() == alloc;
  90. }
  91. : vect_(first, last, alloc)
  92. {}
  93. /* implicit */ vector(vector const& other)
  94. postcondition {
  95. if constexpr(boost::has_equal_to<T>::value) {
  96. *this == other;
  97. }
  98. }
  99. : vect_(other.vect_)
  100. {}
  101. vector& operator=(vector const& other)
  102. postcondition(result) {
  103. if constexpr(boost::has_equal_to<T>::value) {
  104. *this == other;
  105. result == *this;
  106. }
  107. }
  108. {
  109. if(this != &other) vect_ = other.vect_;
  110. return *this;
  111. }
  112. virtual ~vector() {}
  113. void reserve(size_type count)
  114. precondition {
  115. count < max_size();
  116. }
  117. postcondition {
  118. capacity() >= count;
  119. }
  120. {
  121. vect_.reserve(count);
  122. }
  123. size_type capacity() const
  124. postcondition(result) {
  125. result >= size();
  126. }
  127. {
  128. return vect_.capacity();
  129. }
  130. iterator begin()
  131. postcondition {
  132. if(empty()) result == end();
  133. }
  134. {
  135. return vect_.begin();
  136. }
  137. const_iterator begin() const
  138. postcondition(result) {
  139. if(empty()) result == end();
  140. }
  141. {
  142. return vect_.begin();
  143. }
  144. iterator end() {
  145. return vect_.end();
  146. }
  147. const_iterator end() const {
  148. return vect_.end();
  149. }
  150. reverse_iterator rbegin()
  151. postcondition(result) {
  152. if(empty()) result == rend();
  153. }
  154. {
  155. return vect_.rbegin();
  156. }
  157. const_reverse_iterator rbegin() const
  158. postcondition(result) {
  159. if(empty()) result == rend();
  160. }
  161. {
  162. return vect_.rbegin();
  163. }
  164. reverse_iterator rend() {
  165. return vect_.rend();
  166. }
  167. const_reverse_iterator rend() const {
  168. return vect_.rend();
  169. }
  170. void resize(size_type count, T const& value = T())
  171. postcondition {
  172. size() == count;
  173. if constexpr(boost::has_equal_to<T>::value) {
  174. if(count > oldof(size())) {
  175. boost::algorithm::all_of_equal(begin() + oldof(size()),
  176. end(), value);
  177. }
  178. }
  179. }
  180. {
  181. vect_.resize(count, value);
  182. }
  183. size_type size() const
  184. postcondition(result) {
  185. result <= capacity();
  186. }
  187. {
  188. return vect_.size();
  189. }
  190. size_type max_size() const
  191. postcondition(result) {
  192. result >= capacity();
  193. }
  194. {
  195. return vect_.max_size();
  196. }
  197. bool empty() const
  198. postcondition(result) {
  199. result == (size() == 0);
  200. }
  201. {
  202. return vect_.empty();
  203. }
  204. Alloctor get_allocator() const {
  205. return vect_.get_allocator();
  206. }
  207. reference at(size_type index) {
  208. // No precondition (throw out_of_range for invalid index).
  209. return vect_.at(index);
  210. }
  211. const_reference at(size_type index) const {
  212. // No precondition (throw out_of_range for invalid index).
  213. return vect_.at(index);
  214. }
  215. reference operator[](size_type index)
  216. precondition {
  217. index < size();
  218. }
  219. {
  220. return vect_[index];
  221. }
  222. const_reference operator[](size_type index) const
  223. precondition {
  224. index < size();
  225. }
  226. {
  227. return vect_[index];
  228. }
  229. reference front()
  230. precondition {
  231. !empty();
  232. }
  233. {
  234. return vect_.front();
  235. }
  236. const_reference front() const
  237. precondition {
  238. !empty();
  239. }
  240. {
  241. return vect_.front();
  242. }
  243. reference back()
  244. precondition {
  245. !empty();
  246. }
  247. {
  248. return vect_.back();
  249. }
  250. const_reference back() const
  251. precondition {
  252. !empty();
  253. }
  254. {
  255. return vect_.back();
  256. }
  257. void push_back(T const& value)
  258. precondition {
  259. size() < max_size();
  260. }
  261. postcondition {
  262. size() == oldof(size()) + 1;
  263. capacity() >= oldof(capacity())
  264. if constexpr(boost::has_equal_to<T>::value) {
  265. back() == value;
  266. }
  267. }
  268. {
  269. vect_.push_back(value);
  270. }
  271. void pop_back()
  272. precondition {
  273. !empty();
  274. }
  275. postcondition {
  276. size() == oldof(size()) - 1;
  277. }
  278. {
  279. vect_.pop_back();
  280. }
  281. template<typename InputIter>
  282. void assign(InputIter first, InputIter last)
  283. // Precondition: [begin(), end()) does not contain [first, last).
  284. postcondition {
  285. std::distance(first, last) == int(size());
  286. }
  287. {
  288. vect_.assign(first, last);
  289. }
  290. void assign(size_type count, T const& vallue)
  291. precondition {
  292. count <= max_size();
  293. }
  294. postcondition {
  295. if constexpr(boost::has_equal_to<T>::value) {
  296. boost::algorithm::all_of_equal(begin(), end(), value);
  297. }
  298. }
  299. {
  300. vect_.assign(count, value);
  301. }
  302. iterator insert(iterator where, T const& value)
  303. precondition {
  304. size() < max_size();
  305. }
  306. postcondition(result) {
  307. size() == oldof(size()) + 1;
  308. capacity() >= oldof(capacity());
  309. if constexpr(boost::has_equal_to<T>::value) {
  310. *result == value;
  311. }
  312. // if(capacity() > oldof(capacity()))
  313. // [begin(), end()) is invalid
  314. // else
  315. // [where, end()) is invalid
  316. }
  317. {
  318. return vect_.insert(where, value);
  319. }
  320. void insert(iterator where, size_type count, T const& value)
  321. precondition {
  322. size() + count < max_size();
  323. }
  324. postcondition {
  325. size() == oldof(size()) + count;
  326. capacity() >= oldof(capacity());
  327. if(capacity() == oldof(capacity())) {
  328. if constexpr(boost::has_equal_to<T>::value) {
  329. boost::algorithm::all_of_equal(boost::prior(oldof(where)),
  330. boost::prior(oldof(where)) + count, value);
  331. }
  332. // [where, end()) is invalid
  333. }
  334. // else [begin(), end()) is invalid
  335. }
  336. {
  337. vect_.insert(where, count, value);
  338. }
  339. template<typename InputIter>
  340. void insert(iterator where, Iterator first, Iterator last)
  341. precondition {
  342. size() + std::distance(first, last) < max_size();
  343. // [first, last) is not contained in [begin(), end())
  344. }
  345. postcondition {
  346. size() == oldof(size()) + std::distance(first, last);
  347. capacity() >= oldof(capacity());
  348. if(capacity() == oldof(capacity())) {
  349. if constexpr(boost::has_equal_to<T>::value) {
  350. boost::algorithm::all_of_equal(first, last, oldof(where));
  351. }
  352. // [where, end()) is invalid
  353. }
  354. // else [begin(), end()) is invalid
  355. }
  356. {
  357. vect_.insert(where, first, last);
  358. }
  359. iterator erase(iterator where)
  360. precondition {
  361. !empty();
  362. where != end();
  363. }
  364. postcondition(result) {
  365. size() == oldod size() - 1;
  366. if(empty()) result == end();
  367. // [where, end()) is invalid
  368. }
  369. {
  370. return vect_.erase(where);
  371. }
  372. iterator erase(iterator first, iterator last)
  373. precondition {
  374. size() >= std::distance(first, lasst);
  375. }
  376. postcondition(result) {
  377. size() == oldof(size()) - std::distance(first, last);
  378. if(empty()) result == end();
  379. // [first, last) is invalid
  380. }
  381. {
  382. return vect_.erase(first, last);
  383. }
  384. void clear()
  385. postcondition {
  386. empty();
  387. }
  388. {
  389. vect_.clear();
  390. }
  391. void swap(vector& other)
  392. precondition {
  393. get_allocator() == other.get_allocator();
  394. }
  395. postcondition {
  396. if constexpr(boost::has_equal_to<T>::value) {
  397. *this == oldof(other);
  398. other == oldof(*this);
  399. }
  400. }
  401. {
  402. vect_.swap(other);
  403. }
  404. friend bool operator==(vector const& left, vector const& right) {
  405. // Cannot check class invariants for left and right objects.
  406. return left.vect_ == right.vect_;
  407. }
  408. private:
  409. std::vector<T, Allocator> vect_;
  410. };
  411. // End.
  412. //]