Debian Codegraph @

dwsys/DataStore.h

raw symbols SCIP
1 #ifndef _DataStore_h_
2 #define _DataStore_h_
3 /* DataStore.h
4 *
5 * Copyright (C) 2023 David Weenink
6 *
7 * This code is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at
10 * your option) any later version.
11 *
12 * This code is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this work. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "Data.h"
22 #include "Collection.h"
23 #include "Simple.h"
24
25 Thing_define (StoredData, Daata) {
26 autoDaata data;
27 autoSimpleString description;
28 };
29
30
31 autoStoredData StoredData_create (Daata data, conststring32 description);
32
33 Collection_define (StoredDataList, OrderedOf, StoredData) {
34 };
35
36 /*
37 Active data structure. 'current' is position of the cursor in the history list
38 Queries and insertions are at the current position.
39 Inserting new data at current position invalidates data at current+1, current+2, ...
40 Invariants:
41 0 <= current <= size + 1;
42 */
43
44 class autoDataStore {
45
46 autoStoredDataList storedData;
47 integer current;
48
49 public:
50
51 autoDataStore () {
52 storedData = StoredDataList_create ();
53 current = 0;
54 }
55
56 void forth () {
57 if (current <= storedData -> size)
58 current ++;
59 }
60 /*
61 Precondition: ! offright
62 Postcondition: my current++;
63 */
64
65 void back () {
66 if (current > 0)
67 current --;
68 }
69 /*
70 Precondition: ! offleft
71 Postcondition: my current--;
72 */
73
74 StoredData getData () {
75 Melder_assert (current > 0 && current <= storedData -> size);
76 return storedData -> at [current];
77 }
78 /*
79 return StoredData at my item [my current];
80 */
81
82 void insertData_move (Daata data, conststring32 description) {
83 for (integer i = storedData -> size; i >= current + 1; i --)
84 storedData -> removeItem (i);
85 autoStoredData thee = StoredData_create (data, description);
86 storedData -> addItem_move (thee.move());
87 current = storedData -> size;
88 }
89 /*
90 1. forget about item [ current+1..size ]
91 2. insert item after current.
92 3. new_size = current+1
93 */
94
95 bool isEmpty () {
96 return storedData -> size == 0;
97 }
98
99 bool isOffleft () {
100 return current == 0;
101 }
102
103 bool isOffright (){
104 return storedData -> size == 0 || current == storedData -> size + 1;
105 }
106
107 autostring32 getDataDescription (integer offsetFromCurrent) {
108 const integer pos = current + offsetFromCurrent;
109 autostring32 result;
110 if (pos >= 1 && pos <= storedData -> size) {
111 StoredData data = storedData -> at [pos];
112 SimpleString ss = data -> description.get();
113 result = Melder_dup (ss ->string.get());
114 }
115 return result;
116 }
117 /*
118 offsetFromCurrent may be zero, positive or negative.
119 References outside the list will return nullptr.
120 */
121 };
122
123 #endif /* _DataStore_h_ */