diff --git a/Common/Data/Collections/TinySet.h b/Common/Data/Collections/TinySet.h index 219a26d3cd..5c8c8e78e6 100644 --- a/Common/Data/Collections/TinySet.h +++ b/Common/Data/Collections/TinySet.h @@ -43,6 +43,20 @@ struct TinySet { slowLookup_->push_back(t); return slowLookup_->back(); } + void append(const TinySet &other) { + size_t otherSize = other.size(); + if (size() + otherSize <= MaxFastSize) { + // Fast case + for (int i = 0; i < otherSize; i++) { + fastLookup_[fastCount + i] = other.fastLookup_[i]; + } + fastCount += other.fastCount; + } else { + for (int i = 0; i < otherSize; i++) { + push_back(other[i]); + } + } + } bool contains(T t) const { for (int i = 0; i < fastCount; i++) { if (fastLookup_[i] == t) @@ -79,7 +93,7 @@ struct TinySet { return fastCount == 0; } size_t size() const { - if (fastCount <= MaxFastSize) { + if (!slowLookup_) { return fastCount; } else { return slowLookup_->size() + MaxFastSize; @@ -92,6 +106,9 @@ struct TinySet { return (*slowLookup_)[index - MaxFastSize]; } } + const T &back() const { + return (*this)[size() - 1]; + } private: void insertSlow(T t) { diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index 78a08515d6..4f8239a11b 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -40,6 +40,7 @@ #include #endif +#include "Common/Data/Collections/TinySet.h" #include "Common/Data/Text/Parsers.h" #include "Common/Data/Text/WrapText.h" #include "Common/Data/Encoding/Utf8.h" @@ -308,6 +309,44 @@ bool TestParsers() { return true; } +bool TestTinySet() { + TinySet a; + EXPECT_EQ_INT((int)a.size(), 0); + a.push_back(1); + EXPECT_EQ_INT((int)a.size(), 1); + a.push_back(2); + EXPECT_EQ_INT((int)a.size(), 2); + TinySet b; + b.push_back(8); + b.push_back(9); + b.push_back(10); + EXPECT_EQ_INT((int)b.size(), 3); + + a.append(b); + EXPECT_EQ_INT((int)a.size(), 5); + EXPECT_EQ_INT((int)b.size(), 3); + + b.append(b); + EXPECT_EQ_INT((int)b.size(), 6); + + EXPECT_EQ_INT(a[0], 1); + EXPECT_EQ_INT(a[1], 2); + EXPECT_EQ_INT(a[2], 8); + EXPECT_EQ_INT(a[3], 9); + EXPECT_EQ_INT(a[4], 10); + a.append(a); + EXPECT_EQ_INT(a.size(), 10); + EXPECT_EQ_INT(a[9], 10); + + b.push_back(11); + EXPECT_EQ_INT((int)b.size(), 7); + b.push_back(12); + EXPECT_EQ_INT((int)b.size(), 8); + b.push_back(13); + EXPECT_EQ_INT(b.size(), 9); + return true; +} + bool TestVFPUSinCos() { float sine, cosine; InitVFPUSinCos(); @@ -792,6 +831,7 @@ TestItem availableTests[] = { TEST_ITEM(AndroidContentURI), TEST_ITEM(ThreadManager), TEST_ITEM(WrapText), + TEST_ITEM(TinySet), }; int main(int argc, const char *argv[]) {