Problem: 
While adding .bib files via "BibTeX Database form," they are sorted
alphabetically. However, sometimes this is not desired. For example, I
put all "@string" into a abbrev.bib file and would like this file to be
listed at first. If there is another aaa.bib file which uses those
@string definitions, auto sorting will result in bibtex error.

Ver: I check 1.2.3 and 1.3.0 and both have this problem.

Root:
FormBibtex.C calls eliminate_duplicates(dbase) to remove duplicates in
dbase. In eliminate_duplicates (defined in lyxalgo.h), dbase is first
sorted.

Fix: (src/support/lyxalgo.h)
A quick patch is to rewrite eliminate_duplicates as 

template<class C>
void eliminate_duplicates(C & c)
{
    for (size_t i = 1; i < c.size(); ++i) {
        typename C::iterator ci = c.begin() + i;
        if (std::find(c.begin(), ci, *ci) != ci) {
            c.erase(ci);
            --i;
        }
    }
}

This works fine and is efficient for small size c.

If we expect c to be very large (which is very unlikely), we can use:

#include <set>       // put this to the beginning of the file

template<class C>
void eliminate_duplicates(C & c)
{
    C unique_c;
    std::set<typename C::value_type> s;
    
    for (typename C::iterator p = c.begin(); p != c.end(); ++p) {
        if (s.count(*p) == 0) {
            unique_c.push_back(*p);
            s.insert(*p);
        }
    }
    c = unique_c;
}

This patch is against 1.3.0 source. Sorry I didn't check the CVS.

--Ling


Reply via email to