vector.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef _SCTL_VECTOR_HPP_
  2. #define _SCTL_VECTOR_HPP_
  3. #include SCTL_INCLUDE(mem_mgr.hpp)
  4. #include SCTL_INCLUDE(common.hpp)
  5. #include <vector>
  6. #include <cstdlib>
  7. #include <cstdint>
  8. // TODO: Implement dynamic-vector which can be created with variable size from a stack memory pool
  9. namespace SCTL_NAMESPACE {
  10. template <class ValueType> class ConstVector {
  11. public:
  12. typedef ValueType value_type;
  13. typedef ValueType& reference;
  14. typedef const ValueType& const_reference;
  15. typedef Iterator<ValueType> iterator;
  16. typedef ConstIterator<ValueType> const_iterator;
  17. typedef Long difference_type;
  18. typedef Long size_type;
  19. ConstVector();
  20. ConstVector(Long dim_, ConstIterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
  21. ConstVector(const ConstVector& V, bool own_data_ = true);
  22. ConstVector(const std::vector<ValueType>& V, bool own_data_ = true);
  23. ~ConstVector();
  24. void Swap(ConstVector<ValueType>& v1);
  25. void ReInit(Long dim_, ConstIterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
  26. void Write(const char* fname) const;
  27. Long Dim() const;
  28. ConstIterator<ValueType> begin() const;
  29. ConstIterator<ValueType> end() const;
  30. // Element access
  31. const ValueType& operator[](Long j) const;
  32. // ConstVector-ConstVector operations
  33. Vector<ValueType> operator+(const ConstVector& V) const;
  34. Vector<ValueType> operator-(const ConstVector& V) const;
  35. Vector<ValueType> operator*(const ConstVector& V) const;
  36. Vector<ValueType> operator/(const ConstVector& V) const;
  37. // ConstVector-Scalar operations
  38. Vector<ValueType> operator+(ValueType s) const;
  39. Vector<ValueType> operator-(ValueType s) const;
  40. Vector<ValueType> operator*(ValueType s) const;
  41. Vector<ValueType> operator/(ValueType s) const;
  42. protected:
  43. void Init(Long dim_, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
  44. Long dim;
  45. Long capacity;
  46. Iterator<ValueType> data_ptr;
  47. bool own_data;
  48. };
  49. template <class ValueType> class Vector : public ConstVector<ValueType> {
  50. public:
  51. typedef ValueType value_type;
  52. typedef ValueType& reference;
  53. typedef const ValueType& const_reference;
  54. typedef Iterator<ValueType> iterator;
  55. typedef ConstIterator<ValueType> const_iterator;
  56. typedef Long difference_type;
  57. typedef Long size_type;
  58. Vector();
  59. Vector(Long dim_, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
  60. Vector(const Vector& V);
  61. Vector(const std::vector<ValueType>& V);
  62. //~Vector();
  63. void Swap(Vector<ValueType>& v1);
  64. void ReInit(Long dim_, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
  65. void Read(const char* fname);
  66. void SetZero();
  67. ConstIterator<ValueType> begin() const { return ConstVector<ValueType>::begin(); }
  68. Iterator<ValueType> begin();
  69. ConstIterator<ValueType> end() const { return ConstVector<ValueType>::end(); }
  70. Iterator<ValueType> end();
  71. void PushBack(const ValueType& x);
  72. // Element access
  73. const ValueType& operator[](Long j) const { return this->ConstVector<ValueType>::operator[](j); }
  74. ValueType& operator[](Long j);
  75. // Vector-Vector operations
  76. Vector& operator=(const std::vector<ValueType>& V);
  77. Vector& operator=(const Vector& V);
  78. Vector& operator+=(const Vector& V);
  79. Vector& operator-=(const Vector& V);
  80. Vector& operator*=(const Vector& V);
  81. Vector& operator/=(const Vector& V);
  82. // Vector-Scalar operations
  83. Vector& operator=(ValueType s);
  84. Vector& operator+=(ValueType s);
  85. Vector& operator-=(ValueType s);
  86. Vector& operator*=(ValueType s);
  87. Vector& operator/=(ValueType s);
  88. };
  89. template <class ValueType> ConstVector<ValueType> operator+(ValueType s, const ConstVector<ValueType>& V);
  90. template <class ValueType> ConstVector<ValueType> operator-(ValueType s, const ConstVector<ValueType>& V);
  91. template <class ValueType> ConstVector<ValueType> operator*(ValueType s, const ConstVector<ValueType>& V);
  92. template <class ValueType> ConstVector<ValueType> operator/(ValueType s, const ConstVector<ValueType>& V);
  93. template <class ValueType> std::ostream& operator<<(std::ostream& output, const ConstVector<ValueType>& V);
  94. } // end namespace
  95. #include SCTL_INCLUDE(vector.txx)
  96. #endif //_SCTL_VECTOR_HPP_