nouveau fichier : include/Appli/Selected.hpp

nouveau fichier : include/Appli/Selected.tpp
	nouveau fichier : src/Appli/Selected.cpp
This commit is contained in:
Git Merciol 2018-02-20 18:26:32 +01:00
parent a2d21182a7
commit 03626d1e71
3 changed files with 241 additions and 0 deletions

View File

@ -0,0 +1,97 @@
#ifndef _OTB_TRISKELE_SELECTED_HPP
#define _OTB_TRISKELE_SELECTED_HPP
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <boost/algorithm/string.hpp>
namespace otb {
namespace triskele {
using namespace std;
using namespace boost;
// ========================================
class Selected {
private:
/*!
* \brief set Vector qui correspond à l'ensemble
*/
vector<int> set;
/*!
* \brief Indicateur si le max doit etre defini (à l'aide de la methode setMaxValue())
*/
bool minFlag = false, maxFlag = false;
int minInter, maxInter;
public:
/*!
* \brief getSet Getter de l'ensemble
* \return L'ensemble set
*/
inline const vector<int> &getSet () const;
/*!
* \brief empty Vérifie si l'ensemble est vide ou non
* \return true si l'ensemble est vide, false sinon
*/
inline bool empty () const;
/*!
* \brief size Retourne la taille de l'ensemble
* \return La taille de l'ensemble
*/
inline int size () const;
/*!
* \brief first Retourne la première valeur de l'ensemble
* \return La première valeur de l'ensemble
*/
inline int first () const;
/*!
* \brief last Retourne la dernière valeur de l'ensemble
* \return La dernière valeur de l'ensemble
*/
inline int last () const;
/*!
* \brief Selected Constructeur par défaut
*/
Selected ();
/*!
* \brief Selected Constructeur avec un interval
*/
Selected (int first, int last);
/*!
* \brief Selected Constructeur de copie
* \param selected Objet à copier
*/
Selected (const Selected &selected);
/*!
* \brief Selected Constructeur qui convertit la chaîne option en ensemble
* \param option Chaîne de caractère à convertir
*/
Selected (const string &option);
/*!
* \brief Fixe les bornes en cas option "-x" ou "x-"
* \param max la valeur de l'index de la bande max
*/
void setLimits (int minOpt, int maxOpt);
/*!
* \brief toSet Conversion en ensemble (On retire les doublons et ordonne le vector)
* \return L'objet Selected
*/
Selected &toSet ();
/*!
* \brief contains Vérifie si la valeur value est contenue dans l'ensemble
* \param value valeur à vérifier
* \return true si la valeur est contenue dans l'ensemble, false sinon
*/
bool contains (int value) const;
};
inline ostream &operator << (ostream &out, const Selected &selected);
#include "Selected.tpp"
}//namespace triskele
}//namespace otb
#endif //_OTB_TRISKELE_SELECTED_HPP

View File

@ -0,0 +1,40 @@
#ifndef _OTB_TRISKELE_SELECTED_TPP
#define _OTB_TRISKELE_SELECTED_TPP
inline const vector<int> &
Selected::getSet () const {
return set;
}
inline bool
Selected::empty () const {
return set.empty ();
}
inline int
Selected::size () const {
return set.size ();
}
inline int
Selected::first () const {
return set[0];
}
inline int
Selected::last () const {
return set[set.size ()-1];
}
inline ostream &
operator << (ostream &out, const Selected &selected) {
out << "[";
string sep = "";
for (auto& i : selected.getSet ()) {
out << sep << i;
sep = ", ";
}
return out << "]";
}
#endif // _OTB_TRISKELE_SELECTED_TPP

104
src/Appli/Selected.cpp Normal file
View File

@ -0,0 +1,104 @@
#include "Appli/Selected.hpp"
using namespace otb::triskele;
using namespace boost;
Selected::Selected () {
}
Selected::Selected (int first, int last)
: set (last-first+1) {
std::iota (std::begin (set), std::end (set), first);
}
Selected::Selected (const Selected &selected)
: set (selected.set) {
}
Selected::Selected (const string &option) {
if (option.empty ())
return;
vector <string> fields;
vector <string> intervals;
split (fields, option, is_any_of (","));
for (auto &field : fields) {
intervals.clear();
split (intervals, field, is_any_of ("-"));
switch (intervals.size ()) {
case 0:
continue;
case 1:
set.push_back (stoi (intervals[0]));
continue;
case 2: {
bool noMin = !intervals[0][0] || intervals[0][0] == '*';
bool noMax = !intervals[1][0] || intervals[1][0] == '*';
if (noMin && noMax) {
minFlag = true;
maxFlag = true;
continue;
}
if (noMin) {
int val = stoi (intervals[1]);
minInter = minFlag ? max (minInter, val) : val;
minFlag = true;
continue;
}
if (noMax) {
int val = stoi (intervals[0]);
maxInter = maxFlag ? min (maxInter, val) : val;
maxFlag = true;
continue;
}
int a = stoi (intervals[0]);
int b = stoi (intervals[1]);
if (a > b)
std::swap (a, b);
for (int i = a; i <= b; ++i)
if (!contains (i))
set.push_back (i);
continue;
}
default:
cerr << "Bad selected option: " << option << endl;
continue;
;
}
}
}
void
Selected::setLimits (int minOpt, int maxOpt) {
if (minOpt > maxOpt)
std::swap (minOpt, maxOpt);
if (minFlag && maxFlag) {
for (int i = minOpt; i <= maxOpt; ++i)
if (!contains (i))
set.push_back (i);
return;
}
if (minFlag) {
for (int i = minOpt; i <= minInter; ++i)
if (!contains (i))
set.push_back (i);
if (maxFlag)
maxInter = max (maxInter, minInter+1);
}
if (maxFlag)
for (int i = maxInter; i <= maxOpt; ++i)
if (!contains (i))
set.push_back (i);
}
Selected &
Selected::toSet () {
sort (set.begin (), set.end ());
set.erase (unique (set.begin (), set.end ()), set.end ());
return *this;
}
bool
Selected::contains (int value) const {
return size () && find (set.begin (), set.end (), value) != set.end ();
}