File: array-span.hpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (114 lines) | stat: -rw-r--r-- 3,143 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once

#include <nall/array-view.hpp>

namespace nall {

template<typename T> struct array_span : array_view<T> {
  using type = array_span;
  using super = array_view<T>;

  array_span() {
    super::_data = nullptr;
    super::_size = 0;
  }

  array_span(nullptr_t) {
    super::_data = nullptr;
    super::_size = 0;
  }

  array_span(void* data, u64 size) {
    super::_data = (T*)data;
    super::_size = (s32)size;
  }

  template<s32 size> array_span(T (&data)[size]) {
    super::_data = data;
    super::_size = size;
  }

  explicit operator bool() const {
    return super::_data && super::_size > 0;
  }

  explicit operator T*() {
    return (T*)super::_data;
  }

  T& operator*() const {
    return (T&)*super::_data;
  }

  auto operator++() -> type& { super::_data++; super::_size--; return *this; }
  auto operator--() -> type& { super::_data--; super::_size++; return *this; }

  auto operator++(s32) -> type { auto copy = *this; ++(*this); return copy; }
  auto operator--(s32) -> type { auto copy = *this; --(*this); return copy; }

  auto operator[](u32 index) -> T& { return (T&)super::operator[](index); }

  template<typename U = T> auto data() -> U* { return (U*)super::_data; }
  template<typename U = T> auto data() const -> const U* { return (const U*)super::_data; }

  auto begin() -> iterator<T> { return {(T*)super::_data, (u32)0}; }
  auto end() -> iterator<T> { return {(T*)super::_data, (u32)super::_size}; }

  auto rbegin() -> reverse_iterator<T> { return {(T*)super::_data, (u32)super::_size - 1}; }
  auto rend() -> reverse_iterator<T> { return {(T*)super::_data, (u32)-1}; }

  auto write(T value) -> void {
    operator[](0) = value;
    super::_data++;
    super::_size--;
  }

  auto span(u32 offset, u32 length) -> type {
    #ifdef DEBUG
    struct out_of_bounds {};
    if(offset + length >= super::_size) throw out_of_bounds{};
    #endif
    return {(T*)super::_data + offset, length};
  }

  //array_span<u8> specializations
  template<typename U> auto writel(U value, u32 size) -> void;
  template<typename U> auto writem(U value, u32 size) -> void;
  template<typename U> auto writevn(U value, u32 size) -> void;
  template<typename U> auto writevi(U value, u32 size) -> void;
};

//array_span<u8>

template<> inline auto array_span<u8>::write(u8 value) -> void {
  operator[](0) = value;
  _data++;
  _size--;
}

template<> template<typename U> inline auto array_span<u8>::writel(U value, u32 size) -> void {
  for(u32 byte : range(size)) write(value >> byte * 8);
}

template<> template<typename U> inline auto array_span<u8>::writem(U value, u32 size) -> void {
  for(u32 byte : reverse(range(size))) write(value >> byte * 8);
}

template<> template<typename U> inline auto array_span<u8>::writevn(U value, u32 size) -> void {
  while(true) {
    auto byte = value & 0x7f;
    value >>= 7;
    if(value == 0) return write(0x80 | byte);
    write(byte);
    value--;
  }
}

template<> template<typename U> inline auto array_span<u8>::writevi(U value, u32 size) -> void {
  bool negate = value < 0;
  if(negate) value = ~value;
  value = value << 1 | negate;
  writevn(value);
}

}