1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
#ifndef DEF_serviceFuns
#define DEF_serviceFuns
#include "IncludeDefine.h"
template <class T>
T sum1D(T* a, uint N) {
T s=0;
for (uint ii=0;ii<N;ii++) s+=a[ii];
return s;
};
template <class numT> int funCompareNumbers (const void *a, const void *b) {
numT va= *((numT*) a);
numT vb= *((numT*) b);
if (va>vb) {
return 1;
} else if (va==vb) {
return 0;
} else {
return -1;
};
};
template <class numT> int funCompareNumbersReverse (const void *a, const void *b) {
numT va= *((numT*) a);
numT vb= *((numT*) b);
if (va>vb) {
return -1;
} else if (va==vb) {
return 0;
} else {
return 1;
};
};
template <class numT, int Shift> int funCompareNumbersReverseShift (const void *a, const void *b) {
numT va= *((numT*) a + Shift);
numT vb= *((numT*) b + Shift);
if (va>vb) {
return -1;
} else if (va==vb) {
return 0;
} else {
return 1;
};
};
inline int funCompareUint1 (const void *a, const void *b) {
uint va= *((uint*) a);
uint vb= *((uint*) b);
if (va>vb) {
return 1;
} else if (va==vb) {
return 0;
} else {
return -1;
};
};
inline int funCompareUint2 (const void *a, const void *b) {
uint va= *((uint*) a);
uint vb= *((uint*) b);
uint va1=*(((uint*) a)+1);
uint vb1=*(((uint*) b)+1);
if (va>vb) {
return 1;
} else if (va==vb && va1>vb1) {
return 1;
} else if (va==vb && va1==vb1) {
return 0;
} else {
return -1;
};
};
template <class arrayType, int arraySize>
inline int funCompareArrays (const void *a, const void *b) {
arrayType* va= (arrayType*) a;
arrayType* vb= (arrayType*) b;
for (int ii=0;ii<arraySize;ii++) {
if (va[ii]>vb[ii]) {
return 1;
} else if (va[ii]<vb[ii]) {
return -1;
};
};
return 0;
};
template <class arrayType, int arraySize>
inline int funCompareArraysReverse (const void *a, const void *b) {
arrayType* va= (arrayType*) a;
arrayType* vb= (arrayType*) b;
for (int ii=0;ii<arraySize;ii++) {
if (va[ii]>vb[ii]) {
return -1;
} else if (va[ii]<vb[ii]) {
return 1;
};
};
return 0;
};
template <class arrayType, int arraySize, int Shift>
inline int funCompareArraysShift (const void *a, const void *b) {
arrayType* va= ((arrayType*) a) + Shift;
arrayType* vb= ((arrayType*) b) + Shift;
for (int ii=0;ii<arraySize;ii++) {
if (va[ii]>vb[ii]) {
return 1;
} else if (va[ii]<vb[ii]) {
return -1;
};
};
return 0;
};
template <class Type>
inline int funCompareTypeSecondFirst (const void *a, const void *b) {
Type va= *( ((Type*) a) + 1 );
Type vb= *( ((Type*) b) + 1 );
Type va1= *( ((Type*) a) + 0 );
Type vb1= *( ((Type*) b) + 0 );
if (va>vb) {
return 1;
} else if (va==vb && va1>vb1) {
return 1;
} else if (va==vb && va1==vb1) {
return 0;
} else {
return -1;
};
};
template <class Type, int Shift>
inline int funCompareTypeShift (const void *a, const void *b) {
Type va= *( ((Type*) a)+Shift );
Type vb= *( ((Type*) b)+Shift );
if (va>vb) {
return 1;
} else if (va==vb) {
return 0;
} else {
return -1;
};
};
inline int splitString(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
int maxL=0;
elems.clear();
while (std::getline(ss, item, delim)) {
maxL=max(maxL, (int)item.size());
elems.push_back(item);
};
return maxL;//returns max string size
};
/*
* std::vector<std::string> splitString(const std::string &s, char delim) {
std::stringstream ss(s);
std::string item;
std::vector<std::string> elems;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
};
return elems;
};
*/
template <class argType>
inline uint32 binarySearch1(argType x, argType *X, uint32 N) {
//binary search in the sorted list
//check the boundaries first
if (x>X[N-1] || x<X[0]) return -1;
uint32 i1=0, i2=N-1, i3=N/2;
while (i2>i1+1) {//binary search
i3=(i1+i2)/2;
if (X[i3]>x) {
i2=i3;
} else {
i1=i3;
};
};
while (i1<N-1 && x==X[i1+1]) ++i1; //go forward to check for equals
return i1;
};
template <class argType>
inline bool binarySearch_leLeft(argType x, argType *X, uint32 N, uint32 & i1) {
//binary search in the sorted list
//Retrun false if x is outside of X[0], X[N-1]
//Return i1 = index of the element which is <= x, the leftmost element if equal.
if (x>X[N-1] || x<X[0])
return false;
i1=0;
uint32 i2=N-1, i3=N/2;
while (i2>i1+1) {//binary search
i3=(i1+i2)/2;
if (X[i3]>x) {
i2=i3;
} else {// X[i3] <= x
i1=i3;
};
};
while (i1>0 && x==X[i1-1])
--i1; //go left to check for equals
return true;
};
template <class argType>
inline int32 binarySearch1a(argType x, argType *X, int32 N) {
//binary search in the sorted list
//check the boundaries first
if (x>X[N-1]) {
return N-1;
} else if (x<X[0]) {
return -1;
};
int32 i1=0, i2=N-1, i3=N/2;
while (i2>i1+1) {//binary search
i3=(i1+i2)/2;
if (X[i3]>x) {
i2=i3;
} else {
i1=i3;
};
};
while (i1<N-1 && x==X[i1+1]) ++i1; //go forward to check for equals
return i1;
};
template <class argType>
inline int32 binarySearch1b(argType x, argType *X, int32 N)
{
//binary search in the sorted list
//check the boundaries first
//1b returns the first X element that is >= x
//X are all distinct
//if x>X[N-1], -1 is returned
if (x>X[N-1]) {
return -1;
} else if (x<=X[0]) {
return 0;
};
int32 i1=0, i2=N-1, i3=N/2;
while (i2>i1+1) {//binary search
i3=(i1+i2)/2;
if (X[i3]>=x) {
i2=i3;
} else {
i1=i3;
};
};
return i2;
};
template <class argType>
inline int64 binarySearchExact(argType x, argType *X, uint64 N) {
//binary search in the sorted list
//check the boundaries first
//returns -1 if no match found
//if X are not all distinct, no guarantee which element is returned
if (x>X[N-1] || x<X[0])
return -1;
int32 i1=0, i2=N-1, i3=N/2;
while (i2>i1+1) {//binary search
i3=(i1+i2)/2;
if (X[i3]>=x) {
i2=i3;
} else {
i1=i3;
};
};
if (x==X[i2]) {
return i2;
} else if (x==X[i1]) {
return i1;
} else {
return -1;
};
};
// template <class argType>
// inline uint64 binarySearchStride(argType *X, uint64 N, artgType x, iStart, stride )
// {
// //binary search in the sorted list wit stride
// //1b returns the first X element that is >= x
// //X are all distinct
// //if x>X[N-1], -1 is returned
//
// if (x>X[N-1]) {
// return -1;
// } else if (x<=X[0]) {
// return 0;
// };
//
// int32 i1=0, i2=N-1, i3=N/2;
// while (i2>i1+1) {//binary search
// i3=(i1+i2)/2;
// if (X[i3]>=x) {
// i2=i3;
// } else {
// i1=i3;
// };
// };
//
// return i2;
// };
#endif
|