]> git.uio.no Git - u/mrichter/AliRoot.git/blob - Vc/include/Vc/scalar/writemaskedvector.h
Vc package added (version 0.6.79-dev)
[u/mrichter/AliRoot.git] / Vc / include / Vc / scalar / writemaskedvector.h
1 /*  This file is part of the Vc library.
2
3     Copyright (C) 2009-2012 Matthias Kretz <kretz@kde.org>
4
5     Vc is free software: you can redistribute it and/or modify
6     it under the terms of the GNU Lesser General Public License as
7     published by the Free Software Foundation, either version 3 of
8     the License, or (at your option) any later version.
9
10     Vc is distributed in the hope that it will be useful, but
11     WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with Vc.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20 #ifndef VC_SCALAR_WRITEMASKEDVECTOR_H
21 #define VC_SCALAR_WRITEMASKEDVECTOR_H
22
23 namespace Vc
24 {
25 namespace Scalar
26 {
27
28 template<typename T> class WriteMaskedVector
29 {
30     friend class Vector<T>;
31     typedef typename Vector<T>::Mask Mask;
32     typedef typename Vector<T>::EntryType EntryType;
33     public:
34         //prefix
35         inline Vector<T> &operator++() { if (mask) ++vec->m_data; return *vec; }
36         inline Vector<T> &operator--() { if (mask) --vec->m_data; return *vec; }
37         //postfix
38         inline Vector<T> operator++(int) { if (mask) vec->m_data++; return *vec; }
39         inline Vector<T> operator--(int) { if (mask) vec->m_data--; return *vec; }
40
41         inline Vector<T> &operator+=(Vector<T> x) { if (mask) vec->m_data += x.m_data; return *vec; }
42         inline Vector<T> &operator-=(Vector<T> x) { if (mask) vec->m_data -= x.m_data; return *vec; }
43         inline Vector<T> &operator*=(Vector<T> x) { if (mask) vec->m_data *= x.m_data; return *vec; }
44         inline Vector<T> &operator/=(Vector<T> x) { if (mask) vec->m_data /= x.m_data; return *vec; }
45
46         inline Vector<T> &operator=(Vector<T> x) {
47             vec->assign(x, mask);
48             return *vec;
49         }
50
51         inline Vector<T> &operator+=(EntryType x) { if (mask) vec->m_data += x; return *vec; }
52         inline Vector<T> &operator-=(EntryType x) { if (mask) vec->m_data -= x; return *vec; }
53         inline Vector<T> &operator*=(EntryType x) { if (mask) vec->m_data *= x; return *vec; }
54         inline Vector<T> &operator/=(EntryType x) { if (mask) vec->m_data /= x; return *vec; }
55
56         inline Vector<T> &operator=(EntryType x) {
57             vec->assign(Vector<T>(x), mask);
58             return *vec;
59         }
60
61         template<typename F> inline void call(const F &f) const {
62             vec->call(f, mask);
63         }
64         template<typename F> inline void call(F &f) const {
65             vec->call(f, mask);
66         }
67         template<typename F> inline Vector<T> apply(const F &f) const {
68             if (mask) {
69                 return Vector<T>(f(vec->m_data));
70             } else {
71                 return *vec;
72             }
73         }
74         template<typename F> inline Vector<T> apply(F &f) const {
75             if (mask) {
76                 return Vector<T>(f(vec->m_data));
77             } else {
78                 return *vec;
79             }
80         }
81     private:
82         WriteMaskedVector(Vector<T> *v, Mask k) : vec(v), mask(k) {}
83         Vector<T> *const vec;
84         Mask mask;
85 };
86
87 } // namespace Scalar
88 } // namespace Vc
89 #endif // VC_SCALAR_WRITEMASKEDVECTOR_H