adaptive-array.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //deprecated
  2. #pragma once
  3. #include <nall/range.hpp>
  4. #include <nall/traits.hpp>
  5. namespace nall {
  6. template<typename T, uint Capacity>
  7. struct adaptive_array {
  8. auto capacity() const -> uint { return Capacity; }
  9. auto size() const -> uint { return _size; }
  10. auto reset() -> void {
  11. for(uint n : range(_size)) _pool.t[n].~T();
  12. _size = 0;
  13. }
  14. auto operator[](uint index) -> T& {
  15. #ifdef DEBUG
  16. struct out_of_bounds {};
  17. if(index >= Capacity) throw out_of_bounds{};
  18. #endif
  19. return _pool.t[index];
  20. }
  21. auto operator[](uint index) const -> const T& {
  22. #ifdef DEBUG
  23. struct out_of_bounds {};
  24. if(index >= Capacity) throw out_of_bounds{};
  25. #endif
  26. return _pool.t[index];
  27. }
  28. auto append() -> T& {
  29. new(_pool.t + _size) T;
  30. return _pool.t[_size++];
  31. }
  32. auto append(const T& value) -> void {
  33. new(_pool.t + _size++) T(value);
  34. }
  35. auto append(T&& value) -> void {
  36. new(_pool.t + _size++) T(move(value));
  37. }
  38. auto begin() { return &_pool.t[0]; }
  39. auto end() { return &_pool.t[_size]; }
  40. auto begin() const { return &_pool.t[0]; }
  41. auto end() const { return &_pool.t[_size]; }
  42. private:
  43. union U {
  44. U() {}
  45. ~U() {}
  46. T t[Capacity];
  47. } _pool;
  48. uint _size = 0;
  49. };
  50. }