vector.h - Engine C API Reference

vector.h
  1. #pragma once
  2. #include "allocator.h"
  3. #include "template_tools.h"
  4. #include "assert.h"
  5. #include "math.h"
  6. #include <string.h> // memmove
  7. #include <algorithm>
  8. #if !defined(PS4)
  9. #pragma warning(disable:4345)
  10. #endif
  11. namespace stingray_plugin_foundation {
  12. struct NoAllocator {};
  13. // A replacement for the std::vector class.
  14. //
  15. // The main difference between an std::vector and this vector class, is that this vector
  16. // uses a specific allocator (a pointer to the allocator is stored) and allocates all its
  17. // memory through that allocator. This makes it easy to support various types of memory
  18. // allocation strategies.
  19. //
  20. // This vector implementation assumes that the types are "raw-movable"... i.e. that they
  21. // can be bit-wise copied from one memory location to another without invoking a copy-
  22. // constructor and a destructor.
  23. //
  24. // Unimplemented functions from standard stl (add if needed):
  25. // - vector(unsigned n, const T& t)
  26. // - vector(InputIterator, InputIterator),
  27. // - rbegin()
  28. // - rend()
  29. // - max_size(),
  30. // - void insert(iterator pos, InputIterator f, InputIterator l)
  31. // - void insert(iterator pos, unsigned n, const T& x)
  32. // - void resize(n, t = T())
  33. // - bool operator==(const vector&, const vector&)
  34. // - bool operator<(const vector&, const vector&)
  35. template <class T>
  36. class Vector
  37. {
  38. public:
  39. ALLOCATOR_AWARE;
  40. // Type definitions
  41. typedef Vector<T> this_type;
  42. typedef T value_type;
  43. typedef T* pointer;
  44. typedef const T* const_pointer;
  45. typedef T& reference;
  46. typedef const T& const_reference;
  47. typedef pointer iterator;
  48. typedef const_pointer const_iterator;
  49. // Constructors
  50. // Creates a vector that uses the specified allocator to allocate memory.
  51. Vector(Allocator &allocator);
  52. // Creates a vector and presizes it to the specified size.
  53. Vector(unsigned size, Allocator &allocator);
  54. // Creates a vector without an allocator, one must be provided later with
  55. // set_allocator() before the vector is resized.
  56. Vector(const NoAllocator &);
  57. // Sets the allocator of the vector. You can only call this before any
  58. // allocations have been made by the vector.
  59. void set_allocator(Allocator &allocator);
  60. Vector( const Vector<T> &o );
  61. ~Vector() {reset();}
  62. // Asignment operator -- note that we do not support chained assignments.
  63. void operator=(const Vector<T> &o);
  64. // Serializes the vector to the stream.
  65. template <class STREAM> void serialize(STREAM &stream);
  66. // std::vector interface
  67. iterator begin() {return _data;}
  68. const_iterator begin() const {return _data;}
  69. iterator end() {return _data + _size;}
  70. const_iterator end() const {return _data + _size;}
  71. unsigned size() const {return _size;}
  72. unsigned capacity() const {return _capacity;}
  73. bool any() const {return _size != 0;}
  74. bool empty() const {return _size == 0;}
  75. reference operator[](unsigned i);
  76. const_reference operator[](unsigned i) const;
  77. void reserve(unsigned capacity);
  78. reference front() {XENSURE(_size > 0); return _data[0];}
  79. const_reference front() const {XENSURE(_size > 0); return _data[0];}
  80. reference back() {XENSURE(_size > 0); return _data[_size-1];}
  81. const_reference back() const {XENSURE(_size > 0); return _data[_size-1];}
  82. template <class ASSIGNABLE> void push_back(const ASSIGNABLE &item);
  83. void pop_back();
  84. void swap(Vector<T> &o);
  85. template <class ASSIGNABLE> iterator insert(iterator pos, const ASSIGNABLE & x);
  86. iterator insert(iterator pos);
  87. void insert(iterator pos, const_iterator from, const_iterator to);
  88. iterator erase(iterator pos);
  89. iterator erase(iterator first, iterator last);
  90. void clear() {resize(0);}
  91. void resize(unsigned size);
  92. bool operator==(const Vector<T> &o) const;
  93. bool operator<(const Vector<T> &o) const;
  94. // Vector extensions
  95. // Resets the vector to initial state (no memory allocated).
  96. void reset() {set_capacity(0);}
  97. // Extends the vector with the specified number of elements.
  98. void extend(unsigned elements) { resize(size() + elements); }
  99. // Extends the vector with one elements and returns a reference to the newly created element.
  100. reference extend() { resize(size() + 1); return back(); }
  101. // Returns the allocator of the vector.
  102. Allocator &allocator() const {return *_allocator;}
  103. // Sets the capacity of the vector to exactly the specified amount.
  104. // This operation always reallocates the memory of the vector, no
  105. // swap trick is necessary to enforce reallocation.
  106. void set_capacity(unsigned capacity);
  107. // Trims the vector so that the capacity corresponds to its current size.
  108. void trim() {set_capacity(size());}
  109. // Finds the first occurrence of x in the vector and returns it.
  110. template <typename EQUATABLE> iterator find(const EQUATABLE &x) {return std::find(begin(), end(), x);}
  111. // Finds the first occurrence of x in the vector and returns it.
  112. template <typename EQUATABLE> const_iterator find(const EQUATABLE &x) const {return std::find(begin(), end(), x);}
  113. // Returns the index of x in the vector (or size() if it is not in the vector).
  114. template <typename EQUATABLE> unsigned index_of(const EQUATABLE &x) const {return (unsigned)(find(x) - begin());}
  115. // Returns if x is in the vector
  116. template <typename EQUATABLE> bool has(const EQUATABLE &x) const {return (find(x) != end());}
  117. // Erases the first occurrence of x in the vector.
  118. template <typename EQUATABLE> void erase(const EQUATABLE &item);
  119. private:
  120. void move(pointer to, pointer from, ptrdiff_t n);
  121. void construct(pointer p, const Int2Type<true> &) {new (p) T(*_allocator);}
  122. void construct(pointer p, const Int2Type<false> &) {new (p) T();}
  123. void grow(unsigned min_capacity = 0);
  124. private:
  125. unsigned _size;
  126. unsigned _capacity;
  127. pointer _data;
  128. Allocator * _allocator;
  129. };
  130. }
  131. #include "vector.inl"