X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FAuthor.cpp;h=79e7de0864b942d059a5ec1502eccb475f17b792;hb=c6e1db7682dc8d58a68147b5eee1d004829ef6d2;hp=90c319e8a6e6340cd4a1c4807d00f7830dca1f12;hpb=0c9da47505379b979994bb8cf01feb2653774d14;p=lyx.git diff --git a/src/Author.cpp b/src/Author.cpp index 90c319e8a6..79e7de0864 100644 --- a/src/Author.cpp +++ b/src/Author.cpp @@ -12,9 +12,8 @@ #include "Author.h" -#include "support/lstrings.h" - #include "support/lassert.h" +#include "support/lstrings.h" #include #include @@ -24,6 +23,24 @@ using namespace lyx::support; namespace lyx { +static int computeHash(docstring const & name, + docstring const & email) +{ + string const full_author_string = to_utf8(name + email); + // Bernstein's hash function + unsigned int hash = 5381; + for (unsigned int i = 0; i < full_author_string.length(); ++i) + hash = ((hash << 5) + hash) + (unsigned int)(full_author_string[i]); + return int(hash); +} + + +Author::Author(docstring const & name, docstring const & email) + : name_(name), email_(email), used_(true) +{ + buffer_id_ = computeHash(name_, email_); +} + bool operator==(Author const & l, Author const & r) { @@ -34,12 +51,13 @@ bool operator==(Author const & l, Author const & r) ostream & operator<<(ostream & os, Author const & a) { // FIXME UNICODE - os << a.buffer_id() << " \"" << to_utf8(a.name()) - << "\" " << to_utf8(a.email()); - + os << a.buffer_id_ << " \"" << to_utf8(a.name_) + << "\" " << to_utf8(a.email_); + return os; } + istream & operator>>(istream & is, Author & a) { string s; @@ -52,30 +70,30 @@ istream & operator>>(istream & is, Author & a) } -bool author_smaller(Author const & lhs, Author const & rhs) { - return lhs.buffer_id() < rhs.buffer_id(); +bool author_smaller(Author const & lhs, Author const & rhs) +{ + return lhs.bufferId() < rhs.bufferId(); } AuthorList::AuthorList() : last_id_(0) -{ -} +{} int AuthorList::record(Author const & a) { + // If we record an author which equals the current + // author, we copy the buffer_id, so that it will + // keep the same id in the file. + if (!authors_.empty() && a == authors_[0]) + authors_[0].setBufferId(a.bufferId()); + Authors::const_iterator it(authors_.begin()); Authors::const_iterator itend(authors_.end()); - for (int i = 0; it != itend; ++it, ++i) { - if (*it == a) { - if (it->buffer_id() == 0) - // The current author is internally represented as - // author 0, but it appears he has already an id. - it->setBufferId(a.buffer_id()); + if (*it == a) return i; - } } authors_.push_back(a); return last_id_++; @@ -84,8 +102,7 @@ int AuthorList::record(Author const & a) void AuthorList::record(int id, Author const & a) { - LASSERT(unsigned(id) < authors_.size(), /**/); - + LBUFERR(unsigned(id) < authors_.size()); authors_[id] = a; } @@ -99,7 +116,7 @@ void AuthorList::recordCurrentAuthor(Author const & a) Author const & AuthorList::get(int id) const { - LASSERT(id < (int)authors_.size() , /**/); + LASSERT(id < (int)authors_.size() , return authors_[0]); return authors_[id]; } @@ -116,36 +133,20 @@ AuthorList::Authors::const_iterator AuthorList::end() const } -void AuthorList::sort() { +void AuthorList::sort() +{ std::sort(authors_.begin(), authors_.end(), author_smaller); } -ostream & operator<<(ostream & os, AuthorList const & a) { +ostream & operator<<(ostream & os, AuthorList const & a) +{ // Copy the authorlist, because we don't want to sort the original AuthorList sorted = a; sorted.sort(); AuthorList::Authors::const_iterator a_it = sorted.begin(); AuthorList::Authors::const_iterator a_end = sorted.end(); - - // Find the buffer id for the current author (internal id 0), - // if he doesn't have a buffer_id yet. - if (sorted.get(0).buffer_id() == 0) { - unsigned int cur_id = 1; - for (; a_it != a_end; ++a_it) { - if (a_it->buffer_id() == cur_id) - ++cur_id; - else if (a_it->buffer_id() > cur_id) { - break; - } - } - // Set the id in both the original authorlist, - // as in the copy. - a.get(0).setBufferId(cur_id); - sorted.get(0).setBufferId(cur_id); - sorted.sort(); - } for (a_it = sorted.begin(); a_it != a_end; ++a_it) { if (a_it->used())