Skip to content

Commit

Permalink
Add SparseByteSet::size() method
Browse files Browse the repository at this point in the history
Summary:
Useful, when `SparseByteSet` used as fast pre check for heavyweight
operation.

Reviewed By: marksantaniello

Differential Revision: D60285173

fbshipit-source-id: 8deac984b0f00096448af51fdfee283d0bb13ca2
  • Loading branch information
ilvokhin authored and facebook-github-bot committed Jul 29, 2024
1 parent a11e16e commit 99a4f5a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions folly/container/SparseByteSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ class SparseByteSet {
*/
inline void clear() { size_ = 0; }

/***
* size()
*
* O(1), non-amortized.
*/
inline uint16_t size() { return size_; }

private:
uint16_t size_; // can't use uint8_t because it would overflow if all
// possible values were inserted.
Expand Down
12 changes: 12 additions & 0 deletions folly/container/test/SparseByteSetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,15 @@ TEST_F(SparseByteSetTest, remove_nop) {
bool r = s.remove(12);
EXPECT_FALSE(r);
}

TEST_F(SparseByteSetTest, size) {
EXPECT_EQ(s.size(), 0);

s.add(1);
s.add(2);
s.add(3);
EXPECT_EQ(s.size(), 3);

s.remove(1);
EXPECT_EQ(s.size(), 2);
}

0 comments on commit 99a4f5a

Please sign in to comment.