1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 1997--2020 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef DRUL_ARRAY_HH
21 #define DRUL_ARRAY_HH
22 
23 #include "direction.hh"
24 #include "real.hh"
25 
26 /**
27    Left/right or Up/down arrays. Drul is nicer sounding than udlr
28 */
29 template<class T>
30 struct Drul_array
31 {
32   T array_[2];
atDrul_array33   T &at (Direction d)
34   {
35     assert (d == 1 || d == -1);
36     return array_[d > 0];
37   }
atDrul_array38   T const &at (Direction d) const
39   {
40     assert (d == 1 || d == -1);
41     return array_[d > 0];
42   }
operator []Drul_array43   T &operator [] (Direction d)
44   {
45     return at (d);
46   }
operator []Drul_array47   T const &operator [] (Direction d) const
48   {
49     return at (d);
50   }
Drul_arrayDrul_array51   Drul_array ()
52   {
53   }
Drul_arrayDrul_array54   Drul_array (T const &t1, T const &t2)
55   {
56     set (t1, t2);
57   }
setDrul_array58   void set (T const &t1, T const &t2)
59   {
60     array_[0] = t1;
61     array_[1] = t2;
62   }
deltaDrul_array63   T delta () const
64   {
65     return at (RIGHT) - at (LEFT);
66   }
67   Real linear_combination (Real x) const;
68 };
69 
70 template<class T>
71 void
scale_drul(Drul_array<T> * dr,T x)72 scale_drul (Drul_array<T> *dr, T x)
73 {
74   dr->at (LEFT) *= x;
75   dr->at (RIGHT) *= x;
76 }
77 
78 template <>
79 inline Real
linear_combination(Real x) const80 Drul_array<Real>::linear_combination (Real x) const
81 {
82   return ((1.0 - x) * at (LEFT)
83           + (x + 1.0) * at (RIGHT)) * 0.5;
84 }
85 
86 #endif /* DRUL_ARRAY_HH */
87