src/generics/ring.h

Sat, 24 Mar 2018 15:54:41 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 24 Mar 2018 15:54:41 +0200
changeset 1335
5530161fcd47
parent 1326
69a90bd2dba2
permissions
-rw-r--r--

MainWindow now stores its state in the config file so its state and geometry is preserved across instances

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

mercurial