src/ring.h

Tue, 28 Sep 2021 23:07:23 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 28 Sep 2021 23:07:23 +0300
changeset 145
4dea24d3eda0
parent 115
ed884a2fb009
permissions
-rw-r--r--

Use QSaveFile to save the file more safely

19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 namespace iter
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 namespace _imp
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
25 template<typename T, typename Tint>
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 class RingAdapter;
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 template<typename T>
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
30 _imp::RingAdapter<T, int> ring(T&& collection);
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
32 template<typename T, typename Tint>
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
33 _imp::RingAdapter<T, Tint> ring(T&& collection, Tint count);
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 /*
115
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
37 * Implements a ring adapter over T. This class corrects indices given to the element-operator so that they're within
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
38 * bounds. The maximum amount can be specified manually.
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 * Example:
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 * int A[] = {10,20,30,40};
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 * ring(A)[0] == A[0 % 4] == A[0]
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 * ring(A)[5] == A[5 % 4] == A[1]
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 * ring(A)[-1] == ring(A)[-1 + 4] == A[3]
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 */
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
47 template<typename T, typename Tint>
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 class iter::_imp::RingAdapter
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 private:
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 // The private section must come first because _collection is used in decltype() below.
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
52 T&& collection;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
53 const Tint count;
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 public:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
56 RingAdapter(T&& collection, Tint count) :
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 collection {collection},
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 count {count} {}
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
60 decltype(collection[Tint{}]) operator[](Tint index)
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 if (count == 0)
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 // Argh! ...let the collection deal with this case.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 return this->collection[index];
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 else
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 index %= this->count;
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 // Fix negative modulus...
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 if (index < 0)
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 index += this->count;
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 return this->collection[index];
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
79 Tint size() const
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 return this->count;
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 };
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 /*
115
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
86 * Convenience function for RingAdapter so that the template parameter does not have to be provided. The ring
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
87 * amount is assumed to be the amount of elements in the collection.
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 */
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 template<typename T>
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
90 iter::_imp::RingAdapter<T, int> iter::ring(T&& collection)
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 return {collection, countof(collection)};
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 /*
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 * Version of ring() that allows manual specification of the count.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 */
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
98 template<typename T, typename Tint>
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
99 iter::_imp::RingAdapter<T, Tint> iter::ring(T&& collection, Tint count)
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 return {collection, count};
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
104 template<typename T, typename Tint>
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
105 int countof(const iter::_imp::RingAdapter<T, Tint>& ring)
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 return ring.size();
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 }

mercurial