Hi, thanks for your answer.
May be exist a assembler instruction to check iteratively in same range, and would be quicker (MMX?), but this may be would be mission of C++ to implement it for a standard library ("if_any") and for specialization for integer.
Reason is this because, a call from a kernel of a algorithm is to this function, and it is often. The algorithm' mission is to calculate a "minimal combination's of pih's", and this is part of a algorithm to found optimal algorithms for order elements by comparison. :)
I already try to minimize a call to this function, maintaining another array, which count how many place of F are less as 0.
greatings, Daniel
You're trying to do X, and you thought of solution Y. So you're asking about solution Y, without even mentioning X. The problem is, there might be a better solution, but we can't know that unless you describe what X is.
Even now it is not evident that solution Z is better as solution Y.
All depend of Y have a fast way or not, using processors capabilities.
If Y have a faster way, then Y would be better as Z, so i ask about this.
Even using solution Z is not vey much better as use solution without quick way.
Anyway i can give a code, from calling function. Explain everything would be very long.
int foundMinPihcInLine(int minPihc,int validLineI,int nextPihcsB)
{
int lineI = m_validLines[validLineI];
int lastP = m_lines[lineI];//point to last pihs,last pointers from this line
if (haveFree(F)) {
//so we can simply choice a minimal of F(x)
int minLPih = pasa;
int minLPihc = pasa;
int minLPihPlace = 0;
int size = m_freePihss[lineI].size();//freePihs.size();
for(int t=0;t<size;t++) {
int pih = m_freePihss[lineI][t];//freePihs[t];
int pihc = convertPihToPihc(pih,m_Fs[lineI]);
if (pihc<minLPihc) { minLPihc=pihc; minLPih=pih; minLPihPlace=t;}
}
if (minLPihc<minPihc) {
minPihc = minLPihc;
m_newValidLines.clear();
}
if (minLPihc<=minPihc) { //simply continue line
m_pihs.push_back(minLPih);//data place 1
m_pointers.push_back(lastP);//it is continuation from previous
ivec newF(m_Fs[lineI]);//ivec newF(F);
vector<int> newFreePihs(m_freePihss[lineI]);
newFreePihs[minLPihPlace] = m_freePihss[lineI].back();//freePihs.back();
newFreePihs.resize(size-1);
m_lines.push_back(m_pihs.size()-1);//line data 1
m_Fs.push_back(newF);//line data 2
m_freePihss.push_back(newFreePihs);//line data 3
}
return minPihc;
}
//we collect all pih's which result in a minimal pihc (minPihc)
ivec minPihs;
ivec pihPlaces;//places inside freePihs
int size = m_freePihss[lineI].size();//freePihs.size();
for(int t=0;t<size;t++) {
int pih = m_freePihss[lineI][t];//freePihs[t];
int minPihcPih = foundMinPihcForPih(pih,m_Fs[lineI],lineI);//similar to foundMinRec2
if (minPihcPih==minPihc) {
minPihs.push_back(pih);
pihPlaces.push_back(t);
}
elseif (minPihcPih<minPihc) {
minPihs.clear();
minPihs.push_back(pih);
minPihc = minPihcPih;
pihPlaces.clear();
pihPlaces.push_back(t);
m_newValidLines.clear();//we invalidate all previous lines, which give a smaller pihc
}
}
int xc = pairsP[minPihc].first;
int yc = pairsP[minPihc].second;
int lastFreePih = m_freePihss[lineI].back();//freePihs.back();
//save all optimal results :
for(int t=0;t<minPihs.size();t++) {
int pih = minPihs[t];
int line = m_lines.size();
m_newValidLines.push_back(line);
int pointTo = m_pihs.size();
m_pihs.push_back(pih);//data place 1
m_pointers.push_back(lastP);//data place 2
ivec newF(m_Fs[lineI]);//ivec newF(F);
newF[pairsP[pih].first] = xc;
newF[pairsP[pih].second] = yc;
//vector<int> newFreePihs(freePihs);
vector<int> newFreePihs(m_freePihss[lineI]);
newFreePihs[pihPlaces[t]] = lastFreePih;
newFreePihs.resize(size-1);
m_lines.push_back(pointTo);//line data 1
m_Fs.push_back(newF);//line data 2
m_freePihss.push_back(newFreePihs);//line data 3
}
return minPihc;
}
You aren't going to find a magic solution to doing an O(n) search any faster.
If you want to improve the speed, use a std::unordered_set to check for membership, and the std::vector for the sequence ordering. Notice the trade-off: use more memory for a faster membership predicate.
A std::unordered_set is quick for check something, but use more memory and also, if i need to copy a unordered_set, its more slowly. Sometimes i need to copy everything.
So, at this moment my solution for this is use another array of counters, which count how many places of F are below of 0. Actually code look like :
int foundMinPihcInLine(int minPihc,int validLineI,int nextPihcsB)
{
int lineI = m_validLines[validLineI];
int lastP = m_lines[lineI];//point to last pihs,last pointers from this line
if (m_FsFreeN[lineI]==0) {
//so we can simply choice a minimal of F(x)
int minLPih = pasa;
int minLPihc = pasa;
int minLPihPlace = 0;
int size = m_freePihss[lineI].size();//freePihs.size();
for(int t=0;t<size;t++) {
int pih = m_freePihss[lineI][t];//freePihs[t];
int pihc = convertPihToPihc(pih,m_Fs[lineI]);
if (pihc<minLPihc) { minLPihc=pihc; minLPih=pih; minLPihPlace=t;}
}
if (minLPihc<minPihc) {
minPihc = minLPihc;
m_newValidLines.clear();
}
if (minLPihc<=minPihc) { //simply continue line
m_pihs.push_back(minLPih);//data place 1
m_pointers.push_back(lastP);//it is continuation from previous
ivec newF(m_Fs[lineI]);//ivec newF(F);
vector<int> newFreePihs(m_freePihss[lineI]);
newFreePihs[minLPihPlace] = m_freePihss[lineI].back();//freePihs.back();
newFreePihs.resize(size-1);
m_lines.push_back(m_pihs.size()-1);//line data 1
m_Fs.push_back(newF);//line data 2
m_FsFreeN.push_back(0);
m_freePihss.push_back(newFreePihs);//line data 3
}
return minPihc;
}
//we collect all pih's which result in a minimal pihc (minPihc)
ivec minPihs;
ivec pihPlaces;//places inside freePihs
int size = m_freePihss[lineI].size();//freePihs.size();
for(int t=0;t<size;t++) {
int pih = m_freePihss[lineI][t];//freePihs[t];
int minPihcPih = foundMinPihcForPih(pih,m_Fs[lineI],lineI);//similar to foundMinRec2
if (minPihcPih==minPihc) {
minPihs.push_back(pih);
pihPlaces.push_back(t);
}
elseif (minPihcPih<minPihc) {
minPihs.clear();
minPihs.push_back(pih);
minPihc = minPihcPih;
pihPlaces.clear();
pihPlaces.push_back(t);
m_newValidLines.clear();//we invalidate all previous lines, which give a smaller pihc
}
}
int xc = pairsP[minPihc].first;
int yc = pairsP[minPihc].second;
int lastFreePih = m_freePihss[lineI].back();//freePihs.back();
//save all optimal results :
for(int t=0;t<minPihs.size();t++) {
int pih = minPihs[t];
int line = m_lines.size();
m_newValidLines.push_back(line);
int pointTo = m_pihs.size();
m_pihs.push_back(pih);//data place 1
m_pointers.push_back(lastP);//data place 2
ivec newF(m_Fs[lineI]);//ivec newF(F);
newF[pairsP[pih].first] = xc;
newF[pairsP[pih].second] = yc;
vector<int> newFreePihs(m_freePihss[lineI]);
newFreePihs[pihPlaces[t]] = lastFreePih;
newFreePihs.resize(size-1);
m_lines.push_back(pointTo);//line data 1
m_Fs.push_back(newF);//line data 2
int FsFreeN = m_FsFreeN[lineI];
if (m_Fs[lineI][pairsP[pih].first]<0) FsFreeN--;
if (m_Fs[lineI][pairsP[pih].second]<0) FsFreeN--;
m_FsFreeN.push_back(FsFreeN);
m_freePihss.push_back(newFreePihs);//line data 3
}
return minPihc;
}
I have a question in mind. Since the int is 4 byte, By using reference in for loop, Is it make the code quicker or not? for(int& x: f) ...
Is using reference make code faster in bigger data type or it is just memory saver?