BlosSOM
Interactive dimensionality reduction on large datasets (EmbedSOM and FLOWER combined)
dirty.h
Go to the documentation of this file.
1/* This file is part of BlosSOM.
2 *
3 * Copyright (C) 2021 Mirek Kratochvil
4 *
5 * BlosSOM is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free
7 * Software Foundation, either version 3 of the License, or (at your option)
8 * any later version.
9 *
10 * BlosSOM is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * BlosSOM. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#ifndef DIRTY_H
20#define DIRTY_H
21
22#include <cstddef>
23#include <tuple>
24
25/** A piece of dirt for dirtying the caches.
26 *
27 * This is supposed to be a part of classes that are cached elsewhere; having
28 * the `dirty` counter allows us to reliably observe if someting changed and
29 * run the updates accordingly. Use with `Cleaner` to observe the changes.
30 */
31struct Dirt
32{
33 int dirt;
34
36 : dirt(0)
37 {
38 }
39 /** Make the cache dirty
40 *
41 * Call this if something changed and the caches need to be refreshed
42 */
43 void touch() { ++dirt; }
44};
45
46/** A piece of cache that keeps track of the dirty status.
47 *
48 * This is a natural counterpart of the `Dirt` class.
49 */
50struct Cleaner
51{
53
55 : cleaned(-1)
56 {
57 }
58
59 /** Returns true if the cache needs to be refreshed */
60 bool dirty(const Dirt &d)
61 {
62 // handle overflows!
63 return d.dirt - cleaned > 0;
64 }
65
66 /** Call this when the cache is refreshed. */
67 void clean(const Dirt &d)
68 {
69 if (cleaned < d.dirt)
70 cleaned = d.dirt;
71 }
72};
73
74/** Multi-piece cache-dirtying object.
75 *
76 * This is to be inherited into data objects that are cached elsewhere, but
77 * have more parts that may be transformed independently. Use with `Sweeper`.
78 *
79 * User structures are responsible for filling in `n` correctly.
80 */
81struct Dirts : public Dirt
82{
83 size_t n; /// Number of objects that should be cached.
84 Dirts(size_t n = 0)
85 : n(n)
86 {
87 }
88};
89
90/** A piece of multi-object cache.
91 *
92 * This is the counterpart of `Dirts`.
93 */
94struct Sweeper : public Cleaner
95{
96 size_t begin, dirts;
97
99 : begin(0)
100 , dirts(0)
101 {
102 }
103
104 /** Force-refresh the whole range */
105 void refresh(const Dirts &d) { dirts = d.n; }
106 void refresh(size_t n_dirts) { dirts = n_dirts; }
107
108 /** Find the range to refresh
109 *
110 * Return the beginning index and size of the range that needs to be
111 * refreshed. Note that the range is cyclic!
112 */
113 std::tuple<size_t, size_t> dirty_range(const Dirts &d)
114 {
115 if (dirty(d)) {
116 refresh(d);
117 clean(d);
118 }
119 if (begin >= d.n)
120 begin = 0;
121 return { begin, dirts };
122 }
123
124 /** Clean a range of the cache
125 *
126 * Call this to tell the Sweeper that you refresh `n` cache elements,
127 * starging at the beginning index returned from dirty_range().
128 * The indexing is cyclic modulo `d.n`.
129 */
130 void clean_range(const Dirts &d, size_t n)
131 {
132 if (n > dirts)
133 n = dirts;
134 dirts -= n;
135 if (!dirts)
136 begin = 0;
137 else {
138 begin += n;
139 if (begin >= d.n)
140 begin -= d.n;
141 }
142 }
143};
144
145#endif
A piece of cache that keeps track of the dirty status.
Definition: dirty.h:51
void clean(const Dirt &d)
Call this when the cache is refreshed.
Definition: dirty.h:67
bool dirty(const Dirt &d)
Returns true if the cache needs to be refreshed.
Definition: dirty.h:60
int cleaned
Definition: dirty.h:52
Cleaner()
Definition: dirty.h:54
A piece of dirt for dirtying the caches.
Definition: dirty.h:32
int dirt
Definition: dirty.h:33
void touch()
Make the cache dirty.
Definition: dirty.h:43
Dirt()
Definition: dirty.h:35
Multi-piece cache-dirtying object.
Definition: dirty.h:82
Dirts(size_t n=0)
Number of objects that should be cached.
Definition: dirty.h:84
size_t n
Definition: dirty.h:83
A piece of multi-object cache.
Definition: dirty.h:95
size_t dirts
Definition: dirty.h:96
std::tuple< size_t, size_t > dirty_range(const Dirts &d)
Find the range to refresh.
Definition: dirty.h:113
void refresh(size_t n_dirts)
Definition: dirty.h:106
Sweeper()
Definition: dirty.h:98
size_t begin
Definition: dirty.h:96
void clean_range(const Dirts &d, size_t n)
Clean a range of the cache.
Definition: dirty.h:130
void refresh(const Dirts &d)
Force-refresh the whole range.
Definition: dirty.h:105