From 1fe4ff8ef36a6b3e8c84790c0217a16bf4b97a32 Mon Sep 17 00:00:00 2001 From: Vincent van Ravesteijn Date: Sun, 10 Jan 2010 00:06:02 +0000 Subject: [PATCH] Add a vector class that can be referenced with both positive and negative indices. It is internally represented as two vectors, one for non-zero indices and one for negative indices. In this way, the vector can grow in both directions. If an index is not available in the vector, the default value is returned. If an object is put in the vector beyond its size, the empty spots in between are also filled with the default value. I hope I'm not reinventing the wheel too much here. This might change anyway if this appears to be a performance bottleneck. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@32929 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Compare.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/Compare.cpp b/src/Compare.cpp index cfbfdce459..d1c2cd9381 100644 --- a/src/Compare.cpp +++ b/src/Compare.cpp @@ -183,6 +183,70 @@ static DocRangePair stepIntoInset(DocPair const & inset_location) } +/** + * This class is designed to hold a vector that has both positive as + * negative indices. It is internally represented as two vectors, one + * for non-zero indices and one for negative indices. In this way, the + * vector can grow in both directions. + * If an index is not available in the vector, the default value is + * returned. If an object is put in the vector beyond its size, the + * empty spots in between are also filled with the default value. + */ +template +class compl_vector { +public: + compl_vector() {} + + void reset(T const & def) + { + default_ = def; + Vp_.clear(); + Vn_.clear(); + } + + + /// Gets the value at index. If it is not in the vector + /// the default value is returned. + T & get(int index) { + if (-index <= int(Vn_.size()) && index < int(Vp_.size())) + return index >= 0 ? Vp_[index] : Vn_[-index-1]; + else + return default_; + } + + /// Sets the value at index if it already + /// is in the vector. Otherwise it will be added to the + /// end padded with the default value. + void set(int index, T const & t) { + if (index >= -int(Vn_.size()) && index < int(Vp_.size())) { + if (index >= 0) + Vp_[index] = t; + else + Vn_[-index-1] = t; + } else { + while (index > int(Vp_.size())) + Vp_.push_back(default_); + while (index < -int(Vn_.size()) - 1) + Vn_.push_back(default_); + + if (index >= 0) + Vp_.push_back(t); + else + Vn_.push_back(t); + } + } + +private: + /// The vector for positive indices + vector Vp_; + /// The vector for negative indices + vector Vn_; + /// The default value that is inserted in the vector + /// if more space is needed + T default_; +}; + + /** * The implementation of the algorithm that does the comparison * between two documents.