vector.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef _SCTL_VECTOR_HPP_
  2. #define _SCTL_VECTOR_HPP_
  3. #include <sctl/common.hpp>
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <cstdint>
  7. #include <initializer_list>
  8. namespace SCTL_NAMESPACE {
  9. template <class ValueType> Iterator<ValueType> NullIterator();
  10. template <class ValueType> class Vector {
  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. Vector();
  20. explicit Vector(Long dim_, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
  21. Vector(const Vector& V);
  22. explicit Vector(const std::vector<ValueType>& V);
  23. explicit Vector(std::initializer_list<ValueType> V);
  24. ~Vector();
  25. void Swap(Vector<ValueType>& v1);
  26. void ReInit(Long dim_, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
  27. void Write(const char* fname) const;
  28. void Read(const char* fname);
  29. Long Dim() const;
  30. Long Capacity() const;
  31. void SetZero();
  32. Iterator<ValueType> begin();
  33. ConstIterator<ValueType> begin() const;
  34. Iterator<ValueType> end();
  35. ConstIterator<ValueType> end() const;
  36. void PushBack(const ValueType& x);
  37. // Element access
  38. ValueType& operator[](Long j);
  39. const ValueType& operator[](Long j) const;
  40. // Vector-Vector operations
  41. Vector& operator=(const std::vector<ValueType>& 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);
  46. Vector& operator/=(const Vector& V);
  47. Vector operator+(const Vector& V) const;
  48. Vector operator-(const Vector& V) const;
  49. Vector operator*(const Vector& V) const;
  50. Vector operator/(const Vector& V) const;
  51. Vector operator-() const ;
  52. // Vector-Scalar operations
  53. template <class VType> Vector& operator=(VType s);
  54. template <class VType> Vector& operator+=(VType s);
  55. template <class VType> Vector& operator-=(VType s);
  56. template <class VType> Vector& operator*=(VType s);
  57. template <class VType> Vector& operator/=(VType s);
  58. template <class VType> Vector operator+(VType s) const;
  59. template <class VType> Vector operator-(VType s) const;
  60. template <class VType> Vector operator*(VType s) const;
  61. template <class VType> Vector operator/(VType s) const;
  62. private:
  63. void Init(Long dim_, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
  64. Long dim;
  65. Long capacity;
  66. Iterator<ValueType> data_ptr;
  67. bool own_data;
  68. };
  69. template <class VType, class ValueType> Vector<ValueType> operator+(VType s, const Vector<ValueType>& V);
  70. template <class VType, class ValueType> Vector<ValueType> operator-(VType s, const Vector<ValueType>& V);
  71. template <class VType, class ValueType> Vector<ValueType> operator*(VType s, const Vector<ValueType>& V);
  72. template <class VType, class ValueType> Vector<ValueType> operator/(VType s, const Vector<ValueType>& V);
  73. template <class ValueType> std::ostream& operator<<(std::ostream& output, const Vector<ValueType>& V);
  74. } // end namespace
  75. #include SCTL_INCLUDE(vector.txx)
  76. #endif //_SCTL_VECTOR_HPP_