1 #ifndef _snl_fei_ArrayUtils_hpp_
2 #define _snl_fei_ArrayUtils_hpp_
36 if (list[0] != item) {
45 unsigned end = len - 1;
47 while(end - start > 1) {
48 unsigned mid = (start + end) >> 1;
49 if (list[mid] < item) start = mid;
53 if (list[end] == item)
return(end);
54 if (list[start] == item)
return(start);
68 for (i=1; i < len; i++) {
70 companion = companions[i];
72 while ((j > 0) && (array[j-1] > index))
74 array[j] = array[j-1];
75 companions[j] = companions[j-1];
79 companions[j] = companion;
94 if (len < 1)
return 0;
97 unsigned end = len - 1;
99 while(end - start > 1) {
100 unsigned mid = (start + end) >> 1;
101 if (list[mid] < item) start = mid;
105 if (list[end] < item) {
109 if (list[start] < item) {
142 if (list[0] < item) {
146 if (item < list[0]) {
156 unsigned end = len - 1;
158 while(end - start > 1) {
159 unsigned mid = (start + end) >> 1;
160 if (list[mid] < item) start = mid;
164 if (list[end] < item) {
169 if (item < list[end]) {
170 if (list[start] < item) {
175 if (item < list[start]) {
191 int binarySearch(
const T& item,
const std::vector<T>& list,
int& insertPoint)
193 if (list.size() == 0) {
197 return(
binarySearch(item, &list[0], list.size(), insertPoint) );
205 if (list.size() == 0)
return(-1);
222 int start,
int end,
int& insertPoint)
224 int length = end - start + 1;
232 if (list[start] < item) {
233 insertPoint = start+1;
236 if (item < list[start]) {
245 unsigned ustart = start;
248 while(uend - ustart > 1) {
249 unsigned mid = (ustart + uend) >> 1;
250 if (list[mid] < item) ustart = mid;
255 if (list[uend] < item) {
256 insertPoint = uend+1;
260 if (item < list[uend]) {
261 if (list[ustart] < item) {
266 if (item < list[ustart]) {
267 insertPoint = ustart;
292 const T* list,
int listLength)
295 if (numItems < 1 || listLength < 1) {
296 if (listLength < 1) {
297 for(i=0; i<numItems; ++i) offsets[i] = -1;
302 int end = listLength -1;
303 int insertPoint = -1;
304 for(i=0; i<numItems; ++i) {
305 tmp =
binarySearch(items[i], list, listLength, start, end, insertPoint);
306 start = tmp > -1 ? tmp : insertPoint;
320 typename std::vector<T>::iterator iter =
321 std::lower_bound(list.begin(), list.end(), item);
323 if (iter == list.end() || *iter != item) {
324 iter = list.insert(iter, item);
325 return( iter - list.begin() );
336 int i, insertPoint = -1;
339 if (len >= allocLen) {
341 T* newlist =
new T[allocLen];
342 for(i=0; i<insertPoint; ++i) newlist[i] = list[i];
343 newlist[insertPoint] = item;
344 for(i=insertPoint; i<len; ++i) newlist[i+1] = list[i];
349 for(i=len; i>insertPoint; --i) {
352 list[insertPoint] = item;
365 int& usedLength,
int& allocatedLength,
366 int allocChunkSize=200)
368 if (offset < 0 || offset > usedLength) {
372 if (usedLength < allocatedLength) {
373 for(
int i=usedLength; i>offset; --i) {
381 T* newlist =
new T[allocatedLength+allocChunkSize];
383 allocatedLength += allocChunkSize;
385 for(i=0; i<offset; ++i) {
386 newlist[i] = list[i];
389 newlist[offset] = item;
391 for(i=offset+1; i<=usedLength; ++i) {
392 newlist[i] = list[i-1];
407 for(
int i=0; i<len; ++i) {
408 if (list[i] == item) {
417 #endif // _snl_fei_ArrayUtils_hpp_
int searchList(const T &item, const T *list, int len)
void insertion_sort_with_companions(int len, int *array, T *companions)
int sortedListInsert(const T &item, std::vector< T > &list)
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 binarySearch(const T &item, const T *list, int len)