range.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. #pragma once
  2. #include <library/cpp/yt/assert/assert.h>
  3. #include <library/cpp/yt/misc/hash.h>
  4. #include <vector>
  5. #include <array>
  6. #include <optional>
  7. #include <initializer_list>
  8. // For size_t.
  9. #include <stddef.h>
  10. namespace google::protobuf {
  11. ////////////////////////////////////////////////////////////////////////////////
  12. // Forward declarations
  13. template <class T>
  14. class RepeatedField;
  15. template <class T>
  16. class RepeatedPtrField;
  17. ////////////////////////////////////////////////////////////////////////////////
  18. } // namespace google::protobuf
  19. namespace NYT {
  20. ////////////////////////////////////////////////////////////////////////////////
  21. // Forward declarations
  22. template <class T, size_t N>
  23. class TCompactVector;
  24. ////////////////////////////////////////////////////////////////////////////////
  25. //! TRange (inspired by TArrayRef from LLVM)
  26. /*!
  27. * Represents a constant reference to an array (zero or more elements
  28. * consecutively in memory), i. e. a start pointer and a length. It allows
  29. * various APIs to take consecutive elements easily and conveniently.
  30. *
  31. * This class does not own the underlying data, it is expected to be used in
  32. * situations where the data resides in some other buffer, whose lifetime
  33. * extends past that of the TRange. For this reason, it is not in general
  34. * safe to store an TRange.
  35. *
  36. * This is intended to be trivially copyable, so it should be passed by
  37. * value.
  38. */
  39. template <class T>
  40. class TRange
  41. {
  42. public:
  43. using iterator = const T*;
  44. using const_iterator = const T*;
  45. using size_type = size_t;
  46. //! Constructs a null TRange.
  47. TRange()
  48. : Data_(nullptr)
  49. , Length_(0)
  50. { }
  51. //! Constructs a TRange from a pointer and length.
  52. TRange(const T* data, size_t length)
  53. : Data_(data)
  54. , Length_(length)
  55. { }
  56. //! Constructs a TRange from a range.
  57. TRange(const T* begin, const T* end)
  58. : Data_(begin)
  59. , Length_(end - begin)
  60. { }
  61. //! Constructs a TRange from a TCompactVector.
  62. template <size_t N>
  63. TRange(const TCompactVector<T, N>& elements)
  64. : Data_(elements.data())
  65. , Length_(elements.size())
  66. { }
  67. //! Constructs a TRange from an std::vector.
  68. template <class A>
  69. TRange(const std::vector<T, A>& elements)
  70. : Data_(elements.empty() ? nullptr : elements.data())
  71. , Length_(elements.size())
  72. { }
  73. //! Constructs a TRange from a C array.
  74. template <size_t N>
  75. TRange(const T (&elements)[N])
  76. : Data_(elements)
  77. , Length_(N)
  78. { }
  79. //! Constructs a TRange from std::initializer_list.
  80. TRange(std::initializer_list<T> elements)
  81. : Data_(elements.begin())
  82. , Length_(elements.size())
  83. { }
  84. //! Constructs a TRange from std::array.
  85. template <size_t N>
  86. TRange(const std::array<T, N>& elements)
  87. : Data_(elements.data())
  88. , Length_(N)
  89. { }
  90. //! Constructs a TRange from std::optional.
  91. //! Range will contain 0-1 elements.
  92. explicit TRange(const std::optional<T>& element)
  93. : Data_(element ? &*element : nullptr)
  94. , Length_(element ? 1 : 0)
  95. { }
  96. const_iterator Begin() const
  97. {
  98. return Data_;
  99. }
  100. // STL interop, for gcc.
  101. const_iterator begin() const
  102. {
  103. return Begin();
  104. }
  105. const_iterator End() const
  106. {
  107. return Data_ + Length_;
  108. }
  109. // STL interop, for gcc.
  110. const_iterator end() const
  111. {
  112. return End();
  113. }
  114. bool Empty() const
  115. {
  116. return Length_ == 0;
  117. }
  118. bool empty() const
  119. {
  120. return Empty();
  121. }
  122. explicit operator bool() const
  123. {
  124. return Data_ != nullptr;
  125. }
  126. size_t Size() const
  127. {
  128. return Length_;
  129. }
  130. size_t size() const
  131. {
  132. return Size();
  133. }
  134. const T* Data() const
  135. {
  136. return Data_;
  137. }
  138. const T* data() const
  139. {
  140. return Data();
  141. }
  142. const T& operator[](size_t index) const
  143. {
  144. YT_ASSERT(index < Size());
  145. return Data_[index];
  146. }
  147. const T& Front() const
  148. {
  149. YT_ASSERT(Length_ > 0);
  150. return Data_[0];
  151. }
  152. const T& Back() const
  153. {
  154. YT_ASSERT(Length_ > 0);
  155. return Data_[Length_ - 1];
  156. }
  157. TRange<T> Slice(size_t startOffset, size_t endOffset) const
  158. {
  159. YT_ASSERT(startOffset <= endOffset && endOffset <= Size());
  160. return TRange<T>(Begin() + startOffset, endOffset - startOffset);
  161. }
  162. std::vector<T> ToVector() const
  163. {
  164. return std::vector<T>(Data_, Data_ + Length_);
  165. }
  166. protected:
  167. //! The start of the array, in an external buffer.
  168. const T* Data_;
  169. //! The number of elements.
  170. size_t Length_;
  171. };
  172. // STL interop.
  173. template <class T>
  174. typename TRange<T>::const_iterator begin(TRange<T> ref)
  175. {
  176. return ref.Begin();
  177. }
  178. template <class T>
  179. typename TRange<T>::const_iterator end(TRange<T> ref)
  180. {
  181. return ref.End();
  182. }
  183. ////////////////////////////////////////////////////////////////////////////////
  184. //! Constructs a TRange from a pointer and length.
  185. template <class T>
  186. TRange<T> MakeRange(const T* data, size_t length)
  187. {
  188. return TRange<T>(data, length);
  189. }
  190. //! Constructs a TRange from a native range.
  191. template <class T>
  192. TRange<T> MakeRange(const T* begin, const T* end)
  193. {
  194. return TRange<T>(begin, end);
  195. }
  196. //! Constructs a TRange from a TCompactVector.
  197. template <class T, size_t N>
  198. TRange<T> MakeRange(const TCompactVector<T, N>& elements)
  199. {
  200. return elements;
  201. }
  202. //! "Copy-constructor".
  203. template <class T>
  204. TRange<T> MakeRange(TRange<T> range)
  205. {
  206. return range;
  207. }
  208. //! Constructs a TRange from an std::vector.
  209. template <class T>
  210. TRange<T> MakeRange(const std::vector<T>& elements)
  211. {
  212. return elements;
  213. }
  214. //! Constructs a TRange from an std::array.
  215. template <class T, size_t N>
  216. TRange<T> MakeRange(const std::array<T, N>& elements)
  217. {
  218. return elements;
  219. }
  220. //! Constructs a TRange from a C array.
  221. template <class T, size_t N>
  222. TRange<T> MakeRange(const T (& elements)[N])
  223. {
  224. return TRange<T>(elements);
  225. }
  226. //! Constructs a TRange from RepeatedField.
  227. template <class T>
  228. TRange<T> MakeRange(const google::protobuf::RepeatedField<T>& elements)
  229. {
  230. return TRange<T>(elements.data(), elements.size());
  231. }
  232. //! Constructs a TRange from RepeatedPtrField.
  233. template <class T>
  234. TRange<const T*> MakeRange(const google::protobuf::RepeatedPtrField<T>& elements)
  235. {
  236. return TRange<const T*>(elements.data(), elements.size());
  237. }
  238. template <class U, class T>
  239. TRange<U> ReinterpretCastRange(TRange<T> range)
  240. {
  241. static_assert(sizeof(T) == sizeof(U), "T and U must have equal sizes.");
  242. return TRange<U>(reinterpret_cast<const U*>(range.Begin()), range.Size());
  243. }
  244. ////////////////////////////////////////////////////////////////////////////////
  245. // TMutableRange (inspired by TMutableArrayRef from LLVM)
  246. /*
  247. * Represents a mutable reference to an array (zero or more elements
  248. * consecutively in memory), i. e. a start pointer and a length.
  249. * It allows various APIs to take and modify consecutive elements easily and
  250. * conveniently.
  251. *
  252. * This class does not own the underlying data, it is expected to be used in
  253. * situations where the data resides in some other buffer, whose lifetime
  254. * extends past that of the TMutableRange. For this reason, it is not in
  255. * general safe to store a TMutableRange.
  256. *
  257. * This is intended to be trivially copyable, so it should be passed by value.
  258. */
  259. template <class T>
  260. class TMutableRange
  261. : public TRange<T>
  262. {
  263. public:
  264. using iterator = T*;
  265. //! Constructs a null TMutableRange.
  266. TMutableRange()
  267. { }
  268. //! Constructs a TMutableRange from a pointer and length.
  269. TMutableRange(T* data, size_t length)
  270. : TRange<T>(data, length)
  271. { }
  272. //! Constructs a TMutableRange from a range.
  273. TMutableRange(T* begin, T* end)
  274. : TRange<T>(begin, end)
  275. { }
  276. //! Constructs a TMutableRange from a TCompactVector.
  277. template <size_t N>
  278. TMutableRange(TCompactVector<T, N>& elements)
  279. : TRange<T>(elements)
  280. { }
  281. //! Constructs a TMutableRange from an std::vector.
  282. TMutableRange(std::vector<T>& elements)
  283. : TRange<T>(elements)
  284. { }
  285. //! Constructs a TMutableRange from std::array.
  286. template <size_t N>
  287. TMutableRange(std::array<T, N>& elements)
  288. : TRange<T>(elements.data(), N)
  289. { }
  290. //! Construct a TMutableRange from an std::optional
  291. //! Range will contain 0-1 elements.
  292. explicit TMutableRange(std::optional<T>& optional)
  293. : TRange<T>(optional)
  294. { }
  295. //! Constructs a TMutableRange from a C array.
  296. template <size_t N>
  297. TMutableRange(T (& elements)[N])
  298. : TRange<T>(elements)
  299. { }
  300. using TRange<T>::Begin;
  301. using TRange<T>::End;
  302. using TRange<T>::Front;
  303. using TRange<T>::Back;
  304. using TRange<T>::operator[];
  305. iterator Begin() const
  306. {
  307. return const_cast<T*>(this->Data_);
  308. }
  309. // STL interop, for gcc.
  310. iterator begin() const
  311. {
  312. return Begin();
  313. }
  314. iterator End() const
  315. {
  316. return this->Begin() + this->Size();
  317. }
  318. // STL interop, for gcc.
  319. iterator end() const
  320. {
  321. return End();
  322. }
  323. T& operator[](size_t index)
  324. {
  325. YT_ASSERT(index <= this->Size());
  326. return Begin()[index];
  327. }
  328. T& Front()
  329. {
  330. YT_ASSERT(this->Length_ > 0);
  331. return Begin()[0];
  332. }
  333. T& Back()
  334. {
  335. YT_ASSERT(this->Length_ > 0);
  336. return Begin()[this->Length_ - 1];
  337. }
  338. TMutableRange<T> Slice(size_t startOffset, size_t endOffset) const
  339. {
  340. YT_ASSERT(startOffset <= endOffset && endOffset <= this->Size());
  341. return TMutableRange<T>(Begin() + startOffset, endOffset - startOffset);
  342. }
  343. TMutableRange<T> Slice(T* begin, T* end) const
  344. {
  345. YT_ASSERT(begin >= Begin());
  346. YT_ASSERT(end <= End());
  347. return TMutableRange<T>(begin, end);
  348. }
  349. };
  350. // STL interop.
  351. template <class T>
  352. typename TMutableRange<T>::iterator begin(TMutableRange<T> ref)
  353. {
  354. return ref.Begin();
  355. }
  356. template <class T>
  357. typename TMutableRange<T>::iterator end(TMutableRange<T> ref)
  358. {
  359. return ref.End();
  360. }
  361. ////////////////////////////////////////////////////////////////////////////////
  362. //! Constructs a TMutableRange from a pointer and length.
  363. template <class T>
  364. TMutableRange<T> MakeMutableRange(T* data, size_t length)
  365. {
  366. return TMutableRange<T>(data, length);
  367. }
  368. //! Constructs a TMutableRange from a native range.
  369. template <class T>
  370. TMutableRange<T> MakeMutableRange(T* begin, T* end)
  371. {
  372. return TMutableRange<T>(begin, end);
  373. }
  374. //! Constructs a TMutableRange from a TCompactVector.
  375. template <class T, size_t N>
  376. TMutableRange<T> MakeMutableRange(TCompactVector<T, N>& elements)
  377. {
  378. return elements;
  379. }
  380. //! "Copy-constructor".
  381. template <class T>
  382. TMutableRange<T> MakeMutableRange(TMutableRange<T> range)
  383. {
  384. return range;
  385. }
  386. //! Constructs a TMutableRange from an std::vector.
  387. template <class T>
  388. TMutableRange<T> MakeMutableRange(std::vector<T>& elements)
  389. {
  390. return elements;
  391. }
  392. //! Constructs a TMutableRange from an std::array.
  393. template <class T, size_t N>
  394. TMutableRange<T> MakeMutableRange(std::array<T, N>& elements)
  395. {
  396. return elements;
  397. }
  398. //! Constructs a TMutableRange from a C array.
  399. template <class T, size_t N>
  400. TMutableRange<T> MakeMutableRange(T (& elements)[N])
  401. {
  402. return TMutableRange<T>(elements);
  403. }
  404. //! Constructs a TMutableRange from RepeatedField.
  405. template <class T>
  406. TMutableRange<T> MakeMutableRange(google::protobuf::RepeatedField<T>& elements)
  407. {
  408. return TMutableRange<T>(elements.data(), elements.size());
  409. }
  410. //! Constructs a TMutableRange from RepeatedPtrField.
  411. template <class T>
  412. TMutableRange<T*> MakeMutableRange(google::protobuf::RepeatedPtrField<T>& elements)
  413. {
  414. return TMutableRange<const T*>(elements.data(), elements.size());
  415. }
  416. template <class U, class T>
  417. TMutableRange<U> ReinterpretCastMutableRange(TMutableRange<T> range)
  418. {
  419. static_assert(sizeof(T) == sizeof(U), "T and U must have equal sizes.");
  420. return TMutableRange<U>(reinterpret_cast<U*>(range.Begin()), range.Size());
  421. }
  422. ////////////////////////////////////////////////////////////////////////////////
  423. // Mark TMutableRange and TMutableRange as PODs.
  424. namespace NMpl {
  425. template <class T>
  426. struct TIsPod;
  427. template <class T>
  428. struct TIsPod<TRange<T>>
  429. {
  430. static const bool Value = true;
  431. };
  432. template <class T>
  433. struct TIsPod<TMutableRange<T>>
  434. {
  435. static const bool Value = true;
  436. };
  437. } // namespace NMpl
  438. ////////////////////////////////////////////////////////////////////////////////
  439. } // namespace NYT
  440. template <class T>
  441. struct hash<NYT::TRange<T>>
  442. {
  443. size_t operator()(const NYT::TRange<T>& range) const
  444. {
  445. size_t result = 0;
  446. for (const auto& element : range) {
  447. NYT::HashCombine(result, element);
  448. }
  449. return result;
  450. }
  451. };
  452. template <class T>
  453. struct hash<NYT::TMutableRange<T>>
  454. {
  455. size_t operator()(const NYT::TMutableRange<T>& range) const
  456. {
  457. size_t result = 0;
  458. for (const auto& element : range) {
  459. NYT::HashCombine(result, element);
  460. }
  461. return result;
  462. }
  463. };