void TRL::indexx(vector<double> &arr, vector<int> &indx) { const int M=7,NSTACK=50; int i,indxt,ir,j,k,jstack=-1,l=0; double a; vector<int> istack(NSTACK); int n=arr.size(); ir=n-1; indx.resize(n); for (j=0;j<n;j++) indx[j]=j; for (;;) { if (ir-l < M) { for (j=l+1;j<=ir;j++) { indxt=indx[j]; a=arr[indxt]; for (i=j-1;i>=l;i--) { if (arr[indx[i]] <= a) break; indx[i+1]=indx[i]; } indx[i+1]=indxt; } if (jstack < 0) break; ir=istack[jstack--]; l=istack[jstack--]; } else { k=(l+ir) >> 1; SWAP(indx[k],indx[l+1]); if (arr[indx[l]] > arr[indx[ir]]) { SWAP(indx[l],indx[ir]); } if (arr[indx[l+1]] > arr[indx[ir]]) { SWAP(indx[l+1],indx[ir]); } if (arr[indx[l]] > arr[indx[l+1]]) { SWAP(indx[l],indx[l+1]); } i=l+1; j=ir; indxt=indx[l+1]; a=arr[indxt]; for (;;) { do i++; while (arr[indx[i]] < a); do j--; while (arr[indx[j]] > a); if (j < i) break; SWAP(indx[i],indx[j]); } indx[l+1]=indx[j]; indx[j]=indxt; jstack += 2; if (jstack >= NSTACK) cerr<<"NSTACK too small in indexx."; if (ir-i+1 >= j-l) { istack[jstack]=ir; istack[jstack-1]=i; ir=j-1; } else { istack[jstack]=j-1; istack[jstack-1]=l; l=i; } } } }
ItemStack read_item(lua_State* L, int index,Server* srv) { if(index < 0) index = lua_gettop(L) + 1 + index; if(lua_isnil(L, index)) { return ItemStack(); } else if(lua_isuserdata(L, index)) { // Convert from LuaItemStack LuaItemStack *o = LuaItemStack::checkobject(L, index); return o->getItem(); } else if(lua_isstring(L, index)) { // Convert from itemstring std::string itemstring = lua_tostring(L, index); IItemDefManager *idef = srv->idef(); try { ItemStack item; item.deSerialize(itemstring, idef); return item; } catch(SerializationError &e) { warningstream<<"unable to create item from itemstring" <<": "<<itemstring<<std::endl; return ItemStack(); } } else if(lua_istable(L, index)) { // Convert from table IItemDefManager *idef = srv->idef(); std::string name = getstringfield_default(L, index, "name", ""); int count = getintfield_default(L, index, "count", 1); int wear = getintfield_default(L, index, "wear", 0); ItemStack istack(name, count, wear, idef); lua_getfield(L, index, "metadata"); // Support old metadata format by checking type int fieldstable = lua_gettop(L); if (lua_istable(L, fieldstable)) { lua_pushnil(L); while (lua_next(L, fieldstable) != 0) { // key at index -2 and value at index -1 std::string key = lua_tostring(L, -2); size_t value_len; const char *value_cs = lua_tolstring(L, -1, &value_len); std::string value(value_cs, value_len); istack.metadata.setString(name, value); lua_pop(L, 1); // removes value, keeps key for next iteration } } else { // BACKWARDS COMPATIBLITY std::string value = getstringfield_default(L, index, "metadata", ""); istack.metadata.setString("", value); } lua_getfield(L, index, "meta"); fieldstable = lua_gettop(L); if (lua_istable(L, fieldstable)) { lua_pushnil(L); while (lua_next(L, fieldstable) != 0) { // key at index -2 and value at index -1 std::string key = lua_tostring(L, -2); size_t value_len; const char *value_cs = lua_tolstring(L, -1, &value_len); std::string value(value_cs, value_len); istack.metadata.setString(name, value); lua_pop(L, 1); // removes value, keeps key for next iteration } } return istack; } else { throw LuaError("Expecting itemstack, itemstring, table or nil"); } }
void NR::sort2(Vec_IO_DP &arr, Vec_IO_DP &brr) { const int M=7,NSTACK=50; int i,ir,j,k,jstack=-1,l=0; DP a,b; Vec_INT istack(NSTACK); int n=arr.size(); ir=n-1; for (;;) { if (ir-l < M) { for (j=l+1;j<=ir;j++) { a=arr[j]; b=brr[j]; for (i=j-1;i>=l;i--) { if (arr[i] <= a) break; arr[i+1]=arr[i]; brr[i+1]=brr[i]; } arr[i+1]=a; brr[i+1]=b; } if (jstack < 0) break; ir=istack[jstack--]; l=istack[jstack--]; } else { k=(l+ir) >> 1; SWAP(arr[k],arr[l+1]); SWAP(brr[k],brr[l+1]); if (arr[l] > arr[ir]) { SWAP(arr[l],arr[ir]); SWAP(brr[l],brr[ir]); } if (arr[l+1] > arr[ir]) { SWAP(arr[l+1],arr[ir]); SWAP(brr[l+1],brr[ir]); } if (arr[l] > arr[l+1]) { SWAP(arr[l],arr[l+1]); SWAP(brr[l],brr[l+1]); } i=l+1; j=ir; a=arr[l+1]; b=brr[l+1]; for (;;) { do i++; while (arr[i] < a); do j--; while (arr[j] > a); if (j < i) break; SWAP(arr[i],arr[j]); SWAP(brr[i],brr[j]); } arr[l+1]=arr[j]; arr[j]=a; brr[l+1]=brr[j]; brr[j]=b; jstack += 2; if (jstack >= NSTACK) nrerror("NSTACK too small in sort2."); if (ir-i+1 >= j-l) { istack[jstack]=ir; istack[jstack-1]=i; ir=j-1; } else { istack[jstack]=j-1; istack[jstack-1]=l; l=i; } } } }
void Vector<T>::sortIndex(Vector<int>& index, int M) const { const int Nstack=50 ; int i,ir,j,k,l,indext ; Vector<int> istack(Nstack) ; int jstack=0; T a ; ir = this->sze-1 ; l = 0 ; index.resize(this->sze) ; for(i=0; i<index.n(); ++i) index[i] = i ; while(1) { if(ir-l<M) { // perform an insertion sort when the array is small enough for(j=l+1; j<=ir; ++j) { indext = index[j] ; a = this->x[indext] ; for(i=j-1; i>=0; --i) { if(this->x[index[i]] <= a) break ; index[i+1] = index[i] ; } index[i+1] = indext ; } if(jstack==0) break ; ir=istack[--jstack] ; l=istack[--jstack] ; } else { k=(l+ir) >> 1 ; swap(index[k],index[l+1]) ; if(this->x[index[l+1]] > this->x[index[ir]]) { swap(index[l+1],index[ir]) ; } if(this->x[index[l]]> this->x[index[ir]]) { swap(index[l],index[ir]) ; } if(this->x[index[l+1]] > this->x[index[l]]) { swap(index[l+1],index[l]) ; } i=l+1 ; j=ir ; indext = index[l] ; a=this->x[indext] ; while(1) { while(this->x[index[i]] < a) { ++i ; } while(this->x[index[j]] > a) { --j ; } if(j<i) break ; if(this->x[index[i]] == this->x[index[j]]) break ; swap(index[i],index[j]) ; } index[l] = index[j] ; index[j] = indext ; jstack += 2 ; if(jstack>=Nstack) { istack.resize(istack.n()+Nstack) ; // increase the vector size } if(ir-i+1 >= j-l) { istack[jstack-1]=ir ; istack[jstack-2] = i ; ir=j-1 ; } else { istack[jstack-1] = j-1 ; istack[jstack-2] = l ; l=i ; } } } }
void Vector<T>::qSort(int M) { const int Nstack=50 ; int i,ir,j,k,l ; Vector<int> istack(Nstack) ; int jstack=0; T a ; T *v1,*v2 ; ir = this->sze-1 ; l = 0 ; while(1) { if(ir-l<M) { // perform an insertion sort when the array is small enough v1 = &this->x[l] ; for(j=l+1; j<=ir; ++j) { a = *(++v1) ; v2 = v1 ; --v2 ; for(i=j-1; i>=0; --i) { if(*v2 <= a) break ; *(v2+1) = *v2 ; --v2 ; } ++v2 ; *(v2) = a ; } if(jstack==0) break ; ir=istack[--jstack] ; l=istack[--jstack] ; } else { k=(l+ir) >> 1 ; swap(this->x[k],this->x[l+1]) ; if(this->x[l+1] > this->x[ir]) { swap(this->x[l+1],this->x[ir]) ; } if(this->x[l]> this->x[ir]) { swap(this->x[l],this->x[ir]) ; } if(this->x[l+1] > this->x[l]) { swap(this->x[l+1],this->x[l]) ; } i=l+1 ; j=ir ; a=this->x[l] ; v1 = &this->x[i] ; v2 = &this->x[j] ; while(1) { while(*v1 < a) { ++i ; ++v1 ; } while(*v2 > a) { --j ; --v2 ; } if(j<i) break ; if(*v1 == *v2) // both are equal to a... break ; swap(this->x[i],this->x[j]) ; } this->x[l] = this->x[j] ; this->x[j] = a ; jstack += 2 ; if(jstack>=Nstack) { istack.resize(istack.n()+Nstack) ; // increase the vector size } if(ir-i+1 >= j-l) { istack[jstack-1]=ir ; istack[jstack-2] = i ; ir=j-1 ; } else { istack[jstack-1] = j-1 ; istack[jstack-2] = l ; l=i ; } } } }
void NR::indexx(Vec_I_INT &arr, Vec_O_INT &indx) { const int M=7,NSTACK=50; int i,indxt,ir,j,k,jstack=-1,l=0; int a; Vec_INT istack(NSTACK); int n=arr.size(); ir=n-1; for (j=0;j<n;j++) indx[j]=j; for (;;) { if (ir-l < M) { for (j=l+1;j<=ir;j++) { indxt=indx[j]; a=arr[indxt]; for (i=j-1;i>=l;i--) { if (arr[indx[i]] <= a) break; indx[i+1]=indx[i]; } indx[i+1]=indxt; } if (jstack < 0) break; ir=istack[jstack--]; l=istack[jstack--]; } else { k=(l+ir) >> 1; SWAP(indx[k],indx[l+1]); if (arr[indx[l]] > arr[indx[ir]]) { SWAP(indx[l],indx[ir]); } if (arr[indx[l+1]] > arr[indx[ir]]) { SWAP(indx[l+1],indx[ir]); } if (arr[indx[l]] > arr[indx[l+1]]) { SWAP(indx[l],indx[l+1]); } i=l+1; j=ir; indxt=indx[l+1]; a=arr[indxt]; for (;;) { do i++; while (arr[indx[i]] < a); do j--; while (arr[indx[j]] > a); if (j < i) break; SWAP(indx[i],indx[j]); } indx[l+1]=indx[j]; indx[j]=indxt; jstack += 2; if (jstack >= NSTACK) nrerror("NSTACK too small in indexx."); if (ir-i+1 >= j-l) { istack[jstack]=ir; istack[jstack-1]=i; ir=j-1; } else { istack[jstack]=j-1; istack[jstack-1]=l; l=i; } } } }
/** Quicksort. \param v Vector of doubles to be sorted \param NSTACK Lenth of intermediate storage vector. Default is NSTACK = 60. \return dvector object containing the input vector sorted in ascending order. \n\n The implementation of this algorithm was inspired by "Numerical Recipes in C", 2nd edition, Press, Teukolsky, Vetterling, Flannery, chapter 8 */ dvector sort(const dvector& v, int NSTACK) { const int M=7; const int FM=7875; const int FA=211; const int FC=1663; int n=v.size(); dvector arr(v.indexmin(),v.indexmax()); arr=v; arr.shift(1); int l=1,jstack=0,j,ir,iq,i; ivector istack(1,NSTACK+1); long int fx=0L; double a; ir=n; for (;;) { if (ir-l < M) { for (j=l+1;j<=ir;j++) { a=arr[j]; for (i=j-1;i>0 && arr[i]>a;i--) arr[i+1]=arr[i]; arr[i+1]=a; } if (jstack == 0) { arr.shift(v.indexmin()); return arr; } ir=istack[jstack--]; l=istack[jstack--]; } else { i=l; j=ir; fx=(fx*FA+FC) % FM; iq=l+((ir-l+1)*fx)/FM; if (iq<=0) { iq=l+((ir-l+1.0)*fx)/FM; } a=arr[iq]; arr[iq]=arr[l]; for (;;) { while (j > 0 && a < arr[j]) j--; if (j <= i) { arr[i]=a; break; } arr[i++]=arr[j]; while (i <= n && a > arr[i] ) i++; if (j <= i) { arr[(i=j)]=a; break; } arr[j--]=arr[i]; } if (ir-i >= i-l) { istack[++jstack]=i+1; istack[++jstack]=ir; ir=i-1; } else { istack[++jstack]=l; istack[++jstack]=i-1; l=i+1; } if (jstack > NSTACK) { cerr << "Need to increase the stack in sort(const dvector&)\n"; ad_exit(1); } } } }
/** Quicksort. \param _v Vector to be sorted \param _index ivector on return containing the input order of the origian vector. \param NSTACK Lenth of intermediate storage vector. Default is NSTACK = 60. \return dvector object containing the input vector sorted in ascending order. \n\n The implementation of this algorithm was inspired by "Numerical Recipes in C", 2nd edition, Press, Teukolsky, Vetterling, Flannery, chapter 8 */ dvector sort(const dvector& _v, const ivector& _index, int NSTACK) { const int M=7; const int FM=7875; const int FA=211; const int FC=1663; int iii; ivector& index = (ivector&) _index; dvector& v = (dvector&) _v; if (v.size() != index.size()) { cerr << " Incompatible array sizes in vector v and ivector index\n" << " in dvector sort(const dvector& v, const ivector& index)\n"; ad_exit(1); } int n=v.size(); int One=1; int nne=v.indexmin(); index.fill_seqadd(nne,One); dvector arr(v.indexmin(),v.indexmax()); arr=v; arr.shift(1); index.shift(1); int l=1,jstack=0,j,ir,iq,i; ivector istack(1,NSTACK+1); long int fx=0L; double a; ir=n; int i1; double tmp; for (;;) { if (ir-l < M) { for (j=l+1;j<=ir;j++) { a=arr[j]; iii=index[j]; for (i=j-1;i>0;i--) { tmp=arr[i]; i1=i+1; if (tmp <=a) break; arr[i1]=tmp; index[i1]=index[i]; } arr[i+1]=a; index[i+1]=iii; } if (jstack == 0) { arr.shift(v.indexmin()); index.shift(v.indexmin()); return arr; } ir=istack[jstack--]; l=istack[jstack--]; } else { i=l; j=ir; fx=(fx*FA+FC) % FM; iq=l+((ir-l+1)*fx)/FM; if (iq<0) { iq=l+((ir-l+1.0)*fx)/FM; } a=arr[iq]; iii=index[iq]; arr[iq]=arr[l]; index[iq]=index[l]; for (;;) { while (j > 0 && a < arr[j]) j--; if (j <= i) { arr[i]=a; index[i]=iii; break; } arr[i]=arr[j]; index[i++]=index[j]; while (i <= n && a > arr[i]) i++; if (j <= i) { arr[(i=j)]=a; index[i]=iii; break; } arr[j]=arr[i]; index[j--]=index[i]; } if (ir-i >= i-l) { istack[++jstack]=i+1; istack[++jstack]=ir; ir=i-1; } else { istack[++jstack]=l; istack[++jstack]=i-1; l=i+1; } if (jstack > NSTACK) { cerr << "Need to increase the stack in sort(const dvector&)\n"; ad_exit(1); } } } }