LightweightVector.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. #pragma once
  2. #include "ThirdPartyHeadersBegin.h"
  3. #include <algorithm>
  4. #include <new>
  5. #include "ThirdPartyHeadersEnd.h"
  6. #include "MASTER.h"
  7. #include "GLOBAL.h"
  8. #include "ClassMacros.h"
  9. #include "CodeContract.h"
  10. #include "MinMax.h"
  11. #include "ItemAddress.h"
  12. inline uint64_t numBytesForNumBits(uint64_t numBits) { return (numBits + 7) / 8; }
  13. namespace
  14. {
  15. template <typename T>
  16. uint64_t const *getRealMemPtr(T *const &___3251)
  17. {
  18. REQUIRE(VALID_REF(___3251));
  19. uint64_t const *uint64Array = (uint64_t const *)___3251;
  20. return uint64Array - 1;
  21. }
  22. }
  23. namespace
  24. {
  25. template <typename T>
  26. uint64_t *getRealMemPtr(T *&___3251)
  27. {
  28. REQUIRE(VALID_REF(___3251));
  29. uint64_t *uint64Array = (uint64_t *)___3251;
  30. return uint64Array - 1;
  31. }
  32. }
  33. namespace tecplot
  34. {
  35. namespace ___3933
  36. {
  37. template <typename T>
  38. class ___2240
  39. {
  40. public:
  41. #if (defined _MSC_VER && __cplusplus >= 199711L) || __cplusplus >= 201103L
  42. ___2240(___2240 const &) = delete;
  43. ___2240 &operator=(___2240 const &) = delete;
  44. ___2240(___2240 &&___2888) : m_ptr(std::move(___2888.m_ptr))
  45. #if !defined NO_ASSERTS
  46. ,
  47. m_size(std::move(___2888.m_size))
  48. #endif
  49. {
  50. }
  51. ___2240 &operator=(___2240 &&___3392)
  52. {
  53. if (this != &___3392)
  54. {
  55. ___937();
  56. m_ptr = std::move(___3392.m_ptr);
  57. ___3392.m_ptr = nullptr;
  58. #if !defined NO_ASSERTS
  59. m_size = std::move(___3392.m_size);
  60. ___3392.m_size = 0;
  61. #endif
  62. }
  63. return *this;
  64. }
  65. #else
  66. private:
  67. ___2240(___2240 const &);
  68. ___2240 &operator=(___2240 const &);
  69. public:
  70. #endif
  71. void ___2319(char const *container, size_t numElements)
  72. {
  73. #ifdef LARGE_ARRAY_MEMORY_LOGGING
  74. size_t const MEMTRACK_CUTOFF = size_t(1000) * size_t(1000);
  75. if (numElements * sizeof(T) >= MEMTRACK_CUTOFF)
  76. {
  77. FILE *file = fopen("memtrack.txt", "at");
  78. if (file)
  79. {
  80. fprintf(file, "%s\t%"
  81. "I64u"
  82. "\t%"
  83. "I64u"
  84. "\t%s\n",
  85. container, numElements, sizeof(T), typeid(T).___2685());
  86. fclose(file);
  87. }
  88. else
  89. throw std::bad_alloc();
  90. }
  91. #else
  92. ___4278(container);
  93. ___4278(numElements);
  94. #endif
  95. }
  96. private:
  97. T *m_ptr;
  98. #ifndef NO_ASSERTS
  99. uint64_t m_size;
  100. #endif
  101. inline bool allocRawData(T *&___3251, uint64_t requestedSize)
  102. {
  103. REQUIRE(___3251 == NULL);
  104. REQUIRE(requestedSize > 0);
  105. uint64_t const totalBytesRequired = sizeof(uint64_t) + requestedSize * uint64_t(sizeof(T));
  106. uint64_t *mem = NULL;
  107. if (sizeof(size_t) == 4 && totalBytesRequired > uint32_t(-1))
  108. mem = NULL;
  109. else
  110. {
  111. ___2319("LightweightVector", requestedSize);
  112. mem = (uint64_t *)malloc(static_cast<size_t>(totalBytesRequired));
  113. }
  114. bool ___2039 = (mem != NULL);
  115. if (___2039)
  116. {
  117. mem[0] = requestedSize;
  118. ___3251 = (T *)&mem[1];
  119. ___2039 = true;
  120. }
  121. return ___2039;
  122. }
  123. inline void freeRawData(T *&___3251)
  124. {
  125. REQUIRE(VALID_REF(___3251));
  126. free(getRealMemPtr(___3251));
  127. ___3251 = NULL;
  128. }
  129. public:
  130. ___2240() : m_ptr(NULL)
  131. #ifndef NO_ASSERTS
  132. ,
  133. m_size(0)
  134. #endif
  135. {
  136. }
  137. ~___2240() { ___937(); }
  138. inline void swap(___2240<T> &___2888)
  139. {
  140. using std::swap;
  141. swap(m_ptr, ___2888.m_ptr);
  142. #ifndef NO_ASSERTS
  143. swap(m_size, ___2888.m_size);
  144. #endif
  145. }
  146. inline uint64_t size() const
  147. {
  148. uint64_t ___3358;
  149. if (empty())
  150. ___3358 = 0;
  151. else
  152. ___3358 = getRealMemPtr(m_ptr)[0];
  153. ENSURE(___3358 == m_size);
  154. return ___3358;
  155. }
  156. inline uint64_t numBytesAllocated(uint64_t knownSize) const
  157. {
  158. REQUIRE(IMPLICATION(empty(), knownSize == 0));
  159. REQUIRE(IMPLICATION(!empty(), knownSize == size()));
  160. if (empty())
  161. return 0;
  162. else
  163. return sizeof(uint64_t) + knownSize * uint64_t(sizeof(T));
  164. }
  165. inline bool empty() const
  166. {
  167. INVARIANT(EQUIVALENCE(m_ptr == NULL, m_size == 0));
  168. return (m_ptr == NULL);
  169. }
  170. inline bool alloc(uint64_t requestedSize)
  171. {
  172. REQUIRE(empty());
  173. REQUIRE(requestedSize > 0);
  174. bool ___2039;
  175. if (!empty())
  176. ___2039 = false;
  177. else
  178. {
  179. ___2039 = allocRawData(m_ptr, requestedSize);
  180. if (___2039)
  181. {
  182. #ifndef NO_ASSERTS
  183. m_size = requestedSize;
  184. #endif
  185. uint64_t pos = 0;
  186. try
  187. {
  188. for (pos = 0; pos < requestedSize; pos++)
  189. ::new (&m_ptr[pos]) T;
  190. }
  191. catch (...)
  192. {
  193. for (uint64_t pos2 = 0; pos2 < pos; pos++)
  194. m_ptr[pos].~T();
  195. freeRawData(m_ptr);
  196. #ifndef NO_ASSERTS
  197. m_size = 0;
  198. #endif
  199. ___2039 = false;
  200. }
  201. }
  202. }
  203. return ___2039;
  204. }
  205. inline bool alloc(uint64_t requestedSize, T const &padVal)
  206. {
  207. REQUIRE(empty());
  208. REQUIRE(requestedSize > 0);
  209. bool ___2039;
  210. if (!empty())
  211. ___2039 = false;
  212. else
  213. {
  214. ___2039 = allocRawData(m_ptr, requestedSize);
  215. if (___2039)
  216. {
  217. #ifndef NO_ASSERTS
  218. m_size = requestedSize;
  219. #endif
  220. uint64_t pos = 0;
  221. try
  222. {
  223. for (pos = 0; pos < requestedSize; pos++)
  224. ::new (&m_ptr[pos]) T(padVal);
  225. }
  226. catch (...)
  227. {
  228. for (uint64_t pos2 = 0; pos2 < pos; pos++)
  229. m_ptr[pos].~T();
  230. freeRawData(m_ptr);
  231. #ifndef NO_ASSERTS
  232. m_size = 0;
  233. #endif
  234. ___2039 = false;
  235. }
  236. }
  237. }
  238. return ___2039;
  239. }
  240. inline bool reallocate(uint64_t requestedSize)
  241. {
  242. bool ___2039 = true;
  243. if (empty())
  244. {
  245. ___2039 = alloc(requestedSize);
  246. }
  247. else if (size() != requestedSize)
  248. {
  249. uint64_t const origSize = size();
  250. T *newPtr = 0;
  251. ___2039 = allocRawData(newPtr, requestedSize);
  252. if (___2039)
  253. {
  254. uint64_t pos = 0;
  255. try
  256. {
  257. for (pos = 0; pos < requestedSize; ++pos)
  258. ::new (&newPtr[pos]) T;
  259. uint64_t const numItemsToSwap = std::min(origSize, requestedSize);
  260. for (pos = 0; pos < numItemsToSwap; ++pos)
  261. newPtr[pos].swap(m_ptr[pos]);
  262. for (pos = 0; pos < origSize; ++pos)
  263. m_ptr[pos].~T();
  264. freeRawData(m_ptr);
  265. m_ptr = newPtr;
  266. #ifndef NO_ASSERTS
  267. m_size = requestedSize;
  268. #endif
  269. }
  270. catch (...)
  271. {
  272. for (uint64_t pos2 = 0; pos2 < pos; ++pos)
  273. newPtr[pos].~T();
  274. freeRawData(newPtr);
  275. ___2039 = false;
  276. }
  277. }
  278. }
  279. return ___2039;
  280. }
  281. inline void ___937()
  282. {
  283. if (m_ptr)
  284. {
  285. uint64_t const ___2812 = size();
  286. for (uint64_t pos = 0; pos < ___2812; pos++)
  287. m_ptr[pos].~T();
  288. freeRawData(m_ptr);
  289. #ifndef NO_ASSERTS
  290. m_size = 0;
  291. #endif
  292. }
  293. ENSURE(empty());
  294. }
  295. inline T &operator[](uint64_t pos)
  296. {
  297. REQUIRE(pos < size());
  298. return m_ptr[pos];
  299. }
  300. inline T const &operator[](uint64_t pos) const
  301. {
  302. REQUIRE(pos < size());
  303. return m_ptr[pos];
  304. }
  305. inline T *data() { return m_ptr; }
  306. inline T const *data() const { return m_ptr; }
  307. typedef T *iterator;
  308. inline iterator begin() { return m_ptr; }
  309. inline iterator end(uint64_t knownSize)
  310. {
  311. REQUIRE(size() == knownSize);
  312. return m_ptr + knownSize;
  313. }
  314. typedef T const *const_iterator;
  315. inline const_iterator begin() const { return m_ptr; }
  316. inline const_iterator end(uint64_t knownSize) const
  317. {
  318. REQUIRE(size() == knownSize);
  319. return m_ptr + knownSize;
  320. }
  321. };
  322. #define LWV_SPECIALIZE_PLAIN_DATA_VECTORS
  323. #ifdef LWV_SPECIALIZE_PLAIN_DATA_VECTORS
  324. template <typename T>
  325. class ___3094
  326. {
  327. public:
  328. #if (defined _MSC_VER && __cplusplus >= 199711L) || __cplusplus >= 201103L
  329. ___3094(___3094 const &) = delete;
  330. ___3094 &operator=(___3094 const &) = delete;
  331. #if defined _MSC_VER && _MSC_VER <= 1800
  332. #if !defined NO_ASSERTS
  333. ___3094(___3094 &&___2888) : m_ptr(std::move(___2888.m_ptr)), m_size(std::move(___2888.m_size))
  334. {
  335. }
  336. ___3094 &operator=(___3094 &&___3392)
  337. {
  338. if (this != &___3392)
  339. {
  340. m_ptr = std::move(___3392.m_ptr);
  341. m_size = std::move(___3392.m_size);
  342. }
  343. return *this;
  344. }
  345. #else
  346. ___3094(___3094 &&___2888) : m_ptr(std::move(___2888.m_ptr))
  347. {
  348. }
  349. ___3094 &operator=(___3094 &&___3392)
  350. {
  351. if (this != &___3392)
  352. {
  353. m_ptr = std::move(___3392.m_ptr);
  354. }
  355. return *this;
  356. }
  357. #endif
  358. #else
  359. ___3094(___3094 &&) = default;
  360. ___3094 &operator=(___3094 &&) = default;
  361. #endif
  362. #else
  363. private:
  364. ___3094(___3094 const &);
  365. ___3094 &operator=(___3094 const &);
  366. public:
  367. #endif
  368. void ___2319(char const *container, size_t numElements)
  369. {
  370. #ifdef LARGE_ARRAY_MEMORY_LOGGING
  371. size_t const MEMTRACK_CUTOFF = size_t(1000) * size_t(1000);
  372. if (numElements * sizeof(T) >= MEMTRACK_CUTOFF)
  373. {
  374. FILE *file = fopen("memtrack.txt", "at");
  375. if (file)
  376. {
  377. fprintf(file, "%s\t%"
  378. "I64u"
  379. "\t%"
  380. "I64u"
  381. "\t%s\n",
  382. container, numElements, sizeof(T), typeid(T).___2685());
  383. fclose(file);
  384. }
  385. else
  386. throw std::bad_alloc();
  387. }
  388. #else
  389. ___4278(container);
  390. ___4278(numElements);
  391. #endif
  392. }
  393. private:
  394. T *m_ptr;
  395. #ifndef NO_ASSERTS
  396. uint64_t m_size;
  397. #endif
  398. public:
  399. ___3094() : m_ptr(NULL)
  400. #ifndef NO_ASSERTS
  401. ,
  402. m_size(0)
  403. #endif
  404. {
  405. }
  406. ~___3094() { ___937(); }
  407. inline void swap(___3094<T> &___2888)
  408. {
  409. using std::swap;
  410. swap(m_ptr, ___2888.m_ptr);
  411. #ifndef NO_ASSERTS
  412. swap(m_size, ___2888.m_size);
  413. #endif
  414. }
  415. inline uint64_t numBytesAllocated(uint64_t knownSize) const
  416. {
  417. REQUIRE(knownSize == m_size);
  418. REQUIRE(EQUIVALENCE(empty(), knownSize == 0));
  419. if (empty())
  420. return 0;
  421. else
  422. return knownSize * sizeof(T);
  423. }
  424. inline bool empty() const
  425. {
  426. REQUIRE(EQUIVALENCE(m_ptr == NULL, m_size == 0));
  427. return (m_ptr == NULL);
  428. }
  429. #ifndef NO_ASSERTS
  430. inline uint64_t size() const
  431. {
  432. return m_size;
  433. }
  434. #endif
  435. inline bool alloc(uint64_t requestedSize)
  436. {
  437. REQUIRE(empty());
  438. REQUIRE(requestedSize > 0);
  439. bool ___2039;
  440. if (!empty() || requestedSize == 0)
  441. ___2039 = false;
  442. else
  443. {
  444. uint64_t const totalBytesRequired = requestedSize * uint64_t(sizeof(T));
  445. if (sizeof(size_t) == 4 && totalBytesRequired > uint32_t(-1))
  446. m_ptr = NULL;
  447. else
  448. {
  449. ___2319("PlainDataVector", requestedSize);
  450. m_ptr = (T *)malloc(static_cast<size_t>(totalBytesRequired));
  451. }
  452. ___2039 = (m_ptr != NULL);
  453. #ifndef NO_ASSERTS
  454. if (___2039)
  455. m_size = requestedSize;
  456. #endif
  457. }
  458. ENSURE(EQUIVALENCE(___2039, VALID_REF(m_ptr)));
  459. return ___2039;
  460. }
  461. inline bool alloc(uint64_t requestedSize, T padVal)
  462. {
  463. REQUIRE(empty());
  464. bool ___2039 = alloc(requestedSize);
  465. if (___2039)
  466. {
  467. try
  468. {
  469. for (uint64_t pos = 0; pos < requestedSize; pos++)
  470. new (&m_ptr[pos]) T(padVal);
  471. }
  472. catch (...)
  473. {
  474. ___937();
  475. ___2039 = false;
  476. }
  477. }
  478. return ___2039;
  479. }
  480. inline bool reallocate(uint64_t origSize, uint64_t requestedSize)
  481. {
  482. REQUIRE(origSize == size());
  483. bool ___2039 = true;
  484. if (empty())
  485. {
  486. ___2039 = alloc(requestedSize);
  487. }
  488. else if (origSize != requestedSize)
  489. {
  490. T *newPtr = 0;
  491. uint64_t const totalBytesRequired = requestedSize * uint64_t(sizeof(T));
  492. if (sizeof(size_t) == 4 && totalBytesRequired > uint32_t(-1))
  493. newPtr = NULL;
  494. else
  495. {
  496. ___2319("PlainDataVector", requestedSize);
  497. newPtr = (T *)malloc(static_cast<size_t>(totalBytesRequired));
  498. }
  499. ___2039 = (newPtr != NULL);
  500. if (___2039)
  501. {
  502. uint64_t const bytesToCopy = std::min(origSize, requestedSize) * uint64_t(sizeof(T));
  503. uint8_t const *const sourceBytePtr = reinterpret_cast<uint8_t const *>(m_ptr);
  504. uint8_t *const targetBytePtr = reinterpret_cast<uint8_t *>(newPtr);
  505. std::copy(sourceBytePtr, sourceBytePtr + bytesToCopy, targetBytePtr);
  506. free(m_ptr);
  507. m_ptr = newPtr;
  508. #ifndef NO_ASSERTS
  509. m_size = requestedSize;
  510. #endif
  511. }
  512. }
  513. return ___2039;
  514. }
  515. inline void ___937()
  516. {
  517. if (m_ptr)
  518. {
  519. free(m_ptr);
  520. m_ptr = NULL;
  521. #ifndef NO_ASSERTS
  522. m_size = 0;
  523. #endif
  524. }
  525. ENSURE(empty());
  526. }
  527. inline T &operator[](uint64_t pos)
  528. {
  529. REQUIRE(!empty() && pos < size());
  530. return m_ptr[pos];
  531. }
  532. inline T const &operator[](uint64_t pos) const
  533. {
  534. REQUIRE(!empty() && pos < size());
  535. return m_ptr[pos];
  536. }
  537. inline T *data() { return m_ptr; }
  538. inline T const *data() const { return m_ptr; }
  539. typedef T *iterator;
  540. inline iterator begin() { return m_ptr; }
  541. inline iterator end(uint64_t knownSize)
  542. {
  543. REQUIRE(knownSize == size());
  544. return m_ptr + knownSize;
  545. }
  546. typedef T const *const_iterator;
  547. inline const_iterator begin() const { return m_ptr; }
  548. inline const_iterator end(uint64_t knownSize) const
  549. {
  550. REQUIRE(knownSize == size());
  551. return m_ptr + knownSize;
  552. }
  553. };
  554. template <>
  555. class ___2240<___2090> : public ___3094<___2090>
  556. {
  557. };
  558. template <>
  559. class ___2240<___2090::SubzoneAddress> : public ___3094<___2090::SubzoneAddress>
  560. {
  561. };
  562. template <>
  563. class ___2240<double> : public ___3094<double>
  564. {
  565. };
  566. template <>
  567. class ___2240<float> : public ___3094<float>
  568. {
  569. };
  570. template <>
  571. class ___2240<uint64_t> : public ___3094<uint64_t>
  572. {
  573. };
  574. template <>
  575. class ___2240<int64_t> : public ___3094<int64_t>
  576. {
  577. };
  578. template <>
  579. class ___2240<uint32_t> : public ___3094<uint32_t>
  580. {
  581. };
  582. template <>
  583. class ___2240<int32_t> : public ___3094<int32_t>
  584. {
  585. };
  586. template <>
  587. class ___2240<uint16_t> : public ___3094<uint16_t>
  588. {
  589. };
  590. template <>
  591. class ___2240<int16_t> : public ___3094<int16_t>
  592. {
  593. };
  594. template <>
  595. class ___2240<uint8_t> : public ___3094<uint8_t>
  596. {
  597. };
  598. template <>
  599. class ___2240<int8_t> : public ___3094<int8_t>
  600. {
  601. };
  602. template <typename T>
  603. class ___2240<T *> : public ___3094<T *>
  604. {
  605. };
  606. template <>
  607. class ___2240<___2479> : public ___3094<___2479>
  608. {
  609. };
  610. #endif
  611. template <typename T>
  612. inline bool ___3356(___2240<___2240<T>> &twoDLwVector, uint64_t newDim1, uint64_t newDim2)
  613. {
  614. REQUIRE(newDim1 > 0 && newDim2 > 0);
  615. bool ___2039 = false;
  616. try
  617. {
  618. twoDLwVector.alloc(newDim1);
  619. for (uint64_t ___1841 = 0; ___1841 < newDim1; ___1841++)
  620. twoDLwVector[___1841].alloc(newDim2);
  621. ___2039 = true;
  622. }
  623. catch (...)
  624. {
  625. if (!twoDLwVector.empty())
  626. {
  627. ___478(twoDLwVector.size() == newDim1);
  628. for (uint64_t ___1841 = 0; ___1841 < newDim1; ___1841++)
  629. twoDLwVector[___1841].___937();
  630. twoDLwVector.___937();
  631. }
  632. ___2039 = false;
  633. }
  634. ENSURE(IMPLICATION(___2039, twoDLwVector.size() == newDim1));
  635. ENSURE(IMPLICATION(___2039, twoDLwVector[0].size() == newDim2));
  636. ENSURE(IMPLICATION(___2039, twoDLwVector[newDim1 / 2].size() == newDim2));
  637. ENSURE(IMPLICATION(___2039, twoDLwVector[newDim1 - 1].size() == newDim2));
  638. return ___2039;
  639. }
  640. template <typename T>
  641. inline bool ___3356(___2240<___2240<T>> &twoDLwVector, uint64_t newDim1, uint64_t newDim2, T padValue)
  642. {
  643. REQUIRE(newDim1 > 0 && newDim2 > 0);
  644. bool ___2039 = false;
  645. try
  646. {
  647. twoDLwVector.alloc(newDim1);
  648. for (uint64_t ___1841 = 0; ___1841 < newDim1; ___1841++)
  649. twoDLwVector[___1841].alloc(newDim2, padValue);
  650. ___2039 = true;
  651. }
  652. catch (...)
  653. {
  654. if (!twoDLwVector.empty())
  655. {
  656. ___478(twoDLwVector.size() == newDim1);
  657. for (uint64_t ___1841 = 0; ___1841 < newDim1; ___1841++)
  658. twoDLwVector[___1841].___937();
  659. twoDLwVector.___937();
  660. }
  661. ___2039 = false;
  662. }
  663. ENSURE(IMPLICATION(___2039, twoDLwVector.size() == newDim1));
  664. ENSURE(IMPLICATION(___2039, twoDLwVector[0].size() == newDim2));
  665. ENSURE(IMPLICATION(___2039, twoDLwVector[newDim1 / 2].size() == newDim2));
  666. ENSURE(IMPLICATION(___2039, twoDLwVector[newDim1 - 1].size() == newDim2));
  667. return ___2039;
  668. }
  669. }
  670. }