BLI: make Map::Item and Map::MutableItem more accessible
This makes it easier to write range-for loops over all items in the map without using auto.
This commit is contained in:
@@ -745,6 +745,21 @@ class Map {
|
||||
}
|
||||
};
|
||||
|
||||
struct Item {
|
||||
const Key &key;
|
||||
const Value &value;
|
||||
};
|
||||
|
||||
struct MutableItem {
|
||||
const Key &key;
|
||||
Value &value;
|
||||
|
||||
operator Item() const
|
||||
{
|
||||
return Item{key, value};
|
||||
}
|
||||
};
|
||||
|
||||
class ItemIterator final : public BaseIterator<ItemIterator> {
|
||||
public:
|
||||
ItemIterator(const Slot *slots, uint32_t total_slots, uint32_t current_slot)
|
||||
@@ -752,11 +767,6 @@ class Map {
|
||||
{
|
||||
}
|
||||
|
||||
struct Item {
|
||||
const Key &key;
|
||||
const Value &value;
|
||||
};
|
||||
|
||||
Item operator*() const
|
||||
{
|
||||
const Slot &slot = this->current_slot();
|
||||
@@ -771,12 +781,7 @@ class Map {
|
||||
{
|
||||
}
|
||||
|
||||
struct Item {
|
||||
const Key &key;
|
||||
Value &value;
|
||||
};
|
||||
|
||||
Item operator*() const
|
||||
MutableItem operator*() const
|
||||
{
|
||||
Slot &slot = this->current_slot();
|
||||
return {*slot.key(), *slot.value()};
|
||||
|
||||
@@ -185,7 +185,8 @@ TEST(map, ItemIterator)
|
||||
blender::Set<float> values;
|
||||
|
||||
uint iterations = 0;
|
||||
for (auto item : map.items()) {
|
||||
const Map<int, float> &const_map = map;
|
||||
for (auto item : const_map.items()) {
|
||||
keys.add(item.key);
|
||||
values.add(item.value);
|
||||
iterations++;
|
||||
@@ -228,6 +229,26 @@ TEST(map, MutableItemIterator)
|
||||
EXPECT_EQ(map.lookup(2), 3.0f);
|
||||
}
|
||||
|
||||
TEST(map, MutableItemToItemConversion)
|
||||
{
|
||||
Map<int, int> map;
|
||||
map.add(3, 6);
|
||||
map.add(2, 1);
|
||||
|
||||
Vector<int> keys, values;
|
||||
for (Map<int, int>::Item item : map.items()) {
|
||||
keys.append(item.key);
|
||||
values.append(item.value);
|
||||
}
|
||||
|
||||
EXPECT_EQ(keys.size(), 2);
|
||||
EXPECT_EQ(values.size(), 2);
|
||||
EXPECT_TRUE(keys.contains(3));
|
||||
EXPECT_TRUE(keys.contains(2));
|
||||
EXPECT_TRUE(values.contains(6));
|
||||
EXPECT_TRUE(values.contains(1));
|
||||
}
|
||||
|
||||
static float return_42()
|
||||
{
|
||||
return 42.0f;
|
||||
|
||||
Reference in New Issue
Block a user