I felt that the layout lookup became a bit slow with the new layout as string stuff, so I did this patch. Is the added complexity worth it? (I cleaned up the textclass a bit as well...)
all lookup functions has changed from O(n) to O(log n) delete_layout is still O(n), but with a higer constant factor. but of course the lists are never very long, so the constant factor really plays a role here. Index: src/lyxtextclass.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyxtextclass.h,v retrieving revision 1.2 diff -u -p -r1.2 lyxtextclass.h --- src/lyxtextclass.h 2002/03/02 16:39:48 1.2 +++ src/lyxtextclass.h 2002/03/11 17:15:13 @@ -23,6 +23,7 @@ #include "LString.h" #include <vector> +#include <map> class LyXLex; @@ -34,12 +35,14 @@ public: /// typedef std::vector<LyXLayout> LayoutList; /// + typedef std::map<string, int> LayoutMap; + /// typedef LayoutList::const_iterator const_iterator; /// explicit - LyXTextClass (string const & = string(), - string const & = string(), - string const & = string()); + LyXTextClass(string const & = string(), + string const & = string(), + string const & = string()); /// const_iterator begin() const { return layoutlist.begin(); } @@ -60,9 +63,6 @@ public: /// LyXLayout const & operator[](string const & vname) const; - /// - LyXLayout & operator[](string const & vname); - /// Sees to that the textclass structure has been loaded bool load() const; @@ -70,8 +70,7 @@ public: string const defaultLayoutName() const; /// LyXLayout const & defaultLayout() const; - /// - LyXLayout & defaultLayout(); + /// string const & name() const; /// @@ -185,7 +184,8 @@ private: /// LayoutList layoutlist; - + /// + LayoutMap layoutmap; /// Has this layout file been loaded yet? mutable bool loaded; }; Index: src/lyxtextclass.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyxtextclass.C,v retrieving revision 1.8 diff -u -p -r1.8 lyxtextclass.C --- src/lyxtextclass.C 2002/03/05 23:02:37 1.8 +++ src/lyxtextclass.C 2002/03/11 17:15:13 @@ -183,13 +183,16 @@ bool LyXTextClass::Read(string const & f string const name = subst(lexrc.getString(), '_', ' '); if (hasLayout(name)) { - LyXLayout & lay = operator[](name); + LyXLayout & lay = layoutlist[layoutmap[name]]; error = do_readStyle(lexrc, lay); } else { LyXLayout lay; lay.setName(name); - if (!(error = do_readStyle(lexrc, lay))) + if (!(error = do_readStyle(lexrc, lay))) { + layoutmap[name] = size(); layoutlist.push_back(lay); + } + if (defaultlayout_.empty()) { // We do not have a default // layout yet, so we choose @@ -496,10 +499,9 @@ string const & LyXTextClass::rightmargin bool LyXTextClass::hasLayout(string const & n) const { string const name = (n.empty() ? defaultLayoutName() : n); - - return find_if(layoutlist.begin(), layoutlist.end(), - lyx::compare_memfun(&LyXLayout::name, name)) - != layoutlist.end(); + + LayoutMap::const_iterator cit = layoutmap.find(name); + return cit != layoutmap.end(); } @@ -511,49 +513,18 @@ LyXLayout const & LyXTextClass::operator lyxerr << "Operator[] called with empty n" << endl; string const name = (n.empty() ? defaultLayoutName() : n); - - LayoutList::const_iterator cit = - find_if(layoutlist.begin(), - layoutlist.end(), - lyx::compare_memfun(&LyXLayout::name, name)); - if (cit == layoutlist.end()) { + LayoutMap::const_iterator cit = layoutmap.find(name); + if (cit == layoutmap.end()) { lyxerr << "We failed to find the layout '" << name << "' in the layout list. You MUST investigate!" << endl; - - // we require the name to exist - lyx::Assert(false); - } - - return *cit; -} - - -LyXLayout & LyXTextClass::operator[](string const & n) -{ - lyx::Assert(!n.empty()); - - if (n.empty()) - lyxerr << "Operator[] called with empty n" << endl; - string const name = (n.empty() ? defaultLayoutName() : n); - - LayoutList::iterator it = - find_if(layoutlist.begin(), - layoutlist.end(), - lyx::compare_memfun(&LyXLayout::name, name)); - - if (it == layoutlist.end()) { - lyxerr << "We failed to find the layout '" << name - << "' in the layout list. You MUST investigate!" - << endl; - // we require the name to exist lyx::Assert(false); } - - return *it; + + return layoutlist[cit->second]; } @@ -561,13 +532,23 @@ bool LyXTextClass::delete_layout(string { if (name == defaultLayoutName()) return false; + + bool ret = false; + LayoutMap::iterator it = layoutmap.find(name); + if (it != layoutmap.end()) { + // ok it exist. + layoutlist.erase(layoutlist.begin() + it->second); + // Now we must recreate the LayoutMap + layoutmap.clear(); + LayoutList::const_iterator cit = layoutlist.begin(); + LayoutList::const_iterator end = layoutlist.end(); + int i = 0; + for (; cit != end; ++cit) { + layoutmap[cit->name()] = i++; + } + ret = true; + } - LayoutList::iterator it = - remove_if(layoutlist.begin(), layoutlist.end(), - lyx::compare_memfun(&LyXLayout::name, name)); - LayoutList::iterator end = layoutlist.end(); - bool const ret = (it != end); - layoutlist.erase(it, end); return ret; } @@ -603,12 +584,6 @@ string const LyXTextClass::defaultLayout LyXLayout const & LyXTextClass::defaultLayout() const -{ - return operator[](defaultLayoutName()); -} - - -LyXLayout & LyXTextClass::defaultLayout() { return operator[](defaultLayoutName()); } -- Lgb