vector.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef _SCTL_VECTOR_HPP_
  2. #define _SCTL_VECTOR_HPP_
  3. #include SCTL_INCLUDE(common.hpp)
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <cstdint>
  7. namespace SCTL_NAMESPACE {
  8. template <class ValueType> class Vector {
  9. public:
  10. typedef ValueType value_type;
  11. typedef ValueType& reference;
  12. typedef const ValueType& const_reference;
  13. typedef Iterator<ValueType> iterator;
  14. typedef ConstIterator<ValueType> const_iterator;
  15. typedef Long difference_type;
  16. typedef Long size_type;
  17. Vector();
  18. Vector(Long dim_, Iterator<ValueType> data_ = nullptr, bool own_data_ = true);
  19. Vector(const Vector& V);
  20. Vector(const std::vector<ValueType>& V);
  21. ~Vector();
  22. void Swap(Vector<ValueType>& v1);
  23. void ReInit(Long dim_, Iterator<ValueType> data_ = nullptr, bool own_data_ = true);
  24. void Write(const char* fname) const;
  25. void Read(const char* fname);
  26. Long Dim() const;
  27. Long Capacity() const;
  28. void SetZero();
  29. Iterator<ValueType> begin();
  30. ConstIterator<ValueType> begin() const;
  31. Iterator<ValueType> end();
  32. ConstIterator<ValueType> end() const;
  33. void PushBack(const ValueType& x);
  34. // Element access
  35. ValueType& operator[](Long j);
  36. const ValueType& operator[](Long j) const;
  37. // Vector-Vector operations
  38. Vector& operator=(const std::vector<ValueType>& V);
  39. Vector& operator=(const Vector& V);
  40. Vector& operator+=(const Vector& V);
  41. Vector& operator-=(const Vector& V);
  42. Vector& operator*=(const Vector& V);
  43. Vector& operator/=(const Vector& V);
  44. Vector operator+(const Vector& V) const;
  45. Vector operator-(const Vector& V) const;
  46. Vector operator*(const Vector& V) const;
  47. Vector operator/(const Vector& V) const;
  48. // Vector-Scalar operations
  49. Vector& operator=(ValueType s);
  50. Vector& operator+=(ValueType s);
  51. Vector& operator-=(ValueType s);
  52. Vector& operator*=(ValueType s);
  53. Vector& operator/=(ValueType s);
  54. Vector operator+(ValueType s) const;
  55. Vector operator-(ValueType s) const;
  56. Vector operator*(ValueType s) const;
  57. Vector operator/(ValueType s) const;
  58. private:
  59. Long dim;
  60. Long capacity;
  61. Iterator<ValueType> data_ptr;
  62. bool own_data;
  63. };
  64. template <class ValueType> Vector<ValueType> operator+(ValueType s, const Vector<ValueType>& V);
  65. template <class ValueType> Vector<ValueType> operator-(ValueType s, const Vector<ValueType>& V);
  66. template <class ValueType> Vector<ValueType> operator*(ValueType s, const Vector<ValueType>& V);
  67. template <class ValueType> Vector<ValueType> operator/(ValueType s, const Vector<ValueType>& V);
  68. template <class ValueType> std::ostream& operator<<(std::ostream& output, const Vector<ValueType>& V);
  69. } // end namespace
  70. #include SCTL_INCLUDE(vector.txx)
  71. #endif //_SCTL_VECTOR_HPP_