]> git.lyx.org Git - features.git/commitdiff
Introduce a labels&references cache at buffer level. This cache uses the already...
authorAbdelrazak Younes <younes@lyx.org>
Sun, 2 Mar 2008 15:27:35 +0000 (15:27 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Sun, 2 Mar 2008 15:27:35 +0000 (15:27 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23389 a592a061-630c-0410-9148-cb99ea01b6c8

src/Buffer.cpp
src/Buffer.h
src/buffer_funcs.cpp
src/insets/InsetLabel.cpp
src/insets/InsetLabel.h
src/insets/InsetRef.cpp
src/insets/InsetRef.h

index 31e61455b240efae5fbfceeadbab9a75d03d0467..4519737d6bf8f005b09e81ac2fcf4fca43720bfe 100644 (file)
 #include <boost/shared_ptr.hpp>
 
 #include <algorithm>
+#include <fstream>
 #include <iomanip>
-#include <stack>
+#include <map>
 #include <sstream>
-#include <fstream>
+#include <stack>
+#include <vector>
 
 using namespace std;
 using namespace lyx::support;
@@ -117,10 +119,10 @@ namespace {
 
 int const LYX_FORMAT = 316; // JSpitzm: subfig support
 
-} // namespace anon
-
-
 typedef map<string, bool> DepClean;
+typedef map<docstring, pair<InsetLabel const *, Buffer::References> > RefCache;
+
+} // namespace anon
 
 class Buffer::Impl
 {
@@ -205,6 +207,8 @@ public:
        /// A cache for the bibfiles (including bibfiles of loaded child
        /// documents), needed for appropriate update of natbib labels.
        mutable EmbeddedFileList bibfilesCache_;
+
+       mutable RefCache ref_cache_;
 };
 
 /// Creates the per buffer temporary directory
@@ -2064,6 +2068,47 @@ void Buffer::writeParentMacros(odocstream & os) const
 }
 
 
+Buffer::References & Buffer::references(docstring const & label)
+{
+       if (d->parent_buffer)
+               return const_cast<Buffer *>(masterBuffer())->references(label);
+
+       RefCache::iterator it = d->ref_cache_.find(label);
+       if (it != d->ref_cache_.end())
+               return it->second.second;
+
+       static InsetLabel const * dummy_il = 0;
+       static References const dummy_refs;
+       it = d->ref_cache_.insert(make_pair(label, make_pair(dummy_il, dummy_refs))).first;
+       return it->second.second;
+}
+
+
+Buffer::References const & Buffer::references(docstring const & label) const
+{
+       return const_cast<Buffer *>(this)->references(label);
+}
+
+
+void Buffer::setInsetLabel(docstring const & label, InsetLabel const * il)
+{
+       masterBuffer()->d->ref_cache_[label].first = il;
+}
+
+
+InsetLabel const * Buffer::insetLabel(docstring const & label) const
+{
+       return masterBuffer()->d->ref_cache_[label].first;
+}
+
+
+void Buffer::clearReferenceCache() const
+{
+       if (!d->parent_buffer)
+               d->ref_cache_.clear();
+}
+
+
 void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
        InsetCode code)
 {
@@ -2327,9 +2372,6 @@ void Buffer::resetChildDocuments(bool close_them) const
                resetParentBuffer(this, ip, close_them);
        }
 
-       if (use_gui && masterBuffer() == this)
-               updateLabels(*this);
-
        // clear references to children in macro tables
        d->children_positions.clear();
        d->position_to_children.clear();
index c35a2479c0c9cf4f1fa8d6be8be80ee1ce7ee201..4e01081476bf854474ce40c1c38e22f8164e1512 100644 (file)
@@ -31,6 +31,8 @@ class ErrorItem;
 class ErrorList;
 class FuncRequest;
 class Inset;
+class InsetRef;
+class InsetLabel;
 class Font;
 class Format;
 class Lexer;
@@ -451,7 +453,15 @@ public:
        bool isExportable(std::string const & format) const;
        ///
        std::vector<Format const *> exportableFormats(bool only_viewable) const;
-       
+
+       ///
+       typedef std::vector<std::pair<InsetRef *, ParIterator> > References;
+       References & references(docstring const & label);
+       References const & references(docstring const & label) const;
+       void clearReferenceCache() const;
+       void setInsetLabel(docstring const & label, InsetLabel const * il);
+       InsetLabel const * insetLabel(docstring const & label) const;
+
 private:
        /// search for macro in local (buffer) table or in children
        MacroData const * getBufferMacro(docstring const & name,
index 50356170b2e61c4ef6f7260b50487af436b38698..1d9d65126320735616aa2cabb2d91e1cf14e9d9a 100644 (file)
@@ -491,6 +491,7 @@ void updateLabels(Buffer const & buf, bool childonly)
 
                // start over the counters
                textclass.counters().reset();
+               buf.clearReferenceCache();
        }
 
        Buffer & cbuf = const_cast<Buffer &>(buf);
index 4c87e69591affdd09551625e0b2ff0a99721671e..c23cee2b06ae45f32efee58f2d94284b2676a9d1 100644 (file)
 
 #include "InsetLabel.h"
 
+#include "InsetRef.h"
+
 #include "Buffer.h"
 #include "BufferView.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
+#include "InsetIterator.h"
 #include "ParIterator.h"
 #include "sgml.h"
 #include "Text.h"
 #include "TocBackend.h"
 
-#include "support/lstrings.h"
+#include "frontends/alert.h"
+
 #include "support/lyxalgo.h"
+#include "support/gettext.h"
+#include "support/lstrings.h"
 
 using namespace std;
 using namespace lyx::support;
@@ -57,10 +63,32 @@ docstring InsetLabel::screenLabel() const
 }
 
 
+void InsetLabel::updateLabels(ParIterator const & it)
+{
+       docstring const & label = getParam("name");
+       if (buffer().insetLabel(label))
+               // Problem: We already have an InsetLabel with the same name!
+               return;
+       buffer().setInsetLabel(label, this);
+}
+
+
 void InsetLabel::addToToc(ParConstIterator const & cpit) const
 {
+       docstring const & label = getParam("name");
        Toc & toc = buffer().tocBackend().toc("label");
-       toc.push_back(TocItem(cpit, 0, screenLabel()));
+       if (buffer().insetLabel(label) != this) {
+               toc.push_back(TocItem(cpit, 0, _("DUPLICATE: ") + label));
+               return;
+       }
+       toc.push_back(TocItem(cpit, 0, label));
+       Buffer::References const & refs = buffer().references(label);
+       Buffer::References::const_iterator it = refs.begin();
+       Buffer::References::const_iterator end = refs.end();
+       for (; it != end; ++it) {
+               ParConstIterator const ref_pit(it->second);
+               toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
+       }
 }
 
 
index 6e97d511009d71d147ff74db5bd926d3a4881c8a..13284c8db2db2835a6713e60267e931cce2e5909 100644 (file)
@@ -43,6 +43,8 @@ public:
        static bool isCompatibleCommand(std::string const & s) 
                { return s == "label"; }
        ///
+       void updateLabels(ParIterator const & it);
+       ///
        void addToToc(ParConstIterator const &) const;
 protected:
        ///
index 02fc0b9355b27a889cc0955c9e58e3813f5d1cee..88c76fa5530b5e8a2fe9f48216d884a2f4025a8e 100644 (file)
@@ -26,8 +26,8 @@
 #include "support/gettext.h"
 #include "support/lstrings.h"
 
-using namespace std;
 using namespace lyx::support;
+using namespace std;
 
 namespace lyx {
 
@@ -152,32 +152,23 @@ void InsetRef::textString(odocstream & os) const
 }
 
 
-void InsetRef::addToToc(ParConstIterator const & cpit) const
+void InsetRef::updateLabels(ParIterator const & it)
 {
        docstring const & label = getParam("reference");
-       Toc & toc = buffer().tocBackend().toc("label");
-       Toc::iterator it = toc.begin();
-       Toc::iterator end = toc.end();
-       for (; it != end; ++it) {
-               if (it->str() == label)
-                       break;
-       }
+       buffer().references(label).push_back(make_pair(this, it));
+}
 
-       docstring const reflabel = screenLabel();
-       if (it == end) {
-               // This label has not been parsed yet so we just add it temporarily.
-               // InsetLabel::addTocToc() will fix that later.
-               toc.push_back(TocItem(cpit, 0, label));
-               toc.push_back(TocItem(cpit, 1, reflabel));
+
+void InsetRef::addToToc(ParConstIterator const & cpit) const
+{
+       docstring const & label = getParam("reference");
+       if (buffer().insetLabel(label))
+               // This InsetRef has already been taken care of in InsetLabel::addToToc().
                return;
-       }
 
-       // The Toc item for this label already exists so let's add
-       // this inset to this node.
-       ++it;
-       while (it != end && it->str() == reflabel)
-               ++it;
-       toc.insert(it, TocItem(cpit, 1, reflabel));
+       Toc & toc = buffer().tocBackend().toc("label");
+       docstring const reflabel = _("BROKEN: ") + screenLabel();
+       toc.push_back(TocItem(cpit, 0, reflabel));
 }
 
 
index 9bc8d44641021766304b899386dd8b0327b2e1a9..eab494474b1950f26ca8fa9e41ae08e23a1d7818 100644 (file)
@@ -63,6 +63,8 @@ public:
        ///
        static bool isCompatibleCommand(std::string const & s);
        ///
+       void updateLabels(ParIterator const & it);
+       ///
        void addToToc(ParConstIterator const &) const;
 protected:
        ///