Skip to content

Commit 4c15daa

Browse files
committed
feat: add fill method
1 parent 8b36ff2 commit 4c15daa

File tree

2 files changed

+40
-42
lines changed

2 files changed

+40
-42
lines changed

src/core/intrusive_string_set.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,12 @@ class IntrusiveStringSet {
175175

176176
// TODO: Consider using chunks for this as string set
177177
void Fill(IntrusiveStringSet* other) {
178-
// DCHECK(other->entries_.empty());
179-
// other->Reserve(UpperBoundSize());
180-
// other->set_time(time_now());
181-
// for (uint32_t bucket_id = 0; bucket_id < entries_.size(); ++bucket_id) {
182-
// for (auto it = entries_[bucket_id].begin(); it != entries_[bucket_id].end(); ++it) {
183-
// other->Add(it->Key(), true, it.HasExpiry() ? it->ExpiryTime() : UINT32_MAX);
184-
// }
185-
// }
178+
DCHECK(other->entries_.empty());
179+
other->Reserve(UpperBoundSize());
180+
other->set_time(time_now());
181+
for (auto it = begin(), it_end = end(); it != it_end; ++it) {
182+
other->Add(it->Key(), it.HasExpiry() ? it.ExpiryTime() - time_now() : UINT32_MAX);
183+
}
186184
}
187185

188186
/**

src/core/intrusive_string_set_test.cc

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -568,44 +568,44 @@ TEST_F(IntrusiveStringSetTest, Ttl) {
568568
}
569569
}
570570

571-
// TEST_F(IntrusiveStringSetTest, Grow) {
572-
// for (size_t j = 0; j < 10; ++j) {
573-
// for (size_t i = 0; i < 4098; ++i) {
574-
// ss_->Reserve(generator_() % 256);
575-
// auto str = random_string(generator_, 3);
576-
// ss_->Add(str);
577-
// }
578-
// ss_->Clear();
579-
// }
580-
// }
571+
TEST_F(IntrusiveStringSetTest, Grow) {
572+
for (size_t j = 0; j < 10; ++j) {
573+
for (size_t i = 0; i < 4098; ++i) {
574+
ss_->Reserve(generator_() % 256);
575+
auto str = random_string(generator_, 3);
576+
ss_->Add(str);
577+
}
578+
ss_->Clear();
579+
}
580+
}
581581

582-
// TEST_F(IntrusiveStringSetTest, Reserve) {
583-
// vector<string> strs;
582+
TEST_F(IntrusiveStringSetTest, Reserve) {
583+
vector<string> strs;
584584

585-
// for (size_t i = 0; i < 10; ++i) {
586-
// strs.push_back(random_string(generator_, 10));
587-
// ss_->Add(strs.back());
588-
// }
585+
for (size_t i = 0; i < 10; ++i) {
586+
strs.push_back(random_string(generator_, 10));
587+
ss_->Add(strs.back());
588+
}
589589

590-
// for (size_t j = 2; j < 20; j += 3) {
591-
// ss_->Reserve(j * 20);
592-
// for (size_t i = 0; i < 10; ++i) {
593-
// ASSERT_TRUE(ss_->Contains(strs[i]));
594-
// }
595-
// }
596-
// }
590+
for (size_t j = 2; j < 20; j += 3) {
591+
ss_->Reserve(j * 20);
592+
for (size_t i = 0; i < 10; ++i) {
593+
ASSERT_TRUE(ss_->Contains(strs[i]));
594+
}
595+
}
596+
}
597597

598-
// TEST_F(IntrusiveStringSetTest, Fill) {
599-
// for (size_t i = 0; i < 100; ++i) {
600-
// ss_->Add(random_string(generator_, 10));
601-
// }
602-
// IntrusiveStringSet s2;
603-
// ss_->Fill(&s2);
604-
// EXPECT_EQ(s2.UpperBoundSize(), ss_->UpperBoundSize());
605-
// for (const auto& s : *ss_) {
606-
// EXPECT_TRUE(s2.Contains(s.Key()));
607-
// }
608-
// }
598+
TEST_F(IntrusiveStringSetTest, Fill) {
599+
for (size_t i = 0; i < 100; ++i) {
600+
ss_->Add(random_string(generator_, 10));
601+
}
602+
IntrusiveStringSet s2;
603+
ss_->Fill(&s2);
604+
EXPECT_EQ(s2.UpperBoundSize(), ss_->UpperBoundSize());
605+
for (const auto& s : *ss_) {
606+
EXPECT_TRUE(s2.Contains(s.Key()));
607+
}
608+
}
609609

610610
// TEST_F(IntrusiveStringSetTest, IterateEmpty) {
611611
// for (const auto& s : *ss_) {

0 commit comments

Comments
 (0)