iterator.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. namespace nall {
  3. template<typename T> struct iterator {
  4. iterator(T* self, uint64_t offset) : _self(self), _offset(offset) {}
  5. auto operator*() -> T& { return _self[_offset]; }
  6. auto operator!=(const iterator& source) const -> bool { return _offset != source._offset; }
  7. auto operator++() -> iterator& { return _offset++, *this; }
  8. auto offset() const -> uint64_t { return _offset; }
  9. private:
  10. T* _self;
  11. uint64_t _offset;
  12. };
  13. template<typename T> struct iterator_const {
  14. iterator_const(const T* self, uint64_t offset) : _self(self), _offset(offset) {}
  15. auto operator*() -> const T& { return _self[_offset]; }
  16. auto operator!=(const iterator_const& source) const -> bool { return _offset != source._offset; }
  17. auto operator++() -> iterator_const& { return _offset++, *this; }
  18. auto offset() const -> uint64_t { return _offset; }
  19. private:
  20. const T* _self;
  21. uint64_t _offset;
  22. };
  23. template<typename T> struct reverse_iterator {
  24. reverse_iterator(T* self, uint64_t offset) : _self(self), _offset(offset) {}
  25. auto operator*() -> T& { return _self[_offset]; }
  26. auto operator!=(const reverse_iterator& source) const -> bool { return _offset != source._offset; }
  27. auto operator++() -> reverse_iterator& { return _offset--, *this; }
  28. auto offset() const -> uint64_t { return _offset; }
  29. private:
  30. T* _self;
  31. uint64_t _offset;
  32. };
  33. template<typename T> struct reverse_iterator_const {
  34. reverse_iterator_const(const T* self, uint64_t offset) : _self(self), _offset(offset) {}
  35. auto operator*() -> const T& { return _self[_offset]; }
  36. auto operator!=(const reverse_iterator_const& source) const -> bool { return _offset != source._offset; }
  37. auto operator++() -> reverse_iterator_const& { return _offset--, *this; }
  38. auto offset() const -> uint64_t { return _offset; }
  39. private:
  40. const T* _self;
  41. uint64_t _offset;
  42. };
  43. //std::rbegin(), std::rend() is missing from GCC 4.9; which I still target
  44. template<typename T, uint64_t Size> auto rbegin(T (&array)[Size]) { return reverse_iterator<T>{array, Size - 1}; }
  45. template<typename T, uint64_t Size> auto rend(T (&array)[Size]) { return reverse_iterator<T>{array, (uint64_t)-1}; }
  46. template<typename T> auto rbegin(T& self) { return self.rbegin(); }
  47. template<typename T> auto rend(T& self) { return self.rend(); }
  48. template<typename T> struct reverse_wrapper {
  49. auto begin() { return rbegin(_self); }
  50. auto end() { return rend(_self); }
  51. auto begin() const { return rbegin(_self); }
  52. auto end() const { return rend(_self); }
  53. T _self;
  54. };
  55. template<typename T> auto reverse(T& object) -> reverse_wrapper<T&> {
  56. return {object};
  57. }
  58. template<typename T> auto reverse(T&& object) -> reverse_wrapper<T> {
  59. return {object};
  60. }
  61. }