File tree 2 files changed +32
-1
lines changed
2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -444,6 +444,18 @@ class sharing_mapt
444
444
delta_viewt &delta_view,
445
445
const bool only_common = true ) const ;
446
446
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
+
447
459
#if !defined(_MSC_VER)
448
460
// / Stats about sharing between several sharing map instances. An instance of
449
461
// / this class is returned by the get_sharing_map_stats_* functions.
Original file line number Diff line number Diff line change @@ -297,7 +297,7 @@ TEST_CASE("Sharing map collisions", "[core][util]")
297
297
REQUIRE (!sm.has_key (some_keyt (8 )));
298
298
}
299
299
300
- TEST_CASE (" Sharing map views" , " [core][util]" )
300
+ TEST_CASE (" Sharing map views and iteration " , " [core][util]" )
301
301
{
302
302
SECTION (" View of empty map" )
303
303
{
@@ -349,6 +349,25 @@ TEST_CASE("Sharing map views", "[core][util]")
349
349
REQUIRE ((pairs[3 ] == pt (" l" , " 3" )));
350
350
}
351
351
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
+
352
371
SECTION (" Delta view (no sharing, same keys)" )
353
372
{
354
373
sharing_map_standardt sm1;
You can’t perform that action at this time.
0 commit comments