Core: SortedIndex

git-svn-id: svn://ultimatepp.org/upp/trunk@5791 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2013-02-11 10:49:47 +00:00
parent 43d318df0b
commit 44d599832c
2 changed files with 97 additions and 0 deletions

View file

@ -109,6 +109,8 @@ public:
InVector(const InVector& v, int);
void Swap(InVector& b);
friend void Swap(InVector& a, InVector& b) { a.Swap(b); }
STL_VECTOR_COMPATIBILITY(InVector<T>)
@ -301,6 +303,8 @@ public:
void Swap(InArray& b) { iv.Swap(b.iv); }
friend void Swap(InArray& a, InArray& b) { a.Swap(b); }
STL_VECTOR_COMPATIBILITY(InArray<T>)
#ifdef _DEBUG
@ -372,6 +376,62 @@ public:
const T& operator[](int i) const { Iterator h = *this; h += i; return *h; }
};
template <class T, class Less = StdLess<T> >
class SortedIndex : MoveableAndDeepCopyOption< SortedIndex<T, Less> >{
InVector<T> iv;
Less less;
public:
int Add(const T& x) { return iv.InsertUpperBound(x, less); }
int FindAdd(const T& key);
SortedIndex& operator<<(const T& x) { Add(x); return *this; }
int FindLowerBound(const T& x) const { return iv.FindLowerBound(x, less); }
int FindUpperBound(const T& x) const { return iv.FindUpperBound(x, less); }
int Find(const T& x) const { return iv.Find(x, less); }
int FindNext(int i) const;
int FindLast(const T& x) const;
int FindPrev(int i) const;
void Remove(int i) { iv.Remove(i); }
void Remove(int i, int count) { iv.Remove(i, count); }
int RemoveKey(const T& x);
const T& operator[](int i) const { return iv[i]; }
int GetCount() const { return iv.GetCount(); }
bool IsEmpty() const { return iv.IsEmpty(); }
void Clear() { iv.Clear(); }
void Trim(int n) { return iv.Trim(n); }
void Drop(int n = 1) { iv.Drop(n); }
const T& Top() const { return iv.Top(); }
void Shrink() { iv.Shrink(); }
typedef typename InVector<T>::Iterator Iterator;
typedef typename InVector<T>::ConstIterator ConstIterator;
typedef T ValueType;
ConstIterator Begin() const { return iv.Begin(); }
ConstIterator End() const { return iv.End(); }
ConstIterator GetIter(int pos) const { return iv.GetIter(pos); }
Iterator Begin() { return iv.Begin(); }
Iterator End() { return iv.End(); }
Iterator GetIter(int pos) { return iv.GetIter(pos); }
SortedIndex() {}
SortedIndex(const SortedIndex& s, int) : iv(s.iv, 1) {}
void Swap(SortedIndex& a) { iv.Swap(a.iv); }
friend void Swap(SortedIndex& a, SortedIndex& b){ a.Swap(b); }
STL_VECTOR_COMPATIBILITY(SortedIndex<T _cm_ Less>)
};
#define LLOG(x) // DLOG(x)
#include "InVector.hpp"
#undef LLOG

View file

@ -720,3 +720,40 @@ InArray<T>::InArray(const InArray& v, int)
while(n--)
*it++ = new T(*s++);
}
template <class T, class Less>
int SortedIndex<T, Less>::FindAdd(const T& key)
{
int i = FindLowerBound(key);
if(!less(key, iv[i]))
return i;
return Add(key);
}
template <class T, class Less>
int SortedIndex<T, Less>::FindNext(int i) const
{
return i + 1 < iv.GetCount() && !less(iv[i], iv[i + 1]) ? i + 1 : -1;
}
template <class T, class Less>
int SortedIndex<T, Less>::FindLast(const T& x) const
{
int i = iv.FindUpperBound(x, less);
return i > 0 && !less(iv[i - 1], x) ? i - 1 : -1;
}
template <class T, class Less>
int SortedIndex<T, Less>::FindPrev(int i) const
{
return i > 0 && !less(iv[i - 1], iv[i]) ? i - 1 : -1;
}
template <class T, class Less>
int SortedIndex<T, Less>::RemoveKey(const T& x)
{
int l = FindLowerBound(x);
int count = FindUpperBound(x) - l;
Remove(l, count);
return count;
}