array.ipp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_IMPL_ARRAY_IPP
  10. #define BOOST_JSON_IMPL_ARRAY_IPP
  11. #include <boost/container_hash/hash.hpp>
  12. #include <boost/json/array.hpp>
  13. #include <boost/json/pilfer.hpp>
  14. #include <boost/json/detail/except.hpp>
  15. #include <cstdlib>
  16. #include <limits>
  17. #include <new>
  18. #include <utility>
  19. namespace boost {
  20. namespace json {
  21. //----------------------------------------------------------
  22. constexpr array::table::table() = default;
  23. // empty arrays point here
  24. BOOST_JSON_REQUIRE_CONST_INIT
  25. array::table array::empty_;
  26. auto
  27. array::
  28. table::
  29. allocate(
  30. std::size_t capacity,
  31. storage_ptr const& sp) ->
  32. table*
  33. {
  34. BOOST_ASSERT(capacity > 0);
  35. if(capacity > array::max_size())
  36. {
  37. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  38. detail::throw_system_error( error::array_too_large, &loc );
  39. }
  40. auto p = reinterpret_cast<
  41. table*>(sp->allocate(
  42. sizeof(table) +
  43. capacity * sizeof(value),
  44. alignof(value)));
  45. p->capacity = static_cast<
  46. std::uint32_t>(capacity);
  47. return p;
  48. }
  49. void
  50. array::
  51. table::
  52. deallocate(
  53. table* p,
  54. storage_ptr const& sp)
  55. {
  56. if(p->capacity == 0)
  57. return;
  58. sp->deallocate(p,
  59. sizeof(table) +
  60. p->capacity * sizeof(value),
  61. alignof(value));
  62. }
  63. //----------------------------------------------------------
  64. array::
  65. revert_insert::
  66. revert_insert(
  67. const_iterator pos,
  68. std::size_t n,
  69. array& arr)
  70. : arr_(&arr)
  71. , i_(pos - arr_->data())
  72. , n_(n)
  73. {
  74. BOOST_ASSERT(
  75. pos >= arr_->begin() &&
  76. pos <= arr_->end());
  77. if( n_ <= arr_->capacity() -
  78. arr_->size())
  79. {
  80. // fast path
  81. p = arr_->data() + i_;
  82. if(n_ == 0)
  83. return;
  84. relocate(
  85. p + n_,
  86. p,
  87. arr_->size() - i_);
  88. arr_->t_->size = static_cast<
  89. std::uint32_t>(
  90. arr_->t_->size + n_);
  91. return;
  92. }
  93. if(n_ > max_size() - arr_->size())
  94. {
  95. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  96. detail::throw_system_error( error::array_too_large, &loc );
  97. }
  98. auto t = table::allocate(
  99. arr_->growth(arr_->size() + n_),
  100. arr_->sp_);
  101. t->size = static_cast<std::uint32_t>(
  102. arr_->size() + n_);
  103. p = &(*t)[0] + i_;
  104. relocate(
  105. &(*t)[0],
  106. arr_->data(),
  107. i_);
  108. relocate(
  109. &(*t)[i_ + n_],
  110. arr_->data() + i_,
  111. arr_->size() - i_);
  112. t = detail::exchange(arr_->t_, t);
  113. table::deallocate(t, arr_->sp_);
  114. }
  115. array::
  116. revert_insert::
  117. ~revert_insert()
  118. {
  119. if(! arr_)
  120. return;
  121. BOOST_ASSERT(n_ != 0);
  122. auto const pos =
  123. arr_->data() + i_;
  124. arr_->destroy(pos, p);
  125. arr_->t_->size = static_cast<
  126. std::uint32_t>(
  127. arr_->t_->size - n_);
  128. relocate(
  129. pos,
  130. pos + n_,
  131. arr_->size() - i_);
  132. }
  133. //----------------------------------------------------------
  134. void
  135. array::
  136. destroy(
  137. value* first, value* last) noexcept
  138. {
  139. if(sp_.is_not_shared_and_deallocate_is_trivial())
  140. return;
  141. while(last-- != first)
  142. last->~value();
  143. }
  144. void
  145. array::
  146. destroy() noexcept
  147. {
  148. if(sp_.is_not_shared_and_deallocate_is_trivial())
  149. return;
  150. auto last = end();
  151. auto const first = begin();
  152. while(last-- != first)
  153. last->~value();
  154. table::deallocate(t_, sp_);
  155. }
  156. //----------------------------------------------------------
  157. //
  158. // Special Members
  159. //
  160. //----------------------------------------------------------
  161. array::
  162. array(detail::unchecked_array&& ua)
  163. : sp_(ua.storage())
  164. {
  165. BOOST_STATIC_ASSERT(
  166. alignof(table) == alignof(value));
  167. if(ua.size() == 0)
  168. {
  169. t_ = &empty_;
  170. return;
  171. }
  172. t_= table::allocate(
  173. ua.size(), sp_);
  174. t_->size = static_cast<
  175. std::uint32_t>(ua.size());
  176. ua.relocate(data());
  177. }
  178. array::
  179. ~array() noexcept
  180. {
  181. destroy();
  182. }
  183. array::
  184. array(
  185. std::size_t count,
  186. value const& v,
  187. storage_ptr sp)
  188. : sp_(std::move(sp))
  189. {
  190. if(count == 0)
  191. {
  192. t_ = &empty_;
  193. return;
  194. }
  195. t_= table::allocate(
  196. count, sp_);
  197. t_->size = 0;
  198. revert_construct r(*this);
  199. while(count--)
  200. {
  201. ::new(end()) value(v, sp_);
  202. ++t_->size;
  203. }
  204. r.commit();
  205. }
  206. array::
  207. array(
  208. std::size_t count,
  209. storage_ptr sp)
  210. : sp_(std::move(sp))
  211. {
  212. if(count == 0)
  213. {
  214. t_ = &empty_;
  215. return;
  216. }
  217. t_ = table::allocate(
  218. count, sp_);
  219. t_->size = static_cast<
  220. std::uint32_t>(count);
  221. auto p = data();
  222. do
  223. {
  224. ::new(p++) value(sp_);
  225. }
  226. while(--count);
  227. }
  228. array::
  229. array(array const& other)
  230. : array(other, other.sp_)
  231. {
  232. }
  233. array::
  234. array(
  235. array const& other,
  236. storage_ptr sp)
  237. : sp_(std::move(sp))
  238. {
  239. if(other.empty())
  240. {
  241. t_ = &empty_;
  242. return;
  243. }
  244. t_ = table::allocate(
  245. other.size(), sp_);
  246. t_->size = 0;
  247. revert_construct r(*this);
  248. auto src = other.data();
  249. auto dest = data();
  250. auto const n = other.size();
  251. do
  252. {
  253. ::new(dest++) value(
  254. *src++, sp_);
  255. ++t_->size;
  256. }
  257. while(t_->size < n);
  258. r.commit();
  259. }
  260. array::
  261. array(
  262. array&& other,
  263. storage_ptr sp)
  264. : sp_(std::move(sp))
  265. {
  266. if(*sp_ == *other.sp_)
  267. {
  268. // same resource
  269. t_ = detail::exchange(
  270. other.t_, &empty_);
  271. return;
  272. }
  273. else if(other.empty())
  274. {
  275. t_ = &empty_;
  276. return;
  277. }
  278. // copy
  279. t_ = table::allocate(
  280. other.size(), sp_);
  281. t_->size = 0;
  282. revert_construct r(*this);
  283. auto src = other.data();
  284. auto dest = data();
  285. auto const n = other.size();
  286. do
  287. {
  288. ::new(dest++) value(
  289. *src++, sp_);
  290. ++t_->size;
  291. }
  292. while(t_->size < n);
  293. r.commit();
  294. }
  295. array::
  296. array(
  297. std::initializer_list<
  298. value_ref> init,
  299. storage_ptr sp)
  300. : sp_(std::move(sp))
  301. {
  302. if(init.size() == 0)
  303. {
  304. t_ = &empty_;
  305. return;
  306. }
  307. t_ = table::allocate(
  308. init.size(), sp_);
  309. t_->size = 0;
  310. revert_construct r(*this);
  311. value_ref::write_array(
  312. data(), init, sp_);
  313. t_->size = static_cast<
  314. std::uint32_t>(init.size());
  315. r.commit();
  316. }
  317. //----------------------------------------------------------
  318. array&
  319. array::
  320. operator=(array const& other)
  321. {
  322. array(other,
  323. storage()).swap(*this);
  324. return *this;
  325. }
  326. array&
  327. array::
  328. operator=(array&& other)
  329. {
  330. array(std::move(other),
  331. storage()).swap(*this);
  332. return *this;
  333. }
  334. array&
  335. array::
  336. operator=(
  337. std::initializer_list<value_ref> init)
  338. {
  339. array(init,
  340. storage()).swap(*this);
  341. return *this;
  342. }
  343. //----------------------------------------------------------
  344. //
  345. // Element access
  346. //
  347. //----------------------------------------------------------
  348. system::result<value&>
  349. array::try_at(std::size_t pos) noexcept
  350. {
  351. if(pos >= t_->size)
  352. {
  353. system::error_code ec;
  354. BOOST_JSON_FAIL(ec, error::out_of_range);
  355. return ec;
  356. }
  357. return (*t_)[pos];
  358. }
  359. system::result<value const&>
  360. array::try_at(std::size_t pos) const noexcept
  361. {
  362. if(pos >= t_->size)
  363. {
  364. system::error_code ec;
  365. BOOST_JSON_FAIL(ec, error::out_of_range);
  366. return ec;
  367. }
  368. return (*t_)[pos];
  369. }
  370. value const&
  371. array::
  372. array::at(std::size_t pos, source_location const& loc) const&
  373. {
  374. return try_at(pos).value(loc);
  375. }
  376. //----------------------------------------------------------
  377. //
  378. // Capacity
  379. //
  380. //----------------------------------------------------------
  381. void
  382. array::
  383. shrink_to_fit() noexcept
  384. {
  385. if(capacity() <= size())
  386. return;
  387. if(size() == 0)
  388. {
  389. table::deallocate(t_, sp_);
  390. t_ = &empty_;
  391. return;
  392. }
  393. #ifndef BOOST_NO_EXCEPTIONS
  394. try
  395. {
  396. #endif
  397. auto t = table::allocate(
  398. size(), sp_);
  399. relocate(
  400. &(*t)[0],
  401. data(),
  402. size());
  403. t->size = static_cast<
  404. std::uint32_t>(size());
  405. t = detail::exchange(
  406. t_, t);
  407. table::deallocate(t, sp_);
  408. #ifndef BOOST_NO_EXCEPTIONS
  409. }
  410. catch(...)
  411. {
  412. // eat the exception
  413. return;
  414. }
  415. #endif
  416. }
  417. //----------------------------------------------------------
  418. //
  419. // Modifiers
  420. //
  421. //----------------------------------------------------------
  422. void
  423. array::
  424. clear() noexcept
  425. {
  426. if(size() == 0)
  427. return;
  428. destroy(
  429. begin(), end());
  430. t_->size = 0;
  431. }
  432. auto
  433. array::
  434. insert(
  435. const_iterator pos,
  436. value const& v) ->
  437. iterator
  438. {
  439. return emplace(pos, v);
  440. }
  441. auto
  442. array::
  443. insert(
  444. const_iterator pos,
  445. value&& v) ->
  446. iterator
  447. {
  448. return emplace(pos, std::move(v));
  449. }
  450. auto
  451. array::
  452. insert(
  453. const_iterator pos,
  454. std::size_t count,
  455. value const& v) ->
  456. iterator
  457. {
  458. revert_insert r(
  459. pos, count, *this);
  460. while(count--)
  461. {
  462. ::new(r.p) value(v, sp_);
  463. ++r.p;
  464. }
  465. return r.commit();
  466. }
  467. auto
  468. array::
  469. insert(
  470. const_iterator pos,
  471. std::initializer_list<
  472. value_ref> init) ->
  473. iterator
  474. {
  475. revert_insert r(
  476. pos, init.size(), *this);
  477. value_ref::write_array(
  478. r.p, init, sp_);
  479. return r.commit();
  480. }
  481. auto
  482. array::
  483. erase(
  484. const_iterator pos) noexcept ->
  485. iterator
  486. {
  487. BOOST_ASSERT(
  488. pos >= begin() &&
  489. pos <= end());
  490. return erase(pos, pos + 1);
  491. }
  492. auto
  493. array::
  494. erase(
  495. const_iterator first,
  496. const_iterator last) noexcept ->
  497. iterator
  498. {
  499. BOOST_ASSERT(
  500. first >= begin() &&
  501. last >= first &&
  502. last <= end());
  503. std::size_t const n =
  504. last - first;
  505. auto const p = &(*t_)[0] +
  506. (first - &(*t_)[0]);
  507. destroy(p, p + n);
  508. relocate(p, p + n,
  509. t_->size - (last -
  510. &(*t_)[0]));
  511. t_->size = static_cast<
  512. std::uint32_t>(t_->size - n);
  513. return p;
  514. }
  515. void
  516. array::
  517. push_back(value const& v)
  518. {
  519. emplace_back(v);
  520. }
  521. void
  522. array::
  523. push_back(value&& v)
  524. {
  525. emplace_back(std::move(v));
  526. }
  527. void
  528. array::
  529. pop_back() noexcept
  530. {
  531. auto const p = &back();
  532. destroy(p, p + 1);
  533. --t_->size;
  534. }
  535. void
  536. array::
  537. resize(std::size_t count)
  538. {
  539. if(count <= t_->size)
  540. {
  541. // shrink
  542. destroy(
  543. &(*t_)[0] + count,
  544. &(*t_)[0] + t_->size);
  545. t_->size = static_cast<
  546. std::uint32_t>(count);
  547. return;
  548. }
  549. reserve(count);
  550. auto p = &(*t_)[t_->size];
  551. auto const end = &(*t_)[count];
  552. while(p != end)
  553. ::new(p++) value(sp_);
  554. t_->size = static_cast<
  555. std::uint32_t>(count);
  556. }
  557. void
  558. array::
  559. resize(
  560. std::size_t count,
  561. value const& v)
  562. {
  563. if(count <= size())
  564. {
  565. // shrink
  566. destroy(
  567. data() + count,
  568. data() + size());
  569. t_->size = static_cast<
  570. std::uint32_t>(count);
  571. return;
  572. }
  573. count -= size();
  574. revert_insert r(
  575. end(), count, *this);
  576. while(count--)
  577. {
  578. ::new(r.p) value(v, sp_);
  579. ++r.p;
  580. }
  581. r.commit();
  582. }
  583. void
  584. array::
  585. swap(array& other)
  586. {
  587. if(*sp_ == *other.sp_)
  588. {
  589. t_ = detail::exchange(
  590. other.t_, t_);
  591. return;
  592. }
  593. array temp1(
  594. std::move(*this),
  595. other.storage());
  596. array temp2(
  597. std::move(other),
  598. this->storage());
  599. this->~array();
  600. ::new(this) array(
  601. pilfer(temp2));
  602. other.~array();
  603. ::new(&other) array(
  604. pilfer(temp1));
  605. }
  606. //----------------------------------------------------------
  607. //
  608. // Private
  609. //
  610. //----------------------------------------------------------
  611. std::size_t
  612. array::
  613. growth(
  614. std::size_t new_size) const
  615. {
  616. if(new_size > max_size())
  617. {
  618. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  619. detail::throw_system_error( error::array_too_large, &loc );
  620. }
  621. std::size_t const old = capacity();
  622. if(old > max_size() - old / 2)
  623. return new_size;
  624. std::size_t const g =
  625. old + old / 2; // 1.5x
  626. if(g < new_size)
  627. return new_size;
  628. return g;
  629. }
  630. // precondition: new_capacity > capacity()
  631. void
  632. array::
  633. reserve_impl(
  634. std::size_t new_capacity)
  635. {
  636. BOOST_ASSERT(
  637. new_capacity > t_->capacity);
  638. auto t = table::allocate(
  639. growth(new_capacity), sp_);
  640. relocate(
  641. &(*t)[0],
  642. &(*t_)[0],
  643. t_->size);
  644. t->size = t_->size;
  645. t = detail::exchange(t_, t);
  646. table::deallocate(t, sp_);
  647. }
  648. // precondition: pv is not aliased
  649. value&
  650. array::
  651. push_back(
  652. pilfered<value> pv)
  653. {
  654. auto const n = t_->size;
  655. if(n < t_->capacity)
  656. {
  657. // fast path
  658. auto& v = *::new(
  659. &(*t_)[n]) value(pv);
  660. ++t_->size;
  661. return v;
  662. }
  663. auto const t =
  664. detail::exchange(t_,
  665. table::allocate(
  666. growth(n + 1),
  667. sp_));
  668. auto& v = *::new(
  669. &(*t_)[n]) value(pv);
  670. relocate(
  671. &(*t_)[0],
  672. &(*t)[0],
  673. n);
  674. t_->size = n + 1;
  675. table::deallocate(t, sp_);
  676. return v;
  677. }
  678. // precondition: pv is not aliased
  679. auto
  680. array::
  681. insert(
  682. const_iterator pos,
  683. pilfered<value> pv) ->
  684. iterator
  685. {
  686. BOOST_ASSERT(
  687. pos >= begin() &&
  688. pos <= end());
  689. std::size_t const n =
  690. t_->size;
  691. std::size_t const i =
  692. pos - &(*t_)[0];
  693. if(n < t_->capacity)
  694. {
  695. // fast path
  696. auto const p =
  697. &(*t_)[i];
  698. relocate(
  699. p + 1,
  700. p,
  701. n - i);
  702. ::new(p) value(pv);
  703. ++t_->size;
  704. return p;
  705. }
  706. auto t =
  707. table::allocate(
  708. growth(n + 1), sp_);
  709. auto const p = &(*t)[i];
  710. ::new(p) value(pv);
  711. relocate(
  712. &(*t)[0],
  713. &(*t_)[0],
  714. i);
  715. relocate(
  716. p + 1,
  717. &(*t_)[i],
  718. n - i);
  719. t->size = static_cast<
  720. std::uint32_t>(size() + 1);
  721. t = detail::exchange(t_, t);
  722. table::deallocate(t, sp_);
  723. return p;
  724. }
  725. //----------------------------------------------------------
  726. bool
  727. array::
  728. equal(
  729. array const& other) const noexcept
  730. {
  731. if(size() != other.size())
  732. return false;
  733. for(std::size_t i = 0; i < size(); ++i)
  734. if((*this)[i] != other[i])
  735. return false;
  736. return true;
  737. }
  738. } // namespace json
  739. } // namespace boost
  740. //----------------------------------------------------------
  741. //
  742. // std::hash specialization
  743. //
  744. //----------------------------------------------------------
  745. std::size_t
  746. std::hash<::boost::json::array>::operator()(
  747. ::boost::json::array const& ja) const noexcept
  748. {
  749. return ::boost::hash< ::boost::json::array >()( ja );
  750. }
  751. //----------------------------------------------------------
  752. #endif