[Libreoffice-commits] core.git: starmath/inc starmath/source

Takeshi Abe tabe at fixedpoint.jp
Tue May 31 16:15:59 UTC 2016


 starmath/inc/caret.hxx     |   71 ++++++++++-----------------------------------
 starmath/source/caret.cxx  |   45 +++++++++-------------------
 starmath/source/cursor.cxx |   47 +++++++++++++++--------------
 3 files changed, 56 insertions(+), 107 deletions(-)

New commits:
commit 09981cd6383ecb99e4b6c83b98b03af5cf3ff59b
Author: Takeshi Abe <tabe at fixedpoint.jp>
Date:   Tue May 31 15:01:49 2016 +0900

    Use std::vector for SmCaretPosGraph
    
    instead of employing ad hoc linked list and its iterator.
    
    Change-Id: Ibc4709a2e67aa805cf54117303c47d9a8a5eede9
    Reviewed-on: https://gerrit.libreoffice.org/25699
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Michael Stahl <mstahl at redhat.com>

diff --git a/starmath/inc/caret.hxx b/starmath/inc/caret.hxx
index 0dff680..de13ac9 100644
--- a/starmath/inc/caret.hxx
+++ b/starmath/inc/caret.hxx
@@ -15,6 +15,9 @@
 
 #include "node.hxx"
 
+#include <memory>
+#include <vector>
+
 /** Representation of caret position with an equation */
 struct SmCaretPos{
     SmCaretPos(SmNode* selectedNode = nullptr, int iIndex = 0) {
@@ -117,74 +120,34 @@ struct SmCaretPosGraphEntry{
     }
 };
 
-class SmCaretPosGraph;
-
-/** Iterator for SmCaretPosGraph */
-class SmCaretPosGraphIterator{
-public:
-    SmCaretPosGraphIterator(SmCaretPosGraph* graph){
-        pGraph = graph;
-        nOffset = 0;
-        pEntry = nullptr;
-    }
-    /** Get the next entry, NULL if none */
-    SmCaretPosGraphEntry* Next();
-    /** Get the current entry, NULL if none */
-    SmCaretPosGraphEntry* Current(){
-        return pEntry;
-    }
-    /** Get the current entry, NULL if none */
-    SmCaretPosGraphEntry* operator->(){
-        return pEntry;
-    }
-private:
-    /** Next entry to return */
-    int nOffset;
-    /** Current graph */
-    SmCaretPosGraph* pGraph;
-    /** Current entry */
-    SmCaretPosGraphEntry* pEntry;
-};
-
-
 /** A graph over all caret positions
  * @remarks Graphs can only grow, entries cannot be removed!
  */
 class SmCaretPosGraph{
 public:
-    SmCaretPosGraph(){
-        pNext = nullptr;
-        nOffset = 0;
-    }
+    SmCaretPosGraph();
+
     ~SmCaretPosGraph();
-    /** Add a caret position
-     *  @remarks If Left and/or Right are set NULL, they will point back to the entry.
-     */
-    SmCaretPosGraphEntry* Add(SmCaretPosGraphEntry entry);
+
     /** Add a caret position
      *  @remarks If left and/or right are set NULL, they will point back to the entry.
      */
     SmCaretPosGraphEntry* Add(SmCaretPos pos,
                             SmCaretPosGraphEntry* left = nullptr,
-                            SmCaretPosGraphEntry* right = nullptr){
-        SAL_WARN_IF( pos.Index < 0, "starmath", "Index shouldn't be -1!" );
-        return Add(SmCaretPosGraphEntry(pos, left, right));
+                            SmCaretPosGraphEntry* right = nullptr);
+
+    std::vector<std::unique_ptr<SmCaretPosGraphEntry>>::iterator begin()
+    {
+        return mvEntries.begin();
     }
-    /** Get an iterator for this graph */
-    SmCaretPosGraphIterator GetIterator(){
-        return SmCaretPosGraphIterator(this);
+
+    std::vector<std::unique_ptr<SmCaretPosGraphEntry>>::iterator end()
+    {
+        return mvEntries.end();
     }
-    friend class SmCaretPosGraphIterator;
-private:
-    /** Define SmCaretPosGraph to be less than one page 4096 */
-    static const int SmCaretPosGraphSize = 255;
 
-    /** Next graph, to be used when this graph is full */
-    SmCaretPosGraph* pNext;
-    /** Next free entry in graph */
-    int nOffset;
-    /** Entries in this graph segment */
-    SmCaretPosGraphEntry Graph[SmCaretPosGraphSize];
+private:
+    std::vector<std::unique_ptr<SmCaretPosGraphEntry>> mvEntries;
 };
 
 /** \page visual_formula_editing Visual Formula Editing
diff --git a/starmath/source/caret.cxx b/starmath/source/caret.cxx
index 5801fea..1a79d50 100644
--- a/starmath/source/caret.cxx
+++ b/starmath/source/caret.cxx
@@ -8,39 +8,24 @@
  */
 #include "caret.hxx"
 
-/////////////////////////////// SmCaretPosGraph
+#include <o3tl/make_unique.hxx>
 
-SmCaretPosGraphEntry* SmCaretPosGraphIterator::Next(){
-    if(nOffset >= pGraph->nOffset){
-        if(pGraph->pNext){
-            pGraph = pGraph->pNext;
-            nOffset = 0;
-            pEntry = Next();
-        }else
-            pEntry = nullptr;
-    }else
-        pEntry = pGraph->Graph + nOffset++;
-    return pEntry;
-}
+SmCaretPosGraph::SmCaretPosGraph() = default;
 
-SmCaretPosGraphEntry* SmCaretPosGraph::Add(SmCaretPosGraphEntry entry){
-    if(nOffset >= SmCaretPosGraphSize){
-        if(!pNext)
-            pNext = new SmCaretPosGraph();
-        return pNext->Add(entry);
-    }else{
-        //Set Left and Right to point to the entry itself if they are NULL
-        entry.Left = entry.Left ? entry.Left : Graph + nOffset;
-        entry.Right = entry.Right ? entry.Right : Graph + nOffset;
-        //Save the entry
-        Graph[nOffset] = entry;
-        return Graph + nOffset++;
-    }
-}
+SmCaretPosGraph::~SmCaretPosGraph() = default;
 
-SmCaretPosGraph::~SmCaretPosGraph(){
-    delete pNext;
-    pNext = nullptr;
+SmCaretPosGraphEntry* SmCaretPosGraph::Add(SmCaretPos pos,
+                                           SmCaretPosGraphEntry* left,
+                                           SmCaretPosGraphEntry* right)
+{
+    SAL_WARN_IF( pos.Index < 0, "starmath", "Index shouldn't be -1!" );
+    auto entry = o3tl::make_unique<SmCaretPosGraphEntry>(pos, left, right);
+    SmCaretPosGraphEntry* e = entry.get();
+    //Set Left and Right to point to the entry itself if they are NULL
+    entry->Left = entry->Left ? entry->Left : e;
+    entry->Right = entry->Right ? entry->Right : e;
+    mvEntries.push_back(std::move(entry));
+    return e;
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/starmath/source/cursor.cxx b/starmath/source/cursor.cxx
index de4439c..617e100 100644
--- a/starmath/source/cursor.cxx
+++ b/starmath/source/cursor.cxx
@@ -36,12 +36,12 @@ void SmCursor::Move(OutputDevice* pDev, SmMovementDirection direction, bool bMov
                         best_line,  //Best approximated line found so far
                         curr_line;  //Current line
             long dbp_sq = 0;        //Distance squared to best line
-            SmCaretPosGraphIterator it = mpGraph->GetIterator();
-            while(it.Next()){
+            for(auto &pEntry : *mpGraph)
+            {
                 //Reject it if it's the current position
-                if(it->CaretPos == mpPosition->CaretPos) continue;
+                if(pEntry->CaretPos == mpPosition->CaretPos) continue;
                 //Compute caret line
-                curr_line = SmCaretPos2LineVisitor(pDev, it->CaretPos).GetResult();
+                curr_line = SmCaretPos2LineVisitor(pDev, pEntry->CaretPos).GetResult();
                 //Reject anything above if we're moving down
                 if(curr_line.GetTop() <= from_line.GetTop() && direction == MoveDown) continue;
                 //Reject anything below if we're moving up
@@ -57,7 +57,7 @@ void SmCursor::Move(OutputDevice* pDev, SmMovementDirection direction, bool bMov
                 }
                 //Take current line as the best
                 best_line = curr_line;
-                NewPos = it.Current();
+                NewPos = pEntry.get();
                 //Update distance to best line
                 dbp_sq = best_line.SquaredDistanceX(from_line) * HORIZONTICAL_DISTANCE_FACTOR +
                          best_line.SquaredDistanceY(from_line);
@@ -80,11 +80,11 @@ void SmCursor::MoveTo(OutputDevice* pDev, Point pos, bool bMoveAnchor){
     SmCaretPosGraphEntry* NewPos = nullptr;
     long dp_sq = 0,     //Distance to current line squared
          dbp_sq = 1;    //Distance to best line squared
-    SmCaretPosGraphIterator it = mpGraph->GetIterator();
-    while(it.Next()){
-        OSL_ENSURE(it->CaretPos.IsValid(), "The caret position graph may not have invalid positions!");
+    for(auto &pEntry : *mpGraph)
+    {
+        OSL_ENSURE(pEntry->CaretPos.IsValid(), "The caret position graph may not have invalid positions!");
         //Compute current line
-        curr_line = SmCaretPos2LineVisitor(pDev, it->CaretPos).GetResult();
+        curr_line = SmCaretPos2LineVisitor(pDev, pEntry->CaretPos).GetResult();
         //If we have a position compare to it
         if(NewPos){
             //Compute squared distance to current line
@@ -94,7 +94,7 @@ void SmCursor::MoveTo(OutputDevice* pDev, Point pos, bool bMoveAnchor){
         }
         //Accept current position as the best
         best_line = curr_line;
-        NewPos = it.Current();
+        NewPos = pEntry.get();
         //Update distance to best line
         dbp_sq = best_line.SquaredDistanceX(pos) + best_line.SquaredDistanceY(pos);
     }
@@ -126,18 +126,18 @@ void SmCursor::BuildGraph(){
 
     //Restore anchor and position pointers
     if(_anchor.IsValid() || _position.IsValid()){
-        SmCaretPosGraphIterator it = mpGraph->GetIterator();
-        while(it.Next()){
-            if(_anchor == it->CaretPos)
-                mpAnchor = it.Current();
-            if(_position == it->CaretPos)
-                mpPosition = it.Current();
+        for(auto &pEntry : *mpGraph)
+        {
+            if(_anchor == pEntry->CaretPos)
+                mpAnchor = pEntry.get();
+            if(_position == pEntry->CaretPos)
+                mpPosition = pEntry.get();
         }
     }
     //Set position and anchor to first caret position
-    SmCaretPosGraphIterator it = mpGraph->GetIterator();
+    auto it = mpGraph->begin();
     if(!mpPosition)
-        mpPosition = it.Next();
+        mpPosition = (it == mpGraph->end()) ? nullptr : it->get();
     if(!mpAnchor)
         mpAnchor = mpPosition;
 
@@ -146,11 +146,12 @@ void SmCursor::BuildGraph(){
 }
 
 bool SmCursor::SetCaretPosition(SmCaretPos pos){
-    SmCaretPosGraphIterator it = mpGraph->GetIterator();
-    while(it.Next()){
-        if(it->CaretPos == pos){
-            mpPosition = it.Current();
-            mpAnchor = it.Current();
+    for(auto &pEntry : *mpGraph)
+    {
+        if(pEntry->CaretPos == pos)
+        {
+            mpPosition = pEntry.get();
+            mpAnchor = pEntry.get();
             return true;
         }
     }


More information about the Libreoffice-commits mailing list