summaryrefslogtreecommitdiffstats
path: root/engine/include/symbasic/vector.hpp
blob: f8574dc0a61d511f7a7c20f969e1c311896f379b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef __VECTOR_H__
#define __VECTOR_H__ 

#ifdef _USE_VECTOR_OSTREAM
#include <iosfwd>
#endif

#include <cstddef>

template <class T>
class Vector {

public:

    typedef std::size_t size_t;
    
    class iterator
    {
        size_t i;
        Vector<T> *v;

    public:

        iterator(Vector<T> *v, size_t i = 0)
            : v(v), i(i)
        {
        }

        iterator& operator++()
        {
            ++i;    
        }

        iterator& operator--()
        {
            --i;
        }

        bool operator!=(const iterator& it)
        {
            return this->i != it.i;
        }

        bool operator==(const iterator& it)
        {
            return !(*this != it);
        }

        T& operator*()
        {
            return (*v)[i];
        }
    };

    typedef const iterator const_iterator;

    /* For auto implementation */
    /* it allows to loop through components
     *
     * for (auto comp : v)
     * {
     *      // loop content
 * }
     */

    iterator begin();
    iterator end();

    const_iterator begin() const;
    const_iterator end() const;
	
	//Virtual accesseurs

	virtual const size_t size() const = 0;

	/* Accède à la i-ème valeur */
	virtual T& operator[](size_t i) = 0;
	virtual const T& operator[](size_t i) const = 0;

	/* Return the module */
    double module() const;
    double sq_module() const; // squared module
    
    //Operateurs
    
	/* Operateurs de comparaison */
	bool operator==(const Vector<T>&) const;
    bool operator!=(const Vector<T>&) const;

    bool operator>=(const Vector<T>&) const;
    bool operator<=(const Vector<T>&) const;

    bool operator>=(double) const;
    bool operator<=(double) const;

    bool operator>(const Vector<T>&) const;
    bool operator<(const Vector<T>&) const;

    bool operator>(double) const;
    bool operator<(double) const;

    /* Dot product */
    double operator*(const Vector<T>&) const;

    /* For printing a Vector on a ostream */
#ifdef _USE_VECTOR_OSTREAM
    void affiche(std::ostream&) const;
#endif

protected: // not stable operations

    /* Plus / minus */
    Vector<T>& add(const Vector<T>&);
    Vector<T>& sub(const Vector<T>&);

    /* Scalar multiplication / division */
    Vector<T>& mult(double k);
    Vector<T>& divide(double k);

    Vector<T>& reverse();
    Vector<T>& _unit() const;
};

#ifdef _USE_VECTOR_OSTREAM
// forward declaration of ostream

template<class T>
std::ostream& operator<<(std::ostream&, const Vector<T>&);
#endif

#include "vector.tpp"

#endif