replace piForeach* to for(:)

another c++11 try ...
This commit is contained in:
2020-07-30 20:08:33 +03:00
parent 2ffc457566
commit 557f2a4d0d
18 changed files with 151 additions and 235 deletions

View File

@@ -97,141 +97,32 @@
*/
# define piForeachCR(i,c)
/*!\brief Macro for break from any piForeach* loop
* \details \warning C++ ordinary "break" doesn`t work inside piForeach*
* loops! Always use "piBreak" instead!
*/
# define piBreak
#else
# define piBreak {_for._end = true; break;}
template <typename C>
struct _reverse_wrapper {
C & c_;
_reverse_wrapper(C & c): c_(c) {}
_reverse_wrapper(const C & c): c_(const_cast<C&>(c)) {}
typename C::reverse_iterator begin() {return c_.rbegin();}
typename C::reverse_iterator end() {return c_.rend(); }
typename C::const_reverse_iterator begin() const {return c_.rbegin();}
typename C::const_reverse_iterator end() const {return c_.rend(); }
};
template <typename C> _reverse_wrapper<C> _reverse_wrap(C & c) {return _reverse_wrapper<C>(c);}
template <typename C> _reverse_wrapper<C> _reverse_wrap(const C & c) {return _reverse_wrapper<C>(c);}
# define piForTimes(c) for(int _i##c = 0; _i##c < c; ++_i##c)
#ifdef CC_GCC
# define piForeach(i,c) for(i : c)
# define piForeachC(i,c) for(const i : c)
# define piForeachR(i,c) for(i : _reverse_wrap(c))
# define piForeachRC(i,c) for(const i : _reverse_wrap(c))
template<typename Type>
class _PIForeach {
public:
_PIForeach(Type & t): _t(t), _break(false), _end(false) {_it = _t.begin();}
typename Type::value_type _var;
typename Type::iterator _it;
Type & _t;
bool _break, _end;
inline bool isEnd() {return _it == _t.end();}
inline void operator ++() {if (_end) _it = _t.end(); else _it++; _break = false;}
};
template<typename Type>
class _PIForeachR {
public:
_PIForeachR(Type & t): _t(t), _break(false), _end(false) {_rit = _t.rbegin();}
typename Type::value_type _var;
typename Type::reverse_iterator _rit;
Type & _t;
bool _break, _end;
inline bool isEnd() {return _rit == _t.rend();}
inline void operator ++() {if (_end) _rit = _t.rend(); else _rit++; _break = false;}
};
template<typename Type>
class _PIForeachC {
public:
_PIForeachC(const Type & t): _t(t), _break(false), _end(false) {_it = _t.begin();}
typename Type::value_type _var;
typename Type::const_iterator _it;
const Type & _t;
bool _break, _end;
inline bool isEnd() {return _it == _t.end();}
inline void operator ++() {if (_end) _it = _t.end(); else _it++; _break = false;}
};
template<typename Type>
class _PIForeachCR {
public:
_PIForeachCR(const Type & t): _t(t), _break(false), _end(false) {_rit = _t.rbegin();}
typename Type::value_type _var;
typename Type::const_reverse_iterator _rit;
const Type & _t;
bool _break, _end;
inline bool isEnd() {return _rit == _t.rend();}
inline void operator ++() {if (_end) _rit = _t.rend(); else _rit++; _break = false;}
};
#define piForeach(i,c) for(_PIForeach<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(i(*_for._it); !_for._break; _for._break = true)
#define piForeachR(i,c) for(_PIForeachR<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(i(*_for._rit); !_for._break; _for._break = true)
#define piForeachA(i,c) for(_PIForeach<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(typeof(_for._var) & i(*_for._it); !_for._break; _for._break = true)
#define piForeachAR(i,c) for(_PIForeachR<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(typeof(_for._var) & i(*_for._rit); !_for._break; _for._break = true)
#define piForeachC(i,c) for(_PIForeachC<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(const i(*_for._it); !_for._break; _for._break = true)
#define piForeachCR(i,c) for(_PIForeachCR<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(const i(*_for._rit); !_for._break; _for._break = true)
#define piForeachCA(i,c) for(_PIForeachC<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(const typeof(_for._var) & i(*_for._it); !_for._break; _for._break = true)
#define piForeachCAR(i,c) for(_PIForeachCR<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(const typeof(_for._var) & i(*_for._rit); !_for._break; _for._break = true)
#define piForeachRA piForeachAR
#define piForeachAC piForeachCA
#define piForeachCRA piForeachCAR
#define piForeachARC piForeachCAR
#define piForeachACR piForeachCAR
#define piForeachRCA piForeachCAR
#define piForeachRAC piForeachCAR
#else
class _PIForeachBase {public: mutable bool _break, _end; };
template<typename Type>
class _PIForeach: public _PIForeachBase {
public:
_PIForeach(Type & t, bool i = false): _t(t), _inv(i) {_break = _end = false; if (_inv) _rit = _t.rbegin(); else _it = _t.begin();}
mutable typename Type::value_type _var;
mutable typename Type::iterator _it;
mutable typename Type::reverse_iterator _rit;
Type & _t;
bool _inv;
bool isEnd() {if (_inv) return _rit == _t.rend(); else return _it == _t.end();}
void operator ++() {if (_inv) {if (_end) _rit = _t.rend(); else _rit++;} else {if (_end) _it = _t.end(); else _it++;} _break = false;}
};
template<typename Type>
class _PIForeachC: public _PIForeachBase {
public:
_PIForeachC(const Type & t, bool i = false): _t(t), _inv(i) {_break = _end = false; if (_inv) _rit = _t.rbegin(); else _it = _t.begin();}
mutable typename Type::value_type _var;
mutable typename Type::const_iterator _it;
mutable typename Type::const_reverse_iterator _rit;
const Type & _t;
bool _inv;
bool isEnd() {if (_inv) return _rit == _t.rend(); else return _it == _t.end();}
void operator ++() {if (_inv) {if (_end) _rit = _t.rend(); else _rit++;} else {if (_end) _it = _t.end(); else _it++;} _break = false;}
};
template <typename T> inline _PIForeach<T> _PIForeachNew(T & t, bool i = false) {return _PIForeach<T>(t, i);}
template <typename T> inline _PIForeach<T> * _PIForeachCast(_PIForeachBase & c, T & ) {return static_cast<_PIForeach<T> * >(&c);}
template <typename T> inline _PIForeachC<T> _PIForeachNewC(const T & t, bool i = false) {return _PIForeachC<T>(t, i);}
template <typename T> inline _PIForeachC<T> * _PIForeachCastC(_PIForeachBase & c, const T & ) {return static_cast<_PIForeachC<T> * >(&c);}
#define piForeach(i,c) for(_PIForeachBase & _for = _PIForeachNew(c); !_PIForeachCast(_for, c)->isEnd(); ++(*_PIForeachCast(_for, c))) \
for(i = *(_PIForeachCast(_for, c)->_it); !_for._break; _for._break = true)
#define piForeachR(i,c) for(_PIForeachBase & _for = _PIForeachNew(c, true); !_PIForeachCast(_for, c)->isEnd(); ++(*_PIForeachCast(_for, c))) \
for(i = *(_PIForeachCast(_for, c)->_rit); !_for._break; _for._break = true)
#define piForeachC(i,c) for(_PIForeachBase & _for = _PIForeachNewC(c); !_PIForeachCastC(_for, c)->isEnd(); ++(*_PIForeachCastC(_for, c))) \
for(const i = *(_PIForeachCastC(_for, c)->_it); !_for._break; _for._break = true)
#define piForeachCR(i,c) for(_PIForeachBase & _for = _PIForeachNewC(c, false); !_PIForeachCastC(_for, c)->isEnd(); ++(*_PIForeachCastC(_for, c))) \
for(const i = *(_PIForeachCastC(_for, c)->_rit); !_for._break; _for._break = true)
#endif
#define piForeachRC piForeachCR
# define piForeachCR piForeachRC
#endif // DOXYGEN

View File

@@ -70,7 +70,6 @@ public:
}
inline PIDeque<T> & operator =(PIDeque<T> && other) {
if (this == &other) return *this;
swap(other);
return *this;
}
@@ -324,6 +323,12 @@ public:
piSwap<size_t>(pid_rsize, other.pid_rsize);
piSwap<ssize_t>(pid_start, other.pid_start);
}
inline void swap(PIDeque<T> && other) {
piSwap<T*>(pid_data, other.pid_data);
piSwap<size_t>(pid_size, other.pid_size);
piSwap<size_t>(pid_rsize, other.pid_rsize);
piSwap<ssize_t>(pid_start, other.pid_start);
}
typedef int (*CompareFunc)(const T * , const T * );
static int compare_func(const T * t0, const T * t1) {return (*t0) < (*t1) ? -1 : ((*t0) == (*t1) ? 0 : 1);}

View File

@@ -226,6 +226,10 @@ public:
pim_content.swap(other.pim_content);
pim_index.swap(other.pim_index);
}
void swap(PIMap<Key, T> && other) {
pim_content.swap(other.pim_content);
pim_index.swap(other.pim_index);
}
PIMap<Key, T> & insert(const Key & key, const T & value) {
bool f(false);

View File

@@ -69,7 +69,6 @@ public:
}
inline PIVector<T> & operator =(PIVector<T> && other) {
if (this == &other) return *this;
swap(other);
return *this;
}
@@ -301,6 +300,11 @@ public:
piSwap<size_t>(piv_size, other.piv_size);
piSwap<size_t>(piv_rsize, other.piv_rsize);
}
inline void swap(PIVector<T> && other) {
piSwap<T*>(piv_data, other.piv_data);
piSwap<size_t>(piv_size, other.piv_size);
piSwap<size_t>(piv_rsize, other.piv_rsize);
}
typedef int (*CompareFunc)(const T * , const T * );
static int compare_func(const T * t0, const T * t1) {return (*t0) < (*t1) ? -1 : ((*t0) == (*t1) ? 0 : 1);}