TinySet bugfix, add "append"

This commit is contained in:
Henrik Rydgård 2022-09-01 10:46:47 +02:00
parent a854fbbe76
commit bd71d8044b
2 changed files with 58 additions and 1 deletions

View file

@ -43,6 +43,20 @@ struct TinySet {
slowLookup_->push_back(t); slowLookup_->push_back(t);
return slowLookup_->back(); return slowLookup_->back();
} }
void append(const TinySet<T, MaxFastSize> &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 { bool contains(T t) const {
for (int i = 0; i < fastCount; i++) { for (int i = 0; i < fastCount; i++) {
if (fastLookup_[i] == t) if (fastLookup_[i] == t)
@ -79,7 +93,7 @@ struct TinySet {
return fastCount == 0; return fastCount == 0;
} }
size_t size() const { size_t size() const {
if (fastCount <= MaxFastSize) { if (!slowLookup_) {
return fastCount; return fastCount;
} else { } else {
return slowLookup_->size() + MaxFastSize; return slowLookup_->size() + MaxFastSize;
@ -92,6 +106,9 @@ struct TinySet {
return (*slowLookup_)[index - MaxFastSize]; return (*slowLookup_)[index - MaxFastSize];
} }
} }
const T &back() const {
return (*this)[size() - 1];
}
private: private:
void insertSlow(T t) { void insertSlow(T t) {

View file

@ -40,6 +40,7 @@
#include <jni.h> #include <jni.h>
#endif #endif
#include "Common/Data/Collections/TinySet.h"
#include "Common/Data/Text/Parsers.h" #include "Common/Data/Text/Parsers.h"
#include "Common/Data/Text/WrapText.h" #include "Common/Data/Text/WrapText.h"
#include "Common/Data/Encoding/Utf8.h" #include "Common/Data/Encoding/Utf8.h"
@ -308,6 +309,44 @@ bool TestParsers() {
return true; return true;
} }
bool TestTinySet() {
TinySet<int, 4> 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<int, 4> 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() { bool TestVFPUSinCos() {
float sine, cosine; float sine, cosine;
InitVFPUSinCos(); InitVFPUSinCos();
@ -792,6 +831,7 @@ TestItem availableTests[] = {
TEST_ITEM(AndroidContentURI), TEST_ITEM(AndroidContentURI),
TEST_ITEM(ThreadManager), TEST_ITEM(ThreadManager),
TEST_ITEM(WrapText), TEST_ITEM(WrapText),
TEST_ITEM(TinySet),
}; };
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {