Skip to content

Commit aa9dbef

Browse files
committed
Add sharing_mapt::iterate
This gives a simple const iterator without copying the whole dataset.
1 parent 4b8e927 commit aa9dbef

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/util/sharing_map.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,18 @@ class sharing_mapt
444444
delta_viewt &delta_view,
445445
const bool only_common = true) const;
446446

447+
/// Call a function for every key-value pair in the map.
448+
///
449+
/// Complexity: as \ref sharing_mapt::get_view
450+
void
451+
iterate(std::function<void(const key_type &k, const mapped_type &m)> f) const
452+
{
453+
if(empty())
454+
return;
455+
456+
iterate(map, 0, f);
457+
}
458+
447459
#if !defined(_MSC_VER)
448460
/// Stats about sharing between several sharing map instances. An instance of
449461
/// this class is returned by the get_sharing_map_stats_* functions.

unit/util/sharing_map.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ TEST_CASE("Sharing map collisions", "[core][util]")
297297
REQUIRE(!sm.has_key(some_keyt(8)));
298298
}
299299

300-
TEST_CASE("Sharing map views", "[core][util]")
300+
TEST_CASE("Sharing map views and iteration", "[core][util]")
301301
{
302302
SECTION("View of empty map")
303303
{
@@ -349,6 +349,25 @@ TEST_CASE("Sharing map views", "[core][util]")
349349
REQUIRE((pairs[3] == pt("l", "3")));
350350
}
351351

352+
SECTION("Iterate")
353+
{
354+
sharing_map_standardt sm;
355+
fill(sm);
356+
357+
typedef std::pair<dstringt, std::string> pt;
358+
std::vector<pt> pairs;
359+
360+
sm.iterate([&pairs](const irep_idt &key, const std::string &value) {
361+
pairs.push_back({key, value});
362+
});
363+
364+
std::sort(pairs.begin(), pairs.end());
365+
REQUIRE(pairs.size() == 3);
366+
REQUIRE((pairs[0] == pt("i", "0")));
367+
REQUIRE((pairs[1] == pt("j", "1")));
368+
REQUIRE((pairs[2] == pt("k", "2")));
369+
}
370+
352371
SECTION("Delta view (no sharing, same keys)")
353372
{
354373
sharing_map_standardt sm1;

0 commit comments

Comments
 (0)