summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Lippert <drtl@fastmail.fm>2014-07-15 21:05:20 +0200
committerCaolán McNamara <caolanm@redhat.com>2014-07-23 11:33:14 +0000
commit9782ba7f1b7f4d6cc11045b6c953deb6f17f321d (patch)
tree169715724ebc5153d00f65ca825b60ecfe214cf2
parent8c6e900e4724dc7c800047099139c080f5f5d564 (diff)
fdo#76754 Add return first to IndexedStyleSheets to speed up ods writing
Change-Id: I6fc9fe8ce78ad9cc1a7c2fe3e13ed38ce468ce6c Reviewed-on: https://gerrit.libreoffice.org/10347 Reviewed-by: Caolán McNamara <caolanm@redhat.com> Tested-by: Caolán McNamara <caolanm@redhat.com>
-rw-r--r--include/svl/IndexedStyleSheets.hxx4
-rw-r--r--svl/qa/unit/items/test_IndexedStyleSheets.cxx31
-rw-r--r--svl/source/items/IndexedStyleSheets.cxx5
-rw-r--r--svl/source/items/style.cxx3
4 files changed, 40 insertions, 3 deletions
diff --git a/include/svl/IndexedStyleSheets.hxx b/include/svl/IndexedStyleSheets.hxx
index bc5dea7f37a2..8cd8de46b182 100644
--- a/include/svl/IndexedStyleSheets.hxx
+++ b/include/svl/IndexedStyleSheets.hxx
@@ -124,12 +124,14 @@ public:
std::vector<unsigned>
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.
*
* This method is fast because it can use the name-based index
*/
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.
*
diff --git a/svl/qa/unit/items/test_IndexedStyleSheets.cxx b/svl/qa/unit/items/test_IndexedStyleSheets.cxx
index 99b816f2c781..25944e29cc94 100644
--- a/svl/qa/unit/items/test_IndexedStyleSheets.cxx
+++ b/svl/qa/unit/items/test_IndexedStyleSheets.cxx
@@ -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
{
void InstantiationWorks();
@@ -37,6 +44,7 @@ class IndexedStyleSheetsTest : public CppUnit::TestFixture
void StyleSheetsCanBeRetrievedByTheirName();
void KnowsThatItStoresAStyleSheet();
void PositionCanBeQueriedByFamily();
+ void OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed();
// Adds code needed to register the test suite
CPPUNIT_TEST_SUITE(IndexedStyleSheetsTest);
@@ -49,6 +57,7 @@ class IndexedStyleSheetsTest : public CppUnit::TestFixture
CPPUNIT_TEST(StyleSheetsCanBeRetrievedByTheirName);
CPPUNIT_TEST(KnowsThatItStoresAStyleSheet);
CPPUNIT_TEST(PositionCanBeQueriedByFamily);
+ CPPUNIT_TEST(OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed);
// End of test suite definition
CPPUNIT_TEST_SUITE_END();
@@ -174,7 +183,29 @@ void IndexedStyleSheetsTest::PositionCanBeQueriedByFamily()
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());
+}
+
+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);
diff --git a/svl/source/items/IndexedStyleSheets.cxx b/svl/source/items/IndexedStyleSheets.cxx
index 7b9f7b632e75..bc5b1cd7a1b8 100644
--- a/svl/source/items/IndexedStyleSheets.cxx
+++ b/svl/source/items/IndexedStyleSheets.cxx
@@ -130,7 +130,7 @@ IndexedStyleSheets::FindPositionsByName(const rtl::OUString& name) const
std::vector<unsigned>
IndexedStyleSheets::FindPositionsByNameAndPredicate(const rtl::OUString& name,
- StyleSheetPredicate& predicate) const
+ StyleSheetPredicate& predicate, SearchBehavior behavior) const
{
std::vector<unsigned> r;
MapType::const_iterator it = mPositionsByName.find(name);
@@ -139,6 +139,9 @@ IndexedStyleSheets::FindPositionsByNameAndPredicate(const rtl::OUString& name,
SfxStyleSheetBase *ssheet = mStyleSheets.at(pos).get();
if (predicate.Check(*ssheet)) {
r.push_back(pos);
+ if (behavior == RETURN_FIRST) {
+ break;
+ }
}
}
return r;
diff --git a/svl/source/items/style.cxx b/svl/source/items/style.cxx
index 9d212345711e..e61f3eb8b50b 100644
--- a/svl/source/items/style.cxx
+++ b/svl/source/items/style.cxx
@@ -530,7 +530,8 @@ SfxStyleSheetBase* SfxStyleSheetIterator::Find(const OUString& rStr)
DoesStyleMatchStyleSheetPredicate predicate(this);
std::vector<unsigned> positions =
- pBasePool->mIndexedStyleSheets->FindPositionsByNameAndPredicate(rStr, predicate);
+ pBasePool->mIndexedStyleSheets->FindPositionsByNameAndPredicate(rStr, predicate,
+ svl::IndexedStyleSheets::RETURN_FIRST);
if (positions.empty()) {
return NULL;
}