LMMS
Loading...
Searching...
No Matches
juce_FlagCache.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26#if ! DOXYGEN
27
28namespace juce
29{
30
31template <size_t requiredFlagBitsPerItem>
33{
35
36public:
37 FlagCache() = default;
38
39 explicit FlagCache (size_t items)
40 : flags (divCeil (items, groupsPerWord))
41 {
42 std::fill (flags.begin(), flags.end(), 0);
43 }
44
45 void set (size_t index, FlagType bits)
46 {
47 const auto flagIndex = index / groupsPerWord;
48 jassert (flagIndex < flags.size());
49 const auto groupIndex = index - (flagIndex * groupsPerWord);
50 flags[flagIndex].fetch_or (moveToGroupPosition (bits, groupIndex), std::memory_order_acq_rel);
51 }
52
53 /* Calls the supplied callback for any entries with non-zero flags, and
54 sets all flags to zero.
55 */
56 template <typename Callback>
57 void ifSet (Callback&& callback)
58 {
59 for (size_t flagIndex = 0; flagIndex < flags.size(); ++flagIndex)
60 {
61 const auto prevFlags = flags[flagIndex].exchange (0, std::memory_order_acq_rel);
62
63 for (size_t group = 0; group < groupsPerWord; ++group)
64 {
65 const auto masked = moveFromGroupPosition (prevFlags, group);
66
67 if (masked != 0)
68 callback ((flagIndex * groupsPerWord) + group, masked);
69 }
70 }
71 }
72
73 void clear()
74 {
75 std::fill (flags.begin(), flags.end(), 0);
76 }
77
78private:
79 /* Given the flags for a single item, and a group index, shifts the flags
80 so that they are positioned at the appropriate location for that group
81 index.
82
83 e.g. If the flag type is a uint32_t, and there are 2 flags per item,
84 then each uint32_t will hold flags for 16 items. The flags for item 0
85 are the least significant two bits; the flags for item 15 are the most
86 significant two bits.
87 */
88 static constexpr FlagType moveToGroupPosition (FlagType ungrouped, size_t groupIndex)
89 {
90 return (ungrouped & groupMask) << (groupIndex * bitsPerFlagGroup);
91 }
92
93 /* Given a set of grouped flags for multiple items, and a group index,
94 extracts the flags set for an item at that group index.
95
96 e.g. If the flag type is a uint32_t, and there are 2 flags per item,
97 then each uint32_t will hold flags for 16 items. Asking for groupIndex
98 0 will return the least significant two bits; asking for groupIndex 15
99 will return the most significant two bits.
100 */
101 static constexpr FlagType moveFromGroupPosition (FlagType grouped, size_t groupIndex)
102 {
103 return (grouped >> (groupIndex * bitsPerFlagGroup)) & groupMask;
104 }
105
106 static constexpr size_t findNextPowerOfTwoImpl (size_t n, size_t shift)
107 {
108 return shift == 32 ? n : findNextPowerOfTwoImpl (n | (n >> shift), shift * 2);
109 }
110
111 static constexpr size_t findNextPowerOfTwo (size_t value)
112 {
113 return findNextPowerOfTwoImpl (value - 1, 1) + 1;
114 }
115
116 static constexpr size_t divCeil (size_t a, size_t b)
117 {
118 return (a / b) + ((a % b) != 0);
119 }
120
121 static constexpr size_t bitsPerFlagGroup = findNextPowerOfTwo (requiredFlagBitsPerItem);
122 static constexpr size_t groupsPerWord = (8 * sizeof (FlagType)) / bitsPerFlagGroup;
123 static constexpr FlagType groupMask = ((FlagType) 1 << requiredFlagBitsPerItem) - 1;
124
125 std::vector<std::atomic<FlagType>> flags;
126};
127
128template <size_t requiredFlagBitsPerItem>
130{
131public:
132 FlaggedFloatCache() = default;
133
134 explicit FlaggedFloatCache (size_t sizeIn)
135 : values (sizeIn),
136 flags (sizeIn)
137 {
138 std::fill (values.begin(), values.end(), 0.0f);
139 }
140
141 size_t size() const noexcept { return values.size(); }
142
143 void setValue (size_t index, float value)
144 {
145 jassert (index < size());
146 values[index].store (value, std::memory_order_relaxed);
147 }
148
149 void setBits (size_t index, uint32_t bits) { flags.set (index, bits); }
150
151 void setValueAndBits (size_t index, float value, uint32_t bits)
152 {
153 setValue (index, value);
154 setBits (index, bits);
155 }
156
157 float get (size_t index) const noexcept
158 {
159 jassert (index < size());
160 return values[index].load (std::memory_order_relaxed);
161 }
162
163 /* Calls the supplied callback for any entries which have been modified
164 since the last call to this function.
165 */
166 template <typename Callback>
167 void ifSet (Callback&& callback)
168 {
169 flags.ifSet ([this, &callback] (size_t groupIndex, uint32_t bits)
170 {
171 callback (groupIndex, values[groupIndex].load (std::memory_order_relaxed), bits);
172 });
173 }
174
175private:
176 std::vector<std::atomic<float>> values;
178};
179
180} // namespace juce
181
182#endif
#define noexcept
Definition DistrhoDefines.h:72
uint8_t a
Definition Spc_Cpu.h:141
Definition juce_FlagCache.h:33
std::vector< std::atomic< FlagType > > flags
Definition juce_FlagCache.h:125
static constexpr size_t bitsPerFlagGroup
Definition juce_FlagCache.h:121
static constexpr FlagType groupMask
Definition juce_FlagCache.h:123
static constexpr size_t findNextPowerOfTwoImpl(size_t n, size_t shift)
Definition juce_FlagCache.h:106
static constexpr FlagType moveToGroupPosition(FlagType ungrouped, size_t groupIndex)
Definition juce_FlagCache.h:88
void set(size_t index, FlagType bits)
Definition juce_FlagCache.h:45
uint32_t FlagType
Definition juce_FlagCache.h:34
static constexpr size_t groupsPerWord
Definition juce_FlagCache.h:122
FlagCache(size_t items)
Definition juce_FlagCache.h:39
FlagCache()=default
void clear()
Definition juce_FlagCache.h:73
static constexpr size_t findNextPowerOfTwo(size_t value)
Definition juce_FlagCache.h:111
void ifSet(Callback &&callback)
Definition juce_FlagCache.h:57
static constexpr FlagType moveFromGroupPosition(FlagType grouped, size_t groupIndex)
Definition juce_FlagCache.h:101
static constexpr size_t divCeil(size_t a, size_t b)
Definition juce_FlagCache.h:116
void ifSet(Callback &&callback)
Definition juce_FlagCache.h:167
void setValue(size_t index, float value)
Definition juce_FlagCache.h:143
void setValueAndBits(size_t index, float value, uint32_t bits)
Definition juce_FlagCache.h:151
size_t size() const noexcept
Definition juce_FlagCache.h:141
void setBits(size_t index, uint32_t bits)
Definition juce_FlagCache.h:149
std::vector< std::atomic< float > > values
Definition juce_FlagCache.h:176
float get(size_t index) const noexcept
Definition juce_FlagCache.h:157
FlaggedFloatCache(size_t sizeIn)
Definition juce_FlagCache.h:134
FlagCache< requiredFlagBitsPerItem > flags
Definition juce_FlagCache.h:177
static PuglViewHint int value
Definition pugl.h:1708
#define jassert(expression)
unsigned int uint32_t
Definition mid.cpp:100
Definition carla_juce.cpp:31
@ group
Definition juce_AccessibilityRole.h:61
RECT const char void(* callback)(const char *droppath))) SWELL_API_DEFINE(BOOL
Definition swell-functions.h:1004
int n
Definition crypt.c:458
b
Definition crypt.c:628
ulg size
Definition extract.c:2350
#define const
Definition zconf.h:137