multi_span 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #ifndef GSL_MULTI_SPAN_H
  17. #define GSL_MULTI_SPAN_H
  18. #include <gsl/gsl_assert> // for Expects
  19. #include <gsl/gsl_byte> // for byte
  20. #include <gsl/gsl_util> // for narrow_cast
  21. #include <algorithm> // for transform, lexicographical_compare
  22. #include <array> // for array
  23. #include <cstddef> // for std::ptrdiff_t, size_t, nullptr_t
  24. #include <cstdint> // for PTRDIFF_MAX
  25. #include <functional> // for divides, multiplies, minus, negate, plus
  26. #include <initializer_list> // for initializer_list
  27. #include <iterator> // for iterator, random_access_iterator_tag
  28. #include <limits> // for numeric_limits
  29. #include <new>
  30. #include <numeric>
  31. #include <stdexcept>
  32. #include <string> // for basic_string
  33. #include <type_traits> // for enable_if_t, remove_cv_t, is_same, is_co...
  34. #include <utility>
  35. #if defined(_MSC_VER) && !defined(__clang__)
  36. // turn off some warnings that are noisy about our Expects statements
  37. #pragma warning(push)
  38. #pragma warning(disable : 4127) // conditional expression is constant
  39. #pragma warning(disable : 4702) // unreachable code
  40. // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
  41. #pragma warning(disable : 26495) // uninitalized member when constructor calls constructor
  42. #pragma warning(disable : 26473) // in some instantiations we cast to the same type
  43. #pragma warning(disable : 26490) // TODO: bug in parser - attributes and templates
  44. #pragma warning(disable : 26465) // TODO: bug - suppression does not work on template functions
  45. #pragma warning(disable : 4996) // use of function or classes marked [[deprecated]]
  46. #if _MSC_VER < 1910
  47. #pragma push_macro("constexpr")
  48. #define constexpr /*constexpr*/
  49. #endif // _MSC_VER < 1910
  50. #endif // _MSC_VER
  51. #if defined(__GNUC__) || defined(__clang__)
  52. #pragma GCC diagnostic push
  53. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  54. #endif
  55. // GCC 7 does not like the signed unsigned missmatch (size_t std::ptrdiff_t)
  56. // While there is a conversion from signed to unsigned, it happens at
  57. // compiletime, so the compiler wouldn't have to warn indiscriminently, but
  58. // could check if the source value actually doesn't fit into the target type
  59. // and only warn in those cases.
  60. #if defined(__GNUC__) && __GNUC__ > 6
  61. #pragma GCC diagnostic push
  62. #pragma GCC diagnostic ignored "-Wsign-conversion"
  63. #endif
  64. namespace gsl
  65. {
  66. /*
  67. ** begin definitions of index and bounds
  68. */
  69. namespace details
  70. {
  71. template <typename SizeType>
  72. struct [[deprecated]] SizeTypeTraits
  73. {
  74. static const SizeType max_value = std::numeric_limits<SizeType>::max();
  75. };
  76. template <typename... Ts>
  77. class [[deprecated]] are_integral : public std::integral_constant<bool, true>
  78. {
  79. };
  80. template <typename T, typename... Ts>
  81. class [[deprecated]] are_integral<T, Ts...>
  82. : public std::integral_constant<bool,
  83. std::is_integral<T>::value && are_integral<Ts...>::value>
  84. {
  85. };
  86. } // namespace details
  87. template <std::size_t Rank>
  88. class [[deprecated]] multi_span_index final {
  89. static_assert(Rank > 0, "Rank must be greater than 0!");
  90. template <std::size_t OtherRank>
  91. friend class multi_span_index;
  92. public:
  93. static const std::size_t rank = Rank;
  94. using value_type = std::ptrdiff_t;
  95. using size_type = value_type;
  96. using reference = std::add_lvalue_reference_t<value_type>;
  97. using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
  98. constexpr multi_span_index() noexcept {}
  99. constexpr multi_span_index(const value_type(&values)[Rank]) noexcept
  100. {
  101. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  102. GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
  103. std::copy(values, values + Rank, elems);
  104. }
  105. template <typename... Ts, typename = std::enable_if_t<(sizeof...(Ts) == Rank) &&
  106. details::are_integral<Ts...>::value>>
  107. constexpr multi_span_index(Ts... ds) noexcept : elems{narrow_cast<value_type>(ds)...}
  108. {}
  109. constexpr multi_span_index(const multi_span_index& other) noexcept = default;
  110. constexpr multi_span_index& operator=(const multi_span_index& rhs) noexcept = default;
  111. // Preconditions: component_idx < rank
  112. GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
  113. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  114. constexpr reference operator[](std::size_t component_idx)
  115. {
  116. Expects(component_idx < Rank); // Component index must be less than rank
  117. return elems[component_idx];
  118. }
  119. // Preconditions: component_idx < rank
  120. GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
  121. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  122. constexpr const_reference operator[](std::size_t component_idx) const
  123. {
  124. Expects(component_idx < Rank); // Component index must be less than rank
  125. return elems[component_idx];
  126. }
  127. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  128. GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
  129. constexpr bool operator==(const multi_span_index& rhs) const
  130. {
  131. return std::equal(elems, elems + rank, rhs.elems);
  132. }
  133. constexpr bool operator!=(const multi_span_index& rhs) const { return !(*this == rhs); }
  134. constexpr multi_span_index operator+() const noexcept { return *this; }
  135. constexpr multi_span_index operator-() const
  136. {
  137. multi_span_index ret = *this;
  138. std::transform(ret, ret + rank, ret, std::negate<value_type>{});
  139. return ret;
  140. }
  141. constexpr multi_span_index operator+(const multi_span_index& rhs) const
  142. {
  143. multi_span_index ret = *this;
  144. ret += rhs;
  145. return ret;
  146. }
  147. constexpr multi_span_index operator-(const multi_span_index& rhs) const
  148. {
  149. multi_span_index ret = *this;
  150. ret -= rhs;
  151. return ret;
  152. }
  153. constexpr multi_span_index& operator+=(const multi_span_index& rhs)
  154. {
  155. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  156. GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
  157. std::transform(elems, elems + rank, rhs.elems, elems, std::plus<value_type>{});
  158. return *this;
  159. }
  160. constexpr multi_span_index& operator-=(const multi_span_index& rhs)
  161. {
  162. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  163. GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
  164. std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{});
  165. return *this;
  166. }
  167. constexpr multi_span_index operator*(value_type v) const
  168. {
  169. multi_span_index ret = *this;
  170. ret *= v;
  171. return ret;
  172. }
  173. constexpr multi_span_index operator/(value_type v) const
  174. {
  175. multi_span_index ret = *this;
  176. ret /= v;
  177. return ret;
  178. }
  179. friend constexpr multi_span_index operator*(value_type v, const multi_span_index& rhs)
  180. {
  181. return rhs * v;
  182. }
  183. constexpr multi_span_index& operator*=(value_type v)
  184. {
  185. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  186. GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
  187. std::transform(elems, elems + rank, elems,
  188. [v](value_type x) { return std::multiplies<value_type>{}(x, v); });
  189. return *this;
  190. }
  191. constexpr multi_span_index& operator/=(value_type v)
  192. {
  193. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  194. GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
  195. std::transform(elems, elems + rank, elems,
  196. [v](value_type x) { return std::divides<value_type>{}(x, v); });
  197. return *this;
  198. }
  199. private:
  200. value_type elems[Rank] = {};
  201. };
  202. #if !defined(_MSC_VER) || _MSC_VER >= 1910
  203. struct [[deprecated]] static_bounds_dynamic_range_t
  204. {
  205. template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
  206. constexpr operator T() const noexcept
  207. {
  208. return narrow_cast<T>(-1);
  209. }
  210. };
  211. constexpr bool operator==(static_bounds_dynamic_range_t, static_bounds_dynamic_range_t) noexcept
  212. {
  213. return true;
  214. }
  215. constexpr bool operator!=(static_bounds_dynamic_range_t, static_bounds_dynamic_range_t) noexcept
  216. {
  217. return false;
  218. }
  219. template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
  220. constexpr bool operator==(static_bounds_dynamic_range_t, T other) noexcept
  221. {
  222. return narrow_cast<T>(-1) == other;
  223. }
  224. template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
  225. constexpr bool operator==(T left, static_bounds_dynamic_range_t right) noexcept
  226. {
  227. return right == left;
  228. }
  229. template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
  230. constexpr bool operator!=(static_bounds_dynamic_range_t, T other) noexcept
  231. {
  232. return narrow_cast<T>(-1) != other;
  233. }
  234. template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
  235. constexpr bool operator!=(T left, static_bounds_dynamic_range_t right) noexcept
  236. {
  237. return right != left;
  238. }
  239. constexpr static_bounds_dynamic_range_t dynamic_range{};
  240. #else
  241. const std::ptrdiff_t dynamic_range = -1;
  242. #endif
  243. struct [[deprecated]] generalized_mapping_tag
  244. {
  245. };
  246. struct[[deprecated]] contiguous_mapping_tag : generalized_mapping_tag{};
  247. namespace details
  248. {
  249. template <std::ptrdiff_t Left, std::ptrdiff_t Right>
  250. struct [[deprecated]] LessThan
  251. {
  252. static const bool value = Left < Right;
  253. };
  254. template <std::ptrdiff_t... Ranges>
  255. struct [[deprecated]] BoundsRanges {
  256. using size_type = std::ptrdiff_t;
  257. static const size_type Depth = 0;
  258. static const size_type DynamicNum = 0;
  259. static const size_type CurrentRange = 1;
  260. static const size_type TotalSize = 1;
  261. // TODO : following signature is for work around VS bug
  262. template <typename OtherRange>
  263. constexpr BoundsRanges(const OtherRange&, bool /* firstLevel */)
  264. {}
  265. constexpr BoundsRanges(const std::ptrdiff_t* const) {}
  266. constexpr BoundsRanges() noexcept = default;
  267. template <typename T, std::size_t Dim>
  268. constexpr void serialize(T&) const
  269. {}
  270. template <typename T, std::size_t Dim>
  271. constexpr size_type linearize(const T&) const
  272. {
  273. return 0;
  274. }
  275. template <typename T, std::size_t Dim>
  276. constexpr size_type contains(const T&) const
  277. {
  278. return -1;
  279. }
  280. constexpr size_type elementNum(std::size_t) const noexcept { return 0; }
  281. constexpr size_type totalSize() const noexcept { return TotalSize; }
  282. constexpr bool operator==(const BoundsRanges&) const noexcept { return true; }
  283. };
  284. template <std::ptrdiff_t... RestRanges>
  285. struct[[deprecated]] BoundsRanges<dynamic_range, RestRanges...> : BoundsRanges<RestRanges...>
  286. {
  287. using Base = BoundsRanges<RestRanges...>;
  288. using size_type = std::ptrdiff_t;
  289. static const std::size_t Depth = Base::Depth + 1;
  290. static const std::size_t DynamicNum = Base::DynamicNum + 1;
  291. static const size_type CurrentRange = dynamic_range;
  292. static const size_type TotalSize = dynamic_range;
  293. private:
  294. size_type m_bound;
  295. public:
  296. GSL_SUPPRESS(
  297. f.23) // NO-FORMAT: attribute // this pointer type is cannot be assigned nullptr - issue in not_null
  298. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  299. constexpr BoundsRanges(const std::ptrdiff_t* const arr)
  300. : Base(arr + 1), m_bound(*arr * this->Base::totalSize())
  301. {
  302. Expects(0 <= *arr);
  303. }
  304. constexpr BoundsRanges() noexcept : m_bound(0) {}
  305. template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
  306. constexpr BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other,
  307. bool /* firstLevel */ = true)
  308. : Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false)
  309. , m_bound(other.totalSize())
  310. {}
  311. template <typename T, std::size_t Dim = 0>
  312. constexpr void serialize(T & arr) const
  313. {
  314. arr[Dim] = elementNum();
  315. this->Base::template serialize<T, Dim + 1>(arr);
  316. }
  317. template <typename T, std::size_t Dim = 0>
  318. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  319. constexpr size_type linearize(const T& arr) const
  320. {
  321. const size_type index = this->Base::totalSize() * arr[Dim];
  322. Expects(index < m_bound);
  323. return index + this->Base::template linearize<T, Dim + 1>(arr);
  324. }
  325. template <typename T, std::size_t Dim = 0>
  326. constexpr size_type contains(const T& arr) const
  327. {
  328. const std::ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
  329. if (last == -1) return -1;
  330. const std::ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
  331. return cur < m_bound ? cur + last : -1;
  332. }
  333. GSL_SUPPRESS(
  334. c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
  335. constexpr size_type totalSize() const noexcept { return m_bound; }
  336. GSL_SUPPRESS(
  337. c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
  338. constexpr size_type elementNum() const noexcept
  339. {
  340. return totalSize() / this->Base::totalSize();
  341. }
  342. GSL_SUPPRESS(
  343. c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
  344. constexpr size_type elementNum(std::size_t dim) const noexcept
  345. {
  346. if (dim > 0)
  347. return this->Base::elementNum(dim - 1);
  348. else
  349. return elementNum();
  350. }
  351. constexpr bool operator==(const BoundsRanges& rhs) const noexcept
  352. {
  353. return m_bound == rhs.m_bound &&
  354. static_cast<const Base&>(*this) == static_cast<const Base&>(rhs);
  355. }
  356. };
  357. template <std::ptrdiff_t CurRange, std::ptrdiff_t... RestRanges>
  358. struct[[deprecated]] BoundsRanges<CurRange, RestRanges...> : BoundsRanges<RestRanges...>
  359. {
  360. using Base = BoundsRanges<RestRanges...>;
  361. using size_type = std::ptrdiff_t;
  362. static const std::size_t Depth = Base::Depth + 1;
  363. static const std::size_t DynamicNum = Base::DynamicNum;
  364. static const size_type CurrentRange = CurRange;
  365. static const size_type TotalSize =
  366. Base::TotalSize == dynamic_range ? dynamic_range : CurrentRange * Base::TotalSize;
  367. constexpr BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr) {}
  368. constexpr BoundsRanges() = default;
  369. template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
  370. constexpr BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other,
  371. bool firstLevel = true)
  372. : Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false)
  373. {
  374. GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: false positive
  375. (void) firstLevel;
  376. }
  377. template <typename T, std::size_t Dim = 0>
  378. constexpr void serialize(T & arr) const
  379. {
  380. arr[Dim] = elementNum();
  381. this->Base::template serialize<T, Dim + 1>(arr);
  382. }
  383. template <typename T, std::size_t Dim = 0>
  384. constexpr size_type linearize(const T& arr) const
  385. {
  386. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  387. Expects(arr[Dim] >= 0 && arr[Dim] < CurrentRange); // Index is out of range
  388. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  389. const std::ptrdiff_t d = arr[Dim];
  390. return this->Base::totalSize() * d + this->Base::template linearize<T, Dim + 1>(arr);
  391. }
  392. template <typename T, std::size_t Dim = 0>
  393. constexpr size_type contains(const T& arr) const
  394. {
  395. if (arr[Dim] >= CurrentRange) return -1;
  396. const size_type last = this->Base::template contains<T, Dim + 1>(arr);
  397. if (last == -1) return -1;
  398. return this->Base::totalSize() * arr[Dim] + last;
  399. }
  400. GSL_SUPPRESS(
  401. c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
  402. constexpr size_type totalSize() const noexcept
  403. {
  404. return CurrentRange * this->Base::totalSize();
  405. }
  406. GSL_SUPPRESS(
  407. c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
  408. constexpr size_type elementNum() const noexcept { return CurrentRange; }
  409. GSL_SUPPRESS(
  410. c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
  411. constexpr size_type elementNum(std::size_t dim) const noexcept
  412. {
  413. if (dim > 0)
  414. return this->Base::elementNum(dim - 1);
  415. else
  416. return elementNum();
  417. }
  418. constexpr bool operator==(const BoundsRanges& rhs) const noexcept
  419. {
  420. return static_cast<const Base&>(*this) == static_cast<const Base&>(rhs);
  421. }
  422. };
  423. template <typename SourceType, typename TargetType>
  424. struct[[deprecated]] BoundsRangeConvertible
  425. : public std::integral_constant<bool, (SourceType::TotalSize >= TargetType::TotalSize ||
  426. TargetType::TotalSize == dynamic_range ||
  427. SourceType::TotalSize == dynamic_range ||
  428. TargetType::TotalSize == 0)>{};
  429. template <typename TypeChain>
  430. struct [[deprecated]] TypeListIndexer {
  431. const TypeChain& obj_;
  432. constexpr TypeListIndexer(const TypeChain& obj) : obj_(obj) {}
  433. template <std::size_t N>
  434. constexpr const TypeChain& getObj(std::true_type)
  435. {
  436. return obj_;
  437. }
  438. template <std::size_t N, typename MyChain = TypeChain,
  439. typename MyBase = typename MyChain::Base>
  440. constexpr auto getObj(std::false_type)
  441. ->decltype(TypeListIndexer<MyBase>(static_cast<const MyBase&>(obj_)).template get<N>())
  442. {
  443. return TypeListIndexer<MyBase>(static_cast<const MyBase&>(obj_)).template get<N>();
  444. }
  445. template <std::size_t N>
  446. constexpr auto get()->decltype(getObj<N - 1>(std::integral_constant<bool, N == 0>()))
  447. {
  448. return getObj<N - 1>(std::integral_constant<bool, N == 0>());
  449. }
  450. };
  451. template <typename TypeChain>
  452. constexpr TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain& obj)
  453. {
  454. return TypeListIndexer<TypeChain>(obj);
  455. }
  456. template <std::size_t Rank, bool Enabled = (Rank > 1),
  457. typename Ret = std::enable_if_t<Enabled, multi_span_index<Rank - 1>>>
  458. constexpr Ret shift_left(const multi_span_index<Rank>& other) noexcept
  459. {
  460. Ret ret{};
  461. for (std::size_t i = 0; i < Rank - 1; ++i)
  462. {
  463. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  464. ret[i] = other[i + 1];
  465. }
  466. return ret;
  467. }
  468. } // namespace details
  469. template <typename IndexType>
  470. class [[deprecated]] bounds_iterator;
  471. template <std::ptrdiff_t... Ranges>
  472. class [[deprecated]] static_bounds {
  473. public:
  474. static_bounds(const details::BoundsRanges<Ranges...>&) {}
  475. };
  476. template <std::ptrdiff_t FirstRange, std::ptrdiff_t... RestRanges>
  477. class[[deprecated]] static_bounds<FirstRange, RestRanges...>
  478. {
  479. using MyRanges = details::BoundsRanges<FirstRange, RestRanges...>;
  480. MyRanges m_ranges;
  481. constexpr static_bounds(const MyRanges& range) noexcept : m_ranges(range) {}
  482. template <std::ptrdiff_t... OtherRanges>
  483. friend class static_bounds;
  484. public:
  485. static const std::size_t rank = MyRanges::Depth;
  486. static const std::size_t dynamic_rank = MyRanges::DynamicNum;
  487. static const std::ptrdiff_t static_size = MyRanges::TotalSize;
  488. using size_type = std::ptrdiff_t;
  489. using index_type = multi_span_index<rank>;
  490. using const_index_type = std::add_const_t<index_type>;
  491. using iterator = bounds_iterator<const_index_type>;
  492. using const_iterator = bounds_iterator<const_index_type>;
  493. using difference_type = std::ptrdiff_t;
  494. using sliced_type = static_bounds<RestRanges...>;
  495. using mapping_type = contiguous_mapping_tag;
  496. constexpr static_bounds() /*noexcept*/ = default;
  497. template <typename SourceType, typename TargetType, std::size_t Rank>
  498. struct BoundsRangeConvertible2;
  499. template <std::size_t Rank, typename SourceType, typename TargetType,
  500. typename Ret = BoundsRangeConvertible2<typename SourceType::Base,
  501. typename TargetType::Base, Rank>>
  502. static auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type)->Ret;
  503. template <std::size_t Rank, typename SourceType, typename TargetType>
  504. static auto helpBoundsRangeConvertible(SourceType, TargetType, ...)->std::false_type;
  505. template <typename SourceType, typename TargetType, std::size_t Rank>
  506. struct BoundsRangeConvertible2
  507. : decltype(helpBoundsRangeConvertible<Rank - 1>(
  508. SourceType(), TargetType(),
  509. std::integral_constant<bool,
  510. SourceType::Depth == TargetType::Depth &&
  511. (SourceType::CurrentRange == TargetType::CurrentRange ||
  512. TargetType::CurrentRange == dynamic_range ||
  513. SourceType::CurrentRange == dynamic_range)>()))
  514. {
  515. };
  516. template <typename SourceType, typename TargetType>
  517. struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type
  518. {
  519. };
  520. template <typename SourceType, typename TargetType, std::ptrdiff_t Rank = TargetType::Depth>
  521. struct BoundsRangeConvertible
  522. : decltype(helpBoundsRangeConvertible<Rank - 1>(
  523. SourceType(), TargetType(),
  524. std::integral_constant<bool,
  525. SourceType::Depth == TargetType::Depth &&
  526. (!details::LessThan<SourceType::CurrentRange,
  527. TargetType::CurrentRange>::value ||
  528. TargetType::CurrentRange == dynamic_range ||
  529. SourceType::CurrentRange == dynamic_range)>()))
  530. {
  531. };
  532. template <typename SourceType, typename TargetType>
  533. struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type
  534. {
  535. };
  536. template <std::ptrdiff_t... Ranges,
  537. typename = std::enable_if_t<details::BoundsRangeConvertible<
  538. details::BoundsRanges<Ranges...>,
  539. details::BoundsRanges<FirstRange, RestRanges...>>::value>>
  540. constexpr static_bounds(const static_bounds<Ranges...>& other) : m_ranges(other.m_ranges)
  541. {
  542. Expects((MyRanges::DynamicNum == 0 && details::BoundsRanges<Ranges...>::DynamicNum == 0) ||
  543. MyRanges::DynamicNum > 0 || other.m_ranges.totalSize() >= m_ranges.totalSize());
  544. }
  545. constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
  546. {
  547. // Size of the initializer list must match the rank of the array
  548. Expects((MyRanges::DynamicNum == 0 && il.size() == 1 && *il.begin() == static_size) ||
  549. MyRanges::DynamicNum == il.size());
  550. // Size of the range must be less than the max element of the size type
  551. Expects(m_ranges.totalSize() <= PTRDIFF_MAX);
  552. }
  553. constexpr sliced_type slice() const noexcept
  554. {
  555. return sliced_type{static_cast<const details::BoundsRanges<RestRanges...>&>(m_ranges)};
  556. }
  557. constexpr size_type stride() const noexcept { return rank > 1 ? slice().size() : 1; }
  558. constexpr size_type size() const noexcept { return m_ranges.totalSize(); }
  559. constexpr size_type total_size() const noexcept { return m_ranges.totalSize(); }
  560. constexpr size_type linearize(const index_type& idx) const { return m_ranges.linearize(idx); }
  561. constexpr bool contains(const index_type& idx) const noexcept
  562. {
  563. return m_ranges.contains(idx) != -1;
  564. }
  565. constexpr size_type operator[](std::size_t idx) const noexcept
  566. {
  567. return m_ranges.elementNum(idx);
  568. }
  569. template <std::size_t Dim = 0>
  570. constexpr size_type extent() const noexcept
  571. {
  572. static_assert(Dim < rank,
  573. "dimension should be less than rank (dimension count starts from 0)");
  574. return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
  575. }
  576. template <typename IntType>
  577. constexpr size_type extent(IntType dim) const
  578. {
  579. static_assert(std::is_integral<IntType>::value,
  580. "Dimension parameter must be supplied as an integral type.");
  581. auto real_dim = narrow_cast<std::size_t>(dim);
  582. Expects(real_dim < rank);
  583. return m_ranges.elementNum(real_dim);
  584. }
  585. constexpr index_type index_bounds() const noexcept
  586. {
  587. size_type extents[rank] = {};
  588. m_ranges.serialize(extents);
  589. return {extents};
  590. }
  591. template <std::ptrdiff_t... Ranges>
  592. constexpr bool operator==(const static_bounds<Ranges...>& rhs) const noexcept
  593. {
  594. return this->size() == rhs.size();
  595. }
  596. template <std::ptrdiff_t... Ranges>
  597. constexpr bool operator!=(const static_bounds<Ranges...>& rhs) const noexcept
  598. {
  599. return !(*this == rhs);
  600. }
  601. constexpr const_iterator begin() const noexcept { return const_iterator(*this, index_type{}); }
  602. constexpr const_iterator end() const noexcept
  603. {
  604. return const_iterator(*this, this->index_bounds());
  605. }
  606. };
  607. template <std::size_t Rank>
  608. class [[deprecated]] strided_bounds {
  609. template <std::size_t OtherRank>
  610. friend class strided_bounds;
  611. public:
  612. static const std::size_t rank = Rank;
  613. using value_type = std::ptrdiff_t;
  614. using reference = std::add_lvalue_reference_t<value_type>;
  615. using const_reference = std::add_const_t<reference>;
  616. using size_type = value_type;
  617. using difference_type = value_type;
  618. using index_type = multi_span_index<rank>;
  619. using const_index_type = std::add_const_t<index_type>;
  620. using iterator = bounds_iterator<const_index_type>;
  621. using const_iterator = bounds_iterator<const_index_type>;
  622. static const value_type dynamic_rank = rank;
  623. static const value_type static_size = dynamic_range;
  624. using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
  625. using mapping_type = generalized_mapping_tag;
  626. constexpr strided_bounds(const strided_bounds&) noexcept = default;
  627. constexpr strided_bounds& operator=(const strided_bounds&) noexcept = default;
  628. constexpr strided_bounds(const value_type(&values)[rank], index_type strides)
  629. : m_extents(values), m_strides(std::move(strides))
  630. {}
  631. constexpr strided_bounds(const index_type& extents, const index_type& strides) noexcept
  632. : m_extents(extents), m_strides(strides)
  633. {}
  634. constexpr index_type strides() const noexcept { return m_strides; }
  635. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  636. constexpr size_type total_size() const noexcept
  637. {
  638. size_type ret = 0;
  639. for (std::size_t i = 0; i < rank; ++i) { ret += (m_extents[i] - 1) * m_strides[i]; }
  640. return ret + 1;
  641. }
  642. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  643. constexpr size_type size() const noexcept
  644. {
  645. size_type ret = 1;
  646. for (std::size_t i = 0; i < rank; ++i) { ret *= m_extents[i]; }
  647. return ret;
  648. }
  649. constexpr bool contains(const index_type& idx) const noexcept
  650. {
  651. for (std::size_t i = 0; i < rank; ++i)
  652. {
  653. if (idx[i] < 0 || idx[i] >= m_extents[i]) return false;
  654. }
  655. return true;
  656. }
  657. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  658. constexpr size_type linearize(const index_type& idx) const
  659. {
  660. size_type ret = 0;
  661. for (std::size_t i = 0; i < rank; i++)
  662. {
  663. Expects(idx[i] < m_extents[i]); // index is out of bounds of the array
  664. ret += idx[i] * m_strides[i];
  665. }
  666. return ret;
  667. }
  668. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  669. constexpr size_type stride() const noexcept { return m_strides[0]; }
  670. template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
  671. constexpr sliced_type slice() const
  672. {
  673. return {details::shift_left(m_extents), details::shift_left(m_strides)};
  674. }
  675. template <std::size_t Dim = 0>
  676. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  677. constexpr size_type extent() const noexcept
  678. {
  679. static_assert(Dim < Rank,
  680. "dimension should be less than rank (dimension count starts from 0)");
  681. return m_extents[Dim];
  682. }
  683. constexpr index_type index_bounds() const noexcept { return m_extents; }
  684. constexpr const_iterator begin() const noexcept { return const_iterator{*this, index_type{}}; }
  685. constexpr const_iterator end() const noexcept { return const_iterator{*this, index_bounds()}; }
  686. private:
  687. index_type m_extents;
  688. index_type m_strides;
  689. };
  690. template <typename T>
  691. struct[[deprecated]] is_bounds : std::integral_constant<bool, false>{};
  692. template <std::ptrdiff_t... Ranges>
  693. struct[[deprecated]] is_bounds<static_bounds<Ranges...>> : std::integral_constant<bool, true>{};
  694. template <std::size_t Rank>
  695. struct[[deprecated]] is_bounds<strided_bounds<Rank>> : std::integral_constant<bool, true>{};
  696. template <typename IndexType>
  697. class [[deprecated]] bounds_iterator {
  698. public:
  699. static const std::size_t rank = IndexType::rank;
  700. using iterator_category = std::random_access_iterator_tag;
  701. using value_type = IndexType;
  702. using difference_type = std::ptrdiff_t;
  703. using pointer = value_type*;
  704. using reference = value_type&;
  705. using index_type = value_type;
  706. using index_size_type = typename IndexType::value_type;
  707. template <typename Bounds>
  708. explicit bounds_iterator(const Bounds& bnd, value_type curr) noexcept
  709. : boundary_(bnd.index_bounds()), curr_(std::move(curr))
  710. {
  711. static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
  712. }
  713. constexpr reference operator*() const noexcept { return curr_; }
  714. constexpr pointer operator->() const noexcept { return &curr_; }
  715. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  716. GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
  717. constexpr bounds_iterator& operator++() noexcept
  718. {
  719. for (std::size_t i = rank; i-- > 0;)
  720. {
  721. if (curr_[i] < boundary_[i] - 1)
  722. {
  723. curr_[i]++;
  724. return *this;
  725. }
  726. curr_[i] = 0;
  727. }
  728. // If we're here we've wrapped over - set to past-the-end.
  729. curr_ = boundary_;
  730. return *this;
  731. }
  732. constexpr bounds_iterator operator++(int) noexcept
  733. {
  734. auto ret = *this;
  735. ++(*this);
  736. return ret;
  737. }
  738. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  739. constexpr bounds_iterator& operator--()
  740. {
  741. if (!less(curr_, boundary_))
  742. {
  743. // if at the past-the-end, set to last element
  744. for (std::size_t i = 0; i < rank; ++i) { curr_[i] = boundary_[i] - 1; }
  745. return *this;
  746. }
  747. for (std::size_t i = rank; i-- > 0;)
  748. {
  749. if (curr_[i] >= 1)
  750. {
  751. curr_[i]--;
  752. return *this;
  753. }
  754. curr_[i] = boundary_[i] - 1;
  755. }
  756. // If we're here the preconditions were violated
  757. // "pre: there exists s such that r == ++s"
  758. Expects(false);
  759. return *this;
  760. }
  761. constexpr bounds_iterator operator--(int) noexcept
  762. {
  763. auto ret = *this;
  764. --(*this);
  765. return ret;
  766. }
  767. constexpr bounds_iterator operator+(difference_type n) const noexcept
  768. {
  769. bounds_iterator ret{*this};
  770. return ret += n;
  771. }
  772. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  773. constexpr bounds_iterator& operator+=(difference_type n)
  774. {
  775. auto linear_idx = linearize(curr_) + n;
  776. std::remove_const_t<value_type> stride = 0;
  777. stride[rank - 1] = 1;
  778. for (std::size_t i = rank - 1; i-- > 0;) { stride[i] = stride[i + 1] * boundary_[i + 1]; }
  779. for (std::size_t i = 0; i < rank; ++i)
  780. {
  781. curr_[i] = linear_idx / stride[i];
  782. linear_idx = linear_idx % stride[i];
  783. }
  784. // index is out of bounds of the array
  785. Expects(!less(curr_, index_type{}) && !less(boundary_, curr_));
  786. return *this;
  787. }
  788. constexpr bounds_iterator operator-(difference_type n) const noexcept
  789. {
  790. bounds_iterator ret{*this};
  791. return ret -= n;
  792. }
  793. constexpr bounds_iterator& operator-=(difference_type n) noexcept { return *this += -n; }
  794. constexpr difference_type operator-(const bounds_iterator& rhs) const noexcept
  795. {
  796. return linearize(curr_) - linearize(rhs.curr_);
  797. }
  798. constexpr value_type operator[](difference_type n) const noexcept { return *(*this + n); }
  799. constexpr bool operator==(const bounds_iterator& rhs) const noexcept
  800. {
  801. return curr_ == rhs.curr_;
  802. }
  803. constexpr bool operator!=(const bounds_iterator& rhs) const noexcept { return !(*this == rhs); }
  804. constexpr bool operator<(const bounds_iterator& rhs) const noexcept
  805. {
  806. return less(curr_, rhs.curr_);
  807. }
  808. constexpr bool operator<=(const bounds_iterator& rhs) const noexcept { return !(rhs < *this); }
  809. constexpr bool operator>(const bounds_iterator& rhs) const noexcept { return rhs < *this; }
  810. constexpr bool operator>=(const bounds_iterator& rhs) const noexcept { return !(rhs > *this); }
  811. void swap(bounds_iterator & rhs) noexcept
  812. {
  813. std::swap(boundary_, rhs.boundary_);
  814. std::swap(curr_, rhs.curr_);
  815. }
  816. private:
  817. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  818. constexpr bool less(index_type & one, index_type & other) const noexcept
  819. {
  820. for (std::size_t i = 0; i < rank; ++i)
  821. {
  822. if (one[i] < other[i]) return true;
  823. }
  824. return false;
  825. }
  826. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  827. constexpr index_size_type linearize(const value_type& idx) const noexcept
  828. {
  829. // TODO: Smarter impl.
  830. // Check if past-the-end
  831. index_size_type multiplier = 1;
  832. index_size_type res = 0;
  833. if (!less(idx, boundary_))
  834. {
  835. res = 1;
  836. for (std::size_t i = rank; i-- > 0;)
  837. {
  838. res += (idx[i] - 1) * multiplier;
  839. multiplier *= boundary_[i];
  840. }
  841. }
  842. else
  843. {
  844. for (std::size_t i = rank; i-- > 0;)
  845. {
  846. res += idx[i] * multiplier;
  847. multiplier *= boundary_[i];
  848. }
  849. }
  850. return res;
  851. }
  852. value_type boundary_;
  853. std::remove_const_t<value_type> curr_;
  854. };
  855. template <typename IndexType>
  856. bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n,
  857. const bounds_iterator<IndexType>& rhs) noexcept
  858. {
  859. return rhs + n;
  860. }
  861. namespace details
  862. {
  863. template <typename Bounds>
  864. constexpr std::enable_if_t<
  865. std::is_same<typename Bounds::mapping_type, generalized_mapping_tag>::value,
  866. typename Bounds::index_type>
  867. make_stride(const Bounds& bnd) noexcept
  868. {
  869. return bnd.strides();
  870. }
  871. // Make a stride vector from bounds, assuming contiguous memory.
  872. template <typename Bounds>
  873. constexpr std::enable_if_t<
  874. std::is_same<typename Bounds::mapping_type, contiguous_mapping_tag>::value,
  875. typename Bounds::index_type>
  876. make_stride(const Bounds& bnd) noexcept
  877. {
  878. auto extents = bnd.index_bounds();
  879. typename Bounds::size_type stride[Bounds::rank] = {};
  880. stride[Bounds::rank - 1] = 1;
  881. for (std::size_t i = 1; i < Bounds::rank; ++i)
  882. {
  883. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  884. GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
  885. stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i];
  886. }
  887. return {stride};
  888. }
  889. template <typename BoundsSrc, typename BoundsDest>
  890. void verifyBoundsReshape(const BoundsSrc& src, const BoundsDest& dest)
  891. {
  892. static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value,
  893. "The src type and dest type must be bounds");
  894. static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value,
  895. "The source type must be a contiguous bounds");
  896. static_assert(BoundsDest::static_size == dynamic_range ||
  897. BoundsSrc::static_size == dynamic_range ||
  898. BoundsDest::static_size == BoundsSrc::static_size,
  899. "The source bounds must have same size as dest bounds");
  900. Expects(src.size() == dest.size());
  901. }
  902. } // namespace details
  903. template <typename Span>
  904. class [[deprecated]] contiguous_span_iterator;
  905. template <typename Span>
  906. class [[deprecated]] general_span_iterator;
  907. template <std::ptrdiff_t DimSize = dynamic_range>
  908. struct [[deprecated]] dim_t
  909. {
  910. static const std::ptrdiff_t value = DimSize;
  911. };
  912. template <>
  913. struct [[deprecated]] dim_t<dynamic_range>
  914. {
  915. static const std::ptrdiff_t value = dynamic_range;
  916. const std::ptrdiff_t dvalue;
  917. constexpr dim_t(std::ptrdiff_t size) noexcept : dvalue(size) {}
  918. };
  919. template <std::ptrdiff_t N, class = std::enable_if_t<(N >= 0)>>
  920. constexpr dim_t<N> dim() noexcept
  921. {
  922. return dim_t<N>();
  923. }
  924. template <std::ptrdiff_t N = dynamic_range, class = std::enable_if_t<N == dynamic_range>>
  925. constexpr dim_t<N> dim(std::ptrdiff_t n) noexcept
  926. {
  927. return dim_t<>(n);
  928. }
  929. template <typename ValueType, std::ptrdiff_t FirstDimension = dynamic_range,
  930. std::ptrdiff_t... RestDimensions>
  931. class [[deprecated("gsl::multi_span is deprecated because it is not in the C++ Core Guidelines")]] multi_span;
  932. template <typename ValueType, std::size_t Rank>
  933. class [[deprecated("gsl::strided_span is deprecated because it is not in the C++ Core Guidelines")]] strided_span;
  934. namespace details
  935. {
  936. template <typename T, typename = std::true_type>
  937. struct [[deprecated]] SpanTypeTraits
  938. {
  939. using value_type = T;
  940. using size_type = std::size_t;
  941. };
  942. template <typename Traits>
  943. struct [[deprecated]] SpanTypeTraits<
  944. Traits, typename std::is_reference<typename Traits::span_traits&>::type>
  945. {
  946. using value_type = typename Traits::span_traits::value_type;
  947. using size_type = typename Traits::span_traits::size_type;
  948. };
  949. template <typename T, std::ptrdiff_t... Ranks>
  950. struct [[deprecated]] SpanArrayTraits
  951. {
  952. using type = multi_span<T, Ranks...>;
  953. using value_type = T;
  954. using bounds_type = static_bounds<Ranks...>;
  955. using pointer = T*;
  956. using reference = T&;
  957. };
  958. template <typename T, std::ptrdiff_t N, std::ptrdiff_t... Ranks>
  959. struct [[deprecated]] SpanArrayTraits<T[N], Ranks...> : SpanArrayTraits<T, Ranks..., N>
  960. {
  961. };
  962. template <typename BoundsType>
  963. BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::true_type) // dynamic size
  964. {
  965. Expects(totalSize >= 0 && totalSize <= PTRDIFF_MAX);
  966. return BoundsType{totalSize};
  967. }
  968. template <typename BoundsType>
  969. BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::false_type) // static size
  970. {
  971. Expects(BoundsType::static_size <= totalSize);
  972. return {};
  973. }
  974. template <typename BoundsType>
  975. BoundsType newBoundsHelper(std::ptrdiff_t totalSize)
  976. {
  977. static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
  978. return newBoundsHelperImpl<BoundsType>(
  979. totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
  980. }
  981. struct [[deprecated]] Sep
  982. {
  983. };
  984. template <typename T, typename... Args>
  985. T static_as_multi_span_helper(Sep, Args... args)
  986. {
  987. return T{narrow_cast<typename T::size_type>(args)...};
  988. }
  989. template <typename T, typename Arg, typename... Args>
  990. std::enable_if_t<
  991. !std::is_same<Arg, dim_t<dynamic_range>>::value && !std::is_same<Arg, Sep>::value, T>
  992. static_as_multi_span_helper(Arg, Args... args)
  993. {
  994. return static_as_multi_span_helper<T>(args...);
  995. }
  996. template <typename T, typename... Args>
  997. T static_as_multi_span_helper(dim_t<dynamic_range> val, Args... args)
  998. {
  999. return static_as_multi_span_helper<T>(args..., val.dvalue);
  1000. }
  1001. template <typename... Dimensions>
  1002. struct [[deprecated]] static_as_multi_span_static_bounds_helper
  1003. {
  1004. using type = static_bounds<(Dimensions::value)...>;
  1005. };
  1006. template <typename T>
  1007. struct [[deprecated]] is_multi_span_oracle : std::false_type
  1008. {
  1009. };
  1010. template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
  1011. struct [[deprecated]] is_multi_span_oracle<multi_span<ValueType, FirstDimension, RestDimensions...>>
  1012. : std::true_type
  1013. {
  1014. };
  1015. template <typename ValueType, std::ptrdiff_t Rank>
  1016. struct [[deprecated]] is_multi_span_oracle<strided_span<ValueType, Rank>> : std::true_type
  1017. {
  1018. };
  1019. template <typename T>
  1020. struct [[deprecated]] is_multi_span : is_multi_span_oracle<std::remove_cv_t<T>>
  1021. {
  1022. };
  1023. } // namespace details
  1024. template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
  1025. class [[deprecated("gsl::multi_span is deprecated because it is not in the C++ Core Guidelines")]] multi_span {
  1026. // TODO do we still need this?
  1027. template <typename ValueType2, std::ptrdiff_t FirstDimension2,
  1028. std::ptrdiff_t... RestDimensions2>
  1029. friend class multi_span;
  1030. public:
  1031. using bounds_type = static_bounds<FirstDimension, RestDimensions...>;
  1032. static const std::size_t Rank = bounds_type::rank;
  1033. using size_type = typename bounds_type::size_type;
  1034. using index_type = typename bounds_type::index_type;
  1035. using value_type = ValueType;
  1036. using const_value_type = std::add_const_t<value_type>;
  1037. using pointer = std::add_pointer_t<value_type>;
  1038. using reference = std::add_lvalue_reference_t<value_type>;
  1039. using iterator = contiguous_span_iterator<multi_span>;
  1040. using const_span = multi_span<const_value_type, FirstDimension, RestDimensions...>;
  1041. using const_iterator = contiguous_span_iterator<const_span>;
  1042. using reverse_iterator = std::reverse_iterator<iterator>;
  1043. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  1044. using sliced_type =
  1045. std::conditional_t<Rank == 1, value_type, multi_span<value_type, RestDimensions...>>;
  1046. private:
  1047. pointer data_;
  1048. bounds_type bounds_;
  1049. friend iterator;
  1050. friend const_iterator;
  1051. public:
  1052. // default constructor - same as constructing from nullptr_t
  1053. GSL_SUPPRESS(type.6) // NO-FORMAT: attribute // TODO: false positive
  1054. constexpr multi_span() noexcept : multi_span(nullptr, bounds_type{})
  1055. {
  1056. static_assert(bounds_type::dynamic_rank != 0 ||
  1057. (bounds_type::dynamic_rank == 0 && bounds_type::static_size == 0),
  1058. "Default construction of multi_span<T> only possible "
  1059. "for dynamic or fixed, zero-length spans.");
  1060. }
  1061. // construct from nullptr - get an empty multi_span
  1062. GSL_SUPPRESS(type.6) // NO-FORMAT: attribute // TODO: false positive
  1063. constexpr multi_span(std::nullptr_t) noexcept : multi_span(nullptr, bounds_type{})
  1064. {
  1065. static_assert(bounds_type::dynamic_rank != 0 ||
  1066. (bounds_type::dynamic_rank == 0 && bounds_type::static_size == 0),
  1067. "nullptr_t construction of multi_span<T> only possible "
  1068. "for dynamic or fixed, zero-length spans.");
  1069. }
  1070. // construct from nullptr with size of 0 (helps with template function calls)
  1071. template <class IntType, typename = std::enable_if_t<std::is_integral<IntType>::value>>
  1072. // GSL_SUPPRESS(type.6) // NO-FORMAT: attribute // TODO: false positive // TODO: parser bug
  1073. constexpr multi_span(std::nullptr_t, IntType size) : multi_span(nullptr, bounds_type{})
  1074. {
  1075. static_assert(bounds_type::dynamic_rank != 0 ||
  1076. (bounds_type::dynamic_rank == 0 && bounds_type::static_size == 0),
  1077. "nullptr_t construction of multi_span<T> only possible "
  1078. "for dynamic or fixed, zero-length spans.");
  1079. Expects(size == 0);
  1080. }
  1081. // construct from a single element
  1082. GSL_SUPPRESS(type.6) // NO-FORMAT: attribute // TODO: false positive
  1083. constexpr multi_span(reference data) noexcept : multi_span(&data, bounds_type{1})
  1084. {
  1085. static_assert(bounds_type::dynamic_rank > 0 || bounds_type::static_size == 0 ||
  1086. bounds_type::static_size == 1,
  1087. "Construction from a single element only possible "
  1088. "for dynamic or fixed spans of length 0 or 1.");
  1089. }
  1090. // prevent constructing from temporaries for single-elements
  1091. constexpr multi_span(value_type &&) = delete;
  1092. // construct from pointer + length
  1093. GSL_SUPPRESS(type.6) // NO-FORMAT: attribute // TODO: false positive
  1094. constexpr multi_span(pointer ptr, size_type size) : multi_span(ptr, bounds_type{size}) {}
  1095. // construct from pointer + length - multidimensional
  1096. constexpr multi_span(pointer data, bounds_type bounds) : data_(data), bounds_(std::move(bounds))
  1097. {
  1098. Expects((bounds_.size() > 0 && data != nullptr) || bounds_.size() == 0);
  1099. }
  1100. // construct from begin,end pointer pair
  1101. template <typename Ptr,
  1102. typename = std::enable_if_t<std::is_convertible<Ptr, pointer>::value &&
  1103. details::LessThan<bounds_type::dynamic_rank, 2>::value>>
  1104. constexpr multi_span(pointer begin, Ptr end)
  1105. : multi_span(begin,
  1106. details::newBoundsHelper<bounds_type>(static_cast<pointer>(end) - begin))
  1107. {
  1108. Expects(begin != nullptr && end != nullptr && begin <= static_cast<pointer>(end));
  1109. }
  1110. // construct from n-dimensions static array
  1111. template <typename T, std::size_t N, typename Helper = details::SpanArrayTraits<T, N>>
  1112. constexpr multi_span(T(&arr)[N])
  1113. : multi_span(reinterpret_cast<pointer>(arr), bounds_type{typename Helper::bounds_type{}})
  1114. {
  1115. static_assert(std::is_convertible<typename Helper::value_type(*)[], value_type(*)[]>::value,
  1116. "Cannot convert from source type to target multi_span type.");
  1117. static_assert(std::is_convertible<typename Helper::bounds_type, bounds_type>::value,
  1118. "Cannot construct a multi_span from an array with fewer elements.");
  1119. }
  1120. // construct from n-dimensions dynamic array (e.g. new int[m][4])
  1121. // (precedence will be lower than the 1-dimension pointer)
  1122. template <typename T, typename Helper = details::SpanArrayTraits<T, dynamic_range>>
  1123. constexpr multi_span(T* const& data, size_type size)
  1124. : multi_span(reinterpret_cast<pointer>(data), typename Helper::bounds_type{size})
  1125. {
  1126. static_assert(std::is_convertible<typename Helper::value_type(*)[], value_type(*)[]>::value,
  1127. "Cannot convert from source type to target multi_span type.");
  1128. }
  1129. // construct from std::array
  1130. template <typename T, std::size_t N>
  1131. constexpr multi_span(std::array<T, N> & arr)
  1132. : multi_span(arr.data(), bounds_type{static_bounds<N>{}})
  1133. {
  1134. static_assert(
  1135. std::is_convertible<T(*)[], typename std::remove_const_t<value_type>(*)[]>::value,
  1136. "Cannot convert from source type to target multi_span type.");
  1137. static_assert(std::is_convertible<static_bounds<N>, bounds_type>::value,
  1138. "You cannot construct a multi_span from a std::array of smaller size.");
  1139. }
  1140. // construct from const std::array
  1141. template <typename T, std::size_t N>
  1142. constexpr multi_span(const std::array<T, N>& arr)
  1143. : multi_span(arr.data(), bounds_type{static_bounds<N>{}})
  1144. {
  1145. static_assert(
  1146. std::is_convertible<T(*)[], typename std::remove_const_t<value_type>(*)[]>::value,
  1147. "Cannot convert from source type to target multi_span type.");
  1148. static_assert(std::is_convertible<static_bounds<N>, bounds_type>::value,
  1149. "You cannot construct a multi_span from a std::array of smaller size.");
  1150. }
  1151. // prevent constructing from temporary std::array
  1152. template <typename T, std::size_t N>
  1153. constexpr multi_span(std::array<T, N> && arr) = delete;
  1154. // construct from containers
  1155. // future: could use contiguous_iterator_traits to identify only contiguous containers
  1156. // type-requirements: container must have .size(), operator[] which are value_type compatible
  1157. template <typename Cont, typename DataType = typename Cont::value_type,
  1158. typename = std::enable_if_t<
  1159. !details::is_multi_span<Cont>::value &&
  1160. std::is_convertible<DataType(*)[], value_type(*)[]>::value &&
  1161. std::is_same<std::decay_t<decltype(std::declval<Cont>().size(),
  1162. *std::declval<Cont>().data())>,
  1163. DataType>::value>>
  1164. constexpr multi_span(Cont & cont)
  1165. : multi_span(static_cast<pointer>(cont.data()),
  1166. details::newBoundsHelper<bounds_type>(narrow_cast<size_type>(cont.size())))
  1167. {}
  1168. // prevent constructing from temporary containers
  1169. template <typename Cont, typename DataType = typename Cont::value_type,
  1170. typename = std::enable_if_t<
  1171. !details::is_multi_span<Cont>::value &&
  1172. std::is_convertible<DataType(*)[], value_type(*)[]>::value &&
  1173. std::is_same<std::decay_t<decltype(std::declval<Cont>().size(),
  1174. *std::declval<Cont>().data())>,
  1175. DataType>::value>>
  1176. explicit constexpr multi_span(Cont && cont) = delete;
  1177. // construct from a convertible multi_span
  1178. template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
  1179. typename OtherBounds = static_bounds<OtherDimensions...>,
  1180. typename = std::enable_if_t<std::is_convertible<OtherValueType, ValueType>::value &&
  1181. std::is_convertible<OtherBounds, bounds_type>::value>>
  1182. constexpr multi_span(multi_span<OtherValueType, OtherDimensions...> other)
  1183. : data_(other.data_), bounds_(other.bounds_)
  1184. {}
  1185. // trivial copy and move
  1186. constexpr multi_span(const multi_span&) = default;
  1187. constexpr multi_span(multi_span &&) = default;
  1188. // trivial assignment
  1189. constexpr multi_span& operator=(const multi_span&) = default;
  1190. constexpr multi_span& operator=(multi_span&&) = default;
  1191. // first() - extract the first Count elements into a new multi_span
  1192. template <std::ptrdiff_t Count>
  1193. constexpr multi_span<ValueType, Count> first() const
  1194. {
  1195. static_assert(Count >= 0, "Count must be >= 0.");
  1196. static_assert(bounds_type::static_size == dynamic_range ||
  1197. Count <= bounds_type::static_size,
  1198. "Count is out of bounds.");
  1199. Expects(bounds_type::static_size != dynamic_range || Count <= this->size());
  1200. return {this->data(), Count};
  1201. }
  1202. // first() - extract the first count elements into a new multi_span
  1203. constexpr multi_span<ValueType, dynamic_range> first(size_type count) const
  1204. {
  1205. Expects(count >= 0 && count <= this->size());
  1206. return {this->data(), count};
  1207. }
  1208. // last() - extract the last Count elements into a new multi_span
  1209. template <std::ptrdiff_t Count>
  1210. constexpr multi_span<ValueType, Count> last() const
  1211. {
  1212. static_assert(Count >= 0, "Count must be >= 0.");
  1213. static_assert(bounds_type::static_size == dynamic_range ||
  1214. Count <= bounds_type::static_size,
  1215. "Count is out of bounds.");
  1216. Expects(bounds_type::static_size != dynamic_range || Count <= this->size());
  1217. return {this->data() + this->size() - Count, Count};
  1218. }
  1219. // last() - extract the last count elements into a new multi_span
  1220. constexpr multi_span<ValueType, dynamic_range> last(size_type count) const
  1221. {
  1222. Expects(count >= 0 && count <= this->size());
  1223. return {this->data() + this->size() - count, count};
  1224. }
  1225. // subspan() - create a subview of Count elements starting at Offset
  1226. template <std::ptrdiff_t Offset, std::ptrdiff_t Count>
  1227. constexpr multi_span<ValueType, Count> subspan() const
  1228. {
  1229. static_assert(Count >= 0, "Count must be >= 0.");
  1230. static_assert(Offset >= 0, "Offset must be >= 0.");
  1231. static_assert(bounds_type::static_size == dynamic_range ||
  1232. ((Offset <= bounds_type::static_size) &&
  1233. Count <= bounds_type::static_size - Offset),
  1234. "You must describe a sub-range within bounds of the multi_span.");
  1235. Expects(bounds_type::static_size != dynamic_range ||
  1236. (Offset <= this->size() && Count <= this->size() - Offset));
  1237. return {this->data() + Offset, Count};
  1238. }
  1239. // subspan() - create a subview of count elements starting at offset
  1240. // supplying dynamic_range for count will consume all available elements from offset
  1241. constexpr multi_span<ValueType, dynamic_range> subspan(size_type offset,
  1242. size_type count = dynamic_range) const
  1243. {
  1244. Expects((offset >= 0 && offset <= this->size()) &&
  1245. (count == dynamic_range || (count <= this->size() - offset)));
  1246. return {this->data() + offset, count == dynamic_range ? this->length() - offset : count};
  1247. }
  1248. // section - creates a non-contiguous, strided multi_span from a contiguous one
  1249. constexpr strided_span<ValueType, Rank> section(index_type origin, index_type extents) const
  1250. {
  1251. const size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
  1252. return {&this->operator[](origin), size,
  1253. strided_bounds<Rank>{extents, details::make_stride(bounds())}};
  1254. }
  1255. // length of the multi_span in elements
  1256. constexpr size_type size() const noexcept { return bounds_.size(); }
  1257. // length of the multi_span in elements
  1258. constexpr size_type length() const noexcept { return this->size(); }
  1259. // length of the multi_span in bytes
  1260. constexpr size_type size_bytes() const noexcept
  1261. {
  1262. return narrow_cast<size_type>(sizeof(value_type)) * this->size();
  1263. }
  1264. // length of the multi_span in bytes
  1265. constexpr size_type length_bytes() const noexcept { return this->size_bytes(); }
  1266. constexpr bool empty() const noexcept { return this->size() == 0; }
  1267. static constexpr std::size_t rank() { return Rank; }
  1268. template <std::size_t Dim = 0>
  1269. constexpr size_type extent() const noexcept
  1270. {
  1271. static_assert(Dim < Rank,
  1272. "Dimension should be less than rank (dimension count starts from 0).");
  1273. return bounds_.template extent<Dim>();
  1274. }
  1275. template <typename IntType>
  1276. constexpr size_type extent(IntType dim) const
  1277. {
  1278. return bounds_.extent(dim);
  1279. }
  1280. constexpr bounds_type bounds() const noexcept { return bounds_; }
  1281. constexpr pointer data() const noexcept { return data_; }
  1282. template <typename FirstIndex>
  1283. constexpr reference operator()(FirstIndex idx)
  1284. {
  1285. return this->operator[](narrow_cast<std::ptrdiff_t>(idx));
  1286. }
  1287. template <typename FirstIndex, typename... OtherIndices>
  1288. constexpr reference operator()(FirstIndex firstIndex, OtherIndices... indices)
  1289. {
  1290. const index_type idx = {narrow_cast<std::ptrdiff_t>(firstIndex),
  1291. narrow_cast<std::ptrdiff_t>(indices)...};
  1292. return this->operator[](idx);
  1293. }
  1294. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  1295. constexpr reference operator[](const index_type& idx) const
  1296. {
  1297. return data_[bounds_.linearize(idx)];
  1298. }
  1299. template <bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
  1300. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  1301. constexpr Ret operator[](size_type idx) const
  1302. {
  1303. Expects(idx >= 0 && idx < bounds_.size()); // index is out of bounds of the array
  1304. const size_type ridx = idx * bounds_.stride();
  1305. // index is out of bounds of the underlying data
  1306. Expects(ridx < bounds_.total_size());
  1307. return Ret{data_ + ridx, bounds_.slice()};
  1308. }
  1309. constexpr iterator begin() const noexcept { return iterator{this, true}; }
  1310. constexpr iterator end() const noexcept { return iterator{this, false}; }
  1311. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  1312. constexpr const_iterator cbegin() const noexcept
  1313. {
  1314. return const_iterator{reinterpret_cast<const const_span*>(this), true};
  1315. }
  1316. constexpr const_iterator cend() const noexcept
  1317. {
  1318. return const_iterator{reinterpret_cast<const const_span*>(this), false};
  1319. }
  1320. constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
  1321. constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
  1322. constexpr const_reverse_iterator crbegin() const noexcept
  1323. {
  1324. return const_reverse_iterator{cend()};
  1325. }
  1326. constexpr const_reverse_iterator crend() const noexcept
  1327. {
  1328. return const_reverse_iterator{cbegin()};
  1329. }
  1330. template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
  1331. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1332. std::remove_cv_t<OtherValueType>>::value>>
  1333. constexpr bool operator==(const multi_span<OtherValueType, OtherDimensions...>& other) const
  1334. {
  1335. return bounds_.size() == other.bounds_.size() &&
  1336. (data_ == other.data_ || std::equal(this->begin(), this->end(), other.begin()));
  1337. }
  1338. template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
  1339. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1340. std::remove_cv_t<OtherValueType>>::value>>
  1341. constexpr bool operator!=(const multi_span<OtherValueType, OtherDimensions...>& other) const
  1342. {
  1343. return !(*this == other);
  1344. }
  1345. template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
  1346. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1347. std::remove_cv_t<OtherValueType>>::value>>
  1348. constexpr bool operator<(const multi_span<OtherValueType, OtherDimensions...>& other) const
  1349. {
  1350. return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
  1351. }
  1352. template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
  1353. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1354. std::remove_cv_t<OtherValueType>>::value>>
  1355. constexpr bool operator<=(const multi_span<OtherValueType, OtherDimensions...>& other) const
  1356. {
  1357. return !(other < *this);
  1358. }
  1359. template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
  1360. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1361. std::remove_cv_t<OtherValueType>>::value>>
  1362. constexpr bool operator>(const multi_span<OtherValueType, OtherDimensions...>& other)
  1363. const noexcept
  1364. {
  1365. return (other < *this);
  1366. }
  1367. template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
  1368. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1369. std::remove_cv_t<OtherValueType>>::value>>
  1370. constexpr bool operator>=(const multi_span<OtherValueType, OtherDimensions...>& other) const
  1371. {
  1372. return !(*this < other);
  1373. }
  1374. };
  1375. //
  1376. // Free functions for manipulating spans
  1377. //
  1378. // reshape a multi_span into a different dimensionality
  1379. // DimCount and Enabled here are workarounds for a bug in MSVC 2015
  1380. template <typename SpanType, typename... Dimensions2, std::size_t DimCount = sizeof...(Dimensions2),
  1381. bool Enabled = (DimCount > 0), typename = std::enable_if_t<Enabled>>
  1382. constexpr auto as_multi_span(SpanType s, Dimensions2... dims)
  1383. -> multi_span<typename SpanType::value_type, Dimensions2::value...>
  1384. {
  1385. static_assert(details::is_multi_span<SpanType>::value,
  1386. "Variadic as_multi_span() is for reshaping existing spans.");
  1387. using BoundsType =
  1388. typename multi_span<typename SpanType::value_type, (Dimensions2::value)...>::bounds_type;
  1389. const auto tobounds = details::static_as_multi_span_helper<BoundsType>(dims..., details::Sep{});
  1390. details::verifyBoundsReshape(s.bounds(), tobounds);
  1391. return {s.data(), tobounds};
  1392. }
  1393. // convert a multi_span<T> to a multi_span<const byte>
  1394. template <typename U, std::ptrdiff_t... Dimensions>
  1395. multi_span<const byte, dynamic_range>
  1396. as_bytes(multi_span<U, Dimensions...> s) noexcept
  1397. {
  1398. static_assert(std::is_trivial<std::decay_t<U>>::value,
  1399. "The value_type of multi_span must be a trivial type.");
  1400. return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
  1401. }
  1402. // convert a multi_span<T> to a multi_span<byte> (a writeable byte multi_span)
  1403. // this is not currently a portable function that can be relied upon to work
  1404. // on all implementations. It should be considered an experimental extension
  1405. // to the standard GSL interface.
  1406. template <typename U, std::ptrdiff_t... Dimensions>
  1407. multi_span<byte> as_writeable_bytes(multi_span<U, Dimensions...> s) noexcept
  1408. {
  1409. static_assert(std::is_trivial<std::decay_t<U>>::value,
  1410. "The value_type of multi_span must be a trivial type.");
  1411. return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
  1412. }
  1413. // convert a multi_span<const byte> to a multi_span<const T>
  1414. // this is not currently a portable function that can be relied upon to work
  1415. // on all implementations. It should be considered an experimental extension
  1416. // to the standard GSL interface.
  1417. template <typename U, std::ptrdiff_t... Dimensions>
  1418. constexpr auto as_multi_span(multi_span<const byte, Dimensions...> s) -> multi_span<
  1419. const U, static_cast<std::ptrdiff_t>(
  1420. multi_span<const byte, Dimensions...>::bounds_type::static_size != dynamic_range
  1421. ? (static_cast<std::size_t>(
  1422. multi_span<const byte, Dimensions...>::bounds_type::static_size) /
  1423. sizeof(U))
  1424. : dynamic_range)>
  1425. {
  1426. using ConstByteSpan = multi_span<const byte, Dimensions...>;
  1427. static_assert(
  1428. std::is_trivial<std::decay_t<U>>::value &&
  1429. (ConstByteSpan::bounds_type::static_size == dynamic_range ||
  1430. ConstByteSpan::bounds_type::static_size % narrow_cast<std::ptrdiff_t>(sizeof(U)) == 0),
  1431. "Target type must be a trivial type and its size must match the byte array size");
  1432. Expects((s.size_bytes() % narrow_cast<std::ptrdiff_t>(sizeof(U))) == 0 &&
  1433. (s.size_bytes() / narrow_cast<std::ptrdiff_t>(sizeof(U))) < PTRDIFF_MAX);
  1434. return {reinterpret_cast<const U*>(s.data()),
  1435. s.size_bytes() / narrow_cast<std::ptrdiff_t>(sizeof(U))};
  1436. }
  1437. // convert a multi_span<byte> to a multi_span<T>
  1438. // this is not currently a portable function that can be relied upon to work
  1439. // on all implementations. It should be considered an experimental extension
  1440. // to the standard GSL interface.
  1441. template <typename U, std::ptrdiff_t... Dimensions>
  1442. constexpr auto as_multi_span(multi_span<byte, Dimensions...> s)
  1443. -> multi_span<U, narrow_cast<std::ptrdiff_t>(
  1444. multi_span<byte, Dimensions...>::bounds_type::static_size != dynamic_range
  1445. ? static_cast<std::size_t>(
  1446. multi_span<byte, Dimensions...>::bounds_type::static_size) /
  1447. sizeof(U)
  1448. : dynamic_range)>
  1449. {
  1450. using ByteSpan = multi_span<byte, Dimensions...>;
  1451. static_assert(std::is_trivial<std::decay_t<U>>::value &&
  1452. (ByteSpan::bounds_type::static_size == dynamic_range ||
  1453. ByteSpan::bounds_type::static_size % sizeof(U) == 0),
  1454. "Target type must be a trivial type and its size must match the byte array size");
  1455. Expects((s.size_bytes() % sizeof(U)) == 0);
  1456. return {reinterpret_cast<U*>(s.data()),
  1457. s.size_bytes() / narrow_cast<std::ptrdiff_t>(sizeof(U))};
  1458. }
  1459. template <typename T, std::ptrdiff_t... Dimensions>
  1460. constexpr auto as_multi_span(T* const& ptr, dim_t<Dimensions>... args)
  1461. -> multi_span<std::remove_all_extents_t<T>, Dimensions...>
  1462. {
  1463. return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr),
  1464. details::static_as_multi_span_helper<static_bounds<Dimensions...>>(args...,
  1465. details::Sep{})};
  1466. }
  1467. template <typename T>
  1468. constexpr auto as_multi_span(T* arr, std::ptrdiff_t len) ->
  1469. typename details::SpanArrayTraits<T, dynamic_range>::type
  1470. {
  1471. return {reinterpret_cast<std::remove_all_extents_t<T>*>(arr), len};
  1472. }
  1473. template <typename T, std::size_t N>
  1474. constexpr auto as_multi_span(T (&arr)[N]) ->
  1475. typename details::SpanArrayTraits<T, N>::type
  1476. {
  1477. return {arr};
  1478. }
  1479. template <typename T, std::size_t N>
  1480. constexpr multi_span<const T, N> as_multi_span(const std::array<T, N>& arr)
  1481. {
  1482. return {arr};
  1483. }
  1484. template <typename T, std::size_t N>
  1485. constexpr multi_span<const T, N> as_multi_span(const std::array<T, N>&&) = delete;
  1486. template <typename T, std::size_t N>
  1487. constexpr multi_span<T, N> as_multi_span(std::array<T, N>& arr)
  1488. {
  1489. return {arr};
  1490. }
  1491. template <typename T>
  1492. constexpr multi_span<T, dynamic_range> as_multi_span(T* begin, T* end)
  1493. {
  1494. return {begin, end};
  1495. }
  1496. template <typename Cont>
  1497. constexpr auto as_multi_span(Cont& arr) -> std::enable_if_t<
  1498. !details::is_multi_span<std::decay_t<Cont>>::value,
  1499. multi_span<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
  1500. {
  1501. Expects(arr.size() < PTRDIFF_MAX);
  1502. return {arr.data(), narrow_cast<std::ptrdiff_t>(arr.size())};
  1503. }
  1504. template <typename Cont>
  1505. constexpr auto as_multi_span(Cont&& arr) -> std::enable_if_t<
  1506. !details::is_multi_span<std::decay_t<Cont>>::value,
  1507. multi_span<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
  1508. // from basic_string which doesn't have nonconst .data() member like other contiguous containers
  1509. template <typename CharT, typename Traits, typename Allocator>
  1510. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  1511. constexpr auto as_multi_span(std::basic_string<CharT, Traits, Allocator>& str)
  1512. -> multi_span<CharT, dynamic_range>
  1513. {
  1514. Expects(str.size() < PTRDIFF_MAX);
  1515. return {&str[0], narrow_cast<std::ptrdiff_t>(str.size())};
  1516. }
  1517. // strided_span is an extension that is not strictly part of the GSL at this time.
  1518. // It is kept here while the multidimensional interface is still being defined.
  1519. template <typename ValueType, std::size_t Rank>
  1520. class [[deprecated("gsl::strided_span is deprecated because it is not in the C++ Core Guidelines")]] strided_span
  1521. {
  1522. public:
  1523. using bounds_type = strided_bounds<Rank>;
  1524. using size_type = typename bounds_type::size_type;
  1525. using index_type = typename bounds_type::index_type;
  1526. using value_type = ValueType;
  1527. using const_value_type = std::add_const_t<value_type>;
  1528. using pointer = std::add_pointer_t<value_type>;
  1529. using reference = std::add_lvalue_reference_t<value_type>;
  1530. using iterator = general_span_iterator<strided_span>;
  1531. using const_strided_span = strided_span<const_value_type, Rank>;
  1532. using const_iterator = general_span_iterator<const_strided_span>;
  1533. using reverse_iterator = std::reverse_iterator<iterator>;
  1534. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  1535. using sliced_type =
  1536. std::conditional_t<Rank == 1, value_type, strided_span<value_type, Rank - 1>>;
  1537. private:
  1538. pointer data_;
  1539. bounds_type bounds_;
  1540. friend iterator;
  1541. friend const_iterator;
  1542. template <typename OtherValueType, std::size_t OtherRank>
  1543. friend class strided_span;
  1544. public:
  1545. // from raw data
  1546. constexpr strided_span(pointer ptr, size_type size, bounds_type bounds)
  1547. : data_(ptr), bounds_(std::move(bounds))
  1548. {
  1549. Expects((bounds_.size() > 0 && ptr != nullptr) || bounds_.size() == 0);
  1550. // Bounds cross data boundaries
  1551. Expects(this->bounds().total_size() <= size);
  1552. GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: false positive
  1553. (void) size;
  1554. }
  1555. // from static array of size N
  1556. template <size_type N>
  1557. constexpr strided_span(value_type (&values)[N], bounds_type bounds)
  1558. : strided_span(values, N, std::move(bounds))
  1559. {}
  1560. // from array view
  1561. template <typename OtherValueType, std::ptrdiff_t... Dimensions,
  1562. bool Enabled1 = (sizeof...(Dimensions) == Rank),
  1563. bool Enabled2 = std::is_convertible<OtherValueType*, ValueType*>::value,
  1564. typename = std::enable_if_t<Enabled1 && Enabled2>>
  1565. constexpr strided_span(multi_span<OtherValueType, Dimensions...> av, bounds_type bounds)
  1566. : strided_span(av.data(), av.bounds().total_size(), std::move(bounds))
  1567. {}
  1568. // convertible
  1569. template <typename OtherValueType, typename = std::enable_if_t<std::is_convertible<
  1570. OtherValueType (*)[], value_type (*)[]>::value>>
  1571. constexpr strided_span(const strided_span<OtherValueType, Rank>& other)
  1572. : data_(other.data_), bounds_(other.bounds_)
  1573. {}
  1574. // convert from bytes
  1575. template <typename OtherValueType>
  1576. constexpr strided_span<
  1577. typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type,
  1578. Rank>
  1579. as_strided_span() const
  1580. {
  1581. static_assert((sizeof(OtherValueType) >= sizeof(value_type)) &&
  1582. (sizeof(OtherValueType) % sizeof(value_type) == 0),
  1583. "OtherValueType should have a size to contain a multiple of ValueTypes");
  1584. auto d = narrow_cast<size_type>(sizeof(OtherValueType) / sizeof(value_type));
  1585. const size_type size = this->bounds().total_size() / d;
  1586. GSL_SUPPRESS(type.3) // NO-FORMAT: attribute
  1587. return {const_cast<OtherValueType*>(reinterpret_cast<const OtherValueType*>(this->data())),
  1588. size,
  1589. bounds_type{resize_extent(this->bounds().index_bounds(), d),
  1590. resize_stride(this->bounds().strides(), d)}};
  1591. }
  1592. constexpr strided_span section(index_type origin, index_type extents) const
  1593. {
  1594. const size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
  1595. return {&this->operator[](origin), size,
  1596. bounds_type{extents, details::make_stride(bounds())}};
  1597. }
  1598. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  1599. constexpr reference operator[](const index_type& idx) const
  1600. {
  1601. return data_[bounds_.linearize(idx)];
  1602. }
  1603. template <bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
  1604. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  1605. constexpr Ret operator[](size_type idx) const
  1606. {
  1607. Expects(idx < bounds_.size()); // index is out of bounds of the array
  1608. const size_type ridx = idx * bounds_.stride();
  1609. // index is out of bounds of the underlying data
  1610. Expects(ridx < bounds_.total_size());
  1611. return {data_ + ridx, bounds_.slice().total_size(), bounds_.slice()};
  1612. }
  1613. constexpr bounds_type bounds() const noexcept { return bounds_; }
  1614. template <std::size_t Dim = 0>
  1615. constexpr size_type extent() const noexcept
  1616. {
  1617. static_assert(Dim < Rank,
  1618. "dimension should be less than Rank (dimension count starts from 0)");
  1619. return bounds_.template extent<Dim>();
  1620. }
  1621. constexpr size_type size() const noexcept { return bounds_.size(); }
  1622. constexpr pointer data() const noexcept { return data_; }
  1623. constexpr bool empty() const noexcept { return this->size() == 0; }
  1624. constexpr explicit operator bool() const noexcept { return data_ != nullptr; }
  1625. constexpr iterator begin() const { return iterator{this, true}; }
  1626. constexpr iterator end() const { return iterator{this, false}; }
  1627. constexpr const_iterator cbegin() const
  1628. {
  1629. return const_iterator{reinterpret_cast<const const_strided_span*>(this), true};
  1630. }
  1631. constexpr const_iterator cend() const
  1632. {
  1633. return const_iterator{reinterpret_cast<const const_strided_span*>(this), false};
  1634. }
  1635. constexpr reverse_iterator rbegin() const { return reverse_iterator{end()}; }
  1636. constexpr reverse_iterator rend() const { return reverse_iterator{begin()}; }
  1637. constexpr const_reverse_iterator crbegin() const { return const_reverse_iterator{cend()}; }
  1638. constexpr const_reverse_iterator crend() const { return const_reverse_iterator{cbegin()}; }
  1639. template <typename OtherValueType, std::ptrdiff_t OtherRank,
  1640. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1641. std::remove_cv_t<OtherValueType>>::value>>
  1642. constexpr bool operator==(const strided_span<OtherValueType, OtherRank>& other) const
  1643. {
  1644. return bounds_.size() == other.bounds_.size() &&
  1645. (data_ == other.data_ || std::equal(this->begin(), this->end(), other.begin()));
  1646. }
  1647. template <typename OtherValueType, std::ptrdiff_t OtherRank,
  1648. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1649. std::remove_cv_t<OtherValueType>>::value>>
  1650. constexpr bool operator!=(const strided_span<OtherValueType, OtherRank>& other) const
  1651. {
  1652. return !(*this == other);
  1653. }
  1654. template <typename OtherValueType, std::ptrdiff_t OtherRank,
  1655. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1656. std::remove_cv_t<OtherValueType>>::value>>
  1657. constexpr bool operator<(const strided_span<OtherValueType, OtherRank>& other) const
  1658. {
  1659. return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
  1660. }
  1661. template <typename OtherValueType, std::ptrdiff_t OtherRank,
  1662. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1663. std::remove_cv_t<OtherValueType>>::value>>
  1664. constexpr bool operator<=(const strided_span<OtherValueType, OtherRank>& other) const
  1665. {
  1666. return !(other < *this);
  1667. }
  1668. template <typename OtherValueType, std::ptrdiff_t OtherRank,
  1669. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1670. std::remove_cv_t<OtherValueType>>::value>>
  1671. constexpr bool operator>(const strided_span<OtherValueType, OtherRank>& other) const
  1672. {
  1673. return (other < *this);
  1674. }
  1675. template <typename OtherValueType, std::ptrdiff_t OtherRank,
  1676. typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
  1677. std::remove_cv_t<OtherValueType>>::value>>
  1678. constexpr bool operator>=(const strided_span<OtherValueType, OtherRank>& other) const
  1679. {
  1680. return !(*this < other);
  1681. }
  1682. private:
  1683. static index_type resize_extent(const index_type& extent, std::ptrdiff_t d)
  1684. {
  1685. // The last dimension of the array needs to contain a multiple of new type elements
  1686. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  1687. Expects(extent[Rank - 1] >= d && (extent[Rank - 1] % d == 0));
  1688. index_type ret = extent;
  1689. ret[Rank - 1] /= d;
  1690. return ret;
  1691. }
  1692. template <bool Enabled = (Rank == 1), typename = std::enable_if_t<Enabled>>
  1693. static index_type resize_stride(const index_type& strides, std::ptrdiff_t, void* = nullptr)
  1694. {
  1695. // Only strided arrays with regular strides can be resized
  1696. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  1697. Expects(strides[Rank - 1] == 1);
  1698. return strides;
  1699. }
  1700. template <bool Enabled = (Rank > 1), typename = std::enable_if_t<Enabled>>
  1701. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  1702. static index_type resize_stride(const index_type& strides, std::ptrdiff_t d)
  1703. {
  1704. // Only strided arrays with regular strides can be resized
  1705. Expects(strides[Rank - 1] == 1);
  1706. // The strides must have contiguous chunks of
  1707. // memory that can contain a multiple of new type elements
  1708. Expects(strides[Rank - 2] >= d && (strides[Rank - 2] % d == 0));
  1709. for (std::size_t i = Rank - 1; i > 0; --i)
  1710. {
  1711. // Only strided arrays with regular strides can be resized
  1712. Expects((strides[i - 1] >= strides[i]) && (strides[i - 1] % strides[i] == 0));
  1713. }
  1714. index_type ret = strides / d;
  1715. ret[Rank - 1] = 1;
  1716. return ret;
  1717. }
  1718. };
  1719. template <class Span>
  1720. class [[deprecated]] contiguous_span_iterator {
  1721. public:
  1722. using iterator_category = std::random_access_iterator_tag;
  1723. using value_type = typename Span::value_type;
  1724. using difference_type = std::ptrdiff_t;
  1725. using pointer = value_type*;
  1726. using reference = value_type&;
  1727. private:
  1728. template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
  1729. friend class multi_span;
  1730. pointer data_;
  1731. const Span* m_validator;
  1732. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  1733. void validateThis() const
  1734. {
  1735. // iterator is out of range of the array
  1736. Expects(data_ >= m_validator->data_ && data_ < m_validator->data_ + m_validator->size());
  1737. }
  1738. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  1739. contiguous_span_iterator(const Span* container, bool isbegin)
  1740. : data_(isbegin ? container->data_ : container->data_ + container->size())
  1741. , m_validator(container)
  1742. {}
  1743. public:
  1744. reference operator*() const
  1745. {
  1746. validateThis();
  1747. return *data_;
  1748. }
  1749. pointer operator->() const
  1750. {
  1751. validateThis();
  1752. return data_;
  1753. }
  1754. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  1755. contiguous_span_iterator& operator++() noexcept
  1756. {
  1757. ++data_;
  1758. return *this;
  1759. }
  1760. contiguous_span_iterator operator++(int) noexcept
  1761. {
  1762. auto ret = *this;
  1763. ++(*this);
  1764. return ret;
  1765. }
  1766. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  1767. contiguous_span_iterator& operator--() noexcept
  1768. {
  1769. --data_;
  1770. return *this;
  1771. }
  1772. contiguous_span_iterator operator--(int) noexcept
  1773. {
  1774. auto ret = *this;
  1775. --(*this);
  1776. return ret;
  1777. }
  1778. contiguous_span_iterator operator+(difference_type n) const noexcept
  1779. {
  1780. contiguous_span_iterator ret{*this};
  1781. return ret += n;
  1782. }
  1783. contiguous_span_iterator& operator+=(difference_type n) noexcept
  1784. {
  1785. data_ += n;
  1786. return *this;
  1787. }
  1788. contiguous_span_iterator operator-(difference_type n) const noexcept
  1789. {
  1790. contiguous_span_iterator ret{*this};
  1791. return ret -= n;
  1792. }
  1793. contiguous_span_iterator& operator-=(difference_type n) { return *this += -n; }
  1794. difference_type operator-(const contiguous_span_iterator& rhs) const
  1795. {
  1796. Expects(m_validator == rhs.m_validator);
  1797. return data_ - rhs.data_;
  1798. }
  1799. reference operator[](difference_type n) const { return *(*this + n); }
  1800. bool operator==(const contiguous_span_iterator& rhs) const
  1801. {
  1802. Expects(m_validator == rhs.m_validator);
  1803. return data_ == rhs.data_;
  1804. }
  1805. bool operator!=(const contiguous_span_iterator& rhs) const { return !(*this == rhs); }
  1806. bool operator<(const contiguous_span_iterator& rhs) const
  1807. {
  1808. Expects(m_validator == rhs.m_validator);
  1809. return data_ < rhs.data_;
  1810. }
  1811. bool operator<=(const contiguous_span_iterator& rhs) const { return !(rhs < *this); }
  1812. bool operator>(const contiguous_span_iterator& rhs) const { return rhs < *this; }
  1813. bool operator>=(const contiguous_span_iterator& rhs) const { return !(rhs > *this); }
  1814. void swap(contiguous_span_iterator & rhs) noexcept
  1815. {
  1816. std::swap(data_, rhs.data_);
  1817. std::swap(m_validator, rhs.m_validator);
  1818. }
  1819. };
  1820. template <typename Span>
  1821. contiguous_span_iterator<Span> operator+(typename contiguous_span_iterator<Span>::difference_type n,
  1822. const contiguous_span_iterator<Span>& rhs) noexcept
  1823. {
  1824. return rhs + n;
  1825. }
  1826. template <typename Span>
  1827. class [[deprecated]] general_span_iterator {
  1828. public:
  1829. using iterator_category = std::random_access_iterator_tag;
  1830. using value_type = typename Span::value_type;
  1831. using difference_type = std::ptrdiff_t;
  1832. using pointer = value_type*;
  1833. using reference = value_type&;
  1834. private:
  1835. template <typename ValueType, std::size_t Rank>
  1836. friend class strided_span;
  1837. const Span* m_container;
  1838. typename Span::bounds_type::iterator m_itr;
  1839. general_span_iterator(const Span* container, bool isbegin)
  1840. : m_container(container)
  1841. , m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
  1842. {}
  1843. public:
  1844. reference operator*() noexcept { return (*m_container)[*m_itr]; }
  1845. pointer operator->() noexcept { return &(*m_container)[*m_itr]; }
  1846. general_span_iterator& operator++() noexcept
  1847. {
  1848. ++m_itr;
  1849. return *this;
  1850. }
  1851. general_span_iterator operator++(int) noexcept
  1852. {
  1853. auto ret = *this;
  1854. ++(*this);
  1855. return ret;
  1856. }
  1857. general_span_iterator& operator--() noexcept
  1858. {
  1859. --m_itr;
  1860. return *this;
  1861. }
  1862. general_span_iterator operator--(int) noexcept
  1863. {
  1864. auto ret = *this;
  1865. --(*this);
  1866. return ret;
  1867. }
  1868. general_span_iterator operator+(difference_type n) const noexcept
  1869. {
  1870. general_span_iterator ret{*this};
  1871. return ret += n;
  1872. }
  1873. general_span_iterator& operator+=(difference_type n) noexcept
  1874. {
  1875. m_itr += n;
  1876. return *this;
  1877. }
  1878. general_span_iterator operator-(difference_type n) const noexcept
  1879. {
  1880. general_span_iterator ret{*this};
  1881. return ret -= n;
  1882. }
  1883. general_span_iterator& operator-=(difference_type n) noexcept { return *this += -n; }
  1884. difference_type operator-(const general_span_iterator& rhs) const
  1885. {
  1886. Expects(m_container == rhs.m_container);
  1887. return m_itr - rhs.m_itr;
  1888. }
  1889. GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
  1890. value_type operator[](difference_type n) const { return (*m_container)[m_itr[n]]; }
  1891. bool operator==(const general_span_iterator& rhs) const
  1892. {
  1893. Expects(m_container == rhs.m_container);
  1894. return m_itr == rhs.m_itr;
  1895. }
  1896. bool operator!=(const general_span_iterator& rhs) const { return !(*this == rhs); }
  1897. bool operator<(const general_span_iterator& rhs) const
  1898. {
  1899. Expects(m_container == rhs.m_container);
  1900. return m_itr < rhs.m_itr;
  1901. }
  1902. bool operator<=(const general_span_iterator& rhs) const { return !(rhs < *this); }
  1903. bool operator>(const general_span_iterator& rhs) const { return rhs < *this; }
  1904. bool operator>=(const general_span_iterator& rhs) const { return !(rhs > *this); }
  1905. void swap(general_span_iterator & rhs) noexcept
  1906. {
  1907. std::swap(m_itr, rhs.m_itr);
  1908. std::swap(m_container, rhs.m_container);
  1909. }
  1910. };
  1911. template <typename Span>
  1912. general_span_iterator<Span> operator+(typename general_span_iterator<Span>::difference_type n,
  1913. const general_span_iterator<Span>& rhs) noexcept
  1914. {
  1915. return rhs + n;
  1916. }
  1917. } // namespace gsl
  1918. #if defined(_MSC_VER) && !defined(__clang__)
  1919. #if _MSC_VER < 1910
  1920. #undef constexpr
  1921. #pragma pop_macro("constexpr")
  1922. #endif // _MSC_VER < 1910
  1923. #pragma warning(pop)
  1924. #endif // _MSC_VER
  1925. #if defined(__GNUC__) && __GNUC__ > 6
  1926. #pragma GCC diagnostic pop
  1927. #endif // __GNUC__ > 6
  1928. #if defined(__GNUC__) || defined(__clang__)
  1929. #pragma GCC diagnostic pop
  1930. #endif
  1931. #endif // GSL_MULTI_SPAN_H