[Libreoffice-commits] core.git: include/svl svl/qa svl/source

Tobias Lippert drtl at fastmail.fm
Wed Jul 23 04:33:44 PDT 2014


 include/svl/IndexedStyleSheets.hxx            |    4 ++-
 svl/qa/unit/items/test_IndexedStyleSheets.cxx |   31 ++++++++++++++++++++++++++
 svl/source/items/IndexedStyleSheets.cxx       |    5 +++-
 svl/source/items/style.cxx                    |    3 +-
 4 files changed, 40 insertions(+), 3 deletions(-)

New commits:
commit 9782ba7f1b7f4d6cc11045b6c953deb6f17f321d
Author: Tobias Lippert <drtl at fastmail.fm>
Date:   Tue Jul 15 21:05:20 2014 +0200

    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 at redhat.com>
    Tested-by: Caolán McNamara <caolanm at redhat.com>

diff --git a/include/svl/IndexedStyleSheets.hxx b/include/svl/IndexedStyleSheets.hxx
index bc5dea7..8cd8de4 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 99b816f..25944e2 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 7b9f7b6..bc5b1cd 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 9d21234..e61f3eb 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;
     }


More information about the Libreoffice-commits mailing list