Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef SORTEDVECTOR_H
00028 #define SORTEDVECTOR_H
00029
00030 #include <QtCore>
00031
00032 #include "QGpCoreToolsDLLExport.h"
00033
00034 namespace QGpCoreTools {
00035
00036 template <class Key, class T>
00037 class QGPCORETOOLS_EXPORT SortedVector : private QVector<T *>
00038 {
00039 public:
00040 SortedVector() {_n2=1;}
00041
00042 bool isEmpty() const {return QVector<T *>::isEmpty();}
00043 int count() const {return QVector<T *>::count();}
00044 T * at(int index) const {return QVector<T *>::at(index);}
00045
00046 void insert(T * o);
00047 void remove(int index);
00048 void clear();
00049 void reserve(int n) {QVector<T *>::reserve(n);}
00050
00051 void append(T * o) {QVector<T *>::append(o);}
00052 void sort() {qSort(QVector<T *>::begin(), QVector<T *>::end(), lessThan);}
00053
00054 bool contains(const Key& k) const {return indexOf(k)>-1;}
00055 int indexOf(const Key& k) const;
00056 const QVector<T *>& vector() const {return *this;}
00057 private:
00058 static bool lessThan(const T * o1, const T * o2) {return o1->key()<o2->key();}
00059 int indexAfter(const Key& k) const;
00060
00061 int _n2;
00062 };
00063
00064 template<class Key, class T>
00065 int SortedVector<Key, T>::indexOf(const Key& k) const
00066 {
00067 if(count()<12) {
00068
00069
00070
00071
00072 for(int i=count()-1; i>=0; i--) {
00073 if(k==at(i)->key()) return i;
00074 }
00075 return -1;
00076 } else {
00077
00078
00079
00080
00081
00082
00083
00084
00085 if(k<at(0)->key()) return -1;
00086 if(k==at(0)->key()) return 0;
00087 int lasti=count()-1;
00088 if(k>at(lasti)->key()) return -1;
00089 int i=_n2;
00090 int step2=_n2 >> 1;
00091 while(step2>0) {
00092 if(i>lasti) {
00093 i-=step2;
00094 } else {
00095 const Key& ki=at(i)->key();
00096 if(k<ki) {
00097 i-=step2;
00098 } else if(k>ki) {
00099 i+=step2;
00100 } else {
00101 return i;
00102 }
00103 }
00104 step2=step2 >> 1;
00105 }
00106 if(k==at(i)->key()) {
00107 return i;
00108 } else {
00109 return -1;
00110 }
00111 }
00112 }
00113
00114 template<class Key, class T>
00115 int SortedVector<Key, T>::indexAfter(const Key& k) const
00116 {
00117 if(isEmpty() || k<at(0)->key()) return 0;
00118 int lasti=count()-1;
00119 if(k>=at(lasti)->key()) return count();
00120 int i=_n2;
00121 int step2=_n2 >> 1;
00122 while(step2>0) {
00123 if(i>lasti) {
00124 i-=step2;
00125 } else if(k<=at(i)->key()) {
00126 if(k>at(i-1)->key()) break;
00127 i-=step2;
00128 } else {
00129 i+=step2;
00130 }
00131 step2=step2 >> 1;
00132 }
00133 return i;
00134 }
00135
00136 template<class Key, class T>
00137 void SortedVector<Key, T>::insert(T * o)
00138 {
00139 int i=indexAfter(o->key());
00140 QVector<T *>::insert(i, o);
00141 while(_n2<count()) _n2=_n2 << 1;
00142 }
00143
00144 template<class Key, class T>
00145 void SortedVector<Key, T>::remove(int index)
00146 {
00147 QVector<T *>::remove(index);
00148 int n2=_n2 >> 1;
00149 if(n2>0 && n2>=count()) {
00150 _n2=n2;
00151 }
00152 }
00153
00154 template<class Key, class T>
00155 void SortedVector<Key, T>::clear()
00156 {
00157 QVector<T *>::clear();
00158 }
00159
00160 }
00161
00162 #endif // SORTEDVECTOR_H