1 #ifndef _fei_ArrayUtils_hpp_
2 #define _fei_ArrayUtils_hpp_
12 #include "fei_fwd.hpp"
30 if (len < 1)
return 0;
33 unsigned end = len - 1;
35 while(end - start > 23) {
36 unsigned mid = (start + end) >> 1;
37 if (list[mid] < item) start = mid;
41 while (start+7<=end ) {
42 if ( item <= list[start+0] )
return start+0;
43 if ( item <= list[start+1] )
return start+1;
44 if ( item <= list[start+2] )
return start+2;
45 if ( item <= list[start+3] )
return start+3;
46 if ( item <= list[start+4] )
return start+4;
47 if ( item <= list[start+5] )
return start+5;
48 if ( item <= list[start+6] )
return start+6;
49 if ( item <= list[start+7] )
return start+7;
53 while (start<=end && item > list[start] )
73 if ( result < len && item == list[result] )
87 for (i=1; i < len; i++) {
89 companion = companions[i];
91 while ((j > 0) && (array[j-1] > index))
93 array[j] = array[j-1];
94 companions[j] = companions[j-1];
98 companions[j] = companion;
123 if ( insertPoint < len && item == list[insertPoint] )
131 inline int binarySearch(
const T& item,
const std::vector<T>& list,
int& insertPoint)
133 if (list.size() == 0) {
137 return(
binarySearch(item, &list[0], list.size(), insertPoint) );
143 inline int binarySearch(
const T& item,
const std::vector<T>& list)
145 if (list.size() == 0)
return(-1);
161 inline int binarySearch(
const T& item,
const T* list,
int ,
162 int start,
int end,
int& insertPoint)
164 int result =
binarySearch(item,list+start,end-start+1,insertPoint);
182 inline int binarySearch(
int numItems,
const T* items,
int* offsets,
183 const T* list,
int listLength)
186 if (numItems < 1 || listLength < 1) {
187 if (listLength < 1) {
188 for(i=0; i<numItems; ++i) offsets[i] = -1;
193 int end = listLength -1;
194 int insertPoint = -1;
195 for(i=0; i<numItems; ++i) {
196 tmp =
binarySearch(items[i], list, listLength, start, end, insertPoint);
197 start = tmp > -1 ? tmp : insertPoint;
211 typename std::vector<T>::iterator iter =
212 std::lower_bound(list.begin(), list.end(), item);
214 if (iter == list.end() || *iter != item) {
215 iter = list.insert(iter, item);
216 return( iter - list.begin() );
225 inline int sortedListInsert(
const T& item, T*& list,
int& len,
int& allocLen)
227 int i, insertPoint = -1;
230 if (len >= allocLen) {
232 T* newlist =
new T[allocLen];
233 for(i=0; i<insertPoint; ++i) newlist[i] = list[i];
234 newlist[insertPoint] = item;
235 for(i=insertPoint; i<len; ++i) newlist[i+1] = list[i];
240 for(i=len; i>insertPoint; --i) {
243 list[insertPoint] = item;
255 inline int listInsert(
const T& item,
int offset, T*& list,
256 int& usedLength,
int& allocatedLength,
257 int allocChunkSize=200)
259 if (offset < 0 || offset > usedLength) {
263 if (usedLength < allocatedLength) {
264 for(
int i=usedLength; i>offset; --i) {
272 T* newlist =
new T[allocatedLength+allocChunkSize];
274 allocatedLength += allocChunkSize;
276 for(i=0; i<offset; ++i) {
277 newlist[i] = list[i];
280 newlist[offset] = item;
282 for(i=offset+1; i<=usedLength; ++i) {
283 newlist[i] = list[i-1];
296 inline int searchList(
const T& item,
const T* list,
int len)
298 for(
int i=0; i<len; ++i) {
299 if (list[i] == item) {
308 #endif // _fei_ArrayUtils_hpp_
int sortedListInsert(const T &item, std::vector< T > &list)
void insertion_sort_with_companions(int len, int *array, T *companions)
int binarySearch(const T &item, const T *list, int len)
int lowerBound(const T &item, const T *list, int len)
int listInsert(const T &item, int offset, T *&list, int &usedLength, int &allocatedLength, int allocChunkSize=200)
int searchList(const T &item, const T *list, int len)