1 /* B.Shapr
2  * Beat / envelope shaper LV2 plugin
3  *
4  * Copyright (C) 2019 by Sven Jähnichen
5  *
6  * This program 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, or (at your option)
9  * any later version.
10  *
11  * This program 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 this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef SNAPSHOTS_HPP_
22 #define SNAPSHOTS_HPP_
23 
24 #include <cstddef>
25 #include <array>
26 #include <iostream>
27 
28 template <class T, size_t sz>
29 class Snapshots
30 {
31 protected:
32         std::array<T, sz> store;
33         size_t pos = 0;
34         size_t horizon = 0;
35         size_t size = 0;
36 
37 public:
clear()38         void clear ()
39         {
40                 store.fill (T());
41                 pos = 0;
42                 horizon = 0;
43                 size = 0;
44         }
push(T & value)45         void push (T& value)
46         {
47                 horizon = ((pos + 1) % sz);
48                 store[horizon] = value;
49                 pos = horizon;
50                 size = (size < sz ? size + 1 : sz);
51         }
52 
undo()53         T undo ()
54         {
55                 pos = (size == 0 ? 0 : (((size < sz) && (pos == 0)) ? 0 : ((horizon == ((pos + sz - 1) % sz)) ? pos : ((pos + sz - 1) % sz))));
56                 return store[pos];
57         }
58 
redo()59         T redo ()
60         {
61                 pos = (size == 0 ? 0 : ((horizon == pos) ? pos : ((pos + 1) % sz)));
62                 return store[pos];
63         }
64 };
65 
66 
67 #endif /* SNAPSHOTS_HPP_ */
68