printsorted() implementation

This commit is contained in:
2025-03-04 16:06:16 +01:00
parent ecdd2fefa5
commit ad1bee99a5
4 changed files with 91 additions and 1 deletions

30
vec.h
View File

@@ -491,6 +491,7 @@ public:
//! sort by default in ascending order and return the parity of corresponding permutation resulting to this order
int sort(int direction = 0, int from = 0, int to = -1, int *perm = NULL, bool stable=false);
int sort(int direction, NRPerm<int> &perm, bool stable=false);
void printsorted(std::ostream &s, int direction=1, bool absvalue=false, bool stable=false, typename LA_traits<T>::normtype thr=0, int topmax=0, const NRMat<int> *indexmap=NULL) const;
//! apply given function to each element
NRVec& call_on_me(T (*_F)(const T &) ){
@@ -583,6 +584,35 @@ return r;
}
template<typename T>
void NRVec<T>::printsorted(std::ostream &s, int direction, bool absvalue, bool stable, typename LA_traits<T>::normtype thr, int topmax, const NRMat<int> *indexmap) const
{
NRPerm<int> perm(nn); perm.identity();
NRVec_from1<T> tmp;
NRVec_from1<typename LA_traits<T>::normtype> tmp2;
if(absvalue)
{
tmp2.resize(nn);
for(int i=1; i<=nn; ++i) tmp2[i] = MYABS((*this)[i-1]);
tmp2.sort(direction,perm,stable);
}
else
{
tmp= *this;
tmp.sort(direction,perm,stable);
}
int ii=1;
for(int i=1; i<=nn; ++i)
{
if(topmax && ii>topmax) break;
if(thr!=0 && (absvalue?tmp2[i]:MYABS(tmp[i]))<thr) continue;
if(indexmap) s<<(*indexmap)[perm[i]-1][0]<<" "<<(*indexmap)[perm[i]-1][1]; else s<<perm[i]-1;
s<<' ' << (*this)[perm[i]-1]<<std::endl;
++ii;
}
}
/***************************************************************************//**
* indexing operator giving the element at given position with range checking in
* the DEBUG mode