Advertisement
tinyevil

Untitled

Jul 13th, 2019
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1.  
  2. template<class T>
  3. class base_span{
  4. public:
  5. constexpr base_span(std::nullptr_t):
  6. _begin(nullptr),
  7. _size(0)
  8. {
  9. }
  10.  
  11. constexpr base_span(T* begin, T* end):
  12. _begin(begin),
  13. _size(end - begin)
  14. {
  15. }
  16.  
  17. constexpr base_span(T* begin, uword size) :
  18. _begin(begin),
  19. _size(size) {
  20. }
  21.  
  22. constexpr base_span(std::initializer_list<T> init) :
  23. _begin(init.begin()),
  24. _size(init.size()) {
  25. }
  26.  
  27. template<size_t N>
  28. constexpr base_span(T(&static_array)[N]):
  29. _begin(static_array),
  30. _size(N)
  31. {
  32. }
  33.  
  34. constexpr T* begin() const{
  35. return _begin;
  36. }
  37.  
  38. constexpr T* end() const{
  39. return _begin + _size;
  40. }
  41.  
  42. constexpr T* data() const{
  43. return _begin;
  44. }
  45.  
  46. constexpr uword size() const{
  47. return _size;
  48. }
  49.  
  50. constexpr T& operator[](uword index) const{
  51. octo_assert(index < _size);
  52. return _begin[index];
  53. }
  54.  
  55. constexpr operator base_span<const T>() const{
  56. return {_begin, _size};
  57. }
  58.  
  59. constexpr base_span<T> slice(uword start, uword end) const{
  60. return {_begin + start, _begin + start + end};
  61. }
  62. private:
  63. T* _begin;
  64. uword _size;
  65. };
  66.  
  67. template<class T>
  68. using span = base_span<const T>;
  69.  
  70. template<class T>
  71. using mut_span = base_span<T>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement