From 3cdce8bf670735a29a2f736a2650b6066598630f Mon Sep 17 00:00:00 2001 From: cxl Date: Sun, 10 Feb 2013 20:43:56 +0000 Subject: [PATCH] Core: InVector fix git-svn-id: svn://ultimatepp.org/upp/trunk@5789 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/InVector.h | 38 +++++++++++++++++-------------- uppsrc/Core/InVector.hpp | 49 +++++++++++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 23 deletions(-) diff --git a/uppsrc/Core/InVector.h b/uppsrc/Core/InVector.h index a33839fff..1717b90a4 100644 --- a/uppsrc/Core/InVector.h +++ b/uppsrc/Core/InVector.h @@ -22,10 +22,10 @@ private: void SetBlkPar(); template - int FindUpperBound(const T& val, const L& less, int& off, int& pos); + int FindUpperBound(const T& val, const L& less, int& off, int& pos) const; template - int FindLowerBound(const T& val, const L& less, int& off, int& pos); + int FindLowerBound(const T& val, const L& less, int& off, int& pos) const; bool JoinSmall(int blki); T *Insert0(int ii, int blki, int pos, int off, const T *val); @@ -39,6 +39,10 @@ private: void Chk() const { ASSERT_(!IsPicked(), "Broken pick semantics"); } +#ifdef flagIVTEST + void Check(int blki, int offset) const; +#endif + public: T& Insert(int i) { return *Insert0(i, NULL); } T& Insert(int i, const T& x) { return *Insert0(i, &x); } @@ -74,20 +78,20 @@ public: T Pop() { T h = Top(); Drop(); return h; } template - int FindUpperBound(const T& val, const L& less) { int off, pos; FindUpperBound(val, less, off, pos); return off + pos; } - int FindUpperBound(const T& val) { return FindUpperBound(val, StdLess()); } + int FindUpperBound(const T& val, const L& less) const { int off, pos; FindUpperBound(val, less, off, pos); return off + pos; } + int FindUpperBound(const T& val) const { return FindUpperBound(val, StdLess()); } template - int FindLowerBound(const T& val, const L& less) { int off, pos; FindLowerBound(val, less, off, pos); return off + pos; } - int FindLowerBound(const T& val) { return FindLowerBound(val, StdLess()); } + int FindLowerBound(const T& val, const L& less) const { int off, pos; FindLowerBound(val, less, off, pos); return off + pos; } + int FindLowerBound(const T& val) const { return FindLowerBound(val, StdLess()); } template int InsertUpperBound(const T& val, const L& less); - int InsertUpperBound(const T& val) { return InsertUpperBound(val, StdLess()); } + int InsertUpperBound(const T& val) { return InsertUpperBound(val, StdLess()); } template - int Find(const T& val, const L& less); - int Find(const T& val) { return Find(val, StdLess()); } + int Find(const T& val, const L& less) const; + int Find(const T& val) const { return Find(val, StdLess()); } typedef T ValueType; @@ -264,20 +268,20 @@ public: T Pop() { T h = Top(); Drop(); return h; } template - int FindUpperBound(const T& val, const L& less) { return iv.FindUpperBound((T*)&val, ALess(less)); } - int FindUpperBound(const T& val) { return FindUpperBound(val, StdLess()); } + int FindUpperBound(const T& val, const L& less) const { return iv.FindUpperBound((T*)&val, ALess(less)); } + int FindUpperBound(const T& val) const { return FindUpperBound(val, StdLess()); } template - int FindLowerBound(const T& val, const L& less) { return iv.FindLowerBound((T*)&val, ALess(less)); } - int FindLowerBound(const T& val) { return FindLowerBound(val, StdLess()); } + int FindLowerBound(const T& val, const L& less) const { return iv.FindLowerBound((T*)&val, ALess(less)); } + int FindLowerBound(const T& val) const { return FindLowerBound(val, StdLess()); } template - int InsertUpperBound(const T& val, const L& lss){ return iv.InsertUpperBound(new T(val), ALess(lss)); } - int InsertUpperBound(const T& val) { return InsertUpperBound(val, StdLess()); } + int InsertUpperBound(const T& val, const L& lss) { return iv.InsertUpperBound(new T(val), ALess(lss)); } + int InsertUpperBound(const T& val) { return InsertUpperBound(val, StdLess()); } template - int Find(const T& val, const L& less) { return iv.Find((T*)&val, ALess(less)); } - int Find(const T& val) { return Find(val, StdLess()); } + int Find(const T& val, const L& less) const { return iv.Find((T*)&val, ALess(less)); } + int Find(const T& val) const { return Find(val, StdLess()); } typedef T ValueType; diff --git a/uppsrc/Core/InVector.hpp b/uppsrc/Core/InVector.hpp index cde17bb79..44dffb295 100644 --- a/uppsrc/Core/InVector.hpp +++ b/uppsrc/Core/InVector.hpp @@ -31,6 +31,9 @@ extern thread__ int invector_cache_end_; template force_inline void InVector::SetCache(int blki, int offset) const { +#ifdef flagIVTEST + Check(0, 0); +#endif invector_cache_serial_ = serial; invector_cache_blki_ = blki; invector_cache_offset_ = offset; @@ -158,6 +161,9 @@ void InVector::Reindex() end = w.End(); n = w.GetCount(); } +#ifdef flagIVTEST + Check(0, 0); +#endif } template @@ -299,15 +305,18 @@ void InVector::InsertN(int ii, int n) ASSERT(n == 0); Reindex(); } +#ifdef flagIVTEST + Check(0, 0); +#endif } template void InVector::Remove(int pos, int n) { ASSERT(pos >= 0 && pos + n <= GetCount()); - count -= n; int off; int blki = FindBlock(pos, off); + count -= n; if(pos + n < data[blki].GetCount()) { data[blki].Remove(pos, n); if(JoinSmall(blki)) @@ -325,16 +334,20 @@ void InVector::Remove(int pos, int n) data[b1++].Remove(pos, nn); n -= nn; int b2 = b1; - while(n >= data[b2].GetCount()) { + while(b2 < data.GetCount() && n >= data[b2].GetCount()) { n -= min(n, data[b2].GetCount()); b2++; } data.Remove(b1, b2 - b1); - data[b1].Remove(0, n); + if(b1 < data.GetCount()) + data[b1].Remove(0, n); JoinSmall(blki + 1); JoinSmall(blki); Reindex(); } +#ifdef flagIVTEST + Check(0, 0); +#endif } template @@ -379,7 +392,7 @@ InVector::InVector(const InVector& v, int) template template -int InVector::FindUpperBound(const T& val, const L& less, int& off, int& pos) +int InVector::FindUpperBound(const T& val, const L& less, int& off, int& pos) const { if(data.GetCount() == 0) { pos = off = 0; @@ -399,6 +412,9 @@ int InVector::FindUpperBound(const T& val, const L& less, int& off, int& pos) ii += ii; half >>= 1; } +#ifdef flagIVTEST + Check(blki, offset); +#endif if(blki < data.GetCount()) { if(!less(val, data[blki].Top())) offset += data[blki++].GetCount(); @@ -412,12 +428,13 @@ int InVector::FindUpperBound(const T& val, const L& less, int& off, int& pos) pos = data.Top().GetCount(); off = count - pos; blki--; + SetCache(blki, off); return blki; } template template -int InVector::FindLowerBound(const T& val, const L& less, int& off, int& pos) +int InVector::FindLowerBound(const T& val, const L& less, int& off, int& pos) const { if(data.GetCount() == 0) { pos = off = 0; @@ -437,6 +454,9 @@ int InVector::FindLowerBound(const T& val, const L& less, int& off, int& pos) ii += ii; half >>= 1; } +#ifdef flagIVTEST + Check(blki, offset); +#endif if(blki < data.GetCount()) { if(blki + 1 < data.GetCount() && less(data[blki + 1][0], val)) offset += data[blki++].GetCount(); @@ -450,6 +470,7 @@ int InVector::FindLowerBound(const T& val, const L& less, int& off, int& pos) pos = data.Top().GetCount(); off = count - pos; blki--; + SetCache(blki, off); return blki; } @@ -472,7 +493,7 @@ int InVector::InsertUpperBound(const T& val, const L& less) template template -int InVector::Find(const T& val, const L& less) +int InVector::Find(const T& val, const L& less) const { int i = FindLowerBound(val, less); return i < GetCount() && !less(val, (*this)[i]) ? i : -1; @@ -594,6 +615,22 @@ void InVector::DumpIndex() } #endif +#ifdef flagIVTEST +template +void InVector::Check(int blki, int offset) const +{ + int off = 0; + int all = 0; + for(int i = 0; i < data.GetCount(); i++) { + if(i < blki) + off += data[i].GetCount(); + all += data[i].GetCount(); + } + ASSERT(off == offset); + ASSERT(all == count); +} +#endif + template void InArray::Delete(IVIter it, int count) {