vector.hpp 3.0 KB

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