Kaydet (Commit) 9782ba7f authored tarafından Tobias Lippert's avatar Tobias Lippert Kaydeden (comit) Caolán McNamara

fdo#76754 Add return first to IndexedStyleSheets to speed up ods writing

Change-Id: I6fc9fe8ce78ad9cc1a7c2fe3e13ed38ce468ce6c
Reviewed-on: https://gerrit.libreoffice.org/10347Reviewed-by: 's avatarCaolán McNamara <caolanm@redhat.com>
Tested-by: 's avatarCaolán McNamara <caolanm@redhat.com>
üst 8c6e900e
...@@ -124,12 +124,14 @@ public: ...@@ -124,12 +124,14 @@ public:
std::vector<unsigned> std::vector<unsigned>
FindPositionsByName(const rtl::OUString& name) const; FindPositionsByName(const rtl::OUString& name) const;
enum SearchBehavior { RETURN_ALL, RETURN_FIRST };
/** Obtain the positions of all styles which have a certain name and fulfill a certain condition. /** Obtain the positions of all styles which have a certain name and fulfill a certain condition.
* *
* This method is fast because it can use the name-based index * This method is fast because it can use the name-based index
*/ */
std::vector<unsigned> std::vector<unsigned>
FindPositionsByNameAndPredicate(const rtl::OUString& name, StyleSheetPredicate& predicate) const; FindPositionsByNameAndPredicate(const rtl::OUString& name, StyleSheetPredicate& predicate,
SearchBehavior behavior = RETURN_ALL) const;
/** Obtain the positions of all styles which fulfill a certain condition. /** Obtain the positions of all styles which fulfill a certain condition.
* *
......
...@@ -27,6 +27,13 @@ class MockedStyleSheet : public SfxStyleSheetBase ...@@ -27,6 +27,13 @@ class MockedStyleSheet : public SfxStyleSheetBase
}; };
struct DummyPredicate : public StyleSheetPredicate {
bool Check(const SfxStyleSheetBase& styleSheet) SAL_OVERRIDE {
(void)styleSheet; // fix compiler warning
return true;
}
};
class IndexedStyleSheetsTest : public CppUnit::TestFixture class IndexedStyleSheetsTest : public CppUnit::TestFixture
{ {
void InstantiationWorks(); void InstantiationWorks();
...@@ -37,6 +44,7 @@ class IndexedStyleSheetsTest : public CppUnit::TestFixture ...@@ -37,6 +44,7 @@ class IndexedStyleSheetsTest : public CppUnit::TestFixture
void StyleSheetsCanBeRetrievedByTheirName(); void StyleSheetsCanBeRetrievedByTheirName();
void KnowsThatItStoresAStyleSheet(); void KnowsThatItStoresAStyleSheet();
void PositionCanBeQueriedByFamily(); void PositionCanBeQueriedByFamily();
void OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed();
// Adds code needed to register the test suite // Adds code needed to register the test suite
CPPUNIT_TEST_SUITE(IndexedStyleSheetsTest); CPPUNIT_TEST_SUITE(IndexedStyleSheetsTest);
...@@ -49,6 +57,7 @@ class IndexedStyleSheetsTest : public CppUnit::TestFixture ...@@ -49,6 +57,7 @@ class IndexedStyleSheetsTest : public CppUnit::TestFixture
CPPUNIT_TEST(StyleSheetsCanBeRetrievedByTheirName); CPPUNIT_TEST(StyleSheetsCanBeRetrievedByTheirName);
CPPUNIT_TEST(KnowsThatItStoresAStyleSheet); CPPUNIT_TEST(KnowsThatItStoresAStyleSheet);
CPPUNIT_TEST(PositionCanBeQueriedByFamily); CPPUNIT_TEST(PositionCanBeQueriedByFamily);
CPPUNIT_TEST(OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed);
// End of test suite definition // End of test suite definition
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
...@@ -174,7 +183,29 @@ void IndexedStyleSheetsTest::PositionCanBeQueriedByFamily() ...@@ -174,7 +183,29 @@ void IndexedStyleSheetsTest::PositionCanBeQueriedByFamily()
const std::vector<unsigned>& w = iss.GetStyleSheetPositionsByFamily(SFX_STYLE_FAMILY_ALL); const std::vector<unsigned>& w = iss.GetStyleSheetPositionsByFamily(SFX_STYLE_FAMILY_ALL);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Wildcard works for family queries.", static_cast<size_t>(3), w.size()); CPPUNIT_ASSERT_EQUAL_MESSAGE("Wildcard works for family queries.", static_cast<size_t>(3), w.size());
}
void IndexedStyleSheetsTest::OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed()
{
rtl::OUString name("name1");
rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name, SFX_STYLE_FAMILY_CHAR));
rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name, SFX_STYLE_FAMILY_PARA));
rtl::Reference<SfxStyleSheetBase> sheet3(new MockedStyleSheet(name, SFX_STYLE_FAMILY_CHAR));
IndexedStyleSheets iss;
iss.AddStyleSheet(sheet1);
iss.AddStyleSheet(sheet2);
iss.AddStyleSheet(sheet3);
DummyPredicate predicate; // returns always true, i.e., all style sheets match the predicate.
std::vector<unsigned> v = iss.FindPositionsByNameAndPredicate(name, predicate,
IndexedStyleSheets::RETURN_FIRST);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Only one style sheet is returned.", static_cast<size_t>(1), v.size());
std::vector<unsigned> w = iss.FindPositionsByNameAndPredicate(name, predicate,
IndexedStyleSheets::RETURN_ALL);
CPPUNIT_ASSERT_EQUAL_MESSAGE("All style sheets are returned.", static_cast<size_t>(1), v.size());
} }
CPPUNIT_TEST_SUITE_REGISTRATION(IndexedStyleSheetsTest); CPPUNIT_TEST_SUITE_REGISTRATION(IndexedStyleSheetsTest);
......
...@@ -130,7 +130,7 @@ IndexedStyleSheets::FindPositionsByName(const rtl::OUString& name) const ...@@ -130,7 +130,7 @@ IndexedStyleSheets::FindPositionsByName(const rtl::OUString& name) const
std::vector<unsigned> std::vector<unsigned>
IndexedStyleSheets::FindPositionsByNameAndPredicate(const rtl::OUString& name, IndexedStyleSheets::FindPositionsByNameAndPredicate(const rtl::OUString& name,
StyleSheetPredicate& predicate) const StyleSheetPredicate& predicate, SearchBehavior behavior) const
{ {
std::vector<unsigned> r; std::vector<unsigned> r;
MapType::const_iterator it = mPositionsByName.find(name); MapType::const_iterator it = mPositionsByName.find(name);
...@@ -139,6 +139,9 @@ IndexedStyleSheets::FindPositionsByNameAndPredicate(const rtl::OUString& name, ...@@ -139,6 +139,9 @@ IndexedStyleSheets::FindPositionsByNameAndPredicate(const rtl::OUString& name,
SfxStyleSheetBase *ssheet = mStyleSheets.at(pos).get(); SfxStyleSheetBase *ssheet = mStyleSheets.at(pos).get();
if (predicate.Check(*ssheet)) { if (predicate.Check(*ssheet)) {
r.push_back(pos); r.push_back(pos);
if (behavior == RETURN_FIRST) {
break;
}
} }
} }
return r; return r;
......
...@@ -530,7 +530,8 @@ SfxStyleSheetBase* SfxStyleSheetIterator::Find(const OUString& rStr) ...@@ -530,7 +530,8 @@ SfxStyleSheetBase* SfxStyleSheetIterator::Find(const OUString& rStr)
DoesStyleMatchStyleSheetPredicate predicate(this); DoesStyleMatchStyleSheetPredicate predicate(this);
std::vector<unsigned> positions = std::vector<unsigned> positions =
pBasePool->mIndexedStyleSheets->FindPositionsByNameAndPredicate(rStr, predicate); pBasePool->mIndexedStyleSheets->FindPositionsByNameAndPredicate(rStr, predicate,
svl::IndexedStyleSheets::RETURN_FIRST);
if (positions.empty()) { if (positions.empty()) {
return NULL; return NULL;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment